Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8855805
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T14:13:09+00:00 2026-06-14T14:13:09+00:00

Possible Duplicate: MySQL Count data for last 7 days I have an issue where

  • 0

Possible Duplicate:
MySQL Count data for last 7 days

I have an issue where i need to show the sum of fields. Lets say I have a recordset as follows,

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

And i want to display an output which will show the sum of all the infections for each day as follows,

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

I used this query,

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

but it only gives an out put as follows which is NOT my requirement..

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

any help would be much helpful 🙂
Thank you very much 🙂 much appreciated 🙂

EDIT:
Possible duplicate of : MySQL Count data for last 7 days

but not similar

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T14:13:10+00:00Added an answer on June 14, 2026 at 2:13 pm
    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
    
    • SQLFiddle Demo

    or

    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
    
    • SQLFiddle Demo
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: mysql count into PHP variable I have the following query that returns
Possible Duplicate: count date difference in hours using php and mysql I have a
Possible Duplicate: mysql custom sort I have an array of IDs: $ids = array(5,
Possible Duplicate: Format mysql datetime with php Well.. I have stored date(c) in Mysql
Possible Duplicate: php/Mysql query with inserting date fails I have a datepicker code below:
Possible Duplicate: How can I count the numbers of rows that a mysql query
Possible Duplicate: Combine Multiple child rows into one row MYSQL I have following table,
Possible Duplicate: MySQL GUI Programs I have installed MySql 5.5 on my PC (win7),
Possible Duplicate: MySQL delete row from multiple tables I have 5 tables: members member_videos
Possible Duplicate: MySQL dynamic cross tab I have student_record table ------------------------------------------------------------------- student_id | semester

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.