在SQL Server中使用多个WHEN,WHERE条件

时间:2016-06-21 12:45:30

标签: sql-server

我们有一张如下表格

ID  Primary  Date
12    0     6/6/2011
12    1     6/24/2010
12    0     5/20/2009
12    1     6/21/2010
13    0     5/18/2010
13    0     6/18/2016
13    0     6/24/2010

我要插入其他表格,让我们说#temp

我应该编写一个select查询,以便结果如下:

ID   Primary   Date
12     1      6/21/2010
13     0      5/18/2010

条件:

如果任何id包含Primary -> 1,那么我们应该考虑将主要为1的记录 (无需考虑primary->0记录)和最早的日期,即分钟(日期)

如果id主要为0,那么我们应该考虑最早的日期记录,即分钟(日期)

我尝试使用case语句并在select查询中使用With CTE等但没有得到预期的结果..

任何人都可以帮我解决这个问题。

4 个答案:

答案 0 :(得分:1)

试试这个,。

;with testcte(prim,date,RN)
as
(select prim,min(date),ROW_NUMBER() over (PARTITION by prim order by date) RN
from datetest
group by prim,date)
select prim,date from testcte where RN = 1

OR

select ID, max([primary]) [Primary],case when max([primary]) = 1 then min(date) when max([primary]) = 0 then min(date) end [Date]
from datetest group by id

答案 1 :(得分:0)

如果我理解正确,那么这对我来说很好。当然,因为它具有子查询,所以它不太适合经常使用的例程,特别是在具有大量数据的表上。但这很有效,特别是如果你只想进行一次性导入。

declare @temp TABLE (Id int, [Primary] BIT, [Date] Date)

insert into @temp (Id, [primary], [date]) VALUES (12, 0, GetDate())
insert into @temp (Id, [primary], [date]) VALUES (12, 0, GetDate())
insert into @temp (Id, [primary], [date]) VALUES (12, 1, GetDate())
insert into @temp (Id, [primary], [date]) VALUES (12, 0, GetDate())
insert into @temp (Id, [primary], [date]) VALUES (13, 0, GetDate())
insert into @temp (Id, [primary], [date]) VALUES (13, 0, GetDate())

select * from @temp

SELECT
[Id],
[Primary],
MIN([date])
FROM @temp tt
WHERE
((SELECT Count(*) FROM @temp t WHERE t.[Primary] = 1 and t.Id = tt.Id) > 0 and tt.[Primary] = 1)
    OR (SELECT Count(*) FROM @temp t WHERE t.[Primary] = 1 and t.Id = tt.Id) = 0
GROUP BY Id, [Primary]

答案 2 :(得分:0)

你的这个:

SELECT ID, Primary, MIN(Date)
FROM table
WHERE
(
    ID IN (SELECT DISTINCT ID
           FROM table
           WHERE Primary = 1)
    AND Primary = 1
)
OR
ID NOT IN (SELECT DISTINCT ID
           FROM table
           WHERE Primary = 1)
GROUP BY ID, Primary

答案 3 :(得分:0)

好的,我们来做一些测试数据;

IF OBJECT_ID('tempdb..#TestData') IS NOT NULL DROP TABLE #TestData
GO
CREATE TABLE #TestData (ID int, [Primary] int, Date datetime)
INSERT INTO #TestData (ID, [Primary], Date)
VALUES
(12,0,'2011-06-06')
,(12,1,'2010-06-24')
,(12,0,'2009-05-20')
,(12,1,'2010-06-21')
,(13,0,'2010-05-18')
,(13,0,'2016-06-18')
,(13,0,'2010-06-24')

您可以使用这样的查询,该查询使用一个子查询查看primary = 1的结果,另一个查看primary = 0的结果。然后,它使用coalesce从primary = 1子查询返回值(如果存在),否则从另一个查询中获取值。

SELECT DISTINCT
t.ID
,CASE 
    WHEN pr.date IS NOT NULL 
        THEN 1
    WHEN np.date IS NOT NULL
        THEN 0
 END [Primary]
,COALESCE(pr.date, np.date) [date]
INTO #Temp
FROM #TestData t
LEFT JOIN --Primary = 1 Results
    (
        SELECT 
        a.ID
        ,MIN(date) date
        FROM #TestData a
        WHERE [Primary] = 1
        GROUP BY ID
    ) pr
ON t.ID = pr.ID
LEFT JOIN --Primary = 0 Results
    (
        SELECT 
        ID
        ,MIN(date) date
        FROM #TestData 
        WHERE [Primary] = 0
        GROUP BY ID
    ) np
ON t.ID = np.ID

将这些结果提供给#temp

ID  Primary date
12  1       2010-06-21 00:00:00.000
13  0       2010-05-18 00:00:00.000

此外,请不要使用PrimaryDate等名称作为字段名称。这些是SQL中的保留字,将导致您无法解决问题。

https://msdn.microsoft.com/en-us/library/ms189822.aspx