【问题标题】:Join two tables and convert rows in columns连接两个表并转换列中的行
【发布时间】:2021-04-09 23:43:10
【问题描述】:

我有两张桌子:

表 1

    date1    | id  | t_code | ac_code |  value | kt_id  |
-------------+-----+--------+---------+--------+--------+
 2021-05-07  |  13 | AF12   | #BT8    |    1   | 11201  |
 2021-05-07  |  13 | AF12   | #BT8    |    1   | 11201  |
 2021-05-07  |  13 | AF12   | #BT8    |    2   | 11208  |
 2021-04-08  |  13 | SH11   | #GS5    |    3   | 11201  |
 2021-03-15  |  15 | TG53   | #BA7    |    1   | 11207  |
 2021-03-11  |  17 | AF12   | #BT8    |    2   | 11208  |
 2021-03-11  |  14 | AF15   | #BT5    |    2   | 11209  |

表 2

    date2    | id  | t_code |  value | kt_id  |
-------------+-----+--------+--------+--------+
 2021-05-07  |  13 | AF12   |    5   | 23201  |
 2021-05-07  |  24 | TB39   |    1   | 23203  |
 2021-05-07  |  13 | AF12   |    2   | 23208  |
 2021-04-08  |  13 | AF12   |    3   | 23201  |
 2021-03-11  |  15 | TG53   |    1   | 23207  |
 2021-03-11  |  28 | AK21   |    2   | 23208  |

我想通过内部连接来连接这两个表。 on Condition 应包含日期 (date1, date2)。此外,我将在列中转换行。这意味着,我只想在 kt_id 上选择 id 的 11201(作为 typ_1)和 11208(作为 typ_2)并将它们的值相加。 在表 2 中,我只想在 kt_id 上选择 id 23201(作为 typ_3)。 “id”、“t_code”和日期应该是一样的。

所以我想得到关注结果:

    date     | id  | t_code | typ_1  | typ_2 | typ_3 |
-------------+-----+--------+--------+-------+-------+
 2021-05-07  |  13 | AF12   |   2    |   2   |   5   |
 2021-04-08  |  13 | TB39   |   3    |   0   |   3   |

如何用 sql 查询解决这个问题?

【问题讨论】:

    标签: sql sql-server


    【解决方案1】:

    这应该给你你所需要的,给出解释。使用SUM CASE

    编辑:查询现在在加入之前将两个表中的 CTE 中的数据展平,这样它就不会在一对多的情况下散开并夸大值。

    ;with t1
    as (select date1, id, t_code, kt_id, sum([value]) as [value]
        from table1 
        group by date1, id, t_code, kt_id)
        , t2
        as (select date1, id, t_code, kt_id, sum([value]) as [value]
            from table1 
            group by date1, id, t_code, kt_id)
    
    select t1.date1, t1.id, t1.t_code,
            sum(case when t1.kt_id='11201' then t1.[value] else 0 end) as typ_1,
            sum(case when t1.kt_id='11208' then t1.[value] else 0 end) as typ_2,
            sum(case when t2.kt_id='23201' then t2.[value] else 0 end) as typ_3
    from t1
    join t2
        on t1.id = t2.id
        and t1.t_code = t2.t_code
    group by t1.date1, t1.id, t1.t_code
    order by t1.date1, t1.id, t1.t_code
    

    【讨论】:

    • 谢谢。但是如果我插入 where 子句来限制结果,例如:where t1.id = 13 and t1.t_code = 'AF12' 我得到的 typ_1、typ_2 和 typ_3 的值太高
    • 我已经编辑了上面的查询,以防两个表中有多个匹配项。
    • 我们不能只用一个查询来解决它,对吗?
    • 这是一个查询。这是一个公用表表达式 (CTE)。在此处查看有关它们的更多信息sqlshack.com/sql-server-common-table-expressions-cte
    • 谢谢,查询很合适。我只有一个额外的要求。我必须为 rt_value = 0 过滤表二。仅适用于表二。 rt_value列只有两个值,1和0。我只想选择到0。我已经实现了,但是没有得到typ_3列的任何值。 .... , t2 as (select date1, id, t_code, kt_id, rt_value, sum([value]) as [value] from table1 group by date1, id, t_code, kt_id, rt_value)..... AND t2.rt_value = 0 ....
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-11-20
    • 1970-01-01
    • 1970-01-01
    • 2014-06-22
    • 2019-05-06
    • 2021-07-30
    • 2017-06-14
    相关资源
    最近更新 更多