2017-03-08 90 views
1

我想在TSQL来计算的列的内容的dependendence的内容的依赖性,另一列的内容schould是在分子或分母TSQL计算在coolumn

我有一个表与以下内容:

+------------+----------+----+-------------------------+ 
| machine-nr | duration | ID | timestamp    | 
+------------+----------+----+-------------------------+ 
| 1108  | 47  | 11 | 2017-02-11 00:08:15.000 | 
| 1112  | 195  | 2 | 2017-02-11 00:13:13.000 | 
| 1108  | 339  | 7 | 2017-02-11 00:13:54.000 | 
| 1108  | 19  | 2 | 2017-02-11 00:13:54.000 | 
| 1112  | 20  | 11 | 2017-02-11 00:13:33.000 | 
+------------+----------+----+-------------------------+ 

现在我想要做这种计算
与ID 11的每个时间应在股息和所有期限的,ID分别为11,2和7 schould处于总和除数。 类似的东西(我知道它不工作方式):

SUM(duration if ID = 11)/SUM(duration if ID =11) + SUM(duration if ID = 2) + SUM(Duration if ID = 7) 
group by machine-nr 

我希望你知道我的意思。

+0

'tsql'不是'mysql' ...那么为什么这两个标签? – Rahul

回答

0
select machine_nr, 
     sum(case when id = 11 then duration end) 
     /sum(case when id in (11,2,7) then duration end) as end_calc 
from MyTable 
group by machine_nr 
+0

谢谢。我的工作很好 – Jan