Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Combining 3 queries

Author  Topic 

royhink
Starting Member

3 Posts

Posted - 2011-05-02 : 13:56:54
I'm a newby and am trying to figure out the concept of joins, and don't know the best way to proceed.

I have 3 queries that summarize the pageviews per client for the last 3 months. How do I combine the queries into one?

My queries:

SELECT DISTINCT trackQString AS qs, COUNT(*) AS Apr_views
FROM AZ_Stats_tblTracker
WHERE (trackClientID = 2337) AND (trackDT BETWEEN '4/1/2011' AND DATEADD(day, 1, '4/30/2011'))
GROUP BY trackQString
ORDER BY Apr_views DESC

SELECT DISTINCT trackQString AS qs, COUNT(*) AS Mar_views
FROM AZ_Stats_tblTracker
WHERE (trackClientID = 2337) AND (trackDT BETWEEN '3/1/2011' AND DATEADD(day, 1, '3/31/2011'))
GROUP BY trackQString
ORDER BY Mar_views DESC

SELECT DISTINCT trackQString AS qs, COUNT(*) AS Feb_views
FROM AZ_Stats_tblTracker
WHERE (trackClientID = 2337) AND (trackDT BETWEEN '2/1/2011' AND DATEADD(day, 1, '2/28/2011'))
GROUP BY trackQString
ORDER BY Feb_views DESC

Any help greatly appreciated.

nigelrivett
Master Smack Fu Yak Hacker

3385 Posts

Posted - 2011-05-02 : 14:39:58
SELECT trackQString AS qs,
sum(case when trackDT BETWEEN '4/1/2011' AND DATEADD(day, 1, '4/30/2011') then 1 else 0 end) AS Apr_views,
sum(case when trackDT BETWEEN '3/1/2011' AND DATEADD(day, 1, '3/31/2011') then 1 else 0 end) AS Mar_views,
sum(case when trackDT BETWEEN '2/1/2011' AND DATEADD(day, 1, '2/28/2011') then 1 else 0 end) AS Feb_views,
FROM AZ_Stats_tblTracker
WHERE (trackClientID = 2337)
GROUP BY trackQString


==========================================
Cursors are useful if you don't know sql.
SSIS can be used in a similar way.
Beer is not cold and it isn't fizzy.
Go to Top of Page

mmarovic
Aged Yak Warrior

518 Posts

Posted - 2011-05-05 : 10:23:08
[code]SELECT trackQString AS qs, month(trackDT), count(*) as page_views
FROM AZ_Stats_tblTracker
WHERE
trackClientID = 2337 and
trackDT >= '20110101' and
trackDT < '20110501'
GROUP BY trackQString, month(trackDT)[/code]

Mirko

My blog: http://mirko-marovic-eng.blogspot.com/
Go to Top of Page
   

- Advertisement -