我有多行数据都共享相同的公司ID。
有没有办法使用SQL Server Management Studio 2005“汇总”所有金额,以便为每个公司ID提供一行数据?
例如我目前有以下数据......
Company_Name Company_ID Amount
Company 6 10024 120
Company 6 10024 569
Company 6 10024 53
Company 6 10024 100
Company 6 10024 564
Company 7 10638 9500
Company 7 10638 105
Company 7 10638 624
我想尝试得到的是.......
Company_ Name Company_ID Amount
Company 6 10024 1406
Company 7 10638 10229
有没有办法做到这一点?
任何以正确方式指出我的建议都会很棒。
谢谢,
答案 0 :(得分:7)
SELECT Company_Name, Company_ID, SUM(Amount)
FROM TableName GROUP BY Company_Name, Company_ID
答案 1 :(得分:3)
SELECT Company_Name, Company_ID, sum(Amount)
FROM your table
GROUP BY Company_Name, Company_ID
答案 2 :(得分:3)
试试这个;
SELECT Company_Name, Company_ID, SUM(Amount) AS Amount
FROM Companies
GROUP BY Company_Name, Company_ID;
演示here。
答案 3 :(得分:2)
SELECT Company_Name, Company_ID, sum(Amount)
FROM table
group by Company_Name, Company_ID
答案 4 :(得分:1)
您需要使用GROUP BY
和SUM
功能。
SELECT Company_Name, Company_ID, SUM(Amount) AS TOTAL_AMOUNT
FROM myTable
GROUP BY Company_Name, Company_ID
答案 5 :(得分:1)
WITH T ( Company_Name, Company_ID, Amount )
AS
(
SELECT 'Company 6', '10024', 120 UNION ALL
SELECT 'Company 6', '10024', 569 UNION ALL
SELECT 'Company 6', '10024', 53 UNION ALL
SELECT 'Company 6', '10024', 100 UNION ALL
SELECT 'Company 6', '10024', 564 UNION ALL
SELECT 'Company 7', '10638', 9500 UNION ALL
SELECT 'Company 7', '10638', 105 UNION ALL
SELECT 'Company 7', '10638', 624
)
SELECT DISTINCT Company_Name, Company_ID,
SUM(Amount) OVER (PARTITION BY Company_ID) AS total_amount
FROM T;
答案 6 :(得分:0)
为我工作:
select Company_Name , Company_ID ,sum(Amount) as 'Amount' from Company group by
Company_Name,Company_ID ;
答案 7 :(得分:0)
create table #tempTable
(
Id bigint,
Title varchar(100),
Amount money
)
insert into #TempTable(Title,Id,Amount)
Values
('Company 6', 10024, 120),
('Company 6', 10024, 569),
('Company 6', 10024, 53),
('Company 6', 10024, 100),
('Company 6', 10024, 564),
('Company 7', 10638, 9500),
('Company 7', 10638, 105),
('Company 7', 10638, 624)
select Title, Sum(Amount)Amount from #TempTable
group by Title,ID
drop Table #TempTable