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 2000 Forums
 SQL Server Development (2000)
 Knotty problem

Author  Topic 

KabirPatel
Yak Posting Veteran

54 Posts

Posted - 2007-03-20 : 06:19:57

Hi,

I have 3 tables as follows:

TABLE A

LandName | LandNumber
============================
Land A | 10
Land B | 20
etc....

TABLE B

CountryName | CountryCode
============================
England | EG
Scotland | SC
N Ireland | NI
etc....

TABLE C

LandName | CountryCode
============================
Land A | EG
Land A | SC
Land B | EG
etc...

I have been asked to output this data in the following format:

LandName | EG | SC | NI
===================================================
Land A | Yes | Yes | No
Land B | Yes | No | Yes

What is the easiest way of doing this without a cursor?

Thanks
Kabir


SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2007-03-20 : 06:23:34
SELECT LandName,
MAX(case when countrycode = 'eg' then 'Yes' else 'No' end) as [EG],
MAX(case when countrycode = 'sc' then 'Yes' else 'No' end) as [SC],
MAX(case when countrycode = 'ni' then 'Yes' else 'No' end) as [NI]
from tableC
group by landname
order by landname


Peter Larsson
Helsingborg, Sweden
Go to Top of Page
   

- Advertisement -