如何在Sql Server和Filter记录中对集合进行分组

时间:2012-07-27 03:22:48

标签: sql sql-server-2008 tsql

我的表格看起来像

SALES_ORDER_ID  DATEDIFFERENCE  FLAG
1               -40             1
1               -20             1
2                40             0
2               -10             1
3                12             0
4               -70             1
5                60             0
5                23             0

// DDL

Declare @t table (sales_order_id int,DateDifference int, Flag int)
Insert Into @t 
                Select 1,-40,1 Union All Select 1,-20,1 Union All
                Select 2,40,0 Union All Select 2,-10,1 Union All
                Select 3,12,0 Union All Select 4,-70,1 Union All
                Select 5,60,0 Union All Select 5,23,0
Select *
From @t 

输出应为

sales_order_id  DateDifference  Flag
3                      12          0
5                      60          0
5                      23          0

即。 所有项目的组(Sales_Order_Id)将标记为0。

我的意思是Sales_Order_Id = 2不会出现,因为至少有一个项目不是零(2,-10,1)

如果信息不充分,请问我。

如何进行此查询?

修改

我找到了答案..虽然仍在寻找更优雅的答案

 ;with cte as(
    Select 
        sales_order_id = Case when x.sales_order_id is null then y.sales_order_id else x.sales_order_id end 
        ,x.CountFor0
        ,y.CountFor1
    From 
            (Select sales_order_id,CountFor0 = count(*)
            from @t 
            where Flag = 0
            group by sales_order_id )x
    Full Join 

            (Select sales_order_id,CountFor1 =count(*)
            from @t 
            where Flag = 1
            group by sales_order_id )y
    On x.sales_order_id = y.sales_order_id)

    Select *
    From cte
    where CountFor1 is null

感谢所有

由于我的声誉不高,所以无法在答题部分发表我的答案

6 个答案:

答案 0 :(得分:1)

美好的一天!

我认为这会有所帮助:

Select * from @t WHERE FLAG=0;

答案 1 :(得分:1)

好的,现在我明白你要做什么了。您可以像其他人建议的那样使用NOT EXISTSsqlfiddle):

SELECT *
FROM @t T1
WHERE Flag = 0
AND NOT EXISTS (
    SELECT * 
    FROM @t T2 
    WHERE T1.sales_order_id = T2.sales_order_id
    AND T2.Flag <> 0)

您也可以使用NOT IN,其他人也建议(sqlfiddle):

SELECT *
FROM @t T1
WHERE Flag = 0
AND sales_order_id NOT IN (
    SELECT sales_order_id 
    FROM @t T2 
    WHERE T2.Flag <> 0)

你甚至可以使用JOIN并检查连接表没有行(id为NULL)(sqlfiddle):

SELECT *
FROM @t T1
LEFT OUTER JOIN @t T2 ON T1.sales_order_id = T2.sales_order_id AND T2.Flag <> 0
WHERE T1.Flag = 0
AND T2.sales_order_id IS NULL

原始答案:

您需要WHERE clause来过滤结果:

SELECT * 
FROM @t
WHERE Flag = 0

查看sqlfiddle here

我假设你也希望结果中有(2,40,0)行。

答案 2 :(得分:1)

试试这个..

SELECT sales_order_id,DateDifference,Flag FROM @t WHERE sales_order_id NOT IN 
 (SELECT sales_order_id FROM @t WHERE Flag=1)

答案 3 :(得分:1)

我只想补充一点,这是一个使用Windows功能的好地方:

select sales_order_id, DateDifference, flag
from (select t.*, SUM(flag) over (partition by sales_order_id) as sumflag
      from @t t
     ) t
where sumflag = 0

答案 4 :(得分:0)

select *
from @T as T1
where not exists(select *
                 from @T as T2
                 where T1.sales_order_id = T2.sales_order_id and
                       T2.Flag = 1)

答案 5 :(得分:0)

试试这个:

Select * From @t 
where Flag=0 and sales_order_id not in 
(select sales_order_id from @t  where Flag !=0)