我正在使用sqlserver,它有以下表格,
id companyname credited used
----------------------------------------
1 ABC 100 0
2 ABC 10 0
3 BBB 0 10
4 BBB 0 10
5 BBB 100 0
6 BBB 10 0
我的预期输出如下
id companyname credited used sum_credited sum_used
--------------------------------------------------------------------------
1 ABC 100 0 110 0
2 ABC 10 0 110 0
3 BBB 0 10 0 20
4 BBB 0 10 0 20
5 BBB 100 0 110 0
6 BBB 10 0 110 0
我使用了下面的查询,其中我没有得到所谓的输出
SELECT id,companyname,Credited,Used, SUM(credited) 'Sum_Credited'
FROM tmptichistory1
GROUP BY id,companyname,Credited,Used
我想找到贷记和使用值的总和,并将其显示在原始值的不同列中,如第一个表中所述。任何帮助都很明显。
答案 0 :(得分:3)
sum group by companyname
;
错误(感谢@bluefeet
)已更正为CASE
here is fiddle:
;with cte as
(
select companyname, sum(credited) sum_credited, sum(used) sum_used
from tmptichistory1
group by companyname
)
select t1.id, t1.companyname, t1.credited, t1.used,
case t1.credited when 0 then 0 else cte.sum_credited end sum_credited,
case t1.used when 0 then 0 else cte.sum_used end sum_used
from tmptichistory1 t1 join cte
on t1.companyname = cte.companyname
答案 1 :(得分:1)
SELECT t1.id,
t2.companyname,
t1.credited,
t1.used,
t2.sum_credited,
t2.sum_used
FROM yourTable t1
INNER JOIN
(
SELECT companyname, SUM( credited) sum_credited , SUM(used) sum_used
FROM yourTable
WHERE credited = 0
GROUP BY companyname
UNION
SELECT companyname, SUM( credited) sum_credited , SUM(used) sum_used
FROM yourTable
WHERE used = 0
GROUP BY companyname
) t2 ON t1.companyname = t2.companyname
WHERE (t1.used = 0 AND t2.sum_used = 0) OR (t1.credited = 0 AND t2.sum_credited = 0)
答案 2 :(得分:1)
试试这个可能对你有帮助..
select e1.Id,e1.CompanyNmae, e1.credited, e1.used,e2.SumCredited,e2.SumUsed
from Example e1
inner join (select CompanyNmae, sum(credited) as SumCredited, sum(used) as SumUsed
from Example group by CompanyNmae) as e2
on e1.CompanyNmae=e2.CompanyNmae
答案 3 :(得分:1)
如果您使用的是2008或更高版本,则可以使用以下分析函数:
select
id,
companyname,
credited,
used,
sum(credited) over (partition by companyname) sum_credited,
sum(used) over (partition by companyname) sum_used
from
tmptichistory1
但是当我针对你的数据运行它时,我得到以下输出:
+----+-------------+----------+------+--------------+----------+
| id | companyname | credited | used | sum_credited | sum_used |
+----+-------------+----------+------+--------------+----------+
| 1 | ABC | 100 | 0 | 110 | 0 |
| 2 | ABC | 10 | 0 | 110 | 0 |
| 3 | BBB | 0 | 10 | 110 | 20 |
| 4 | BBB | 0 | 10 | 110 | 20 |
| 5 | BBB | 100 | 0 | 110 | 20 |
| 6 | BBB | 10 | 0 | 110 | 20 |
+----+-------------+----------+------+--------------+----------+
...所以我认为id = 5和id = 6的行应该是不同的公司名称?如果我们说公司名称CCC,那么我们得到以下输出:
+----+-------------+----------+------+--------------+----------+
| id | companyname | credited | used | sum_credited | sum_used |
+----+-------------+----------+------+--------------+----------+
| 1 | ABC | 100 | 0 | 110 | 0 |
| 2 | ABC | 10 | 0 | 110 | 0 |
| 3 | BBB | 0 | 10 | 0 | 20 |
| 4 | BBB | 0 | 10 | 0 | 20 |
| 5 | CCC | 100 | 0 | 110 | 0 |
| 6 | CCC | 10 | 0 | 110 | 0 |
+----+-------------+----------+------+--------------+----------+
看起来更像你的例子。