在数据透视表

时间:2017-02-14 13:31:16

标签: sql sql-server tsql

我遇到USER_KEY多个结果的问题。 我必须按用户总结部门的时间。因此,每个用户在某个部门都有一定的工作时间。

除了用户行重复之外,一切都很酷。我需要对查询执行group by,但没有成功。

以下是查询:

DECLARE @DATEFROM DATETIME = DATEADD(DAY, -14, GETDATE())
DECLARE @DATETO DATETIME = DATEADD(DAY, -12, GETDATE())

DECLARE @COLDEPARTMENTS NVARCHAR(MAX)
SELECT @COLDEPARTMENTS = STUFF((SELECT DISTINCT ',' + QUOTENAME(DEPA_KEY, '[')  FROM CADEPA FOR XML PATH('')), 1, 1, '')
--SELECT @COLDEPARTMENTS

DECLARE @QUERY AS NVARCHAR(MAX)

DECLARE @USERS TABLE
(
  USER_KEY INT,
  USDE_HSU DECIMAL(8,2)
)

DECLARE @USERS_STR NVARCHAR(MAX)

INSERT INTO @USERS (USER_KEY, USDE_HSU)  
SELECT USERS_.USER_KEY, SUMMARY FROM (
SELECT DISTINCT USER_KEY, SUM(USDE_HSU) SUMMARY
FROM CAUSDE_TAS
WHERE USDE_DAT >= @DATEFROM AND USDE_DAT <= @DATETO
GROUP BY USER_KEY
HAVING SUM(USDE_HSU) IS NOT NULL AND SUM(USDE_HSU) > 0) USERS_

SELECT @USERS_STR = STUFF((SELECT DISTINCT ',' + CAST(USER_KEY AS NVARCHAR(9))  FROM @USERS FOR XML PATH('')), 1, 1, '')

SELECT @QUERY = 'SELECT DISTINCT USER_KEY, ' + @COLDEPARTMENTS + '
FROM CAUSDE_TAS 
PIVOT
(
SUM(USDE_HSU)
FOR DEPA_KEY IN (' + @COLDEPARTMENTS + ')
) PIVOT_LOCATIONS
WHERE USDE_DAT >= ''' + format(@DATEFROM, 'MM.dd.yyyy') + ''' AND USDE_DAT <= ''' + format(@DATETO, 'MM.dd.yyyy') + ''' 
AND USER_KEY IN (' + @USERS_STR + ')'

EXECUTE (@QUERY)

问题出在查询的最后一部分:

SELECT @QUERY = 'SELECT DISTINCT USER_KEY, ' + @COLDEPARTMENTS + '
FROM CAUSDE_TAS 
PIVOT
(
SUM(USDE_HSU)
FOR DEPA_KEY IN (' + @COLDEPARTMENTS + ')
) PIVOT_LOCATIONS
WHERE USDE_DAT >= ''' + format(@DATEFROM, 'MM.dd.yyyy') + ''' AND USDE_DAT <= ''' + format(@DATETO, 'MM.dd.yyyy') + ''' 
AND USER_KEY IN (' + @USERS_STR + ')'

我不知道如何在这里制作group by

结果如下:

enter image description here

我尝试过在这里添加GROUP BY

SELECT @QUERY = 'SELECT DISTINCT USER_KEY, ' + @COLDEPARTMENTS + '
FROM CAUSDE_TAS 
PIVOT
(
SUM(USDE_HSU)
FOR DEPA_KEY IN (' + @COLDEPARTMENTS + ')
) PIVOT_LOCATIONS
WHERE USDE_DAT >= ''' + format(@DATEFROM, 'MM.dd.yyyy') + ''' AND USDE_DAT <= ''' + format(@DATETO, 'MM.dd.yyyy') + ''' 
AND USER_KEY IN (' + @USERS_STR + ')
GROUP BY USER_KEY'

但错误是:

  

列'PIVOT_LOCATIONS.110000003'在选择列表中无效   因为它不包含在聚合函数或   GROUP BY子句。

更新 我最后添加了group by user_key, ' + @COLDEPARTMENTS,但USER_KEY仍然是重复的。 (没有错误,但结果不好)

以下是在最后添加group by后查询的查看方式:

