| Author |
Topic |
|
DavidRhodes
Starting Member
40 Posts |
Posted - 2004-03-24 : 06:27:07
|
I've got a table that contains the [ClientID], [SearchEngine], [Keyword], [Page] and [Month] and am trying to create a stored procedure that will show the total listings per search engine for Page1,2,3 and 4+ based on a given month and clientegData Page SearchEngine1 2 SearchEngine1 1 SearchEngine1 2 SearchEngine2 4 SearchEngine3 3SearchEngine4 2SearchEngine5 1SearchEngine5 1 Result wanted Page1 Page2 Page3 Page4+SearchEngine1 2 2 1 4SearchEngine2 6 2 1 5SearchEngine3 3 5 3 4 So far i have gotSELECT [SearchEngine], Count([Page]) AS [Page1], Count([Page]) AS [Page2], Count([Page]) AS [Page3], Count([Page]) AS [Page4+] FROM [tRawData] WHERE [ClientID] = @ClientID AND [Month] = @TheMonthGROUP BY [SearchEngine]ORDER BY [SearchEngine] ASCThe above is obviously wrong, can anyone help?Cheers in advance  |
|
|
Merkin
Funky Drop Bear Fearing SQL Dude!
4970 Posts |
|
|
DavidRhodes
Starting Member
40 Posts |
Posted - 2004-03-24 : 08:34:09
|
Thanks for the link, i'm nearly thereDECLARE @ClientID intDECLARE @TheMonth intDECLARE @Sql varchar(8000)SET @ClientID = 191SELECT @TheMonth = MAX(Month) FROM tRawData WHERE ClientID = @ClientIDSET @Sql = 'SELECT [SearchEngine] FROM [tRawData] WHERE [ClientID]='SET @Sql = @Sql + cast(@ClientID as varchar(5))SET @Sql = @Sql + ' AND [Month]='SET @Sql = @Sql + cast(@TheMonth as varchar(5))SET @Sql = @Sql + ' GROUP BY [SearchEngine] ORDER BY [SearchEngine] ASC'EXECUTE crosstab @Sql, 'Count([Page])', '[Page]', '[tRawData]' It returns all the values for [Page] as column names but I only require 1,2,3, and 4+, the 4+ is the tricky one. Also i'm not guaranteed to be returned 1,2,3,4 but need them there with zero as the value |
 |
|
|
DavidRhodes
Starting Member
40 Posts |
|
|
DavidRhodes
Starting Member
40 Posts |
Posted - 2004-03-24 : 09:45:33
|
Done itSELECT DISTINCT [SearchEngine], isnull((SELECT COUNT([Page]) from [tRawData] where [Page] = 1 and [SearchEngine]=rd.[SearchEngine] AND [ClientID] = @ClientID AND [Month] = @TheMonth),0) as [Page1], isnull((SELECT COUNT([Page]) from [tRawData] where [Page] = 2 and [SearchEngine]=rd.[SearchEngine] AND [ClientID] = @ClientID AND [Month] = @TheMonth),0) as [Page2], isnull((SELECT COUNT([Page]) from [tRawData] where [Page] = 3 and [SearchEngine]=rd.[SearchEngine] AND [ClientID] = @ClientID AND [Month] = @TheMonth),0) as [Page3],isnull((SELECT COUNT([Page]) from [tRawData] where [Page] >= 4 and [SearchEngine]=rd.[SearchEngine] AND [ClientID] = @ClientID AND [Month] = @TheMonth),0) as [Page4+]FROM [tRawData] rdWHERE [ClientID] = @ClientID AND [Month] = @TheMonthGROUP BY [SearchEngine]ORDER BY [SearchEngine] ASC |
 |
|
|
Merkin
Funky Drop Bear Fearing SQL Dude!
4970 Posts |
Posted - 2004-03-24 : 16:32:24
|
Sorry, a little bit of assumption on my part.Usually if someone asks a crosstab question, any attempt at writing a query for them would be answered with "but it needs to be dynamic" Glad you got it sorted outDamian |
 |
|
|
jsmith8858
Dr. Cross Join
7423 Posts |
Posted - 2004-03-24 : 17:19:27
|
I'd recommend this approach: SELECT [SearchEngine], SUM(CASE WHEN Page=1 THEN 1 ELSE 0 END) as Page1, SUM(CASE WHEN Page=2 THEN 1 ELSE 0 END) as Page2, SUM(CASE WHEN Page=3 THEN 1 ELSE 0 END) as Page3, SUM(CASE WHEN Page>3 THEN 1 ELSE 0 END) as [Page4+]FROM [tRawData] rdWHERE [ClientID] = @ClientID AND [Month] = @TheMonthGROUP BY [SearchEngine]ORDER BY [SearchEngine] ASC much shorter and should be much more efficient .- Jeff |
 |
|
|
DavidRhodes
Starting Member
40 Posts |
Posted - 2004-03-25 : 04:36:55
|
quote: Originally posted by jsmith8858 I'd recommend this approach: SELECT [SearchEngine], SUM(CASE WHEN Page=1 THEN 1 ELSE 0 END) as Page1, SUM(CASE WHEN Page=2 THEN 1 ELSE 0 END) as Page2, SUM(CASE WHEN Page=3 THEN 1 ELSE 0 END) as Page3, SUM(CASE WHEN Page>3 THEN 1 ELSE 0 END) as [Page4+]FROM [tRawData] rdWHERE [ClientID] = @ClientID AND [Month] = @TheMonthGROUP BY [SearchEngine]ORDER BY [SearchEngine] ASC much shorter and should be much more efficient .- Jeff
Not quite what I want, I needed the number of columns COUNT() for Page1,2,3,4+ not the SUM() |
 |
|
|
jsmith8858
Dr. Cross Join
7423 Posts |
Posted - 2004-03-25 : 07:30:01
|
| look at it more closely -- what value is it adding up ?1That's how you COUNT !- Jeff |
 |
|
|
|