1

我正在使用具有下表的 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

我想找到贷记值和使用值的总和,并将其显示在具有原始值的不同列中,正如我在第一个表中提到的那样。任何帮助都是可观的。

4

4 回答 4

3

sum group by companyname;

@bluefeet用这里纠正的错误(谢谢)CASE 是小提琴

;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
于 2012-12-21T11:17:27.807 回答
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)
于 2012-12-21T11:15:43.843 回答
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
于 2012-12-21T11:39:00.853 回答
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 的行可能应该用于不同的公司名称?如果我们说 companyname 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 |
+----+-------------+----------+------+--------------+----------+

这看起来更像您在示例中的内容。

于 2012-12-21T11:41:57.030 回答