简体   繁体   English

透视数据

[英]Pivoting the data

This is my SQL statement 这是我的SQL语句

select  id , name, type,  value  from table1 a
INNER JOIN table2 b on a.id = b.id
where  b.type in ('display','contact','ship')

which produces below result 产生以下结果

ID  name     type           value
5   test     display        display1
5   test     contact        contact1
5   test     ship           ship1
6   test2    display        display2
6   test2    contact        contact2
6   test2    ship           ship2

I need to get result in kind of pivoted format like this 我需要以这种枢轴格式获得结果

id  name   display   contact   ship
5   test   display1  contact1 ship1
6   test2  display2  contact2 ship2

I tried this solution : https://stackoverflow.com/a/6849706/2645738 ,but its giving me the same result (3 rows for each data). 我尝试了以下解决方案: https : //stackoverflow.com/a/6849706/2645738 ,但是它给了我相同的结果(每个数据3行)。 It's like i need to group by id and name,but don't know how to make display,contact,ship as columns. 就像我需要按ID和名称分组,但不知道如何将显示,联系方式,运输方式列为一列。

Would you please help me for the same. 您能帮我同样吗。

It is necessary to use PIVOT you could also do that by using simple case expression 必须使用PIVOT您也可以通过使用简单的case表达式来做到这一点

SELECT ID,
      Name,
       MAX(CASE([type]) WHEN 'display' THEN value END) [display],
       MAX(CASE([type]) WHEN 'contact' THEN value END) [contact],
       MAX(CASE([type]) WHEN 'ship' THEN value END) [ship]
FROM <table> GROUP BY ID, Name

Result : 结果:

ID  Name    display     contact     ship
5   test    display1    contact1    ship1
6   test2   display2    contact2    ship2

This query should give you the results you want: 此查询应为您提供所需的结果:

select  a.id , a.name, 
        max(case when b.type = 'display' then value end) as display,
        max(case when b.type = 'contact' then value end) as contact,
        max(case when b.type = 'ship' then value end) as ship
from table1 a
INNER JOIN table2 b on a.id = b.id
where  b.type in ('display','contact','ship')
group by a.id, a.name

This Worked for me 这对我有用

WITH T
AS
(
    SELECT
      id , 
      name, 
      type,  
      value  
      FROM table1 a
        INNER JOIN table2 b 
          ON a.id = b.id
        WHERE  b.type in ('display','contact','ship')
)
SELECT
  *
  FROM T
  PIVOT
  (
    MAX([Value])
    FOR
    [Type] IN
    (
        [display],[Contact],[Ship]
    )
  )PVT

Check the SQLFiddle 检查SQLFiddle

If you want PIVOT : 如果您想要PIVOT

DECLARE @DataSource TABLE
(   
    [id] TINYINT
   ,[name] VARCHAR(12)
   ,[type] VARCHAR(12)
   ,[value] VARCHAR(12)
);

INSERT INTO @DataSource ([id], [name], [type], [value])
VALUES (5, 'test', 'display', 'display1')
      ,(5, 'test', 'contact', 'contact1')
      ,(5, 'test', 'ship', 'ship1')
      ,(6, 'test2', 'display', 'display2')
      ,(6, 'test2', 'contact', 'contact2')
      ,(6, 'test2', 'ship',  'ship2');

SELECT *
FROM @DataSource
PIVOT
(
    MAX([value]) FOR [type] IN ([display], [contact], [ship])
) PVT;

在此处输入图片说明

select id,name,[display],[contact],[ship]

from #b 

pivot(max(value) for type in([display],[contact],[ship])) As d

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM