加入收藏 | 设为首页 | 会员中心 | 我要投稿 安卓应用网 (https://www.0791zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 数据库 > MsSql > 正文

sql – 按两列分组,并在每行中显示总计

发布时间:2020-05-24 17:07:38 所属栏目:MsSql 来源:互联网
导读:以下是列表数据. Code ItemCount Type Amount ----------------------------------------B001 1 Dell 10.00B001 1 Dell 10.00B001 1 Apple 10.00

以下是列表数据.

Code   ItemCount   Type      Amount 
----------------------------------------
B001    1          Dell         10.00
B001    1          Dell         10.00
B001    1          Apple        10.00
B001    2          Apple        20.00
B001    2          Apple        20.00
B114    1          Apple        30.50
B114    1          Apple        10.00

我需要一个结果,按代码和类型进行分组,并且总计ItemCount,并获得每行中的Amount的总计.

这可能吗?

Code   ItemCount    Type      Amount 
----------------------------------------
B001    2          Dell          110.50
B001    5          Apple         110.50
B114    2          Apple         110.50

解决方法

请尝试:
SELECT
    Code,SUM(ItemCount) ItemCount,Type,(SELECT SUM(Amount) FROM YourTable) Amount
FROM
    YourTable
GROUP BY Code,Type

(编辑:安卓应用网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读