水平显示结果

时间:2013-02-12 05:07:44

标签: sql-server for-xml

使用SQL Server 2005

我希望水平显示itemid, deliverydate, remainpurchphysical

目前我正在使用FOR XML PATH,这有效。

当结果是每个itemid的多个交付日期时,我希望它们显示得更好

  [ITEMID]  [DELIVERYDATE AND REMAINPURCH]
  -----------------------------------------
  [1234]    [14/14/2014 25 | 15/15/2015 27 | 16/16/2016 28]

最好用|每个日期之间

这是当前的代码

-select itemid, CONVERT(NVARCHAR, deliverydate, 103),remainpurchphysical
--from purchline where purchstatus =1 --and itemid = '00727-s'
SELECT 
    itemid 
    ,deliverydates = STUFF(
                       (SELECT '   ' + CONVERT(NVARCHAR, deliverydate, 103)
                        FROM purchline b 
                        WHERE b.itemid = a.itemid AND purchstatus = 1 
                        ORDER BY deliverydate 
                        FOR XML PATH('')), 1, 1, '')
    ,remainpurchphysical = STUFF(
                              (SELECT '   ' + CAST(CONVERT(Decimal(9,0), remainpurchphysical) AS VARCHAR)
                               FROM purchline b 
                               WHERE b.itemid = a.itemid AND purchstatus = 1  
                               ORDER BY deliverydate
                               FOR XML PATH('')), 1, 1, '')
FROM purchline a
WHERE purchstatus =1
GROUP BY itemid
ORDER BY itemid

1 个答案:

答案 0 :(得分:0)

这样的事,

SELECT
     [ITEMID],
     STUFF(
         (SELECT ' | ' + [date]
          FROM Table1
          WHERE [ITEMID] = a.[ITEMID]
          FOR XML PATH (''))
          , 1, 1, '')  AS DateList
FROM Table1 AS a
GROUP BY [ITEMID]