Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL LEFT JOIN with GROUP BY and WHERE IN (sub query)

I have one table with some statistics per date, which I want listed out with MySQL. For some dates there will be no statistics, so the result should look something like this:
2013-03-01: 3
2013-03-02: 2
2013-03-03: 0
2013-03-04: 1

I figured out that filling in the gaps with 0 -zero- could be solved with a separate table with all possible dates and LEFT JOIN. So far so good.

The statistics (impressions) is in the table 'campaigndata':

id - int(11)
date - date
campaignid - int(11)
impressions - int(11)

But I want to get only some of the statistics. To be more specific, I only want the rows from 'campaigndata' where 'campaignid' is in the table 'campaignfilter' with 'campaigntype' set to 1 (as an example).

This is the table 'campaignfilter':

id - int(11)
campaigntype - int(11)
campaignid - int(11)

Anyone have clue how this could be done?

PS: The structure of table 'campaigndata' is pretty much locked, since it is based on an automatic import from an external system.


SAMPLE RECORDS

CREATE TABLE demo_campaigndata (
  id int(11) NOT NULL AUTO_INCREMENT,
  date date NOT NULL,
  campaignid int(11) NOT NULL,
  impressions int(11) NOT NULL,
  PRIMARY KEY (id)
);
INSERT INTO demo_campaigndata (id, date, campaignid, impressions) VALUES
(1, '2013-03-03', 1, 100),
(2, '2013-03-03', 2, 100),
(3, '2013-03-03', 3, 100),
(4, '2013-03-04', 2, 100),
(5, '2013-03-05', 1, 100),
(6, '2013-03-05', 2, 100);


CREATE TABLE demo_campaignfilter (
  id int(11) NOT NULL AUTO_INCREMENT,
  campaigntype int(11) NOT NULL,
  campaignid int(11) NOT NULL,
  PRIMARY KEY (id)
);
INSERT INTO demo_campaignfilter (id, campaigntype, campaignid) VALUES
(1, 1, 1),
(2, 1, 3);


CREATE TABLE demo_calendar (
  date date NOT NULL,
  PRIMARY KEY (date)
);
INSERT INTO demo_calendar (date) VALUES
('2013-03-01'),
('2013-03-02'),
('2013-03-03'),
('2013-03-04'),
('2013-03-05');

DESIRED RESULT

2013-03-01: 0
2013-03-02: 0
2013-03-03: 200
2013-03-04: 0
2013-03-05: 100
like image 838
sylling Avatar asked Mar 10 '13 10:03

sylling


People also ask

Can we use GROUP BY and order by in sub query?

An ORDER BY command cannot be used in a subquery, although the main query can use an ORDER BY. The GROUP BY command can be used to perform the same function as the ORDER BY in a subquery. Subqueries that return more than one row can only be used with multiple value operators such as the IN operator.

Can we use subquery in left join?

A subquery can be used with JOIN operation. In the example below, the subquery actually returns a temporary table which is handled by database server in memory. The temporary table from the subquery is given an alias so that we can refer to it in the outer select statement.

Can we use GROUP BY with where clause in MySQL?

Introduction to MySQL GROUP BY clause In other words, it reduces the number of rows in the result set. In this syntax, you place the GROUP BY clause after the FROM and WHERE clauses. After the GROUP BY keywords, you place is a list of comma-separated columns or expressions to group rows.

Can we use GROUP BY in left join?

The Left Join can also be used with the GROUP BY clause.


1 Answers

SELECT  a.date, COUNT(b.campaignid) totalStat
FROM    campaigndata a
        LEFT JOIN campaignfilter b
            ON  a.campaignid = b.campaignid AND
                b.campaigntype = 1
GROUP   BY a.date

To further gain more knowledge about joins, kindly visit the link below:

  • Visual Representation of SQL Joins

UPDATE 1

SELECT  a.date, 
        COALESCE(b.totals,0) totals
FROM    demo_calendar a
        LEFT JOIN
        (
            SELECT  a.date, SUM(impressions) totals
            FROM    demo_campaigndata a
                    INNER JOIN demo_campaignfilter b
                        ON a.campaignid = b.campaignid
            WHERE   b.campaigntype = 1
            GROUP   BY a.date
        ) b ON a.date = b.date
  • SQLFiddle Demo
like image 121
John Woo Avatar answered Oct 03 '22 21:10

John Woo