2017-07-17 166 views
0

我有以下数据算术运算

Tid Did value 
------------------ 
1  123 100 
1  234 200 
2  123 323 
2  234 233 

所有TIDS有一个表DIDS 123和234.因此,对于具有分布式入侵检测系统123和234我想计算value of did 123/value of did 234 * 100每TID即100/200 * 100 对于TID 2它将value of did 123/value of did 234 * 100即233分之323* 100

输出表将是

Tid result 
------------------ 
1  100/200 * 100 
2  323/233 * 100 

任何帮助?

回答

0
select tid, 
     100 * sum(case when did = 123 then value end)/
      sum(case when did = 234 then value end) 
from your_table 
group by tid 
having sum(case when did = 234 then value end) > 0 
+0

在这里,如果do 234的值等于零 – Ritesh

+0

那么应该怎么办呢?你想从结果中排除这些记录吗? –

+0

是的,我希望通过添加where子句,这是哪里值<> 0.是否正确吗? – Ritesh

1

JOIN的 “123” 的行与 “234” 的行:

select t123.tid, t123.value * 100/t234.value 
from 
    (select tid, value from tablename where value = 123) t123 
join 
    (select tid, value from tablename where value = 234) t234 
    on t123.tid = t234.tid 
0

JOIN,所有在ON

select t123.tid, t123.value * 100/t234.value 
from tablename t123 
join tablename t234 on t123.tid = t234.tid and t123.did = 123 and t234.did = 234 
0

下面是该查询。我们可以使用内部连接来实现它。

SELECT T1.Tid,(T1.value/T2.value)*100 AS Result 
FROM Table_1 AS T1 
INNER JOIN 
Table_1 AS T2 
ON (T1.Tid = T2.Tid) 
AND (T1.Did <> T2.Did) 
AND T1.Did = 123