943,867 Members | Top Members by Rank

Ad:
  • MS SQL Discussion Thread
  • Marked Solved
  • Views: 1531
  • MS SQL RSS
Jul 11th, 2008
0

Problems Joining Query

Expand Post »
Hello I seem to be having a problem, I have this query that pulls back correct results

MS SQL Syntax (Toggle Plain Text)
  1. SELECT hourStamp, SUM(CASE WHEN datestamp < GETDATE() THEN 1 ELSE 0 END) / @numberofdays AS HourAverage, SUM(CASE WHEN dateStamp BETWEEN DATEADD(d,DATEDIFF(d,0,GETDATE()),0) AND GETDATE() THEN 1 ELSE 0 END) AS HourToday
  2.  
  3. FROM webstats
  4.  
  5. GROUP BY hourStamp
  6. ORDER BY hourStamp
I also have this query that pulls back correct results
MS SQL Syntax (Toggle Plain Text)
  1. SELECT hourstamp, COUNT(id) AS ActualVisits
  2. FROM webStats
  3. WHERE id IN
  4. (SELECT ws.id
  5. FROM webStats AS ws INNER JOIN webStatsTrace AS wst ON ws.id = wst.statsid
  6. WHERE (ws.datestamp BETWEEN DATEADD(d, DATEDIFF(d, 0, GETDATE()), 0) AND GETDATE())
  7. GROUP BY ws.id HAVING (COUNT(wst.statsid) > 1))
  8. GROUP BY hourstamp
  9. ORDER BY hourstamp
But i am unable to join the two querys, or rewrite them so i run one query and get all the results in one... could anyone please suggest a way?

Thanks
Bart
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
bartbauldry is offline Offline
5 posts
since Jul 2008
Jul 11th, 2008
0

Re: Problems Joining Query

You can easily join two queries with the UNION keyword between the two individual SELECT queries:

SELECT xxxxxx UNION SELECT yyyyyy
Reputation Points: 94
Solved Threads: 8
Practically a Master Poster
khess is offline Offline
638 posts
since Apr 2008
Jul 11th, 2008
0

Re: Problems Joining Query

Thanks, I have tried a UNION but it complains that there they do not have an equal number of expressions on their target list.

I have tried restructuing the query to no avail, the problem is that the fisrt query returns:
MS SQL Syntax (Toggle Plain Text)
  1. hourStamp HourAverage HourToday
  2. ----------- ----------- -----------
  3. 0 50 52
  4. 1 46 34
  5. 2 40 39
  6. 3 41 52
  7. 4 39 46
  8. 5 44 33
  9. 6 43 51
  10. 7 42 43
  11. 8 53 49
  12. 9 62 66
  13. 10 69 56
  14. 11 76 64
  15. 12 66 55
  16. 13 73 65
  17. 14 73 16
  18. 15 81 0
  19. 16 76 0
  20. 17 64 0
  21. 18 70 0
  22. 19 69 0
  23. 20 67 0
  24. 21 69 0
  25. 22 58 0
  26. 23 50 0

And the Latter returns:
MS SQL Syntax (Toggle Plain Text)
  1. hourstamp ActualVisits
  2. ----------- ------------
  3. 0 5
  4. 1 3
  5. 2 2
  6. 3 3
  7. 4 1
  8. 5 6
  9. 6 3
  10. 7 2
  11. 8 6
  12. 9 14
  13. 10 14
  14. 11 24
  15. 12 13
  16. 13 15
  17. 14 4
The latter will add rows as the hours pass....
Any Ideas?

Thanks
Reputation Points: 10
Solved Threads: 0
Newbie Poster
bartbauldry is offline Offline
5 posts
since Jul 2008
Jul 11th, 2008
0

Re: Problems Joining Query

Try this:

SQL Syntax (Toggle Plain Text)
  1. SELECT
  2. hourStamp,
  3. NULL AS ActualVisits,
  4. SUM(CASE WHEN datestamp < GETDATE() THEN 1 ELSE 0 END) / @numberofdays as HourAverage,
  5. SUM(CASE WHEN dateStamp BETWEEN DATEADD(d,DATEDIFF(d,0,GETDATE()),0) AND GETDATE() THEN 1 ELSE 0 END) as HourToday
  6. FROM
  7. webstats
  8. GROUP BY
  9. hourStamp
  10.  
  11. UNION
  12.  
  13. SELECT
  14. hourstamp,
  15. COUNT(id) AS ActualVisits,
  16. NULL AS HourAverage,
  17. NULL AS HourToday
  18. FROM
  19. webStats
  20. WHERE
  21. id IN
  22. (SELECT ws.id
  23. FROM webStats AS ws INNER JOIN webStatsTrace AS wst ON ws.id = wst.statsid
  24. WHERE (ws.datestamp BETWEEN DATEADD(d, DATEDIFF(d, 0, GETDATE()), 0) AND GETDATE())
  25. GROUP BY ws.id HAVING (COUNT(wst.statsid) > 1))
  26. GROUP BY
  27. hourstamp
  28. ORDER BY
  29. hourstamp

