Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/26.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 求和数据列,其中在求和之前计算两行_Sql_Sql Server_Tsql - Fatal编程技术网

Sql 求和数据列,其中在求和之前计算两行

Sql 求和数据列,其中在求和之前计算两行,sql,sql-server,tsql,Sql,Sql Server,Tsql,我有一个多栏的表格 Code Events Time AVG A 1 1 1 B 2 2 1 C 10 5 2 我需要将事件和总行相加,但当涉及到特定代码(B和C)时,我需要将事件除以2,然后将该数字包含在总和中 因此,上述输出将是 TotalEvents TotalTime 7 4.5 事件总数=1+(2/2)+(10/2)

我有一个多栏的表格

Code    Events    Time    AVG
A       1         1       1
B       2         2       1
C       10        5       2
我需要将事件和总行相加,但当涉及到特定代码(B和C)时,我需要将事件除以2,然后将该数字包含在总和中

因此,上述输出将是

TotalEvents    TotalTime
7              4.5
事件总数=1+(2/2)+(10/2)=7

总时间=1+(2/2)+(5/2)=4.5

感谢您的帮助。 谢谢这是你想要的吗

select sum(case when code in ('B', 'C') then events / 2.0 else events end) as total_events,
       sum(case when code in ('B', 'C') then time / 2.0 else time end) as total_time
from t;

有什么问题吗?您是否熟悉
案例。。。结束
construct?完全结束!!我当时在试这个案子,但我不愿意在陈述时把总数放在案子里。只是我从来没有意识到要把整件事都写进sum()里。非常感谢。