如何在SELECT中排除相同的行

时间:2019-01-11 09:21:21

标签: sql-server

我有这个脚本,用于创建表并存储销售订单信息。其背后的场景是,一旦将商品添加到销售订单中,其Status就是'A'意味着Add。后来,客户以某种方式希望删除该项目,因此我们添加了一个具有相同详细信息的新行,但是Status就像'D'表示Delete。 现在,我只想获取活动的销售订单商品,该商品不应包含该商品,即Added,然后是来自订单的Removed。 这是我的脚本。

CREATE TABLE [dbo].[SALE_DETAIL](
    [ORDER_NUMBER] [varchar](50) NULL,
    [ITEM_NAME] [varchar](250) NULL,
    [QUANTITY] [int] NULL,
    [PRICE] [numeric](18, 0) NULL,
    [Status] [varchar](50) NULL
) ON [PRIMARY]

GO
SET ANSI_PADDING OFF
GO
INSERT [dbo].[SALE_DETAIL] ([ORDER_NUMBER], [ITEM_NAME], [QUANTITY], [PRICE], [Status]) VALUES (N'SO-100-ORD-19', N'Double Bed', 5, CAST(70000 AS Numeric(18, 0)), N'A')
GO
INSERT [dbo].[SALE_DETAIL] ([ORDER_NUMBER], [ITEM_NAME], [QUANTITY], [PRICE], [Status]) VALUES (N'SO-100-ORD-19', N'Sofa', 5, CAST(10000 AS Numeric(18, 0)), N'A')
GO
INSERT [dbo].[SALE_DETAIL] ([ORDER_NUMBER], [ITEM_NAME], [QUANTITY], [PRICE], [Status]) VALUES (N'SO-100-ORD-19', N'Dining Table', 1, CAST(50000 AS Numeric(18, 0)), N'A')
GO
INSERT [dbo].[SALE_DETAIL] ([ORDER_NUMBER], [ITEM_NAME], [QUANTITY], [PRICE], [Status]) VALUES (N'SO-100-ORD-19', N'Sofa', 5, CAST(10000 AS Numeric(18, 0)), N'D')   
GO

我正在寻找的预期输出应该是这样的,因为商品'Sofa'已从订单中取消。

ORDER_NUMBER    ITEM_NAME       QTY PRICE
SO-100-ORD-19   Dining Table    1   50000
SO-100-ORD-19   Double Bed      5   70000

查询:

SELECT ORDER_NUMBER, ITEM_NAME, QUANTITY, PRICE FROM [dbo].[SALE_DETAIL]
WHERE Status <> 'D'
GROUP BY ORDER_NUMBER, ITEM_NAME, QUANTITY, PRICE

4 个答案:

答案 0 :(得分:3)

我会使用NOT EXISTS

SELECT SD.ORDER_NUMBER,
       SD.ITEM_NAME,
       SD.QUANTITY,
       SD.PRICE
FROM dbo.[SALE_DETAIL] SD
WHERE NOT EXISTS (SELECT 1
                  FROM dbo.[SALE_DETAIL] e
                  WHERE e.ORDER_NUMBER = SD.ORDER_NUMBER
                    AND e.ITEM_NAME = SD.ITEM_NAME
                    AND e.[Status] = 'D');

答案 1 :(得分:2)

从逻辑上讲,基于集合的答案是使用EXCEPT

#create a binary feature/column vector
feature_set <- vector(mode="logical", length=4)
#loop over all the items:
for(i in 1:4){
    #for each item, replace it with 0 or 1
    feature_set[i] = sample(0:1, 1);
}
#dataset with 4 features/columns
data.frame(X1 = rnorm(100),X2 = rnorm(100),X3 = rnorm(100),X4 = rnorm(100))

产生您所要求的结果。但是,请注意,出于种种原因,这种做法在实践中通常表现不佳,在这种情况下,最好使用Larnu's Answer之类的东西。

答案 2 :(得分:1)

换句话说:选择不存在匹配的'D'-记录的所有'A'-记录。

在SQL中:

SELECT ORDER_NUMBER, ITEM_NAME, QUANTITY, PRICE
FROM [dbo].[SALE_DETAIL] X
WHERE Status = 'A'
AND NOT EXISTS (
  SELECT 1
  FROM [dbo].[SALE_DETAIL] Y
  WHERE Y.Status       = 'D'
  AND   Y.ORDER_NUMBER = X.ORDER_NUMBER
  AND   Y.ITEM_NAME    = X.ITEM_NAME
)

答案 3 :(得分:1)

您可以尝试使用not in运算符获得预期结果,如下所示。

SELECT DISTINCT A.ORDER_NUMBER, A.ITEM_NAME,A.QUANTITY, A.PRICE, A.Status
FROM SALE_DETAIL A
    where A.ITEM_NAME not in (select s.ITEM_NAME from SALE_DETAIL s
    where s.[Status] = 'D')

输出如下所示

ORDER_NUMBER    ITEM_NAME   QUANTITY    PRICE   Status
------------------------------------------------------
SO-100-ORD-19   Dining Table    1       50000   A       
SO-100-ORD-19   Double Bed      5       70000   A        

您可以找到实时演示Live Demo Here