带有IN运算符和WHERE子句的数据透视表

时间:2019-05-13 12:16:42

标签: sql sql-server sql-server-2012 pivot

我正在处理SQL查询并使用运算符PIVOTIN和子句WHERE。完成后,请注意我的SQL脚本以不同的方式对某些列(在这种情况下为ANGLE_1列)进行排序,请帮帮我找出问题所在。

SQL查询:

SELECT *
FROM (
SELECT 
      row_number() over (Partition By [PARAMETER_NAME] Order by [PARAMETER_VALUE]) as new 
      ,[PARAMETER_NAME]
      ,[PARAMETER_VALUE]

  FROM [dbo].table
  where [EXCHANGE_HEADER_ID] in ('1', '2')
  )  
  as s 
  pivot ( 
         min([PARAMETER_VALUE])
         FOR [PARAMETER_NAME] IN  (  DMX
                                    ,TYPE
                                    ,UNBALANCE_1
                                    ,ANGLE_1
                                    ,UNBALANCE_2
                                    ,ANGLE_2
                                    ,STATUS 
 ) )AS PVT

[dbo].table

ID  CREATED PARAMETER_NAME  PARAMETER_VALUE EXCHANGE_HEADER_ID
1   2016-09-09 11:39:21.3927453 TYPE    TYPE 3  1
2   2016-09-09 11:39:21.3927453 UNBALANCE_1 0.094   1
3   2016-09-09 11:39:21.3927453 ANGLE_1 95.7    1
4   2016-09-09 11:39:21.3927453 UNBALANCE_2 0.195   1
5   2016-09-09 11:39:21.3927453 ANGLE_2 135.8   1
6   2016-09-09 11:39:21.3927453 STATUS  0   1
7   2016-09-09 11:39:21.3927453 DMX 75044500454391206509-A1625300635    1
8   2016-09-09 11:39:21.3927453 MACHINE 442002  1
9   2016-09-09 11:39:21.3927453 DATE    2016-09-09 11:39:13 1
10  2016-09-09 23:20:27.2773565 TYPE    TYPE 3  2
11  2016-09-09 23:20:27.2773565 UNBALANCE_1 0.826   2
12  2016-09-09 23:20:27.2773565 ANGLE_1 229.6   2
13  2016-09-09 23:20:27.2773565 UNBALANCE_2 0.835   2
14  2016-09-09 23:20:27.2773565 ANGLE_2 198.1   2
15  2016-09-09 23:20:27.2773565 STATUS  0   2
16  2016-09-09 23:20:27.2773565 DMX 75044500454391206509-A1625301504    2
17  2016-09-09 23:20:27.2773565 MACHINE 442002  2
18  2016-09-09 23:20:27.2773565 DATE    2016-09-09 23:20:02 2

输出:

new DMX     TYPE    UNBALANCE_1 ANGLE_1 UNBALANCE_2 ANGLE_2 STATUS
1   750..   TYPE 3  0.094           229.6   0.195   135.8   0
2   750..   TYPE 3  0.826           95.7    0.835   198.1   0

预期输出:

new DMX     TYPE    UNBALANCE_1 ANGLE_1 UNBALANCE_2 ANGLE_2 STATUS
1   750..   TYPE 3  0.094           95.7    0.195   135.8   0
2   750..   TYPE 3  0.826           229.6   0.835   198.1   0

如上所示,问题出在ANGLE_1包含错误的值,可能是由于附加了列row_number() over (Partition By [PARAMETER_NAME] Order by [PARAMETER_VALUE]) as new吗?谢谢您的帮助

1 个答案:

答案 0 :(得分:1)

排序还取决于[ID]值,而不仅取决于parameter_value。这意味着Angle_1与Unbalance_1有关。我希望这会有所帮助。

select * into #table from
( select * from (values
(1,   '2016-09-09 11:39:21.3927453', 'TYPE',    'TYPE 3',  1),
(2,   '2016-09-09 11:39:21.3927453', 'UNBALANCE_1', '0.094',   1),
(3,   '2016-09-09 11:39:21.3927453', 'ANGLE_1', '95.7',    1),
(4,   '2016-09-09 11:39:21.3927453', 'UNBALANCE_2', '0.195',   1),
(5,   '2016-09-09 11:39:21.3927453', 'ANGLE_2', '135.8',   1),
(6,   '2016-09-09 11:39:21.3927453', 'STATUS',  '0',   1),
(7,   '2016-09-09 11:39:21.3927453', 'DMX', '75044500454391206509-A1625300635',    1),
(8,   '2016-09-09 11:39:21.3927453', 'MACHINE', '442002',  1),
(9,   '2016-09-09 11:39:21.3927453', 'DATE',    '2016-09-09 11:39:13', 1),
(10,  '2016-09-09 23:20:27.2773565', 'TYPE',    'TYPE 3',  2),
(11,  '2016-09-09 23:20:27.2773565', 'UNBALANCE_1', '0.826',   2),
(12,  '2016-09-09 23:20:27.2773565', 'ANGLE_1', '229.6',   2),
(13,  '2016-09-09 23:20:27.2773565', 'UNBALANCE_2', '0.835',   2),
(14,  '2016-09-09 23:20:27.2773565', 'ANGLE_2', '198.1',   2),
(15,  '2016-09-09 23:20:27.2773565', 'STATUS',  '0',   2),
(16,  '2016-09-09 23:20:27.2773565', 'DMX', '75044500454391206509-A1625301504',    2),
(17,  '2016-09-09 23:20:27.2773565', 'MACHINE', '442002',  2),
(18,  '2016-09-09 23:20:27.2773565', 'DATE',    '2016-09-09 23:20:02', 2)) tabledata (ID,  CREATED, PARAMETER_NAME,  PARAMETER_VALUE, EXCHANGE_HEADER_ID)
) x

SELECT *
FROM (
SELECT 
      row_number() over (Partition By [PARAMETER_NAME] Order by [ID] asc, [PARAMETER_VALUE] desc) as new 
      ,[PARAMETER_NAME]
      ,[PARAMETER_VALUE]

  FROM #table
  where [EXCHANGE_HEADER_ID] in ('1', '2')
  )  
  as s 
  pivot ( 
         min([PARAMETER_VALUE])
         FOR [PARAMETER_NAME] IN  (  DMX
                                    ,TYPE
                                    ,UNBALANCE_1
                                    ,ANGLE_1
                                    ,UNBALANCE_2
                                    ,ANGLE_2
                                    ,STATUS 
 ) )AS PVT;



 drop table #table;