SQL 循环检查
SQL loop through check
我在创建 SQL 查询时遇到了一个小问题。
我有一个 table 有 4 列 'Enquiry Date' , 'Taken Date' , 'Indication Date' , 'Cancelled Date'
table 被称为 tbl_Sales
.
我想做的是创建一个 t-sql 来表示查看 4 列,日期最接近 GETDATE()
的就是当前状态。
因此,例如,如果该行在每一列中都有一个日期,并且填写的最后日期是昨天在 'Cancelled Date'
中,那么该行的状态应显示为 canceled
。
可以这样做吗?
如果你没有一些状态字段,你的设计就很糟糕。这是暴力解决方案:
SELECT *,
CASE WHEN [Cancelled Date] IS NOT NULL AND
([Cancelled Date] > [Indication Date] OR [Indication Date] IS NULL) AND
([Cancelled Date] > [Taken Date] OR [Taken Date] IS NULL) AND
([Cancelled Date] > [Enquiry Date] OR [Enquiry Date] IS NULL) THEN 'Canceled'
WHEN [Indication Date] IS NOT NULL AND
([Indication Date] > [Cancelled Date] OR [Cancelled Date] IS NULL) AND
([Indication Date] > [Taken Date] OR [Taken Date] IS NULL) AND
([Indication Date] > [Enquiry Date] OR [Enquiry Date] IS NULL) THEN 'Indication'
WHEN [Taken Date] IS NOT NULL AND
([Taken Date] > [Indication Date] OR [Indication Date] IS NULL) AND
([Taken Date] > [Cancelled Date] OR [Cancelled Date] IS NULL) AND
([Taken Date] > [Enquiry Date] OR [Enquiry Date] IS NULL) THEN 'Taken'
WHEN [Enquiry Date] IS NOT NULL AND
([Enquiry Date] > [Indication Date] OR [Indication Date] IS NULL) AND
([Enquiry Date] > [Taken Date] OR [Taken Date] IS NULL) AND
([Enquiry Date] > [Cancelled Date] OR [Cancelled Date] IS NULL) THEN 'Enquiry'
END
FROM Sales
编辑:
这里是一些简化版:
SELECT *
FROM Sales
CROSS APPLY ( SELECT TOP 1
st
FROM ( VALUES ( [Enquiry Date], 'Enquiry'),
( [Taken Date], 'Taken'),
( [Indication Date], 'Indication'),
( [Cancelled Date], 'Canceled') ) d ( dt, st )
ORDER BY dt DESC
) ca
我在创建 SQL 查询时遇到了一个小问题。
我有一个 table 有 4 列 'Enquiry Date' , 'Taken Date' , 'Indication Date' , 'Cancelled Date'
table 被称为 tbl_Sales
.
我想做的是创建一个 t-sql 来表示查看 4 列,日期最接近 GETDATE()
的就是当前状态。
因此,例如,如果该行在每一列中都有一个日期,并且填写的最后日期是昨天在 'Cancelled Date'
中,那么该行的状态应显示为 canceled
。
可以这样做吗?
如果你没有一些状态字段,你的设计就很糟糕。这是暴力解决方案:
SELECT *,
CASE WHEN [Cancelled Date] IS NOT NULL AND
([Cancelled Date] > [Indication Date] OR [Indication Date] IS NULL) AND
([Cancelled Date] > [Taken Date] OR [Taken Date] IS NULL) AND
([Cancelled Date] > [Enquiry Date] OR [Enquiry Date] IS NULL) THEN 'Canceled'
WHEN [Indication Date] IS NOT NULL AND
([Indication Date] > [Cancelled Date] OR [Cancelled Date] IS NULL) AND
([Indication Date] > [Taken Date] OR [Taken Date] IS NULL) AND
([Indication Date] > [Enquiry Date] OR [Enquiry Date] IS NULL) THEN 'Indication'
WHEN [Taken Date] IS NOT NULL AND
([Taken Date] > [Indication Date] OR [Indication Date] IS NULL) AND
([Taken Date] > [Cancelled Date] OR [Cancelled Date] IS NULL) AND
([Taken Date] > [Enquiry Date] OR [Enquiry Date] IS NULL) THEN 'Taken'
WHEN [Enquiry Date] IS NOT NULL AND
([Enquiry Date] > [Indication Date] OR [Indication Date] IS NULL) AND
([Enquiry Date] > [Taken Date] OR [Taken Date] IS NULL) AND
([Enquiry Date] > [Cancelled Date] OR [Cancelled Date] IS NULL) THEN 'Enquiry'
END
FROM Sales
编辑:
这里是一些简化版:
SELECT *
FROM Sales
CROSS APPLY ( SELECT TOP 1
st
FROM ( VALUES ( [Enquiry Date], 'Enquiry'),
( [Taken Date], 'Taken'),
( [Indication Date], 'Indication'),
( [Cancelled Date], 'Canceled') ) d ( dt, st )
ORDER BY dt DESC
) ca