| Author |
Topic |
|
mike123
Master Smack Fu Yak Hacker
1462 Posts |
Posted - 2008-08-01 : 14:57:03
|
Hi,Just got my query working from a different thread. I've isolated the mess from the other thread so just creating a new thread for this to make it easier. Hopefully this is clean and simple.My query is currently bringing back my desired results in a format like this.referDate / page / totalreferrers20080801 1 1020080801 10 120080801 12 120080801 14 120080801 16 220080801 2 420080801 20 120080801 25 120080801 27 120080801 3 120080801 30 120080801 5 320080801 6 220080801 7 120080801 8 120080801 9 120080731 1 1820080731 11 120080731 12 220080731 14 420080731 16 220080731 18 220080731 2 1120080731 20 320080731 21 220080731 22 220080731 23 120080731 25 220080731 29 320080731 3 6Ive been playing with the PIVOT command for a bit, but unable to get it going. I keep ending up with all 0's in the fields or having multiple date colunms.I want to PIVOT these results so I have a result set that is in a format like :referDate,page1,page2,page3,page4,page5,(keep goin to column20)20080801, 1 , 2 , 2 , 1 , 2 , etc..20080731, 1 , 1 , 9 , 3 , 9 , etc..20080730, 4 , 3 , 7 , 1 , 4 , etc..20080729, 1 , 2 , 2 , 4 , 1 , etc.. My syntax or logic seems to be a bit off..Is something obviously wrong in this query? I'm pretty stumped. Its bringing back all 0's, with multiple repeat dates.Thanks again for all the help!mike123   SELECT p.referDate, p.Page1, p.Page2, p.Page3, p.Page4, p.Page5, p.Page6, p.Page7, p.Page8, p.Page9, p.Page10, p.Page11, p.Page12, p.Page13, p.Page14, p.Page15, p.Page16, p.Page17, p.Page18, p.Page19, p.Page20, p.Page21, p.Page22, p.Page23, p.Page24, p.Page25FROM ( SELECT CONVERT(varchar(10), land.queryDate, 112) as referDate,thePage ,COUNT(*) AS TotalReferrers FROM tbl2 as land GROUP BY CONVERT(varchar(10), land.queryDate, 112), thepage ) AS sPIVOT ( COUNT(thepage) FOR thepage IN ([page1], [Page2], [Page3], [Page4], [Page5], [Page6], [Page7], [Page8], [Page9], [Page10], [Page11], [Page12], [Page13], [Page14], [Page15], [Page16], [Page17], [Page18], [Page19], [Page20], [Page21], [Page22], [Page23], [Page24], [Page25]) ) AS pORDER BY referDate DESC |
|
|
robc
Yak Posting Veteran
60 Posts |
Posted - 2008-08-01 : 15:48:27
|
| Too hard.p.s hi mike. |
 |
|
|
mike123
Master Smack Fu Yak Hacker
1462 Posts |
Posted - 2008-08-11 : 04:43:39
|
bump for if anyone can help on this one, still stuck...much appreciated as always! thx,mike123 |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2008-08-11 : 05:03:16
|
[code]DECLARE @sample TABLE( referDate datetime, page int, totalreferrers int)INSERT INTO @sampleSELECT '20080801', 1, 10 UNION ALLSELECT '20080801', 10, 1 UNION ALLSELECT '20080801', 12, 1 UNION ALLSELECT '20080801', 14, 1 UNION ALLSELECT '20080801', 16, 2 UNION ALLSELECT '20080801', 2, 4 UNION ALLSELECT '20080801', 20, 1 UNION ALLSELECT '20080801', 25, 1 UNION ALLSELECT '20080801', 27, 1 UNION ALLSELECT '20080801', 3, 1 UNION ALLSELECT '20080801', 30, 1 UNION ALLSELECT '20080801', 5, 3 UNION ALLSELECT '20080801', 6, 2 UNION ALLSELECT '20080801', 7, 1 UNION ALLSELECT '20080801', 8, 1 UNION ALLSELECT '20080801', 9, 1 UNION ALLSELECT '20080731', 1, 18 UNION ALLSELECT '20080731', 11, 1 UNION ALLSELECT '20080731', 12, 2 UNION ALLSELECT '20080731', 14, 4 UNION ALLSELECT '20080731', 16, 2 UNION ALLSELECT '20080731', 18, 2 UNION ALLSELECT '20080731', 2, 11 UNION ALLSELECT '20080731', 20, 3 UNION ALLSELECT '20080731', 21, 2 UNION ALLSELECT '20080731', 22, 2 UNION ALLSELECT '20080731', 23, 1 UNION ALLSELECT '20080731', 25, 2 UNION ALLSELECT '20080731', 29, 3 UNION ALLSELECT '20080731', 3, 6SELECT *FROM @sample s pivot ( SUM(totalreferrers) FOR page IN ([1], [2], [3], [4], [5]) ) p[/code] KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-08-11 : 06:02:33
|
Didn't we cover this issue here http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=107775withSELECT p.landDate, p.Page1, p.Page2, p.Page3, p.Page4, p.Page5, p.Page6, p.Page7, p.Page8, p.Page9, p.Page10FROM ( SELECT landDate, 'Page' + PARSENAME(REPLACE(landingPage, '_', '.'), 2) AS thePage FROM tblLandingPages ) AS sPIVOT ( COUNT(thePage) FOR thePage IN ([Page1], [Page2], [Page3], [Page4], [Page5], [Page6], [Page7], [Page8], [Page9], [Page10]) ) AS p E 12°55'05.25"N 56°04'39.16" |
 |
|
|
mike123
Master Smack Fu Yak Hacker
1462 Posts |
Posted - 2008-08-11 : 08:05:12
|
| Hi Khtan,Perfect. got it thank you.Peso - we did address the query there and we got it going great, i was just unable to follow the PIVOT part exactly and integrating it into my query was confusing. Using the temp table I was able to better understand what was going on exactly and got everything going perfectly now.thanks to both of you!! much appreciatedmike123 |
 |
|
|
|
|
|