如何在 table 中获取 2 组记录之间的差异
How to get difference between 2 group of records in a table
请考虑这个table:
ID Code Parent SomeInfo Year
-----------------------------------------------------------------------
1 11100 00000/10000/11000 SomeInfo1 2016
2 11101 00000/10000/11000/11100 SomeInfo2 2016
3 11000 00000/10000 SomeInfo3 2016
4 12000 00000/10000 SomeInfo4 2016
5 12100 00000/10000/12000 SomeInfo5 2016
6 11100 00000/10000/11000 SomeInfo6 2016
7 11101 00000/10000/11000/11300 SomeInfo7 2017
8 11000 00000/10000 SomeInfo8 2017
9 14100 00000/10000/14000 SomeInfo9 2017
10 15100 00000/10000/15000 SomeInfo10 2017
11 16100 00000/10000/16000 SomeInfo11 2017
Parent Column是Top节点的路径(它是'00000')
我有 2 个问题:
1) 我如何根据 Year = 2016 区分 Parent 列。例如在上面的例子中 Code=11101 在年份中的 Parent 列有不同的值:2016 & 2017 .
期望输出是:
Code Parent2016 Parent2017
----------------------------------------------------------------
11101 00000/10000/11000/11100 00000/10000/11000/11300
2) 如何获取 2017 中存在而 2016 中不存在的不同代码?
期望输出是:
Code Parent
-------------------------------
14100 00000/10000/14000
15100 00000/10000/15000
16100 00000/10000/16000
谢谢
对于您的第一个查询,您可以在代码列上聚合 (GROUP BY
),并保留 2016 年和 2017 年具有两个不同父级的代码。
SELECT Code
FROM yourTable
WHERE Year IN (2016, 2017)
GROUP BY Code
HAVING COUNT(DISTINCT Parent) = 2
有多种方法可以回答您的第二个问题。我可能会使用的方法是将您 table 的 2017 年记录与您 table 的 2016 年记录进行自我连接,然后检查 2017 年的哪些旧代码与 2016 年的任何内容都不匹配。
SELECT
t1.Code,
t1.Parent
FROM yourTable t1
LEFT JOIN yourTable t2
ON t1.Code = t2.Code AND
t2.year = 2016
WHERE
t1.year = 2017 AND
t2.Code IS NULL
您可以使用 条件聚合
为 2016/17 年调整 parents
max(case when year = 2016 then Parent else '' end) AS Parent2016
然后申请
HAVING Parent2016 <> Parent2017
这导致
select code,
max(case when year = 2016 then Parent else '' end) AS Parent2016,
max(case when year = 2017 then Parent else '' end) AS Parent2017
from mytable
group by code
having
max(case when year = 2016 then Parent else '' end) <>
max(case when year = 2017 then Parent else '' end)
你的第二个问题简单地转化为相关子查询:
select code, parent
from mytable as t1
where year = 2017
and not exists
( select * from mytable as t2
where year = 2016
and t1.code = t2.code
)
请考虑这个table:
ID Code Parent SomeInfo Year
-----------------------------------------------------------------------
1 11100 00000/10000/11000 SomeInfo1 2016
2 11101 00000/10000/11000/11100 SomeInfo2 2016
3 11000 00000/10000 SomeInfo3 2016
4 12000 00000/10000 SomeInfo4 2016
5 12100 00000/10000/12000 SomeInfo5 2016
6 11100 00000/10000/11000 SomeInfo6 2016
7 11101 00000/10000/11000/11300 SomeInfo7 2017
8 11000 00000/10000 SomeInfo8 2017
9 14100 00000/10000/14000 SomeInfo9 2017
10 15100 00000/10000/15000 SomeInfo10 2017
11 16100 00000/10000/16000 SomeInfo11 2017
Parent Column是Top节点的路径(它是'00000')
我有 2 个问题:
1) 我如何根据 Year = 2016 区分 Parent 列。例如在上面的例子中 Code=11101 在年份中的 Parent 列有不同的值:2016 & 2017 .
期望输出是:
Code Parent2016 Parent2017
----------------------------------------------------------------
11101 00000/10000/11000/11100 00000/10000/11000/11300
2) 如何获取 2017 中存在而 2016 中不存在的不同代码?
期望输出是:
Code Parent
-------------------------------
14100 00000/10000/14000
15100 00000/10000/15000
16100 00000/10000/16000
谢谢
对于您的第一个查询,您可以在代码列上聚合 (GROUP BY
),并保留 2016 年和 2017 年具有两个不同父级的代码。
SELECT Code
FROM yourTable
WHERE Year IN (2016, 2017)
GROUP BY Code
HAVING COUNT(DISTINCT Parent) = 2
有多种方法可以回答您的第二个问题。我可能会使用的方法是将您 table 的 2017 年记录与您 table 的 2016 年记录进行自我连接,然后检查 2017 年的哪些旧代码与 2016 年的任何内容都不匹配。
SELECT
t1.Code,
t1.Parent
FROM yourTable t1
LEFT JOIN yourTable t2
ON t1.Code = t2.Code AND
t2.year = 2016
WHERE
t1.year = 2017 AND
t2.Code IS NULL
您可以使用 条件聚合
为 2016/17 年调整 parentsmax(case when year = 2016 then Parent else '' end) AS Parent2016
然后申请
HAVING Parent2016 <> Parent2017
这导致
select code,
max(case when year = 2016 then Parent else '' end) AS Parent2016,
max(case when year = 2017 then Parent else '' end) AS Parent2017
from mytable
group by code
having
max(case when year = 2016 then Parent else '' end) <>
max(case when year = 2017 then Parent else '' end)
你的第二个问题简单地转化为相关子查询:
select code, parent
from mytable as t1
where year = 2017
and not exists
( select * from mytable as t2
where year = 2016
and t1.code = t2.code
)