1

我有一个h包含这样数据的表(好吧,不是真的,这只是一个例子):

subj_id  q1  q2  q3  q4  q5  q6  num
      1   1   0   0   1   0   0    1
      1   0   0   0   1   0   0    2
      2   1   1   1   1   0   1    1
      2   1   0   0   1   0   0    2
      2   1   1   1   0   0   1    3
      3   0   1   0   0   1   1    1

我想总结每个 q 的subj_id结果,得到如下输出:

subj_id   num1   num2   num3  
      1      2      1   null
      2      5      2      4
      3      3   null   null

但相反,我得到以下信息:

subj_id   num1   num2   num3
      1      2      1   null
      1      2      1   null
      2      5      2      4
      2      5      2      4
      2      5      2      4
      3      3   null   null

subj_id其中求和的行重复的次数与表中出现的次数一样多。

我的查询(postgres)如下所示:

select h.subj_id, n1.sum as num1, n2.sum as num2, n3.sum as num3 from ((( h
    left join (select subj_id, q1+q2+q3+q4+q5+q6 as sum from h where num=1) as n1 on h.subj_id=n1.subj_id)
    left join (select subj_id, q1+q2+q3+q4+q5+q6 as sum from h where num=2) as n2 on h.subj_id=n2.subj_id)
    left join (select subj_id, q1+q2+q3+q4+q5+q6 as sum from h where num=3) as n3 on h.subj_id=n3.subj_id) order by h.subj_id

左连接显然不是在这里使用的技巧,但是如何跳过重复行?

提前致谢!

4

1 回答 1

1

您的查询可以很容易地修改为:

with cte as (
    select subj_id, q1 + q2 + q3 + q4 + q5 + q6 as q, num
    from h
)
select
    subj_id,
    sum(case when num = 1 then q end) as num1,
    sum(case when num = 2 then q end) as num2,
    sum(case when num = 3 then q end) as num3
from cte
group by subj_id
order by subj_id

我认为计划会更好 - 根本没有加入。

=> sql 小提琴演示

简要说明为什么您的查询不起作用以及如何改进它:

  • 您会收到更多您想要的行,因为基本上您的查询所做的是从表中选择每一行,然后从表中加入h它的总和。您的初始表中有 6 行,您的结果中有 6 行是合乎逻辑的;hnum = 1, 2, 3
  • 如果您要进行这样的查询,我强烈建议您在内部查询中为表使用别名。我会帮助你理解查询。在某些情况下,它还可以帮助您避免不正确的结果 - 请参阅我在本主题中的答案 - SQL IN 查询会产生奇怪的结果

-

select
    h.subj_id, n1.sum as num1, n2.sum as num2, n3.sum as num3
from h
    left join (
        select h1.subj_id, h1.q1+h1.q2+h1.q3+h1.q4+h1.q5+h1.q6 as sum
        from h as h1
        where h1.num=1
     ) as n1 on h.subj_id=n1.subj_id
    left join (
        select h2.subj_id, h2.q1+h2.q2+h2.q3+h2.q4+h2.q5+h2.q6 as sum
        from h as h2
        where h2.num=2
    ) as n2 on h.subj_id=n2.subj_id
    left join (
        select h3.subj_id, h3.q1+h3.q2+h3.q3+h3.q4+h3.q5+h3.q6 as sum
        from h as h3
        where h3.num=3
    ) as n3 on h.subj_id=n3.subj_id
order by h.subj_id
于 2013-09-06T04:59:45.133 回答