SQL在Group by时获取UID

时间:2010-04-08 06:40:10

标签: sql sql-server tsql

我从表中选择

[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?它到处都是一样的,但是当我尝试使用返回值获取SO_UID时出现错误...

SO_UID不一定像'7C7035C8-56DD-4A44-93CC-F16FD66280A3'这样,所以我不能手动将它作为字符串添加。

2 个答案:

答案 0 :(得分:2)

您需要使用aggregate function来获取其他列数据。由于此列始终具有相同的值(感谢WHERE子句),因此您可以使用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 

答案 1 :(得分:0)

正确答案很简单,问题就是愚蠢:

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