-1
ID| FROM |  TO   |     Type    |
--|------|-------|-------------|
1 | Brij |Nilesh |   Call_In   |
--|------|-------|-------------|
2 |Jaydip| Vipul |   Call_In   |
--|------|-------|-------------|  
3 |Rajesh|Nilesh |   Call_In   |
--|------|-------|-------------|
4 |Jaydip| vipul |   Call_Out  |
--|------|-------|-------------|
5 |Jaydip| vipul |   Call_In   |
--|------|-------|-------------|
6 | Brij | Nilesh|   Call_Out  |
--|------|-------|-------------|
7 |vipul | Brij  |   Call_In   |

所以我曾经使用这个查询从数据中获取最频繁的对

Select FROM,TO, COUNT('FROM' & 'TO') AS howmany
from table
GROUP BY FROM,TO

这给了我这样的结果

  FROM  |   TO   |howmany|
--------|--------|-------|
  Brij  | Nilesh |   2   |
--------|--------|-------|
 Jaydip | Vipul  |   3   |
--------|--------|-------|
 Rajesh | Nilesh |   1   |
--------|--------|-------|
 vipul  | Brij   |   1   |

但我也想要这些组中的呼叫类型的总和,例如

Brij  | Nilesh |howmany| Call_In | Call_Out |
------|--------|-------|---------|----------|
Jaydip| vipul  |   3   |    2    |    1     |
------|--------|-------|---------|----------|
Brij  | Nilesh |   2   |    1    |    1     |
------|--------|-------|---------|----------|
Rajesh| Nilesh |   1   |    1    |    0     |
------|--------|-------|---------|----------|
vipul | Brij   |   1   |    1    |    0     |

所以请建议我查询

4

4 回答 4

1
SELECT FROM, TO, COUNT(*) AS howmany,
       SUM(Type = 'Call_In') AS Call_In, SUM(Type = 'Call_Out') AS Call_Out
FROM table
GROUP BY FROM, TO
于 2013-10-09T18:12:28.887 回答
1

尝试

Select 
  `FROM`, `TO`, 
  COUNT(*) AS howmany,
  SUM(CASE Type WHEN 'Call_In' THEN 1 ELSE 0 END) AS Call_In,
  SUM(CASE Type WHEN 'Call_Out' THEN 1 ELSE 0 END) AS Call_Out
from table GROUP BY `FROM`, `TO`
于 2013-10-09T18:13:20.027 回答
1

感谢您的评论我明白了

于 2013-10-19T04:06:23.933 回答
0

干得好:

Select FROM, TO, COUNT(*) AS howmany,
  sum(case when type = 'Call_In' then 1 else 0 end) as Call_In,
  sum(case when type = 'Call_Out' then 1 else 0 end) as Call_Out
from table 
GROUP BY FROM,TO
于 2013-10-09T18:13:02.013 回答