我无法按时间顺序订购FOR XML路径中的季度和年份。虽然我订购但它不起作用。可以让我知道我哪里出错了
SELECT DISTINCT
LON.DATE
,STUFF((SELECT DISTINCT ',' + 'Q' + (COALESCE(CAST(LON1.QTR AS Varchar(10))+ '/ ', '') + + CAST(LON1.YR AS Varchar(10)))
FROM LOANTABLE LON1
WHERE LON.DATE = LON1.DATE
FOR XML PATH('')),1,1,'') AS QuarterAndYear
FROM
LOANTABLE LON
GROUP BY
LON.DATE
ORDER BY
QuarterAndYear
这是结果数据:
Date QuarterAndYear
2011-04-01 Q1/ 2008,Q3/ 2009,Q1/ 2010,Q2/ 2009,Q4/ 2009,Q2/ 2008,Q2/ 2010,Q3/ 2010,Q3/ 2008,Q4/ 2008,Q4/ 2010,Q1/ 2011,Q2/ 2011,Q1/
2009 2010-12-01 Q1/ 2010,Q2/ 2010,Q3/ 2010,Q4/
2010 2011-07-01 Q1/ 2010,Q3/ 2011,Q2/ 2010,Q3/ 2010,Q4/ 2010,Q1/ 2011,Q2/
2011 2012-01-09 Q1/ 2010,Q3/ 2011,Q2/ 2010,Q3/ 2010,Q4/ 2011,Q4/ 2010,Q1/ 2011,Q2/
2011 2011-07-08 Q1/ 2010,Q4/ 2009,Q2/ 2010,Q3/ 2010,Q4/ 2010,Q1/ 2011 –
答案 0 :(得分:0)
使用以下格式:
SELECT DISTINCT LON.DATE
,STUFF((SELECT DISTINCT ',' + 'Q' + (COALESCE(CAST(z.QTR AS Varchar(10))+ '/ ', '') + + CAST(z.YR AS Varchar(10)))
FROM (SELECT TOP 100 PERCENT QTR, YR
FROM LOANTABLE LON1
WHERE LON.DATE = LON1.DATE
ORDER BY LON1.QTR DESC
)z
FOR XML PATH('')),1,1,'') AS QuarterAndYear
FROM LOANTABLE LON
GROUP BY LON.DATE
ORDER BY QuarterAndYear