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 |
|
oguzkaygun
Yak Posting Veteran
53 Posts |
Posted - 2011-04-06 : 03:59:51
|
| HiI have a table like below.id type price1 Bill 1002 Bank 503 Ticket 10So i want to display like belowBill Bank Ticket100 50 10How can i do with SQL query ? |
|
|
oguzkaygun
Yak Posting Veteran
53 Posts |
Posted - 2011-04-06 : 04:02:36
|
| Sorry for first posted. It should be like belowBill Bank Ticket100 --- ------ 50 ------ --- 10 |
 |
|
|
McDebil
Starting Member
23 Posts |
Posted - 2011-04-06 : 04:45:50
|
| SELECT '---' AS Bill, '---' AS Bank, CAST(Price AS VARCHAR(5)) AS TicketFROM [dbo].[TestTable]WHERE [type] = 'Ticket'UNIONSELECT '---' AS Bill, CAST(Price AS VARCHAR(5)) AS Bank, '---' AS TicketFROM [dbo].[TestTable]WHERE [type] = 'Bank'UNIONSELECT CAST(Price AS VARCHAR(5)) AS Bill, '---' AS Bank, '---' AS TicketFROM [dbo].[TestTable]WHERE [type] = 'Bill'ORDER BY Bill DESC, Bank DESCMcDebil |
 |
|
|
raghuveer125
Constraint Violating Yak Guru
285 Posts |
Posted - 2011-04-06 : 05:29:04
|
I think you are looking this S declare @q nvarchar(max),@p nvarchar(max)select @q=coalesce(@q+ ',['+type+']','['+type+']') from TestTableset @p=N'select * from (select [Price],type from TestTable ) p pivot (MAX([price]) for type in('+@q+')) as pvt'exec(@p)Raghu' |
 |
|
|
|
|
|