【发布时间】:2012-11-07 06:52:18
【问题描述】:
我有这 3 张桌子,
tbl_1-
ip |isp |infection
----------------------
1 |aaaa |malware
2 |bbbb |malware
3 |cccc |ddos
3 |cccc |trojan
4 |dddd |ddos
tbl_2-
ip |isp |infection
----------------------
1 |aaaa |malware
3 |cccc |ddos
4 |dddd |trojan
5 |eeee |trojan
6 |ffff |other
tbl_3-
ip |isp |infection
----------------------
1 |aaaa |ddos
6 |ffff |
2 |bbbb |other
我需要得到如下结果,
result-
ip |isp |infection |ipCount |ispCount |infectionCount
--------------------------------------------------------------
1 |aaaa |malware |3 |3 |2
1 |aaaa |ddos |3 |3 |1
2 |bbbb |other |2 |2 |1
2 |bbbb |malware |2 |2 |1
3 |cccc |ddos |3 |3 |2
3 |cccc |trojan |3 |3 |1
4 |dddd |ddos |2 |2 |1
4 |dddd |trojan |2 |2 |1
5 |eeee |trojan |1 |1 |1
6 |ffff |other |2 |2 |1
6 |ffff | |2 |2 |1
ipCount, ispCount -> count of matching ip and isp
eg-there are 3 records with ip = 1 and isp = aaaa
infectionCount -> count of matching infections per ip and isp
eg-there are 2 infections that says malware where ip = 1 and isp = aaaa
我想我需要一个嵌套查询,但我不知道如何计算两个条件;你能帮忙吗?
EDIT:我试过的代码,
SELECT ip, isp, infection, count(ip), count(isp), count(infection)
FROM (
SELECT ip, isp, infection
FROM tbl_1
UNION ALL
SELECT ip, isp, infectionType
FROM tbl_2
UNION ALL
SELECT ip, isp, infection
FROM tbl_3
)x
GROUP BY ip, isp, infection
但它没有给出我想要的结果,因为我不知道如何在一个查询中进行 2 种类型的计数
【问题讨论】:
-
我试过工会,我贴出代码..
-
ipCount何时与ispCount不同? -
除非isp名称不同,否则都是一样的
-
@hims056 我发布了我尝试过的代码