If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > Stumped Missing months in SQL

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 06-08-11, 18:10
kiwimediapro kiwimediapro is offline
Registered User
 
Join Date: May 2011
Posts: 13
Exclamation Stumped Missing months in SQL

I have this query:

Code:
SELECT SUM(t1.totalprice) as totalprice,
SUM(t1.sales) as salessubtotal, t1.`year` as `year`,
t1.mnth as `month`
from 
(SELECT
Sum(crm_sales.totalprice) AS totalprice,
MONTHNAME(crm_sales.date) AS mnth,
YEAR(crm_sales.date) AS `year`,
Count(crm_sales.totalprice) AS sales,
MONTH(crm_sales.date) AS `ordering`
FROM `crm_sales`
WHERE `date` BETWEEN '2009-01-01' AND '2010-05-28'
GROUP BY crm_sales.date
) as t1
GROUP BY t1.`year`,t1.mnth
ORDER BY t1.`year`, t1.ordering
TotalPrice Sales Year Month

130567.41 635 2009 May
139533.66 682 2009 June
141245.63 681 2009 July
121226.32 588 2009 August
121582.47 588 2009 September
134636.40 660 2009 October
107815.05 524 2009 November
106716.81 521 2009 December
123851.60 609 2010 January
126752.72 619 2010 February
151362.32 732 2010 March
124616.72 605 2010 April
150702.71 736 2010 May

The query returns the correct data but i need the missing months to be 0 for sales.

ANy ideas
Reply With Quote
  #2 (permalink)  
Old 06-08-11, 18:57
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
create a table with two columns -- year and month -- and populate it with rows for all the years/months that you want to see

then do a LEFT OUTER JOIN to your query as a derived table (subquery in the FROM clause)

vwalah!

__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 06-09-11, 15:19
kiwimediapro kiwimediapro is offline
Registered User
 
Join Date: May 2011
Posts: 13
Question Clarification request

Thanks for the reply.

WOuld the table be a temporary table?

If a temp table is possible would I then need to use a stored procedure?

WOuld it work with just the months asthe years are dynamic?
Reply With Quote
  #4 (permalink)  
Old 06-09-11, 15:51
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
Quote:
Originally Posted by kiwimediapro View Post
WOuld the table be a temporary table?
only if you want it to be

Quote:
Originally Posted by kiwimediapro View Post
If a temp table is possible would I then need to use a stored procedure?
no

Quote:
Originally Posted by kiwimediapro View Post
WOuld it work with just the months asthe years are dynamic?
only if you extracted the distinct years dynamically


how would you like to proceed?
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #5 (permalink)  
Old 06-09-11, 16:46
kiwimediapro kiwimediapro is offline
Registered User
 
Join Date: May 2011
Posts: 13
Unhappy I have this but....

The error is:
Table 'crm.crmsales' doesn't exist


Code:
CREATE TEMPORARY TABLE Months ( monthname VARCHAR(50) NOT NULL) ENGINE=MEMORY;

INSERT INTO Months (monthname) VALUES ('January');
INSERT INTO Months (monthname) VALUES ('February');
INSERT INTO Months (monthname) VALUES ('March');
INSERT INTO Months (monthname) VALUES ('April');
INSERT INTO Months (monthname) VALUES ('May');
INSERT INTO Months (monthname) VALUES ('June');
INSERT INTO Months (monthname) VALUES ('July');
INSERT INTO Months (monthname) VALUES ('August');
INSERT INTO Months (monthname) VALUES ('September');
INSERT INTO Months (monthname) VALUES ('October');
INSERT INTO Months (monthname) VALUES ('November');
INSERT INTO Months (monthname) VALUES ('December');



SELECT m.monthname, 
			COALESCE(s.salessubtotal, 0)
FROM months m
LEFT OUTER JOIN
(
SELECT SUM(t1.totalprice) as totalprice,
SUM(t1.sales) as salessubtotal, t1.`year` as `year`,
t1.mnth as `month`
from 
(SELECT
Sum(crmsales.totalprice) AS totalprice,
MONTHNAME(crmsales.date) AS mnth,
YEAR(crmsales.date) AS `year`,
Count(crmsales.totalprice) AS sales,
MONTH(crmsales.date) AS `ordering`
FROM `crmsales`
WHERE `date` BETWEEN '2009-01-01' AND '2010-05-28'
GROUP BY crmsales.date
) as t1
GROUP BY t1.`year`,t1.mnth
ORDER BY t1.`year`, t1.ordering) s 
on m.monthname = s.`month`
Reply With Quote
  #6 (permalink)  
Old 06-09-11, 17:31
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
Quote:
Originally Posted by kiwimediapro View Post
The error is:
Table 'crm.crmsales' doesn't exist
i don't see anywhere in your query where you mention crm.crmsales

are you sure that's the error message?

are you sure that's the query you ran?
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #7 (permalink)  
Old 06-09-11, 18:18
kiwimediapro kiwimediapro is offline
Registered User
 
Join Date: May 2011
Posts: 13
Reply

That is what it spits out.... :-)

The database is 'crm' the table is 'crmsales'
Reply With Quote
  #8 (permalink)  
Old 06-09-11, 18:38
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
well, apparently you don't gots a database or table with that name
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book

Last edited by r937; 06-09-11 at 19:18.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On