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)
 How to fom a query for this

Author  Topic 

ceema
Yak Posting Veteran

80 Posts

Posted - 2006-12-07 : 20:54:34
Hello all,

I have a table "News" with the following fields, I want to get the result according to categoryid

NewsID numeric 9
CategoryID smallint
Title nvarchar(100)

For example It's values are

NewsID CategoryID Title
--------------------------
1 3 All are well
2 1 No Rain Today
3 1 Heavy rain today
4 2 New flowers


so, I should get title with categoryid 1 as weather,2 as nature,3 as life


I tried

select (select Title from News where CategoryID=1) as weather,
(select Title from News where CategoryID=2) as nature,
(select Title from News where CategoryID=3) as life from News, but as you can imagine, it's not working

Thanks
Ceema

PSamsig
Constraint Violating Yak Guru

384 Posts

Posted - 2006-12-08 : 02:13:14
[code]SELECT CASE WHEN CategoryID = 1 THEN Title END AS Weather,
CASE WHEN CategoryID = 2 THEN Title END AS Nature,
CASE WHEN CategoryID = 3 THEN Title END AS Life
FROM News[/code]

-- If you give someone a program, you will frustrate them for a day; if you teach them how to program, you will frustrate them for a lifetime.
Go to Top of Page

ceema
Yak Posting Veteran

80 Posts

Posted - 2006-12-08 : 02:22:26
PSamsig,

Thank you so much. It's working fine.

Regards
Ceema
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2006-12-08 : 02:45:47
Read about Cross-tab Reports from more informations

Madhivanan

Failing to plan is Planning to fail
Go to Top of Page
   

- Advertisement -