如何在SQL中获取列明智的总和?

发布于 2025-01-31 06:07:03 字数 1841 浏览 4 评论 0原文

我有一个复杂的查询,其中我可以在单独的列中获得各种类别的计数。

这是我查询的输出:

    district |  colA  |  colB  |  colC
    ------------------------------------
    DistA    |  1     |   1    |   3
    DistB    |  2     |   0    |   2
    DistC    |  2     |   1    |   0
    DistD    |  0     |   3    |   4
    ..

这是我的查询:

select
  q1."district",
  coalesce(max(case q1."type" when 'colA' then q1."type_count" else 0 end), 0) as "colA",
  coalesce(max(case q1."type" when 'colB' then q1."type_count" else 0 end), 0) as "colB",
  coalesce(max(case q1."type" when 'colC' then q1."type_count" else 0 end), 0) as "colC"
from (
  select
    d."name" as "district",
    t."name" as "type",
    count(t.id) as "type_count"
  from
    main_entity as m
  inner join type_entity as t on
    m."type_id" = t.id
  inner join district as d on 
    m."district_id" = d.id
  where
    m."delete_at" is null
  group by
    d."name",
    t.id
) as q1
group by
  q1."district"

我想修改此查询,以便我可以在最后一行中获取每列的总和,类似的内容:

 district |  colA  |  colB  |  colC
    ------------------------------------
    DistA    |  1     |   1    |   3
    DistB    |  2     |   0    |   2
    DistC    |  2     |   1    |   0
    DistD    |  0     |   3    |   4
    ..
    Total    |  5     |   5    |   9

我尝试使用>使用group。 + lollup带有上述查询,只需添加以下内容:

...
group by rollup (q1."district")

它在底部添加一个行,但值与前一行的值相似,而不是前行之前的所有行总和,基本上是这样的东西:

 district |  colA  |  colB  |  colC
    ------------------------------------
    DistA    |  1     |   1    |   3
    ..
    DistD    |  0     |   3    |   4
    Total    |  0     |   3    |   4

那么,我该如何从查询中获得一些列?

I have a complex query where I am getting the count of various categories in separate columns.

Here's the output of my query:

    district |  colA  |  colB  |  colC
    ------------------------------------
    DistA    |  1     |   1    |   3
    DistB    |  2     |   0    |   2
    DistC    |  2     |   1    |   0
    DistD    |  0     |   3    |   4
    ..

And here's my query:

select
  q1."district",
  coalesce(max(case q1."type" when 'colA' then q1."type_count" else 0 end), 0) as "colA",
  coalesce(max(case q1."type" when 'colB' then q1."type_count" else 0 end), 0) as "colB",
  coalesce(max(case q1."type" when 'colC' then q1."type_count" else 0 end), 0) as "colC"
from (
  select
    d."name" as "district",
    t."name" as "type",
    count(t.id) as "type_count"
  from
    main_entity as m
  inner join type_entity as t on
    m."type_id" = t.id
  inner join district as d on 
    m."district_id" = d.id
  where
    m."delete_at" is null
  group by
    d."name",
    t.id
) as q1
group by
  q1."district"

I want to modify this query so that I can get the sum of each column in the last row, something like this:

 district |  colA  |  colB  |  colC
    ------------------------------------
    DistA    |  1     |   1    |   3
    DistB    |  2     |   0    |   2
    DistC    |  2     |   1    |   0
    DistD    |  0     |   3    |   4
    ..
    Total    |  5     |   5    |   9

I have tried using group by + rollup with the above query by just adding the following:

...
group by rollup (q1."district")

It adds a row at the bottom but the values are similar to the values of a row before it, and not the sum of all the rows before it, so basically something like this:

 district |  colA  |  colB  |  colC
    ------------------------------------
    DistA    |  1     |   1    |   3
    ..
    DistD    |  0     |   3    |   4
    Total    |  0     |   3    |   4

So, how can I get the column-wise some from my query?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

千纸鹤带着心事 2025-02-07 06:07:03

尝试以下操作:

With temp as 
(    --your query from above
    select
      q1."district",
      coalesce(max(case q1."type" when 'colA' then q1."type_count" else 0 end), 0) as "colA",
      coalesce(max(case q1."type" when 'colB' then q1."type_count" else 0 end), 0) as "colB",
      coalesce(max(case q1."type" when 'colC' then q1."type_count" else 0 end), 0) as "colC"
    from (
      select
        d."name" as "district",
        t."name" as "type",
        count(t.id) as "type_count"
      from
        main_entity as m
      inner join type_entity as t on
        m."type_id" = t.id
      inner join district as d on 
        m."district_id" = d.id
      where
        m."delete_at" is null
      group by
        d."name",
        t.id
    ) as q1
    group by
      q1."district"
)

select t.* from temp t
UNION
select sum(t1.colA),sum(t1.colB),sum(t1.colC) from temp t1

Try this:

With temp as 
(    --your query from above
    select
      q1."district",
      coalesce(max(case q1."type" when 'colA' then q1."type_count" else 0 end), 0) as "colA",
      coalesce(max(case q1."type" when 'colB' then q1."type_count" else 0 end), 0) as "colB",
      coalesce(max(case q1."type" when 'colC' then q1."type_count" else 0 end), 0) as "colC"
    from (
      select
        d."name" as "district",
        t."name" as "type",
        count(t.id) as "type_count"
      from
        main_entity as m
      inner join type_entity as t on
        m."type_id" = t.id
      inner join district as d on 
        m."district_id" = d.id
      where
        m."delete_at" is null
      group by
        d."name",
        t.id
    ) as q1
    group by
      q1."district"
)

select t.* from temp t
UNION
select sum(t1.colA),sum(t1.colB),sum(t1.colC) from temp t1
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文