Sql 比较两组数据
非常抱歉,如果已经以某种方式回答了这个问题。我查遍了,弄不清楚 我需要在postgresql中找到一种方法来比较每周的数据。所有数据都存在于同一个表中,并具有周数列。数据不会总是完全重叠,但我需要在组内比较数据 假设以下是数据集:Sql 比较两组数据,sql,postgresql,Sql,Postgresql,非常抱歉,如果已经以某种方式回答了这个问题。我查遍了,弄不清楚 我需要在postgresql中找到一种方法来比较每周的数据。所有数据都存在于同一个表中,并具有周数列。数据不会总是完全重叠,但我需要在组内比较数据 假设以下是数据集: Week 2 +--------+--------+------+---------+-------+ | group | num | color| ID | week #| +--------+--------+------+---------+-
Week 2
+--------+--------+------+---------+-------+
| group | num | color| ID | week #|
+--------+--------+------+---------+-------+
| a | 1 | red | a1red | 2 |
| a | 2 | blue | a2blue | 2 |
| b | 3 | blue | b3blue | 2 |
| c | 7 | black| c7black | 2 |
| d | 8 | black| d8black | 2 |
| d | 9 | red | d9red | 2 |
| d | 10 | gray | d10gray | 2 |
+--------+--------+------+---------+-------+
Week 3
+--------+--------+------+---------+-------+
| group | num | color| ID | week #|
+--------+--------+------+---------+-------+
| a | 1 | red | a1red | 3 |
| a | 2 | green| a2green | 3 |
| b | 3 | blue | b3blue | 3 |
| b | 5 | green| b5green | 3 |
| c | 7 | black| c7black | 3 |
| e | 11 | blue | d11blue | 3 |
| e | 12 | other| d12other| 3 |
| e | 14 | brown| d14brown| 3 |
+--------+--------+------+---------+-------+
每行都有一个由组、编号和颜色值组成的ID
我需要查询以获取第3周中的所有组,然后获取第2周中存在的第3周中的任何组:
select coalesce(w1.group_nr, w2.group_nr) as group_nr,
coalesce(w1.num, w2.num) as num,
case
when w1.group_nr is null then 'missing in first week'
when w2.group_nr is null then 'missing in second week'
when (w1.color, w1.id) is distinct from (w2.color, w2.id) then 'data has changed'
else 'no change'
end as status,
case
when
w1.group_nr is not null
and w2.group_nr is not null
and w1.color is distinct from w2.color then 'color is different'
end as color_change,
case
when
w1.group_nr is not null
and w2.group_nr is not null
and w1.id is distinct from w2.id then 'id is different'
end as id_change
from (
select group_nr, num, color, id, hstore
from data
where week = 2
) as w1
full outer join (
select group_nr, num, color, id
from data
where week = 3
) w2 on (w1.group_nr, w1.num) = (w2.group_nr, w2.num)
获取已更改的属性有点笨拙。如果您可以使用文本表示,则可以使用hstore
扩展来显示差异:
select coalesce(w1.group_nr, w2.group_nr) as group_nr,
coalesce(w1.num, w2.num) as num,
case
when w1.group_nr is null then 'missing in first week'
when w2.group_nr is null then 'missing in second week'
when (w1.color, w1.id) is distinct from (w2.color, w2.id) then 'data has changed'
else 'no change'
end as status,
w2.attributes - w1.attributes as changed_attributes
from (
select group_nr, num, color, id, hstore(data) - 'week'::text as attributes
from data
where week = 2
) as w1
full outer join (
select group_nr, num, color, id, hstore(data) - 'week'::text as attributes
from data
where week = 3
) w2 on (w1.group_nr, w1.num) = (w2.group_nr, w2.num);
表中隐藏的周数在哪里?它只是另一列。更新了表格来澄清这一点。啊!非常感谢你。在你规划好基本的思维过程之后,一切都很顺利,现在运行得很好。