Reputation: 7197
I have a problem with multiple results by USER_KEY. I must summarize hours from departments by user. So, each user has made certain amount of hours in certain department.
Everything is cool, except for users rows are repeating. I need to do group by
on the query, but with no success.
Here is the query:
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)
Problem is with last part of the 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 + ')'
I don't know how to make group by
here?
Results are like this:
What I've tried is to add GROUP BY
here:
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'
But the error is:
Column 'PIVOT_LOCATIONS.110000003' is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause.
Update:
I've added group by user_key, ' + @COLDEPARTMENTS
at the end, but USER_KEY
is still duplicating. (no error, but result is not good)
Here is how query looks after adding group by
at the end:
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]
Upvotes: 0
Views: 164
Reputation: 31785
Instead of adding every column to the group by, only group by the ones you don't want duplicates in. Aggregate the rest. Something like this:
SELECT USER_KEY, SUM(col1), SUM(col2), ... SUM(colN)
FROM {all the stuff in the middle}
GROUP BY USER_KEY
Upvotes: 1