获取包含2个状态但不仅仅包含其中一个的所有数据:SQL查询

时间:2018-05-03 04:55:41

标签: sql sql-server

我正在处理一个查询,我希望它只向我显示具有2个特定状态的所有数据:ORGNSCNTRANSITRCV - 并且TRANSITRCV之前是{ ORGNSCN

请参阅此处的示例数据:

enter image description here

所以我应该InvoiceNumber FCI150142

我创建了一个查询:

SELECT [InvoiceNumber], [StageID] 
FROM [CARGODB].[dbo].[BoxesUpdateLog] 
WHERE [StageID] = 'ORGNSCN' AND 
      [StageID] = 'TRANSITRCV') 
GROUP BY [InvoiceNumber], [StageID];

我的上述思考是,它会向我显示至少包含这2个状态的所有数据(没有日期优先),但它什么也没显示。

我错过了什么?

更新

我创建了一个查询,它为我提供了具有这两种状态的所有数据,但我无法弄清楚如何在TRANSITRCV之前获取ORGNSCN之前输入的所有数据:

SELECT * FROM (
SELECT [InvoiceNumber], 
       MAX(Case When [StageID] = 'ORGNSCN' Then 1 else 0 end) as [ORGNSCN], 
       MAX(Case When [StageID] = 'TRANSITRCV' Then 1 else 0 end) as [TRANSITRCV]
FROM [CARGODB].[dbo].[BoxesUpdateLog] WHERE [StageID] in ( 'ORGNSCN','TRANSITRCV')
GROUP BY [InvoiceNumber]
) x
WHERE [ORGNSCN] > 0 and 
      [TRANSITRCV] > 0

更新:(示例数据)

RecordID    InvoiceNumber   DatePosted                  TransDate               StageID     Status
31478801    FCI150142       2018-04-18  10:30:35.000    2018-04-18 10:30:35.000 TRANSITRCV  RECEIVED
31478851    FCI150142       2018-04-18  11:33:53.330    2018-04-18 11:33:53.330 LOADCNTNR   LOADED
31488281    FCI150142       2018-04-19  09:28:41.000    2018-04-19 09:28:41.000 ORGNSCN     ORIGIN SCAN
31602071    FCI150142       2018-05-01  15:05:20.000    2018-04-19 15:05:20.000 S000000001  RECEIVED
31602883    FCI150142       2018-05-01  15:06:14.000    2018-04-25 15:06:14.000 S000000005  LOADED
31603695    FCI150142       2018-05-01  15:06:37.000    2018-04-28 15:06:37.000 S000000006  DEPARTED

非常感谢您的帮助。

3 个答案:

答案 0 :(得分:1)

试试这个答案:

表架构:

Address

<强>查询:

DECLARE @BoxesUpdateLog TABLE(RecordId BIGINT, [InvoiceNumber] VARCHAR(15), DatePosted DATETIME, TransDate DATETIME, StageID VARCHAR(25), Status VARCHAR(25))

INSERT INTO @BoxesUpdateLog VALUES
(31478801,'FCI150142','2018-04-18  10:30:35.000','2018-04-18 10:30:35.000','TRANSITRCV','RECEIVED   ')
,(31478851,'FCI150142','2018-04-18  11:33:53.330','2018-04-18 11:33:53.330','LOADCNTNR ','LOADED        ')
,(31488281,'FCI150142','2018-04-19  09:28:41.000','2018-04-19 09:28:41.000','ORGNSCN   ','ORIGIN SCAN')
,(31602071,'FCI150142','2018-05-01  15:05:20.000','2018-04-19 15:05:20.000','S000000001','RECEIVED  ')
,(31602883,'FCI150142','2018-05-01  15:06:14.000','2018-04-25 15:06:14.000','S000000005','LOADED        ')
,(31603695,'FCI150142','2018-05-01  15:06:37.000','2018-04-28 15:06:37.000','S000000006','DEPARTED  ')

<强>输出:

SELECT [InvoiceNumber]
FROM [CARGODB].[dbo].[BoxesUpdateLog] 
WHERE [StageID] in ( 'ORGNSCN','TRANSITRCV')
GROUP BY [InvoiceNumber]
HAVING COUNT(DISTINCT [StageID])=2
    AND MAX(CASE WHEN [StageID] = 'ORGNSCN' THEN DatePosted END) > MAX(CASE WHEN [StageID] = 'TRANSITRCV' THEN DatePosted END)

检查#SQL Fiddle

中的结果

答案 1 :(得分:0)

您在where子句后的查询中使用'And',这将永远不会成立。 使用'Or''IN'

SELECT [InvoiceNumber], [StageID] FROM [CARGODB].[dbo].[BoxesUpdateLog] WHERE [StageID] = 'ORGNSCN' OR [StageID] = 'TRANSITRCV' GROUP BY [InvoiceNumber], [StageID];

<强> IN

SELECT [InvoiceNumber], [StageID] FROM [CARGODB].[dbo].[BoxesUpdateLog] WHERE [StageID] IN ( 'ORGNSCN' , 'TRANSITRCV') GROUP BY [InvoiceNumber], [StageID];

答案 2 :(得分:0)

您可以在ORDER BY子句中使用&#39; DatePosted&#39;列,这将导致按照这些记录的发布日期排序的排序结果集。