识别具有相同 ID 的列
Posted
技术标签:
【中文标题】识别具有相同 ID 的列【英文标题】:Identify the columns that have the same ID 【发布时间】:2020-12-24 11:57:30 【问题描述】:我想创建一个新列,在这种情况下,“Reiterate”为具有相同键 (Clave) 并且在 resolutiondate
和 createddate
之间的时间少于 7 天的行标记为“YES”,并且Massive
列必须为“否”。这是我想要得到的示例。
我想尝试使用lead and lag
公式将日期从最低到最高排序,但不知道如何在 SQL 中应用它。我不确定是否有可能获得该专栏。
非常感谢任何帮助。非常感谢
编辑。
我使用了您建议的公式,并且有效! ,再次感谢所有帮助:)
SELECT top 100
CF1.STRINGVALUE AS 'ID OT',
jis.created,
jis.resolutiondate,
IIF(CF3.STRINGVALUE like 'IDR-%','SI','NO') AS 'Massive',
cfo8.customvalue AS 'Solved',
CASE WHEN jis.resolutiondate IS NULL
THEN 'NO'
WHEN LAG(resolutiondate) OVER (PARTITION BY 'ID OT' ORDER BY resolutiondate) > DATEADD(DAY, -7, resolutiondate)
AND LAG(CF3.STRINGVALUE) OVER (PARTITION BY 'ID OT' ORDER BY resolutiondate) = 'NO'
AND LAG(cfo8.customvalue) OVER (PARTITION BY 'ID OT' ORDER BY resolutiondate) = 'NO'
THEN 'NO'
ELSE 'YES'
END AS SLA
FROM [DWH].[JIR].[jiraissue] jis
LEFT JOIN [DWH].[JIR].[customfieldvalue] CF1 ON (CF1.issue = jis.id AND CF1.CUSTOMFIELD = 10004)
LEFT JOIN [DWH].[JIR].[customfieldvalue] CF3 ON (CF3.issue = jis.id AND CF3.CUSTOMFIELD = 10032)
LEFT JOIN [DWH].[JIR].[customfieldvalue] CF14 ON (CF14.issue = jis.id AND CF14.CUSTOMFIELD = 10906)
LEFT JOIN [DWH].[JIR].customfieldoption cfo8 ON (CF14.customfield = cfo8.customfield AND CF14.stringvalue=CAST(cfo8.id AS CHAR))
【问题讨论】:
【参考方案1】:我认为这是你想要的逻辑:
select t.*,
(case when massive = 'SI' then 'NO'
when lag(resolutiondate) over (partition by key) <
dateadd(-7, day, creationdate
then 'No'
else 'Si'
end) as SLA
from t;
【讨论】:
【参考方案2】:如果我没听错,你可以使用窗口函数:
select t.*,
case
when lag(resolutiondate) over(partition by key order by resolutiondate) > dateadd(day, -7, resolutiondate)
and lag(massive) over(partition by key order by resolutiondate) = 'NO'
then 'SI'
else 'NO'
end as sla
from mytable t
这会将当前行的日期与上一行的日期进行比较,并检查上一行的massive
状态。
也许您想检查当前行而不是前一行的状态 - 这有点简单:
select t.*,
case
when lag(resolutiondate) over(partition by key order by resolutiondate) > dateadd(day, -7, resolutiondate)
and massive = 'NO'
then 'SI'
else 'NO'
end as sla
from mytable t
【讨论】:
【参考方案3】:按照这种编写方式,似乎没有必要评估键列。每个密钥的解决日期相同。像这样的
select t.*,
case when t.massive='No' and dt.diff<7
then 'Si'
else 'No' end SLA
from tTable t
cross apply (values (datediff(day, created, resolutiondate))) dt(diff);
【讨论】:
以上是关于识别具有相同 ID 的列的主要内容,如果未能解决你的问题,请参考以下文章