我正在尝试根据ID添加总佣金,但这会产生单独的佣金金额。我需要根据去年提取数据
SELECT
fld.fa_id
,sum(commission_amt) as 'Commission Amount'
,date_id
FROM [dbo].[fa_dim] fa, [dbo].[product_summary] fld
WHERE fa.fa_id = fld.fa_id
AND fld.date_id > 20170523
GROUP BY fld.fa_id , fld.commission_amt , date_id
ORDER BY fld.fa_id
结果我看起来像这样:
ID Commission
7 50
7 12
7 5
8 20
8 10
我需要它们看起来像这样:
ID Commission
7 67
8 30
答案 0 :(得分:4)
删除组中的commission_amt
列列表:
SELECT
fld.fa_id
,sum(commission_amt) as 'Commission Amount'
,date_id
FROM [dbo].[fa_dim] fa, [dbo].[product_summary] fld
WHERE fa.fa_id = fld.fa_id
AND fld.date_id > 20170523
GROUP BY fld.fa_id , date_id
ORDER BY fld.fa_id
答案 1 :(得分:1)
group by
和select
:
SELECT fld.fa_id, sum(commission_amt) as [Commission Amount]
FROM [dbo].[fa_dim] fa JOIN
[dbo].[product_summary] fld
ON fa.fa_id = fld.fa_id AND fld.date_id > 20170523
GROUP BY fld.fa_id
ORDER BY fld.fa_id;
group by
应包含唯一定义结果集中每一行的键。因为您希望每个fld.fa_id
占一行,这是GROUP BY
中唯一需要的列。
请注意,我还修复了JOIN
语法。学习使用正确,明确,标准的JOIN
语法。将列别名放在单引号中也是一个坏主意。 SQL Server中的转义字符是方括号。更好的是不需要转义的列别名。