SQL Get UID when Group by
I do a select from table
[V_RPT_BelegungKostenstelleDetail]
WHERE SO_UID = '7C7035C8-56DD-4A44-93CC-F16FD66280A3'
AND GB_UID = '4FF1B0EE-A5DD-4699-94B7-760922666CE2'
AND GS_UID = '1188759A-54E1-开发者_JAVA技巧4323-8BF2-85E71B3C796E'
AND RM_UID = '088C3559-6E6E-468A-9554-6740840FCBA1'
AND NA_UID= '96A2A8DB-8C83-4C60-9060-F0F55719AF5C'
GROUP BY KST_UID
How can I get SO_UID? It is the same everywhere, but I get an error when I try to get SO_UID with the return values...
SO_UID is not necessarely given like '7C7035C8-56DD-4A44-93CC-F16FD66280A3' here, so I can't just add it manually as string.
You need to use an aggregate function to get other column data. As this column will always have the same value (thanks to the WHERE
clause), you can use MAX()
:
SELECT KST_UID, MAX(SO_UID)
FROM [V_RPT_BelegungKostenstelleDetail]
WHERE SO_UID = '7C7035C8-56DD-4A44-93CC-F16FD66280A3'
AND GB_UID = '4FF1B0EE-A5DD-4699-94B7-760922666CE2'
AND GS_UID = '1188759A-54E1-4323-8BF2-85E71B3C796E'
AND RM_UID = '088C3559-6E6E-468A-9554-6740840FCBA1'
AND NA_UID= '96A2A8DB-8C83-4C60-9060-F0F55719AF5C'
GROUP BY KST_UID
The correct answer is as simple as the question is stupid:
SELECT KST_UID, SO_UID, GB_UID, RM_UID, NA_UID
FROM [V_RPT_BelegungKostenstelleDetail]
WHERE SO_UID = '7C7035C8-56DD-4A44-93CC-F16FD66280A3'
AND GB_UID = '4FF1B0EE-A5DD-4699-94B7-760922666CE2'
AND GS_UID = '1188759A-54E1-4323-8BF2-85E71B3C796E'
AND RM_UID = '088C3559-6E6E-468A-9554-6740840FCBA1'
AND NA_UID = '96A2A8DB-8C83-4C60-9060-F0F55719AF5C'
GROUP BY KST_UID, SO_UID, GB_UID, RM_UID, NA_UID
精彩评论