Sql拆分字符串并在case语句中使用

时间:2016-06-10 08:39:28

标签: sql sql-server sql-server-2008

我在sql中有一个逗号分隔的字符串列。

例如

declare @grouplist nvarchar(max)
set @grouplist='Country,Region,DEPTNO'; 

我需要用逗号分割字符串,并在case sql语句中使用它。

例如,我需要使用像这样的分裂字符串

 Case 
     When Deptno is not null Then   Cast( DEPTNO as Nvarchar(max)) 
     When Region  is not null Then Region
     When Country is not null then Country
     END

我们可以拆分字符串并在sql语句中使用。

我需要生成这样的结果

table

我正在使用这样的查询来生成

 select  
     Case When Deptno is not null Then  Cast( DEPTNO as Nvarchar(max)) When Region  is not null Then Region When Country is not null then Country  END   ,
     Count(EmpNo) EmpCount, 
     Sum(Sal) Total
     from emp group by  Country,Region,DEPTNO   with rollup having country is not null order by  Country,region,Deptno,  EmpCount desc 

这里的问题是group by子句传递给存储过程为 commaseperated string。

由于

0 个答案:

没有答案