将同一列中的值与 SQL 服务器中的其他列进行比较
Compare the values within the same column against other columns in SQL Server
我有一个table如下
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
我想添加一个包含值的列 - 有意或无意
如果具有 Act 的 HandleName 与具有 QL 的 HandleName 具有相同的 ID、CreatedDate 和 ParentName,那么它是有意的.我要找的最后 table 是
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-chart-act' 的 HandleName 是无意的,因为 ParentName 与 'MI-Chart-QL'
不匹配
我使用了下面的代码(如果 Row_Number()>2 我可以有意地标记它们)但是我如何检查它们的父名称是否相同以有意或无意地标记它们?
Row_Number() over (Partition by id, CreatedDate ORDER BY createdDate asc)
您可以使用 window 函数:
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
这是另一种方式:
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;
我有一个table如下
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
我想添加一个包含值的列 - 有意或无意 如果具有 Act 的 HandleName 与具有 QL 的 HandleName 具有相同的 ID、CreatedDate 和 ParentName,那么它是有意的.我要找的最后 table 是
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-chart-act' 的 HandleName 是无意的,因为 ParentName 与 'MI-Chart-QL'
不匹配我使用了下面的代码(如果 Row_Number()>2 我可以有意地标记它们)但是我如何检查它们的父名称是否相同以有意或无意地标记它们?
Row_Number() over (Partition by id, CreatedDate ORDER BY createdDate asc)
您可以使用 window 函数:
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
这是另一种方式:
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;