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.
| Author |
Topic |
|
amran
Starting Member
3 Posts |
Posted - 2011-03-03 : 05:09:06
|
| Hi AllI'm new in SQL, need some help on below Query I need to Query data from a few different databasefor normal select statement I can do it, problem is I not sure how to split data from 1 of the table to be in 1 ColumnI got a few hundred line of data in few database that I need to use the Microsoft Query to extract it to Excelsample data as belowTable1 key name address1 joe USA2 jamie UKTable2 key type tel1 OFF 111111 HP 222221 FAX 333332 OFF 444442 HP 555552 FAX 66666Table3 Key Company Position1 Alpha Manager2 Bravo Executiveend Result should be as below Query Result key name address Company Position OFF HP FAX1 joe USA Alpha Manager 11111 22222 333332 Jamie UK Bravo Executive 44444 55555 66666Appreciate you guys can help me outThanks. |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2011-03-03 : 05:21:48
|
are you using SQL 2005 / 2008 ? KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
vaibhavktiwari83
Aged Yak Warrior
843 Posts |
Posted - 2011-03-03 : 05:27:10
|
| Value in column type are fixed ?like OFF, HP, FAX only three values or may be more than that ?Vaibhav TIf I cant go back, I want to go fast... |
 |
|
|
amran
Starting Member
3 Posts |
Posted - 2011-03-03 : 20:11:03
|
| HiI'm Using SQL 2008The Value in Column "Type" is fix.Thanks. |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2011-03-03 : 22:29:31
|
[code]select *from ( select t1.[key], t1.[name], t1.[address], t3.[Company], t3.[Position], t2.[type], t2.[tel] from Table1 t1 inner join Table3 t3 on t1.[key] = t3.[key] inner join Table2 t2 on t1.[key] = t2.[key] ) d pivot ( max([tel]) for [type] in ([OFF], [HP], [FAX]) )p [/code] KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
amran
Starting Member
3 Posts |
Posted - 2011-03-04 : 02:53:38
|
Thanksbut I made a mistake, we do run sql server 2008, but the Database Compatibility is set to 80 (SQL Server 2000) for this Database, I don't think I can change this as it's the vendor Program/Database, worry after change the Level then the program don't work did try your code in my Test Environment with that database level alter to 100, work perfectlyDo you have the code to do it in SQL 2000Appreciate itThanks. quote: Originally posted by khtan
select *from ( select t1.[key], t1.[name], t1.[address], t3.[Company], t3.[Position], t2.[type], t2.[tel] from Table1 t1 inner join Table3 t3 on t1.[key] = t3.[key] inner join Table2 t2 on t1.[key] = t2.[key] ) d pivot ( max([tel]) for [type] in ([OFF], [HP], [FAX]) )p KH[spoiler]Time is always against us[/spoiler]
|
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2011-03-04 : 06:48:51
|
[code]select t1.[key], t1.[name], t1.[address], t3.[Company], t3.[Position], [OFF] = max(case when t2.[type] = 'OFF' then t2.[tel] end), [HP] = max(case when t2.[type] = 'HP' then t2.[tel] end), [FAX] = max(case when t2.[type] = 'FAX' then t2.[tel] end)from Table1 t1 inner join Table3 t3 on t1.[key] = t3.[key] inner join Table2 t2 on t1.[key] = t2.[key]group by t1.[key], t1.[name], t1.[address], t3.[Company], t3.[Position][/code] KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
|
|
|
|
|