我是这个论坛的新手,也是SQL的新手。您能帮我解决以下问题吗?
我有一张桌子,上面有所有交易。我想查找每个事务相对于Transaction_By(即用户)的开始日期和结束日期。
我尝试使用以下查询,但未给出正确的结果。
提前致谢。
嗨,抱歉,我是这个论坛的新手,所以不确定如何为第二个查询添加图像。请在这里找到我的新查询数据和预期结果。
再次感谢。
嗨,安德烈,请在这里找到我的新数据以及预期结果。
新数据链接:http : //sqlfiddle.com/#!18/8e49b/1/0
预期结果:我的新数据的预期结果
由于SQL Server 2008不支持LEAD / LAG功能,请尝试以下操作:
SELECT
c.Trans_No, c.Trans_By,
COALESCE(p.Trans_Date, c.Start_Date) AS Trans_Start_Date,
COALESCE(p.Transact, 'Initiated') AS Transaction_Start,
c.Trans_Date AS Trans_End_Date, c.Transact AS Transaction_End
FROM My_table AS c
OUTER APPLY (
SELECT TOP(1) *
FROM My_table AS p
WHERE p.Trans_No = c.Trans_No AND p.Trans_Date < c.Trans_Date
ORDER BY p.Trans_No, p.Trans_Date DESC
) AS p
ORDER BY Trans_No, Trans_Start_Date;
输出:
+----------+----------+------------------+-------------------+----------------+-----------------+
| Trans_No | Trans_By | Trans_Start_Date | Transaction_Start | Trans_End_Date | Transaction_End |
+----------+----------+------------------+-------------------+----------------+-----------------+
| 101 | Smith | 2019-01-02 | Initiated | 2019-01-03 | Processed |
| 101 | Mary | 2019-01-03 | Processed | 2019-01-06 | Finalised |
| 102 | Donald | 2019-01-09 | Initiated | 2019-01-10 | Processed |
| 102 | Tony | 2019-01-10 | Processed | 2019-01-11 | Rejected |
| 102 | Jennifer | 2019-01-11 | Rejected | 2019-01-13 | Corrected |
| 102 | George | 2019-01-13 | Corrected | 2019-01-17 | Finalised |
+----------+----------+------------------+-------------------+----------------+-----------------+
使用SQL Fiddle在线进行测试。
更新:
WITH
a AS (
SELECT
c.Trans_No, c.Trans_By, p.Trans_By AS Prev_Trans_By,
COALESCE(p.Trans_Date, c.Start_Date) AS Trans_Start_Date,
ca.pad + COALESCE(p.Transact, 'Initiated') AS Trans_Start,
c.Trans_Date AS Trans_End_Date,
ca.pad + c.Transact AS Trans_End
FROM My_table AS c
CROSS APPLY (
VALUES(STR(DATEDIFF(DAY, 0, c.Trans_Date), 5))
) AS ca(pad)
OUTER APPLY (
SELECT TOP(1) *
FROM My_table AS p
WHERE p.Trans_No = c.Trans_No AND p.Trans_Date < c.Trans_Date
ORDER BY p.Trans_No, p.Trans_Date DESC
) AS p
)
SELECT
a.Trans_No,
a.Trans_By,
MIN(a.Trans_Start_Date) AS Trans_Start_Date,
RIGHT(MIN(a.Trans_Start),
LEN(MIN(a.Trans_Start)) - 5) AS Trans_Start,
MAX(a.Trans_End_Date) AS Trans_End_Date,
RIGHT(MAX(a.Trans_End), LEN(MAX(a.Trans_End)) - 5) AS Trans_End
FROM a
CROSS APPLY (
SELECT
SUM(CASE
WHEN s.Prev_Trans_By IS NULL OR
s.Prev_Trans_By != s.Trans_By THEN 1
ELSE 0
END)
FROM a AS s
WHERE s.Trans_No = a.Trans_No AND
s.Trans_Start_Date <= a.Trans_Start_Date
) AS ca(g)
GROUP BY a.Trans_No, a.Trans_By, ca.g
ORDER BY Trans_No, Trans_Start_Date;
输出:
+----------+----------+------------------+-------------+----------------+-----------+
| Trans_No | Trans_By | Trans_Start_Date | Trans_Start | Trans_End_Date | Trans_End |
+----------+----------+------------------+-------------+----------------+-----------+
| 101 | Smith | 2019-01-02 | Initiated | 2019-01-03 | Processed |
| 101 | Mary | 2019-01-03 | Processed | 2019-01-06 | Finalised |
| 102 | Donald | 2019-01-09 | Initiated | 2019-01-10 | Processed |
| 102 | Tony | 2019-01-10 | Processed | 2019-01-11 | Rejected |
| 102 | Jennifer | 2019-01-11 | Rejected | 2019-01-13 | Corrected |
| 102 | George | 2019-01-13 | Corrected | 2019-01-17 | Finalised |
| 105 | Tony | 2019-03-01 | Initiated | 2019-03-05 | Corrected |
| 105 | Smith | 2019-03-05 | Corrected | 2019-03-07 | Corrected |
| 105 | Jennifer | 2019-03-07 | Corrected | 2019-03-08 | Finalised |
+----------+----------+------------------+-------------+----------------+-----------+
使用SQL Fiddle在线进行测试。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句