如何修改此查询以返回D列

时间:2010-11-19 18:57:39

标签: sql

SELECT   ColA, MAX(CONVERT(varchar(15),ColB) + ' ' + CONVERT(varchar(15),ColC))
FROM     dbo.TableX
GROUPBY  ColA

2 个答案:

答案 0 :(得分:3)

SELECT 
    ColA, MAX(CONVERT(varchar(15),ColB) + ' ' + CONVERT(varchar(15),ColC)), 
    ColD 
FROM 
    dbo.TableX 
GROUP BY 
    ColA, ColD

答案 1 :(得分:0)

RE:您的评论“这会导致返回具有非唯一ColA的行。”

关注your previous question我觉得你需要像

这样的东西
;WITH X AS
(
SELECT   ColA, 
         ColD,
         RANK() OVER (PARTITION BY ColA ORDER BY CONVERT(varchar(15),ColB) + ' ' + CONVERT(varchar(15),ColC) DESC) Rnk
FROM     dbo.TableX
)
SELECT ColA, ColD, Rnk
FROM X
WHERE Rnk = 1;