SQL 已链接服务器记录
SQL Server record chained
我正在研究 SQL Server 2012
我有包含此信息的门票
Ticket No| Date | Closed
T001 | D1 |
T002 | D2 |
T003 | D3 | Yes
T004 | D4 | Yes
T005 | D5 |
T006 | D6 | Yes
我想按 "case" 对它们进行分组,收盘后的第一张票是打开 "case" 以及随后直到收盘的所有票都是 "case"[= 的一部分14=]
Case No | Ticket No| Date | Closed
C001 | T001 | D1 |
C001 | T002 | D2 |
C001 | T003 | D3 | Yes
C002 | T004 | D4 | Yes
C003 | T005 | D5 |
C003 | T006 | D6 | Yes
我已经尝试了好几个小时(使用滞后、等级等),但我找不到方法来做到这一点。我找不到一种方法来生成所需的案例号
你有好主意吗 ?非常感谢
这里是查询:
SELECT
CONCAT('C' , ID_TICKET) As ID_CASE_TEMP
,ID_TICKET
,DT_TICKET
,IIF(IsNull(LAG(IsNull(LB_CLOSING,'No')) OVER (ORDER BY DT_TICKET),'Yes') = 'Yes','Yes','No') As LB_OPENNING
,IsNull(LB_CLOSING,'No') As LB_CLOSING
,LAG(ID_TICKET) OVER (ORDER BY DT_TICKET) As PREVIOUS_TICKET
,LAG(DT_TICKET) OVER (ORDER BY DT_TICKET) As PREVIOUS_TICKET_DATE
,LAG(IsNull(LB_CLOSING,'No')) OVER (ORDER BY DT_TICKET) As PREVIOUS_TICKET_CLOSING
,ROW_NUMBER() OVER (ORDER BY DT_TICKET) As CD_ROW_NUMBER
FROM
TICKETS
这是使用 LAG
window 函数
的一种方法
SELECT 'C' + RIGHT(Concat('00', 1+Sum(CASE WHEN prev_val = 'Yes' THEN 1 ELSE 0 END)
OVER (ORDER BY [date])), 3) AS [Case No],
[Ticket No],
[Date],
[Closed]
FROM (SELECT *, Lag(closed) OVER( ORDER BY [date]) AS prev_val
FROM table1) a
我正在研究 SQL Server 2012 我有包含此信息的门票
Ticket No| Date | Closed
T001 | D1 |
T002 | D2 |
T003 | D3 | Yes
T004 | D4 | Yes
T005 | D5 |
T006 | D6 | Yes
我想按 "case" 对它们进行分组,收盘后的第一张票是打开 "case" 以及随后直到收盘的所有票都是 "case"[= 的一部分14=]
Case No | Ticket No| Date | Closed
C001 | T001 | D1 |
C001 | T002 | D2 |
C001 | T003 | D3 | Yes
C002 | T004 | D4 | Yes
C003 | T005 | D5 |
C003 | T006 | D6 | Yes
我已经尝试了好几个小时(使用滞后、等级等),但我找不到方法来做到这一点。我找不到一种方法来生成所需的案例号 你有好主意吗 ?非常感谢
这里是查询:
SELECT
CONCAT('C' , ID_TICKET) As ID_CASE_TEMP
,ID_TICKET
,DT_TICKET
,IIF(IsNull(LAG(IsNull(LB_CLOSING,'No')) OVER (ORDER BY DT_TICKET),'Yes') = 'Yes','Yes','No') As LB_OPENNING
,IsNull(LB_CLOSING,'No') As LB_CLOSING
,LAG(ID_TICKET) OVER (ORDER BY DT_TICKET) As PREVIOUS_TICKET
,LAG(DT_TICKET) OVER (ORDER BY DT_TICKET) As PREVIOUS_TICKET_DATE
,LAG(IsNull(LB_CLOSING,'No')) OVER (ORDER BY DT_TICKET) As PREVIOUS_TICKET_CLOSING
,ROW_NUMBER() OVER (ORDER BY DT_TICKET) As CD_ROW_NUMBER
FROM
TICKETS
这是使用 LAG
window 函数
SELECT 'C' + RIGHT(Concat('00', 1+Sum(CASE WHEN prev_val = 'Yes' THEN 1 ELSE 0 END)
OVER (ORDER BY [date])), 3) AS [Case No],
[Ticket No],
[Date],
[Closed]
FROM (SELECT *, Lag(closed) OVER( ORDER BY [date]) AS prev_val
FROM table1) a