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

以下是列表数据.
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

相关文章

SELECT a.*,b.dp_name,c.pa_name,fm_name=(CASE WHEN a.fm_n...
if not exists(select name from syscolumns where name=&am...
select a.*,pano=a.pa_no,b.pa_name,f.dp_name,e.fw_state_n...
要在 SQL Server 2019 中设置定时自动重启,可以使用 Window...
您收到的错误消息表明数据库 'EastRiver' 的...
首先我需要查询出需要使用SQL Server Profiler跟踪的数据库标...