sql server - How do I show all combinations in one row? -
sql server - How do I show all combinations in one row? -
i tried find similar problem here couldn't. simplified version of problem. have table
and have table:
i want show results this:
the number of columns here (in lastly resultset) differ according how many combinations there each id. possible do? have limited amount of understanding sql pivot,but not sure pivot. ideas welcome.
you right using pivot u can transpose rows columns.
create table #tablea (id int,name varchar(100)) insert #tablea values (1000,'anna'),(2000,'peter') create table #tableb (id int,city varchar(50),dates varchar(50)) insert #tableb values (1000,'new york','2014-05-10' ),(1000,'atlanta','2014-06-11'), (1000,'los ang','2014-09-11'),(2000,'seattle','2014-02-10'), (2000,'miami','2014-03-11') select id, max([city1]) [city], max([dates1]) [dates], max([city2]) [city], max([dates2]) [dates], max([city3]) [city], max([dates3]) [dates] (select 'city'+ convert(varchar(50), row_number() over(partition b.id order dates)) cityrn, 'dates'+ convert(varchar(50), row_number() over(partition b.id order dates)) datesrn, a.id,a.name,b.city,b.dates #tableb b bring together #tablea on a.id = b.id) pivot (max(city) cityrn in([city1],[city2],[city3])) piv pivot (max(dates) datesrn in ([dates1],[dates2],[dates3])) piv1 grouping id
dynamic version :
declare @citycols varchar(max)='', @datsecols varchar(max)='', @aggcitycols varchar(max)='', @aggdatsecols varchar(max)='' declare @sql nvarchar(max) select @citycols += ',[' + crn + ']' (select distinct 'city'+ convert(varchar(50), row_number() over(partition id order dates)) crn #tableb) select @datsecols += ',[' + drn + ']' (select distinct 'dates'+ convert(varchar(50), row_number() over(partition id order dates)) drn #tableb) select @citycols = right(@citycols, len(@citycols) - 1) select @datsecols = right(@datsecols, len(@datsecols) - 1) select @aggcitycols += ',' + crn (select distinct 'max(city'+ convert(varchar(50), row_number() over(partition id order dates))+ ') city' crn #tableb) select @aggdatsecols += ',' + drn (select distinct 'max(dates'+ convert(varchar(50), row_number() over(partition id order dates))+ ') [date]' drn #tableb) select @aggcitycols = right(@aggcitycols, len(@aggcitycols) - 1) select @aggdatsecols = right(@aggdatsecols, len(@aggdatsecols) - 1) set @sql=' select id,' + @aggcitycols + ',' + @aggdatsecols+ ' (select ''city''+ convert(varchar(50), row_number() over(partition b.id order dates)) cityrn, ''dates''+ convert(varchar(50), row_number() over(partition b.id order dates)) datesrn ,a.id,a.name,b.city,b.dates #tableb b bring together #tablea on a.id = b.id) pivot (max(city) cityrn in(' + @citycols+ ')) piv pivot (max(dates) datesrn in (' + @datsecols+ ')) piv1 grouping id ' exec sp_executesql @sql
output
+-----+--------+-----------+------------+---------+-----------+----------+-------------+ | id | name | city | date | city | date | city | date | +-----+--------+-----------+------------+---------+-----------+----------+-------------+ |1000 | anna | new york | 2014-05-10 | atlanta | 2014-06-11| los ang | 2014-09-11 | +-----+--------+-----------+------------+---------+-----------+----------+-------------+ |2000 | peter | seattle | 2014-02-10 | miami | 2014-03-11| null | null | +-----+--------+-----------+------------+---------+-----------+----------+-------------+
sql-server tsql sql-server-2008-r2
Comments
Post a Comment