| Author |
Topic |
|
dhani
Posting Yak Master
132 Posts |
Posted - 2009-07-23 : 17:14:38
|
| Hello All,please help me with this query resultselectlclog.stVal,(CASE kt1.mcode WHEN 'I' THEN 'Interactive' WHEN 'M' THEN 'Magazine' WHEN 'N' THEN 'Newspaper' WHEN 'NET' THEN 'Network' WHEN 'O' THEN 'Outdoor' WHEN 'R' THEN 'Radio' WHEN 'S' THEN 'Supplement' WHEN 'T' THEN 'Trade' WHEN 'TV' THEN 'TV & Local Cable' WHEN 'X' THEN 'Network Radio' ELSE 'UNDEFINE' END) as Mcode,CASE Count(*) WHEN 1 THEN 0 ELSE count(*) END as TotalDocsfrom lclog left join itms on lclog.statenum = itms.statenum left join idat i on itms.itemnum=i.itemnum left join xItem kx1 on i.itemnum=kx1.itemnum left join kTab kt1 on kx1.keywordnum=kt1.keywordnumwhere flags=12 and scope = 102 and lclog.statenum not in (101,102) and i.status <> 16group by lclog.stVal,kt1.mcodeorder by 1,2,3Stname,MCode,TotalDocsPrecription , Trade, 3Precription , Magazine,0Precription , Newspaper,0Precription , Radio, 5Precription ,Supplement,8Precription ,Network, Radio, 2Precription ,Radio, 119Precription ,TV & Local Cable, 53StandardQue ,Network, 57StandardQue ,Interactive,511StandardQue ,Magazine, 31StandardQue ,Newspaper,17StandardQue ,Outdoor,13StandardQue ,Supplement,7StandardQue ,Trade,13StandardQue ,Interactive ,0StandardQue ,Network Radio, 0StandardQue ,Newspaper, 0StandardQue ,Radio, 11StandardQue ,Supplement, 20StandardQue ,TV & Local Cable, 28StandardQue ,Network Radio, 48StandardQue ,Radio, 722StandardQue ,TV & Local Cable, 363 how can i convery the above results as below (first column values become header like a pivot table)is it possible? in sql serverplease sir kindly help me sir, Prescription StandardQue Trade 3 10Magazine 40 35Newspaper 00Radio 1 300 Supplement 40 59Network 554 TV& Local Cable 43 23interactive ... ... outdoor.............Thanks in a ton advancedhani |
|
|
dhani
Posting Yak Master
132 Posts |
Posted - 2009-07-23 : 18:49:49
|
| i tried palmens suggestion as SELECT mcode, SUM(CASE WHEN stname = 'Prescription' THEN TotalDocs ELSE 0 END) AS Prescription, SUM(CASE WHEN stname = 'StandardQue' THEN TotalDocs ELSE 0 END) AS StandardQueFROM (<your current query here without ORDER BY>) AS TGROUP BY mcode;with this i got the resultit may help to othersthanks to alll |
 |
|
|
dhani
Posting Yak Master
132 Posts |
Posted - 2009-07-23 : 19:10:02
|
| is there any way to show totals @ bottomfor ex Prescription StandardQue Trade 3 10Magazine 40 35Newspaper 00Radio 1 300 TOTAL 44 345please Thanks in advancedhani |
 |
|
|
dhani
Posting Yak Master
132 Posts |
Posted - 2009-07-23 : 19:58:37
|
| Please help me to find outis it possible?please..... |
 |
|
|
sodeep
Master Smack Fu Yak Hacker
7174 Posts |
Posted - 2009-07-24 : 00:15:17
|
| Use Reporting Services Layout to figure out that one. |
 |
