Sql 比较两组数据

Sql 比较两组数据,sql,postgresql,Sql,Postgresql,非常抱歉,如果已经以某种方式回答了这个问题。我查遍了,弄不清楚 我需要在postgresql中找到一种方法来比较每周的数据。所有数据都存在于同一个表中,并具有周数列。数据不会总是完全重叠,但我需要在组内比较数据 假设以下是数据集: Week 2 +--------+--------+------+---------+-------+ | group | num | color| ID | week #| +--------+--------+------+---------+-

非常抱歉,如果已经以某种方式回答了这个问题。我查遍了,弄不清楚

我需要在postgresql中找到一种方法来比较每周的数据。所有数据都存在于同一个表中,并具有周数列。数据不会总是完全重叠,但我需要在组内比较数据

假设以下是数据集:

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周中的任何组:

  • 组中已更改的标志ID,如组A中的标志ID
  • 标记是否向组中添加或删除了任何ID,如在组B中
  • 有一个功能很好,但不是必需的,那就是对第2周中不存在的组进行第3周与第1周的比较

    我曾想过尝试将这两周划分为两周,并使用intercept/except来获得结果,但我无法完全理解如何才能使其正常工作。任何提示都将不胜感激。

    在两(已知)周内,您可以执行以下操作:

    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);
    

    表中隐藏的周数在哪里?它只是另一列。更新了表格来澄清这一点。啊!非常感谢你。在你规划好基本的思维过程之后,一切都很顺利,现在运行得很好。