3

重複の可能性:
過去 7 日間の MySQL カウント データ

フィールドの合計を表示する必要があるという問題があります。次のようなレコードセットがあるとしましょう。

detectDate      |isp    |infection  | count
--------------------------------------
2012-10-02 01:00|aaaa   |malware    |3
2012-10-02 01:30|bbbb   |malware    |2
2012-10-02 01:33|bbbb   |spy-eye    |2
2012-10-02 01:45|aaaa   |DDos       |1
2012-10-03 01:50|cccc   |malware    |2
2012-10-03 02:00|dddd   |TDSS       |2
2012-10-03 04:50|dddd   |TDSS       |3

そして、次のように、毎日のすべての感染の合計を示す出力を表示したいと思います。

detectDate  |infection  | count
-------------------------------
2012-10-02  |DDos       |1
2012-10-02  |malware    |5
2012-10-02  |spy-eye    |2
2012-10-02  |TDSS       |0
2012-10-03  |DDos       |0
2012-10-03  |malware    |2
2012-10-03  |spy-eye    |0
2012-10-03  |TDSS       |5

私はこのクエリを使用しました、

SELECT DATE_FORMAT( detectDate, '%Y-%m-%d' ) AS detectDate, infection, SUM( count )
FROM `tbl_correlateddata`
GROUP BY DATE_FORMAT( detectDate, '%Y-%m-%d' ) , infection

しかし、それは私の要件ではない次のように出力するだけです..

detectDate  |infection  | count
-------------------------------
2012-10-02  |DDos       |1
2012-10-02  |malware    |5
2012-10-02  |spy-eye    |2
2012-10-03  |malware    |2
2012-10-03  |TDSS       |5

どんな助けでもとても役に立ちます:)どうもありがとうございました:)どうもありがとうございました:)

編集: 重複の可能性:過去 7 日間の MySQL カウント データ

しかし似ていない

4

1 に答える 1

7
SELECT e.*, COALESCE(SUM(d.`count`),0) `SUM of count`
FROM
(
  SELECT c.detectDate, a.infection
  FROM
    (
      SELECT  DISTINCT infection
      FROM    tbl_correlateddata
    ) a CROSS JOIN
    (
      SELECT  DISTINCT DATE(detectDate) detectDate 
      FROM    tbl_correlateddata 
    ) c
) e LEFT JOIN tbl_correlateddata d
    ON  DATE(d.detectDate) = e.detectDate AND
        d.infection = e.infection
 GROUP BY detectDate, infection
 ORDER BY e.detectDate, e.infection

また

SELECT  DATE_FORMAT(e.detectDate, '%Y-%m-%d' ), 
        e.infection, 
        COALESCE(SUM(d.`count`),0) `SUM of count`
FROM
(
  SELECT c.detectDate, a.infection
  FROM
    (
      SELECT  DISTINCT infection
      FROM    tbl_correlateddata
    ) a CROSS JOIN
    (
      SELECT  DISTINCT DATE(detectDate) detectDate 
      FROM    tbl_correlateddata 
    ) c
) e LEFT JOIN tbl_correlateddata d
    ON  DATE(d.detectDate) = e.detectDate AND
        d.infection = e.infection
 GROUP BY e.detectDate, e.infection
 ORDER BY e.detectDate, e.infection
于 2012-11-05T04:34:46.580 に答える