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 |
所以请建议我查询
答案 0 :(得分: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
答案 1 :(得分: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`
答案 2 :(得分:1)
感谢您的评论我得到了它我只是添加了一些类型的电话,所以我可以添加它以便进一步使用
答案 3 :(得分: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