我有以下情况(比如tblRecord)
ID RowNum Data
1 1 and seventy nine
1 2 five hundred
1 3 two thousand
Run Code Online (Sandbox Code Playgroud)
我需要输出
ID Data
1 two thousand five hundred and seventy nine
Run Code Online (Sandbox Code Playgroud)
我有以下查询
select ID , Data =
( Select ' ' + cast(Data as varchar(max)) from tblRecord t2
where t2.RowNum= t1.RowNum
and t2.ID =t1.ID
order by t1.RowNum
for xml path(''))
from tblRecord t1
group by t1.ID
Run Code Online (Sandbox Code Playgroud)
但输出是
ID Data
1 five hundred two thousand and seventy nine
Run Code Online (Sandbox Code Playgroud)
需要帮助.
谢谢
mar*_*c_s 14
试试这个:
SELECT DISTINCT
ID, Data = (SELECT ' ' + Data
FROM dbo.tblRecord t2
WHERE t2.ID = t1.ID
ORDER BY t2.RowNum DESC
FOR XML PATH('')
)
FROM dbo.tblRecrd t1
Run Code Online (Sandbox Code Playgroud)
你的第一个问题是ORDER BY t1.RowNum内部选择 - 需要ORDER BY t2.RowNum改为.其次:这种连接条件where t2.RowNum= t1.RowNum不是必需的,会导致问题.第三:GROUP BY既不需要也不帮助 - 只需使用它SELECT DISTINCT来实现你所寻找的东西.
另外 - 不知道为什么你要Data作为VARCHAR(MAX)投射???