BhavikKama
BhavikKama

Reputation: 8800

mysql query for getting max count of row's data by date?

i have one table name as a_ad_display with following column

displayId | campaignId | displaytime(date)

1          |    2      | 2012-12-19 12:41:08
2          |    2      | 2012-12-19 01:41:08
3          |    1      | 2012-12-20 10:41:08
4          |    1      | 2012-12-20 02:41:08
5          |    1      | 2012-12-21 12:41:08
6          |    2      | 2012-12-21 01:41:08
7          |    1      | 2012-12-21 12:41:08
8          |    1      | 2012-12-21 02:41:08

there is a multiple entry with same campaignId should be there.now what i want is to first of all fetch the campaignId with the highest count in a_ad_display and for that campaignId i want to get total number of count within the date

eg..from above table i have 1 number campaignId with highest row count

so for 1 number campaignId i want to display records like this..

        **Date**     | **countPerDay**
 2012-12-19          |      0
 2012-12-20          |      2
 2012-12-21          |      3

how can i achieve this in mysql database...hope anyone can guide me...

Upvotes: 0

Views: 2437

Answers (4)

Smiley
Smiley

Reputation: 108

sory for asking this, but which one of this query is better??

1.the answer by Mr.raheel shan

EXPLAIN SELECT DATE(a.displayTime) AS `DisplayTime`,IFNULL(l.TCount,0) AS TCount
FROM a_ad_display AS a
LEFT JOIN (SELECT
       COUNT(campaignId) AS TCount,
       displayTime
     FROM a_ad_display
     WHERE campaignId = 1
     GROUP BY DATE(displayTime)) AS l
ON DATE(a.displayTime) = DATE(l.displayTime)
GROUP BY DATE(a.displayTime);


id  select_type  table         type  possible_keys  key  key_len  ref  rows  Extra                                            
1   PRIMARY      <derived2>    ALL                                     2                                                   
1   PRIMARY      a             ALL                                     8     Using temporary; Using filesort               
2   DERIVED      a_ad_display  ALL                                     8     Using where; Using temporary; Using filesort  


2.or using this one

EXPLAIN SELECT DATE(a_ad_display.`displaytime`) AS `DATE`, 
               COUNT(a.`campaignId`) AS `COUNT Per Day` 
        FROM a_ad_display 
        LEFT JOIN (SELECT `a_ad_display`.`displayId`,
                          `a_ad_display`.`campaignId` 
                   FROM `a_ad_display` 
                   WHERE `a_ad_display`.`campaignId`=1) a 
        ON (`a_ad_display`.`displayId`=a.`displayId`)
        GROUP BY `a_ad_display`.`displaytime`;

id  select_type  table         type  possible_keys  key  key_len  ref  rows  Extra                              
1   PRIMARY      a_ad_display  ALL                                     8     Using temporary; Using filesort  
1   PRIMARY      <derived2>    ALL                                     5                                       
2   DERIVED      a_ad_display  ALL                                     8     Using where                      

thanks before & after :D


EDIT for highest campaignId occurance

SELECT DATE(a_ad_display.`displaytime`) AS `DATE`, COUNT(a.`campaignId`) AS `COUNT Per Day` FROM a_ad_display 
LEFT JOIN (SELECT `a_ad_display`.`displayId`,`a_ad_display`.`campaignId` FROM `a_ad_display`
   INNER  JOIN (SELECT COUNT(`a_ad_display`.`campaignId`) AS `count`, `a_ad_display`.`campaignId` 
        FROM `a_ad_display` 
        GROUP BY `a_ad_display`.`campaignId` 
        ORDER BY `count` DESC 
        LIMIT 1) b ON (a_ad_display.`campaignId`=b.`campaignId`)
   ) a ON (`a_ad_display`.`displayId`=a.`displayId`)
GROUP BY `DATE`;

GROUP BY a_ad_display.displaytime; /* wrong in this statement ..., sorry*/ it should be working now ..... try again. You can check in here http://sqlfiddle.com/#!2/bc75b/1, hope it's workin :D

Upvotes: 1

Muhammad Raheel
Muhammad Raheel

Reputation: 19882

You can use this query

select 
    count(campaignId) as TCount,
    DATE(displayTime) as `Date`
from a_ad_display
where campaignId = 1
group by DATE(displayTime)

Although it will not display 0.

EDIT :

OK This is modified and fetches your result.

select
  date(a.displayTime) as `DisplayTime`,
  ifnull(l.TCount,0) as TCount
from a_ad_display as a
  left join (select
           count(campaignId) as TCount,
           displayTime
         from a_ad_display
         where campaignId = 1
         group by DATE(displayTime)) as l
    on date(a.displayTime) = date(l.displayTime)
group by DATE(a.displayTime)

Demo

MORE EDITS :

select
  date(a.displayTime) as `DisplayTime`,
  ifnull(l.TCount,0) as TCount
from a_ad_display as a
  left join (select
           count(campaignId) as TCount,
           displayTime
         from a_ad_display
         where campaignId = 1
         group by DATE(displayTime)) as l
    on date(a.displayTime) = date(l.displayTime)
group by DATE(a.displayTime)
order by a.displaytime desc limit 5

This is how you can select latest 5 records. If you want to select the latest campaign you can use a subquery instead of 1 take a look at this query

select
  date(a.displayTime) as `DisplayTime`,
  ifnull(l.TCount,0) as TCount
from a_ad_display as a
  left join (select
       count(campaignId) as TCount,
       displayTime
     from a_ad_display
     where campaignId = (select  campaignId from a_ad_display group by campaignId order by count(campaignId) desc limit 1)
     group by DATE(displayTime)) as l
    on date(a.displayTime) = date(l.displayTime)
group by DATE(a.displayTime)

EDITS :

I have edited the query this will fetch higher occurance campaignId and fetch results accordingly

Demo

Upvotes: 3

Rahul Vasantrao Kamble
Rahul Vasantrao Kamble

Reputation: 251

this will show 0 also

 select 
       sum(case when campaignId = @inputCompainid then 1 else 0 end) as TCount, 
        DATE(displayTime) as `Date`
    from a_ad_display
    group by DATE(displayTime)

Upvotes: 2

Suhel Meman
Suhel Meman

Reputation: 3852

Following query gives u list of records for each campaignId

select campaignId,date(displaytime) as Date,count(*) as countPerDay from a_ad_display group by campaignId,date(displaytime);

if you need for specfic campaignId, use this :

select campaignId,date(displaytime) as Date,count(*) as countPerDay from a_ad_display group by campaignId,date(displaytime) having campaignId=1;

Upvotes: 1

Related Questions