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 2005 Forums
 Transact-SQL (2005)
 Select report

Author  Topic 

paull
Yak Posting Veteran

50 Posts

Posted - 2009-06-04 : 10:03:54
Hi all

I really hope you can help!
I am creating a report listing out comments from a table called Verbs. The report format is this....:

ID Country Variable Comment
111 UK V1 Blah blah
112 France V1 hello
113 UK V2 The quick brown fox
113 UK V3 jumped over the lazy dog

However the source table is as such...:

ID | V1 | V2 | V3 | Country
111| Blah Blah | Null | Null | UK
112| hello | Null | Null | France
113| Null | The quick etc | jumped over etc| UK

Obviously, I wouldn't list out the nulls.
I started going down the Case when not null route but got completely confused!!!

Any help would be greatly appeciated

Thanks

P

khtan
In (Som, Ni, Yak)

17689 Posts

Posted - 2009-06-04 : 10:11:49
if you are using SQL Server 2005 check out the pivot operator


KH
[spoiler]Time is always against us[/spoiler]

Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

52326 Posts

Posted - 2009-06-04 : 13:07:13
[code]
SELECT ID,Country,
MAX(CASE WHEN Variable='V1' THEN Comment ELSE NULL END) AS V1,
MAX(CASE WHEN Variable='V2' THEN Comment ELSE NULL END) AS V2,
MAX(CASE WHEN Variable='V3' THEN Comment ELSE NULL END) AS V3
FROM Table
GROUP BY ID,Country
[/code]
Go to Top of Page

wjohnson16
Starting Member

5 Posts

Posted - 2009-06-05 : 12:36:19
***This will check to see, if data is null then plug in your on words like no comments as substitue for null data

select ID,Country,
isnull(V1, 'No Comments')as Variable1,
isnull(v2, 'No Comments')as Variable2,
isnull(v2, 'No comments')as Varaible3
from source
Go to Top of Page
   

- Advertisement -