Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/84.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 数据透视_Sql_Sql Server_Sql Server 2008_Tsql - Fatal编程技术网

Sql 数据透视

Sql 数据透视,sql,sql-server,sql-server-2008,tsql,Sql,Sql Server,Sql Server 2008,Tsql,这是我的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') 这将产生以下结果 ID name type value 5 test display display1 5 test contact con

这是我的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')
这将产生以下结果

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
我需要得到像这样的数据透视格式的结果

id  name   display   contact   ship
5   test   display1  contact1 ship1
6   test2  display2  contact2 ship2
我尝试了这个解决方案:,但它给了我相同的结果(每个数据3行)。这就像我需要按id和名称分组,但不知道如何显示、联系、作为列发送


你能帮我做同样的事情吗。

有必要使用
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

有必要使用
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

此查询应提供所需的结果:

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

此查询应提供所需的结果:

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
这对我有用

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
检查一下

这对我来说很有效

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

如果要查看轴,请检查轴:

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;

如果您想要
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;

谢谢您抽出时间。这起作用了。您是否可以将display、contact和ship作为逗号分隔的值放在一个变量中进行更改?e、 g.声明@Types varchar(max)=“显示、联系、发货”。我将在存储过程中编写这段代码,这些值可能包含在单个变量中。谢谢您的时间。这起作用了。您是否可以将display、contact和ship作为逗号分隔的值放在一个变量中进行更改?e、 g.声明@Types varchar(max)=“显示、联系、发货”。我将在存储过程中编写此代码,这些值可能来自单个变量。仅代码的答案不会为OP或任何找到此答案的人提供太多信息。请更新您的答案,并详细说明为什么您认为这回答了OP的问题。请查看仅代码的答案并不能为OP或找到此答案的任何人提供太多信息。请更新您的答案,并详细说明为什么您认为这回答了OP的问题。请查看