当列是字符串数据类型时,使用mssql中的“Pivot”将行转换为列

时间:2022-05-03 23:43:58

I need to know whether 'pivot' in MS SQL can be used for converting rows to columns if there is no aggregate function to be used. i saw lot of examples with aggregate function only. my fields are string data type and i need to convert this row data to column data.This is why i wrote this question.i just did it with 'case'. Can anyone help me......Thanks in advance.

我需要知道,如果没有要使用的聚合函数,MS SQL中的“pivot”是否可用于将行转换为列。我看到很多只有聚合函数的例子。我的字段是字符串数据类型,我需要将此行数据转换为列数据。这就是我写这个问题的原因。我只是用'case'做了。任何人都可以帮助我......先谢谢。

3 个解决方案

#1


13  

You can use a PIVOT to perform this operation. When doing the PIVOT you can do it one of two ways, with a Static Pivot that you will code the rows to transform or a Dynamic Pivot which will create the list of columns at run-time:

您可以使用PIVOT执行此操作。在执行PIVOT时,您可以使用以下两种方法之一:使用Static Pivot编写要转换的行,或使用Dynamic Pivot在运行时创建列列表:

Static Pivot (see SQL Fiddle with a Demo):

Static Pivot(参见SQL小提琴演示):

SELECT *
FROM
(
  select empid, wagecode, amount
  from t1
) x
pivot
(
  sum(amount)
  for wagecode in ([basic], [TA], [DA])
) p

Dynamic Pivot:

动态枢轴:

DECLARE @cols AS NVARCHAR(MAX),
    @query  AS NVARCHAR(MAX);

select @cols = STUFF((SELECT distinct ',' + QUOTENAME(wagecode) 
                  FROM t1 
            FOR XML PATH(''), TYPE
            ).value('.', 'NVARCHAR(MAX)') 
        ,1,1,'')

set @query = 'SELECT empid, ' + @cols + ' from 
             (
                 select empid, wagecode, amount
                 from t1
            ) x
            pivot 
            (
                sum(amount)
                for wagecode in (' + @cols + ')
            ) p '

execute(@query)

Both of these will give you the same results

这两个都会给你相同的结果

#2


1  

sample format

样本格式

empid     wagecode    amount
1              basic           1000
1              TA               500
1              DA               500
2              Basic           1500
2              TA               750
2              DA               750

empid工资代码金额1基本1000 1 TA 500 1 DA 500 2基本1500 2 TA 750 2 DA 750

empid      basic       TA        DA
1            1000         500      500
2            1500         750       750

基本TA DA 1 1000 500 500 2 1500 750 750

THE ANSWER I GOT IS

我得知的答案

   SELECT empID , [1bas] as basic, [1tasal] as TA,[1otsal] as DA
   FROM (
   SELECT empID, wage, amount
   FROM table) up
   PIVOT (SUM(amt) FOR wgcod IN ([1bas], [1tasal],[1otsal])) AS pvt
   ORDER BY empID 
   GO

#3


0  

Try this:

尝试这个:

SELECT empid AS EmpID     
, ISNULL(SUM(CASE wagecode WHEN 'basic' THEN Amount ELSE 0 END), 0) AS Basic         
, ISNULL(SUM(CASE wagecode WHEN 'ta' THEN Amount ELSE 0 END), 0) AS TA     
, ISNULL(SUM(CASE wagecode WHEN 'da' THEN Amount ELSE 0 END), 0) AS DA 
FROM Employee
GROUP BY empid 

#1


13  

You can use a PIVOT to perform this operation. When doing the PIVOT you can do it one of two ways, with a Static Pivot that you will code the rows to transform or a Dynamic Pivot which will create the list of columns at run-time:

您可以使用PIVOT执行此操作。在执行PIVOT时,您可以使用以下两种方法之一:使用Static Pivot编写要转换的行,或使用Dynamic Pivot在运行时创建列列表:

Static Pivot (see SQL Fiddle with a Demo):

Static Pivot(参见SQL小提琴演示):

SELECT *
FROM
(
  select empid, wagecode, amount
  from t1
) x
pivot
(
  sum(amount)
  for wagecode in ([basic], [TA], [DA])
) p

Dynamic Pivot:

动态枢轴:

DECLARE @cols AS NVARCHAR(MAX),
    @query  AS NVARCHAR(MAX);

select @cols = STUFF((SELECT distinct ',' + QUOTENAME(wagecode) 
                  FROM t1 
            FOR XML PATH(''), TYPE
            ).value('.', 'NVARCHAR(MAX)') 
        ,1,1,'')

set @query = 'SELECT empid, ' + @cols + ' from 
             (
                 select empid, wagecode, amount
                 from t1
            ) x
            pivot 
            (
                sum(amount)
                for wagecode in (' + @cols + ')
            ) p '

execute(@query)

Both of these will give you the same results

这两个都会给你相同的结果

#2


1  

sample format

样本格式

empid     wagecode    amount
1              basic           1000
1              TA               500
1              DA               500
2              Basic           1500
2              TA               750
2              DA               750

empid工资代码金额1基本1000 1 TA 500 1 DA 500 2基本1500 2 TA 750 2 DA 750

empid      basic       TA        DA
1            1000         500      500
2            1500         750       750

基本TA DA 1 1000 500 500 2 1500 750 750

THE ANSWER I GOT IS

我得知的答案

   SELECT empID , [1bas] as basic, [1tasal] as TA,[1otsal] as DA
   FROM (
   SELECT empID, wage, amount
   FROM table) up
   PIVOT (SUM(amt) FOR wgcod IN ([1bas], [1tasal],[1otsal])) AS pvt
   ORDER BY empID 
   GO

#3


0  

Try this:

尝试这个:

SELECT empid AS EmpID     
, ISNULL(SUM(CASE wagecode WHEN 'basic' THEN Amount ELSE 0 END), 0) AS Basic         
, ISNULL(SUM(CASE wagecode WHEN 'ta' THEN Amount ELSE 0 END), 0) AS TA     
, ISNULL(SUM(CASE wagecode WHEN 'da' THEN Amount ELSE 0 END), 0) AS DA 
FROM Employee
GROUP BY empid