sql server - How to sub total more than 1 in dynamic pivot sql -
i'm trying create query total , 2 sub total 2 coulin in dynamic pivot.
i have table & query this
create table #sampledata ( class varchar(10), name varchar(10), location varchar(10), item varchar(10), temp varchar (10) ) insert #sampledata values ('abc', 'ron', 'a', 'pencil', '12345'), ('abc', 'ron', 'a', 'pen', '2345'), ('abc', 'tom', 'c', 'pencil', '34343'), ('abc', 'tom', 'd', 'pencil', '252525'), ('def', 'ron', 'e', 'pen', '35345'), ('def', 'tom', 'f', 'pencil', '87878'), ('def', 'tom', 'g', 'pen', '9876'), ('ghi', 'ron', 'h', 'pen', '09090'), ('ghi', 'ron', 'i', 'pen', '40404'), ('ghi', 'tom', 'j', 'pencil', '144442345'), ('ghi', 'tom', 'k', 'pencil', '4444333') declare @pivot_columns varchar(max), @select_columns varchar(max) select @pivot_columns = stuff((select distinct ',' + quotename(item) #sampledata xml path('')), 1, 1, '') select @select_columns = stuff((select distinct ',sum(' + quotename(item) + ') '+quotename(item) #sampledata xml path('')), 1, 1, '') declare @sql varchar(max) set @sql = 'select case when grouping(name) = 1 , grouping(class) = 0 ''total''+ '' '' + class when grouping(name) = 1 , grouping(class) = 1 ''total'' else class end class, name, location, temp,' + @select_columns + ' ( select class, name, location, item, temp #sampledata ) pivotdata pivot ( count(item) item in (' + @pivot_columns + ') ) pivotresult group class, name, location, temp rollup ' exec(@sql)
i wanna show subtotal name based on class& subtotal class, in first imager there sub total based on temp. if change 'group class, name rollup' there's show error..
appreciate help, thankyou.
i think can try (i reviewed first case, added case name , added having conditions):
set @sql = 'select case when grouping(name) = 1 , grouping(class) = 0 ''total ''+ '' '' + class when grouping(name) = 1 , grouping(class) = 1 ''total '' else class end class , case when grouping(location)=1 , grouping(temp)=1 ''total ''+name else name end name, location, temp,' + @select_columns + ' ( select class, name, location, item, temp #sampledata ) pivotdata pivot ( count(item) item in (' + @pivot_columns + ') ) pivotresult group class, name, location, temp rollup having (grouping(location)=1 , grouping(temp)=1 ) or (grouping(location)=0 , grouping(temp)=0 , grouping(name)=0 , grouping(class)=0) '
update:
you can use grouping sets, obtaining same result (avoid using having condition in former query):
set @sql = 'select case when grouping(name) = 1 , grouping(class) = 0 ''total ''+ '' '' + class when grouping(name) = 1 , grouping(class) = 1 ''total '' else class end class , case when grouping(location)=1 , grouping(temp)=1 ''total ''+name else name end name, location, temp,' + @select_columns + ' ( select class, name, location, item, temp #sampledata ) pivotdata pivot ( count(item) item in (' + @pivot_columns + ') ) pivotresult group grouping sets ((class,name), (class), (class, name, location, temp),()) '
output:
+------------+-----------+----------+-----------+-----+--------+ | class | name | location | temp | pen | pencil | +------------+-----------+----------+-----------+-----+--------+ | abc | ron | | 12345 | 0 | 1 | | abc | ron | | 2345 | 1 | 0 | | abc | total ron | null | null | 1 | 1 | | abc | tom | c | 34343 | 0 | 1 | | abc | tom | d | 252525 | 0 | 1 | | abc | total tom | null | null | 0 | 2 | | total abc | null | null | null | 1 | 3 | | def | ron | e | 35345 | 1 | 0 | | def | total ron | null | null | 1 | 0 | | def | tom | f | 87878 | 0 | 1 | | def | tom | g | 9876 | 1 | 0 | | def | total tom | null | null | 1 | 1 | | total def | null | null | null | 2 | 1 | | ghi | ron | h | 09090 | 1 | 0 | | ghi | ron | | 40404 | 1 | 0 | | ghi | total ron | null | null | 2 | 0 | | ghi | tom | j | 144442345 | 0 | 1 | | ghi | tom | k | 4444333 | 0 | 1 | | ghi | total tom | null | null | 0 | 2 | | total ghi | null | null | null | 2 | 2 | | total | null | null | null | 5 | 6 | +------------+-----------+----------+-----------+-----+--------+
Comments
Post a Comment