Use aliased NULLs to make the SELECT clauses of the two queries match.
Reputation Points: 23
Solved Threads: 10
Junior Poster in Training
cmhampton is offline Offline
79 posts
since Feb 2008
Jul 11th, 2008
0

Re: Problems Joining Query

Thanks, that has helped, its now returning its a whole but it seems to be duplicating rows of the hours that have already passed (if that makes sence)

MS SQL Syntax (Toggle Plain Text)
  1. hourStamp ActualVisits HourAverage HourToday
  2. ----------- ------------ ----------- -----------
  3. 0 <NULL> 50 52
  4. 0 5 <NULL> <NULL>
  5. 1 <NULL> 46 34
  6. 1 3 <NULL> <NULL>
  7. 2 <NULL> 40 39
  8. 2 2 <NULL> <NULL>
  9. 3 <NULL> 41 52
  10. 3 3 <NULL> <NULL>
  11. 4 <NULL> 39 46
  12. 4 1 <NULL> <NULL>
  13. 5 <NULL> 44 33
  14. 5 6 <NULL> <NULL>
  15. 6 <NULL> 43 51
  16. 6 3 <NULL> <NULL>
  17. 7 <NULL> 42 43
  18. 7 2 <NULL> <NULL>
  19. 8 <NULL> 53 49
  20. 8 6 <NULL> <NULL>
  21. 9 <NULL> 62 66
  22. 9 14 <NULL> <NULL>
  23. 10 <NULL> 69 56
  24. 10 14 <NULL> <NULL>
  25. 11 <NULL> 76 64
  26. 11 24 <NULL> <NULL>
  27. 12 <NULL> 66 55
  28. 12 13 <NULL> <NULL>
  29. 13 <NULL> 73 65
  30. 13 15 <NULL> <NULL>
  31. 14 <NULL> 75 51
  32. 14 16 <NULL> <NULL>
  33. 15 <NULL> 84 85
  34. 15 21 <NULL> <NULL>
  35. 16 <NULL> 80 78
  36. 16 17 <NULL> <NULL>
  37. 17 <NULL> 67 49
  38. 17 5 <NULL> <NULL>
  39. 18 <NULL> 70 0
  40. 19 <NULL> 69 0
  41. 20 <NULL> 67 0
  42. 21 <NULL> 69 0
  43. 22 <NULL> 58 0
  44. 23 <NULL> 50 0

Ive tried to remove the duplicate rows but seem unable to, i thought the union option wouldnt allow duplicate rows?

Thanks
Reputation Points: 10
Solved Threads: 0
Newbie Poster
bartbauldry is offline Offline
5 posts
since Jul 2008
Jul 11th, 2008
0

Re: Problems Joining Query

Can you give me a little more information about what this particular query is trying to do? You may not need to use UNION, but perhaps different JOINs.
Reputation Points: 23
Solved Threads: 10
Junior Poster in Training
cmhampton is offline Offline
79 posts
since Feb 2008
Jul 14th, 2008
0

Re: Problems Joining Query

Hello,

The results I would like from the query is 4 columns, one with an hourstamp (0-23),

the next column with an average hourly hits (i have done as count each hour and / by number of days),

the next column with today’s hits so far,

and final column as the number of hits who have visited more than 1 page

If you would like me to post some sample data i can,

Thanks for your help
Bart
Last edited by bartbauldry; Jul 14th, 2008 at 4:51 am.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
bartbauldry is offline Offline
5 posts
since Jul 2008
Jul 14th, 2008
0

Re: Problems Joining Query

I think that you need to join query one with query two... try:

SELECT t1.*, t2.actualVisits
FROM (
SELECT hourStamp, SUM(CASE WHEN datestamp < GETDATE() THEN 1 ELSE 0 END) / @numberofdays as HourAverage, SUM(CASE WHEN dateStamp BETWEEN DATEADD(d,DATEDIFF(d,0,GETDATE()),0) AND GETDATE() THEN 1 ELSE 0 END) as HourToday FROM webstats GROUP BY hourStamp
) t1 LEFT OUTER JOIN (
SELECT hourstamp, COUNT(id) AS ActualVisits FROM webStats WHERE id IN (SELECT ws.id FROM webStats AS ws INNER JOIN webStatsTrace AS wst ON ws.id = wst.statsid WHERE (ws.datestamp BETWEEN DATEADD(d, DATEDIFF(d, 0, GETDATE()), 0) AND GETDATE()) GROUP BY ws.id HAVING (COUNT(wst.statsid) > 1)) GROUP BY hourstamp
) t2 ON t1.hourStamp=t2.hourStamp ORDER BY t1.hourStamp
Reputation Points: 12
Solved Threads: 7
Newbie Poster
wujtehacjusz is offline Offline
18 posts
since Mar 2007
Jul 14th, 2008
0

Re: Problems Joining Query

It works!

Thanks to everyone for their help.
Thanks again

Bart
Reputation Points: 10
Solved Threads: 0
Newbie Poster
bartbauldry is offline Offline
5 posts
since Jul 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in MS SQL Forum Timeline: Delete top 5 rows from a table
Next Thread in MS SQL Forum Timeline: deleting rows





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC