2013-02-12 87 views
0

使用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 
+0

這裏是當前的代碼 – Makdaddy 2013-02-12 05:08:00

+0

_where_是當前的代碼? – 2013-02-12 05:08:57

+0

我編輯過你的標題。請參見「[應的問題包括‘標籤’,在他們的頭銜?(http://meta.stackexchange.com/questions/19190/)」,這裏的共識是「不,他們不應該」。 – 2013-02-12 05:09:30

回答

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] 
+0

您好JW我的數量不在同一個字段中,我有3個字段。 ItemID,DeliveryDate和RemainPurchPhysical – Makdaddy 2013-02-12 05:28:12