Jump to content

Recommended Posts

hello,

 

I had previously posted about adding qty from 3 separate tales in 1 query.

 

my new question is :

 

can i run 3 separate queries within the same single query on just 1 table ?

 

i have a table with :

 

LID, YEAR, MONTH, WEEK, HITS

1      2010    01      02      50

1      2009    12      52      10

1      2009    12      51      20

1      2009    12      53      15

2      2009    12      52      75

2      2009    12      51      30

3      2009    12      52      10

3      2009    12      50      75

 

the result i want is

lid 1, ttl hits for month 12 = 35, ttl hits for 2009 = 35, ttl hits wk > 51 = 25

lid 2 .....

Link to comment
https://forums.phpfreaks.com/topic/188542-combine-3-queries-into-1/
Share on other sites

Hi

 

You could do it like this:-

 

SELECT a.LID, a.YEAR, a.YearTotal, b.YearMonth, b.YearMonthTotal, c.YearWeek, c.YearWeekTotal
FROM (SELECT LID, YEAR, SUM(HITS) AS YearTotal FROM someTable GROUP BY LID, YEAR) a
JOIN (SELECT LID, CONCAT(YEAR,' ',MONTH) AS YearMonth, SUM(HITS) AS YearTotal FROM someTable GROUP BY LID, YEAR, MONTH) b
ON a.LID = b.LID
JOIN (SELECT LID, CONCAT(YEAR,' ',WEEK) AS YearWeek, SUM(HITS) AS YearTotal FROM someTable GROUP BY LID, YEAR, WEEK) c
ON a.LID = c.LID

 

Although not sure whether it would efficiency wise be worthwhile most of the time.

 

All the best

 

Keith

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.