我有一张桌子如下
id ParentName HandleName CreatedDate
===================================================
139 MI MI-Chart-QL 2018-02-20
139 MI MI-chart-act 2018-02-20
139 MI MI-chart-act 2018-02-20
139 CRA CRA-chart-act 2018-02-20
139 CRA CRA-Chart-act 2018-02-20我想添加一个带有值的列--如果HandleName with Act具有与QL的id、CreatedDate和ParentName相同的id、CreatedDate和ParentName,则它是故意的。
id ParentName HandleName CreatedDate Intentionally/unintentionally
====================================================================================
139 MI MI-Chart-QL 2018-02-20 Intentionally
139 MI MI-chart-act 2018-02-20 Intentionally
139 MI MI-chart-act 2018-02-20 Intentionally
139 CRA CRA-chart-act 2018-02-20 Unintentionally
139 CRA CRA-Chart-act 2018-02-20 Unintentionally 带有“CRA-图表-act”的HandleName是无意中的,因为ParentName与“MI-图表-QL”不匹配。
我使用了下面的代码(如果Row_Number()>2,我可以有意地标记它们),但是如何检查它们的父名称是否相同,以有意或无意地标记它们呢?
Row_Number() over (Partition by id, CreatedDate ORDER BY createdDate asc)发布于 2020-11-10 21:45:57
您可以使用窗口函数:
select t.*,
case when max(case when handlename like '%-QL%' then 1 else 0 end)
over(partition by id, parentname, createddate) = 1
then 'Intentionally'
else 'Unintentionally'
end as status
from mytable t发布于 2020-11-10 22:05:19
另一种方法是:
SELECT
t1.*
, CASE
WHEN t2.Intentionally = 1 THEN 'Intentionally'
ELSE 'Unintentionally'
END AS 'Intentionally/unintentionally'
FROM
mytable t1
OUTER APPLY
(
SELECT TOP 1
1 AS 'Intentionally'
FROM
mytable t2
WHERE
t1.parentName = t2.parentname
AND t1.CreatedDate = t2.CreatedDate
AND t2.HandleName LIKE '%QL'
) t2;https://stackoverflow.com/questions/64777143
复制相似问题