与不合作的枢纽斗争

时间:2018-07-05 16:28:04

标签: sql tsql

我有一个具有大量列的表,看起来像这样:

 SELECT TOP 1000 [EXTVOLUMELABELID]
  ,[FCVOLUMEDRVRID]
  ,[BUSINESSDT]
  ,[H00I1]
  ,[H00I2]
  ,[H00I3]
  ,[H00I4]
  ,[H01I1]
  ,[H01I2]
  ,[H01I3]
  ,[H01I4]
  ,[H02I1]
  ,[H02I2]
  ,[H02I3]
  ,[H02I4]
  ,[H03I1]
  ,[H03I2]
  ,[H03I3]
  ,[H03I4]
  ,[H04I1]
  ,[H04I2]
  ,[H04I3]
  ,[H04I4]
  ,[H05I1]
  ,[H05I2]
  ,[H05I3]
  ,[H05I4]
  ,[H06I1]
  ,[H06I2]
  ,[H06I3]
  ,[H06I4]
  ,[H07I1]
  ,[H07I2]
  ,[H07I3]
  ,[H07I4]
  ,[H08I1]
  ,[H08I2]
  ,[H08I3]
  ,[H08I4]
  ,[H09I1]
  ,[H09I2]
  ,[H09I3]
  ,[H09I4]
  ,[H10I1]
  ,[H10I2]
  ,[H10I3]
  ,[H10I4]
  ,[H11I1]
  ,[H11I2]
  ,[H11I3]
  ,[H11I4]
  ,[H12I1]
  ,[H12I2]
  ,[H12I3]
  ,[H12I4]
  ,[H13I1]
  ,[H13I2]
  ,[H13I3]
  ,[H13I4]
  ,[H14I1]
  ,[H14I2]
  ,[H14I3]
  ,[H14I4]
  ,[H15I1]
  ,[H15I2]
  ,[H15I3]
  ,[H15I4]
  ,[H16I1]
  ,[H16I2]
  ,[H16I3]
  ,[H16I4]
  ,[H17I1]
  ,[H17I2]
  ,[H17I3]
  ,[H17I4]
  ,[H18I1]
  ,[H18I2]
  ,[H18I3]
  ,[H18I4]
  ,[H19I1]
  ,[H19I2]
  ,[H19I3]
  ,[H19I4]
  ,[H20I1]
  ,[H20I2]
  ,[H20I3]
  ,[H20I4]
  ,[H21I1]
  ,[H21I2]
  ,[H21I3]
  ,[H21I4]
  ,[H22I1]
  ,[H22I2]
  ,[H22I3]
  ,[H22I4]
  ,[H23I1]
  ,[H23I2]
  ,[H23I3]
  ,[H23I4]
  ,[OFFSETFROMMIDNIGHTNUM]
  ,[UPDATEDTM]
  FROM [POSVOLUMEHIST]

我希望输出格式仅包含以下列:

EXTVOLUMEID,BUSINESSDT,TIME(从看起来像这样的列中别名:H00I2等),值(时间列中的值)

到目前为止,我已经拥有了:

USE tkcsdb

SELECT  ORGREPORT.ORGNM
  ,CONVERT (DATE, POS.BUSINESSDT) As 'Date'
  ,start_time
  ,footfall_count


  FROM POSVOLUMEHIST POS

  JOIN FCCATEGORY CAT
  ON POS.EXTVOLUMELABELID = CAT.EXTVOLUMELABELID

  JOIN ORG ORG
  ON CAT.ORGIDSID = ORG.ORGIDSID

  JOIN ORG ORGREPORT
  ON ORG.REPORTTOORGID = ORGREPORT.ORGIDSID

