sql server - How to query and display data as a matrix? -


my query :

select *        (select            t.description,          isnull(count(e.employeecode), 0)                    employee e        inner join           department d on e.departmentcode = d.departmentcode      inner join           termreason t on e.termreasoncode = t.termreasoncode                    e.terminationdate not null          , e.terminationdate between @periodstartdate , @periodenddate) s pivot        (count(employeecode) description in          ([finance], [human resources], [nursing])) pvt 

my expected results are:

termination reason department1 department2 department3 etc etc ---------------------------------------------------------------------     aaa       value1       value2        value 3           value4     bbb       value1       value2        value 3           value4     ccc       value1       value2        value 3           value4     ddd       value1       value2        value 3           value4 

dynamic pivot

    declare @dynamicpivotquery nvarchar(max)     declare @columnname nvarchar(max)     --get distinct values of pivot column      select @columnname= isnull(@columnname + ',','')     + quotename(week)     (select distinct week #storesales) weeks     --prepare pivot query using dynamic      set @dynamicpivotquery =        n'select store, ' + @columnname + '         #storesales         pivot(sum(xcount)                week in (' + @columnname + ')) pvttable'     --execute dynamic pivot query     exec sp_executesql @dynamicpivotquery 

Comments

Popular posts from this blog

mysql - Dreamhost PyCharm Django Python 3 Launching a Site -

java - Sending SMS with SMSLib and Web Services -

java - How to resolve The method toString() in the type Object is not applicable for the arguments (InputStream) -