SELECT DISTINCT USER_KEY, [110000003],[120000002],[120000003],[120000004],[120000005],[120000006],[120000007],[120000008],[120000009],[120000010],[120000011],[120000012],[120000013],[120000015],[120000016],[120000017],[120000021],[120000022],[120000023],[120000025],[120000026],[120000027],[120000028],[120000029],[120000030],[120000039],[120000040],[120000042],[120000043],[120000044],[120000045],[120000046],[120000047],[120000048],[120000049],[120000050],[120000051],[130000001],[130000002],[130000003],[130000004],[130000005],[130000006],[130000007],[140000001],[140000002],[140000003],[140000004],[140000005],[140000006],[140000007],[140000008],[140000009],[140000010],[140000011],[140000012],[140000013],[140000014],[140000015],[140000016],[140000017],[140000018],[150000001],[150000002],[150000003],[150000004],[150000005],[150000006],[150000007],[150000008],[150000009],[150000010],[150000011],[150000012],[160000001],[160000002],[160000003],[160000004],[160000005]
FROM CAUSDE_TAS 
PIVOT
(
SUM(USDE_HSU)
FOR DEPA_KEY IN ([110000003],[120000002],[120000003],[120000004],[120000005],[120000006],[120000007],[120000008],[120000009],[120000010],[120000011],[120000012],[120000013],[120000015],[120000016],[120000017],[120000021],[120000022],[120000023],[120000025],[120000026],[120000027],[120000028],[120000029],[120000030],[120000039],[120000040],[120000042],[120000043],[120000044],[120000045],[120000046],[120000047],[120000048],[120000049],[120000050],[120000051],[130000001],[130000002],[130000003],[130000004],[130000005],[130000006],[130000007],[140000001],[140000002],[140000003],[140000004],[140000005],[140000006],[140000007],[140000008],[140000009],[140000010],[140000011],[140000012],[140000013],[140000014],[140000015],[140000016],[140000017],[140000018],[150000001],[150000002],[150000003],[150000004],[150000005],[150000006],[150000007],[150000008],[150000009],[150000010],[150000011],[150000012],[160000001],[160000002],[160000003],[160000004],[160000005])
) PIVOT_LOCATIONS
WHERE USDE_DAT >= '01.31.2017' AND USDE_DAT <= '02.02.2017' 
AND USER_KEY IN (100000002,100000008,100000013,100000014,100000042,100000043,100000052,100000066,110000025,110000030,110000057,120000030,120000033,120000037,120000039,120000052,120000064,130000007,130000017,130000021,130000033,130000041,130000069,130000073,130000096,130000109,130000115,140000031,140000054,140000066,140000073,140000074,150000018,150000019,150000023,150000024,150000045,150000067,150000072,150000095,150000101,150000102,150000115,150000205,150000215,150000281,160000012,160000057,160000058,160000071,160000078,160000107,160000109,160000145,160000146,160000151,160000181,160000182,160000192,160000204,160000220,170000001,170000006,170000008)
group by user_key, [110000003],[120000002],[120000003],[120000004],[120000005],[120000006],[120000007],[120000008],[120000009],[120000010],[120000011],[120000012],[120000013],[120000015],[120000016],[120000017],[120000021],[120000022],[120000023],[120000025],[120000026],[120000027],[120000028],[120000029],[120000030],[120000039],[120000040],[120000042],[120000043],[120000044],[120000045],[120000046],[120000047],[120000048],[120000049],[120000050],[120000051],[130000001],[130000002],[130000003],[130000004],[130000005],[130000006],[130000007],[140000001],[140000002],[140000003],[140000004],[140000005],[140000006],[140000007],[140000008],[140000009],[140000010],[140000011],[140000012],[140000013],[140000014],[140000015],[140000016],[140000017],[140000018],[150000001],[150000002],[150000003],[150000004],[150000005],[150000006],[150000007],[150000008],[150000009],[150000010],[150000011],[150000012],[160000001],[160000002],[160000003],[160000004],[160000005]

1 个答案:

答案 0 :(得分:1)

不是将每个列添加到组中,而是仅按您不希望重复的列进行分组。聚合其余部分。像这样:

SELECT USER_KEY, SUM(col1), SUM(col2), ... SUM(colN)
FROM {all the stuff in the middle}
GROUP BY USER_KEY