2

私はこれらの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

ネストされたクエリが必要だと思いますが、2 つの条件でカウントする方法がわかりません。手伝ってくれますか?

編集:私が試したコード、

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

しかし、それは私が望む結果を与えません.1つのクエリで2種類のカウントを行う方法がわからないからです.

4

2 に答える 2

3

infectionと ( ip& ipc) を別々にグループ化し、次のようにサブクエリを使用してそれらを結合する必要があります。

SELECT t1.ip, t1.isp, t2.infection, t1.ipc, t1. ispc, t2.incount
FROM
    (SELECT ip, isp, infection, COUNT(ip) as ipc, COUNT(isp) as ispc
    FROM (
       SELECT ip, isp, infection
       FROM tbl1
       UNION ALL
       SELECT ip, isp, infection
       FROM tbl2
       UNION ALL
       SELECT ip, isp, infection
       FROM tbl3
       )x
     GROUP BY ip, isp) t1
JOIN
    (SELECT ip, isp, infection, COUNT(infection) as incount
     FROM (
       SELECT ip, isp, infection
       FROM tbl1
       UNION ALL
       SELECT ip, isp, infection
       FROM tbl2
       UNION ALL
       SELECT ip, isp, infection
       FROM tbl3
       )x
    GROUP BY ip, isp,  infection)t2
ON t1.ip = t2.ip
ORDER BY ip, isp, infection Desc

このSQLFiddleを参照してください

注:次の理由により、目的の出力が間違っていると思います。

  1. forはTable3ありませんが、出力にはありますinfectionip=6
  2. infection other出力に がありません (代わりに がありますmalware)
于 2012-11-07T07:33:21.183 に答える
0

すべてのテーブルを結合してから、列の合計を実行し、特定の列でグループ化することができます。

SELECT ip, isp, infection, COUNT(ip) AS ipcount, COUNT(isp) AS ispcount, COUNT(infection) AS infectioncount
FROM
  (
    SELECT ip, isp, infection
    FROM table_1
    UNION ALL
    SELECT ip, isp, infection
    FROM table_2
    UNION ALL
    SELECT ip, isp, infection
    FROM table_3
  )
GROUP BY ip, isp, infection
ORDER BY ip, isp, infection;
于 2012-11-07T07:01:40.710 に答える