UNPIVOT (footfall_count for start_time in

(      POS.H08I1
  ,POS.H08I2
  ,POS.H08I3
  ,POS.H08I4
  ,POS.H09I1
  ,POS.H09I2
  ,POS.H09I3
  ,POS.H09I4
  ,POS.H10I1
  ,POS.H10I2
  ,POS.H10I3
  ,POS.H10I4
  ,POS.H11I1
  ,POS.H11I2
  ,POS.H11I3
  ,POS.H11I4
  ,POS.H12I1
  ,POS.H12I2
  ,POS.H12I3
  ,POS.H12I4
  ,POS.H13I1
  ,POS.H13I2
  ,POS.H13I3
  ,POS.H13I4
  ,POS.H14I1
  ,POS.H14I2
  ,POS.H14I3
  ,POS.H14I4
  ,POS.H15I1
  ,POS.H15I2
  ,POS.H15I3
  ,POS.H15I4
  ,POS.H16I1
  ,POS.H16I2
  ,POS.H16I3
  ,POS.H16I4
  ,POS.H17I1
  ,POS.H17I2
  ,POS.H17I3
  ,POS.H17I4
  ,POS.H18I1
  ,POS.H18I2
  ,POS.H18I3
  ,POS.H18I4
  ,POS.H19I1
  ,POS.H19I2
  ,POS.H19I3
  ,POS.H19I4
  ,POS.H20I1
  ,POS.H20I2
  ,POS.H20I3
  ,POS.H20I4
  ,POS.H21I1
  ,POS.H21I2
  ,POS.H21I3
  ,POS.H21I4
  ,POS.H22I1
))


  WHERE

  CAST (POS.BUSINESSDT as Date) >= 'Mar 26, 2018'

  AND

  CAST (POS.BUSINESSDT as Date) <= 'Apr 01, 2018'

    ORDER BY ORGREPORT.ORGNM, POS.BUSINESSDT;

在关键字“ WHERE”附近,我的语法一直不正确。

我不知道为什么这不起作用。我无法删除where子句进行测试,因为它将返回数百万个结果。

有什么主意我做错了吗?

1 个答案:

答案 0 :(得分:1)

PIVOT的语法是:

SELECT <non-pivoted column>,  
    [first pivoted column] AS <column name>,  
    [second pivoted column] AS <column name>,  
    ...  
    [last pivoted column] AS <column name>  
FROM  
    (<SELECT query that produces the data>)   
    AS <alias for the source query>  
PIVOT  
(  
    <aggregation function>(<column being aggregated>)  
FOR   
[<column that contains the values that will become column headers>]   
    IN ( [first pivoted column], [second pivoted column],  
    ... [last pivoted column])  
) AS <alias for the pivot table>  
<optional ORDER BY clause>; 

如您所见,在WHERE / PIVOT子句的前面或后面没有UNPIVOT子句。

要解决此问题,请在嵌套查询中移动WHERE逻辑:

USE tkcsdb

SELECT  ORGREPORT.ORGNM
  ,CONVERT (DATE, POS.BUSINESSDT) As 'Date'
  ,start_time
  ,footfall_count


  FROM 
  (
    SELECT * 
    FROM POSVOLUMEHIST 
    WHERE CAST (BUSINESSDT as Date) >= 'Mar 26, 2018'
      AND CAST (BUSINESSDT as Date) <= 'Apr 01, 2018'
  ) POS

  JOIN FCCATEGORY CAT
  ON POS.EXTVOLUMELABELID = CAT.EXTVOLUMELABELID

  JOIN ORG ORG
  ON CAT.ORGIDSID = ORG.ORGIDSID

  JOIN ORG ORGREPORT
  ON ORG.REPORTTOORGID = ORGREPORT.ORGIDSID

UNPIVOT (footfall_count for start_time in

(      POS.H08I1
  ,POS.H08I2
  ,POS.H08I3
  ,POS.H08I4
  ,POS.H09I1
  ,POS.H09I2
  ,POS.H09I3
  ,POS.H09I4
  ,POS.H10I1
  ,POS.H10I2
  ,POS.H10I3
  ,POS.H10I4
  ,POS.H11I1
  ,POS.H11I2
  ,POS.H11I3
  ,POS.H11I4
  ,POS.H12I1
  ,POS.H12I2
  ,POS.H12I3
  ,POS.H12I4
  ,POS.H13I1
  ,POS.H13I2
  ,POS.H13I3
  ,POS.H13I4
  ,POS.H14I1
  ,POS.H14I2
  ,POS.H14I3
  ,POS.H14I4
  ,POS.H15I1
  ,POS.H15I2
  ,POS.H15I3
  ,POS.H15I4
  ,POS.H16I1
  ,POS.H16I2
  ,POS.H16I3
  ,POS.H16I4
  ,POS.H17I1
  ,POS.H17I2
  ,POS.H17I3
  ,POS.H17I4
  ,POS.H18I1
  ,POS.H18I2
  ,POS.H18I3
  ,POS.H18I4
  ,POS.H19I1
  ,POS.H19I2
  ,POS.H19I3
  ,POS.H19I4
  ,POS.H20I1
  ,POS.H20I2
  ,POS.H20I3
  ,POS.H20I4
  ,POS.H21I1
  ,POS.H21I2
  ,POS.H21I3
  ,POS.H21I4
  ,POS.H22I1
)) 
    ORDER BY ORGREPORT.ORGNM, POS.BUSINESSDT;