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)
 TSQL help

Author  Topic 

andooran
Starting Member

2 Posts

Posted - 2009-01-05 : 02:20:37
I have a table, Table1, that contains three columns
ID, IntValue1, TypeOfIntValue

Sample rows

ID IntValue1 TypeOfIntValue
1 20 Tax
1 220 Income
2 22 Tax
3 400 Income


TypeOfIntValue can be either "Tax" or "Income"

Each ID can have at most two rows of data - Tax/Income

Now I want to combine data for same ID into a single row. Given that I can only use SQL 2000 compliant T-SQL, how do I write this query? (Cannot use Pivot operation etc.)

Expected result is like this

ID Tax Income
1 20 220
2 22 NULL
3 NULL 400

I dont want the exact query but would appreciate any guidance like suggestions to use certain type of query etc.

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2009-01-05 : 02:33:42
Posted in wrong forum
quote:
Given that I can only use SQL 2000 compliant T-SQL

Cross post http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=117161


E 12°55'05.63"
N 56°04'39.26"
Go to Top of Page

Thiyagu_04
Starting Member

37 Posts

Posted - 2009-01-05 : 02:58:42
select
distinct id,
(select IntValue1 from pvt where id=p.id and Typeofintvalue='Tax') as 'Tax',
(select IntValue1 from pvt where id=p.id and Typeofintvalue='Income') as 'Income'
from tablename p
Go to Top of Page

madhivanan
Premature Yak Congratulator

22864 Posts

Posted - 2009-01-05 : 03:59:55
quote:
Originally posted by Thiyagu_04

select
distinct id,
(select IntValue1 from pvt where id=p.id and Typeofintvalue='Tax') as 'Tax',
(select IntValue1 from pvt where id=p.id and Typeofintvalue='Income') as 'Income'
from tablename p


See the link posted. Peso showed how to do it effeciently

Madhivanan

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

- Advertisement -