|
|
Nageswar9
Aged Yak Warrior
600 Posts |
Posted - 2009-07-24 : 03:19:54
|
HI Try this once,declare @temp table ( Stname varchar(32), MCode varchar(32) ,TotalDocs int )insert into @temp select 'Precription' , 'Trade', 3insert into @temp select 'Precription' , 'Magazine',0insert into @temp select 'Precription' , 'Newspaper',0insert into @temp select 'Precription' , 'Radio', 5insert into @temp select 'Precription' , 'Supplement',8insert into @temp select 'Precription' , 'Network Radio', 2insert into @temp select 'Precription' , 'TV & Local Cable', 53insert into @temp select 'StandardQue' , 'Network', 57insert into @temp select 'StandardQue' , 'Magazine', 31insert into @temp select 'StandardQue' , 'Newspaper',17insert into @temp select 'StandardQue' , 'Outdoor',13insert into @temp select 'StandardQue' , 'Supplement',7insert into @temp select 'StandardQue' , 'Trade',13insert into @temp select 'StandardQue' , 'Interactive' ,0insert into @temp select 'StandardQue' , 'Radio', 11insert into @temp select 'StandardQue' , 'TV & Local Cable', 28insert into @temp select 'StandardQue' , 'Network Radio', 48--SELECT * FROM @TEMPSELECT mcode,Precription,StandardQue from @Temppivot ( min(totaldocs) for Stname in (Precription,StandardQue)) p |
 |
|
|
ayamas
Aged Yak Warrior
552 Posts |
Posted - 2009-07-24 : 03:28:50
|
Expading Nageswars solution you can get the final total like thisdeclare @temp table ( Stname varchar(32), MCode varchar(32) ,TotalDocs int )insert into @temp select 'Precription' , 'Trade', 3insert into @temp select 'Precription' , 'Magazine',0insert into @temp select 'Precription' , 'Newspaper',0insert into @temp select 'Precription' , 'Radio', 5insert into @temp select 'Precription' , 'Supplement',8insert into @temp select 'Precription' , 'Network Radio', 2insert into @temp select 'Precription' , 'TV & Local Cable', 53insert into @temp select 'StandardQue' , 'Network', 57insert into @temp select 'StandardQue' , 'Magazine', 31insert into @temp select 'StandardQue' , 'Newspaper',17insert into @temp select 'StandardQue' , 'Outdoor',13insert into @temp select 'StandardQue' , 'Supplement',7insert into @temp select 'StandardQue' , 'Trade',13insert into @temp select 'StandardQue' , 'Interactive' ,0insert into @temp select 'StandardQue' , 'Radio', 11insert into @temp select 'StandardQue' , 'TV & Local Cable', 28insert into @temp select 'StandardQue' , 'Network Radio', 48--SELECT * FROM @TEMPselect case when GROUPING(mcode)=1 then 'Total' else mcode end,sum(Precription),sum(StandardQue) from(SELECT mcode,Precription,StandardQue from @Temppivot ( min(totaldocs) for Stname in (Precription,StandardQue)) p)t group by mcode with rollup |
 |
|
|
dhani
Posting Yak Master
132 Posts |
Posted - 2009-07-24 : 09:35:48
|
| Thank you very much for your help i will try it and let you knowthanks for your guidanceit is highly appreciatablei will let you know , once i get it done |
 |
|
|
dhani
Posting Yak Master
132 Posts |
Posted - 2009-07-24 : 15:20:12
|
| Dear All,i modified my query as below but i am getting below error please help me <<QUERY removed for security reasons>>----- Result as belowMsg 156, Level 15, State 1, Line 19Incorrect syntax near the keyword 'pivot'.Msg 319, Level 15, State 1, Line 21Incorrect syntax near the keyword 'with'. If this statement is a common table expression or an xmlnamespaces clause, the previous statement must be terminated with a semicolon.could you please help me , i am in the final stage of my problemPlease sirThanks in advancedhani |
 |
|
|
dhani
Posting Yak Master
132 Posts |
Posted - 2009-07-24 : 15:34:24
|
| is it issue with inside group by clause just before PIVOT keyword?please..... |
 |
|
|
dhani
Posting Yak Master
132 Posts |
Posted - 2009-07-24 : 16:16:05
|
| Any Ideas or Suggestions please kindlyRegardsdhani |
 |
|
|
ayamas
Aged Yak Warrior
552 Posts |
Posted - 2009-07-26 : 07:10:12
|
quote: Originally posted by dhani Dear All,<<QUERY removed for security reasons>>
Then atleast post some sample data & your original query based on the sample data. |
 |
|
|
|
|
|