如何解决SQL:如何对 Sum 列求和?
我的查询返回我需要的列,但我想知道是否有办法获取另一列,该列将返回以下内容的总和:sum(CAST(qty AS int)) 作为 qty 列。如果每行都列出就可以了;我只是想要一种不必取出计算器并将所有总和(CAST(qty AS int))相加的方法。
select sql1.item_code,sql1.supplier_so,sum(sql1.qty) Qty,sql1.dt
from
(SELECT supplier_so,item_code,sum(CAST(qty AS int)) as qty,dt FROM "table.name"
where
supplier_so = 'XXXXXXX'
group by supplier_so,dt
UNION
SELECT supplier_so,dt FROM "table.name2"
where
supplier_so = 'XXXXXXX'
and
dt < date '2021-06-01'
group by supplier_so,dt
) sql1
group by supplier_so,dt
解决方法
您可以为此使用 window function:
SELECT
...,SUM(CAST(qty AS int)) OVER ()
FROM ...
,
你为什么不试试这个。
with sql1 as
(SELECT supplier_so,item_code,sum(CAST(qty AS int)) as qty,dt FROM "table.name"
where
supplier_so = 'XXXXXXX'
group by supplier_so,dt
UNION
SELECT supplier_so,dt FROM "table.name2"
where
supplier_so = 'XXXXXXX'
and
dt < date '2021-06-01'
group by supplier_so,dt
)
select sql1.item_code,sql1.supplier_so,sql1.dt,sum(sql1.qty) over () from sql1;
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。