Jump to content

Month as columns query....tricky


jwwceo

Recommended Posts

Hello,

 

I am trying to write a report script that will take all my users, and tell me how many leads each has generated per month in a given year as well as the total per year.  So the ideal outcome would be a table with users as the rows, and 13 columns across the top, one for each month and one for the total.

 

The leads table has an investor ID field which matched the ID in the investor table, so the query needs to count the incidences of that ID when the date matches a certain month. The dates are in UnixTime stamp.

 

Here is what I have so far:

 

SELECT
COUNT( leads.investor ) as total,
MONTHNAME( FROM_UNIXTIME( leads.date ) = 'January' ) AS jan ,
MONTHNAME( FROM_UNIXTIME( leads.date ) = 'February') AS feb ,
MONTHNAME( FROM_UNIXTIME( leads.date ) = 'March' ) AS mar ,
MONTHNAME( FROM_UNIXTIME( leads.date ) = 'April' ) AS apr ,
MONTHNAME( FROM_UNIXTIME( leads.date ) = 'May' ) AS may ,
MONTHNAME( FROM_UNIXTIME( leads.date ) = 'June' ) AS jun ,
investors.first_name, investors.last_name, investors.investors_id
FROM leads, investors
WHERE YEAR( FROM_UNIXTIME( leads.date ) ) = '2010'
AND leads.investor = investors.investors_id
GROUP BY investors.investors_id
LIMIT 0 , 30 

 

I've only added 6 months while testing. The total part is working fine, for the whole year, but I am having a hard time getting the query to tally the leads by month. What am I missing here??

 

Thanks in advance!!

 

James

Link to comment
Share on other sites

Try...

 

SELECT

COUNT( leads.investor ) as total,

COUNT(CASE WHEN MONTHNAME( FROM_UNIXTIME( leads.date ) = 'January' ) THEN 1 ELSE NULL END) AS jan ,

...

investors.first_name, investors.last_name, investors.investors_id

FROM leads, investors

WHERE YEAR( FROM_UNIXTIME( leads.date ) ) = '2010'

AND leads.investor = investors.investors_id

GROUP BY investors.investors_id

LIMIT 0 , 30

Link to comment
Share on other sites

Hmm....gettinng closer....but the query is still not adding the number of leads per month...this is what I am getting now...

 

total 	jan 	feb 	mar 	apr 	may 	jun 	first_name 	last_name 	investors_id
1 	0 	0 	0 	0 	0 	0 	xxx 	        xxx 	        5
3 	0 	0 	0 	0 	0 	0 	Billy 	        Bob 	        112

Link to comment
Share on other sites

OK, I did not feel like looking up the syntax for that monthname you are using, but I bet now that that is where your problem lies, so here it is:

 

SELECT
COUNT( leads.investor ) as total,
COUNT(CASE WHEN MONTHNAME( FROM_UNIXTIME( leads.date ) ) = 'January' THEN 1 ELSE NULL END) AS jan ,
...
investors.first_name, investors.last_name, investors.investors_id
FROM leads, investors
WHERE YEAR( FROM_UNIXTIME( leads.date ) ) = '2010'
AND leads.investor = investors.investors_id
GROUP BY investors.investors_id
LIMIT 0 , 30 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.