2012-08-29 23 views
1

我有一個可怕的時間試圖找出如何將XML路徑添加到我的代碼來連接的一些信息,更別說瞭解XML路徑是如何工作的。我花了過去兩天的更好的一部分工作,並會感謝一些幫助!無法獲取FOR XML PATH工作

這裏是我的工作代碼:

Select Top 100 Percent Agreements.AgrmntID, Agreements.Description As 
    AgrmntDesc, Agreements.Status, AgreementSchedules.SchedDate, DateName(dw, 
    AgreementSchedules.SchedDate), LaborCodeTypes.Description As LaborCode, 
    Customers.CustName, Customers.CompanyName, JobSites.SiteName, 
    AgreementSchedules.AgrmntSchedID 
From Agreements Inner Join 
    AgreementTypes On Agreements.AgrmntTypeID = AgreementTypes.AgrmntTypeID 
    Inner Join 
    AgreementSchedules On Agreements.AgrmntID = AgreementSchedules.AgrmntID 
    Inner Join 
    Customers On Agreements.CustID = Customers.CustID Inner Join 
    JobSites On Agreements.CustSiteID = JobSites.CustSiteID Left Outer Join 
    LaborCodeTypes On AgreementSchedules.RepairID = LaborCodeTypes.RepairID 
Where Agreements.Status = 2 And Month(AgreementSchedules.SchedDate) = 
    Month(GetDate()) 

樣本數據:

| AgreementID | LaborCodeTypes.Description | DateName(dw, AgreementSchedules.SchedDate)| 
| - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -| 
| 1   | Landscaping     | Tuesday         | 
| 1   | Landscaping     | Friday         | 
| 1   | Sweeping      | Monday         | 
| 1   | Sweeping      | Wednesday         | 
| 1   | Sweeping      | Friday         | 
| 2   | Landscaping     | Monday         | 

預期輸出:

| AgreementID | LaborCode | Days Of Week    | 
| - - - - - - - - - - - - - - - - - - - - - - - - - - - | 
| 1   | Landscaping | Tuesday, Friday   | 
| 1   | Sweeping | Monday, Wednesday, Friday | 
| 2   | Landscaping | Monday     | 

我將不勝,大大如果有人能夠欣賞它幫我。

預先感謝您!

傑米小號

回答

2
;with C as 
(
    select A.AgreementID, 
     LCT.Description as LaborCode, 
     Ags.ShedDate 
    from Agreements as A 
    inner join AgreementSchedules as AgS 
     on A.AgreementID = AgS.AgreementID 
    inner join LaborCodeTypes as LCT 
     on AgS.RepairID = LCT.RepairID 
    where A.[Status] = 2 and 
     AgS.ShedDate >= dateadd(month, datediff(month, 0, getdate()), 0) and 
     AgS.ShedDate < dateadd(month, 1+datediff(month, 0, getdate()), 0) 
) 
select C1.AgreementID, 
     C1.LaborCode, 
     stuff((select ', '+datename(weekday, C2.ShedDate) 
       from C as C2 
       where C1.AgreementID = C2.AgreementID and 
        C1.LaborCode = C2.LaborCode 
       order by C2.ShedDate 
       for xml path(''), type).value('.', 'varchar(max)'), 1, 2, '') as [Days Of Week] 
from C as C1 
group by C1.AgreementID, C1.LaborCode; 

SQL Fiddle

+0

非常非常感謝你......我在另一個程序(例如:它不是我的數據庫)中編碼,這段代碼仍然無法正常工作......我將它發送給技術支持,他們說它應該工作但不是。啊。無論如何,感謝您抽出時間! –

+0

嘗試在'with'之前先添加';'。 –

0

我相信我有一個不同的方式來實現你正在尋找...

SELECT * FROM (
    SELECT DISTINCT AgreementId, UPPER(Description), 1 AS IsLabor 
    FROM Table 
    WHERE ... 
    UNION 
    SELECT AgreementId, DateName AS Description, 0 AS IsLabor 
    FROM Table 
    WHERE ... 
) x 
ORDER BY AgreementId, IsLabor DESC, Description 

這應該輸出以下的結果(上只是強調勞動的說明):

| AgreementId | Description | IsLabor | 
--------------------------------------- 
| 1   | LANDSCAPING | 1  | 
| 1   | Friday  | 0  | 
| 1   | Tuesday  | 0  | 
| 2   | SWEEPING | 1  | 
| 2   | Friday  | 0  | 
| 2   | Monday  | 0  | 
| 2   | Wednesday | 0  | 

希望我明白你的問題,這將工作。

+0

謝謝!!除了我更多的尋找(並意識到我應該更好地說明它...現在不能因爲我在評論,不知道如何)| AgreementID |勞動代碼|服務日|將多個服務日連接在一起,以便掃地的人可以快速查看。 –

3

XML路徑是如何工作的

我會盡量解釋,使用此設置:

create table Grp 
(
    GrpID int primary key, 
    Name varchar(10) 
) 

create table Item 
(
    ItemID int identity primary key, 
    Name varchar(10), 
    GrpID int references Grp(GrpID) 
) 

insert into Grp values 
(1, 'G1'), 
(2, 'G2') 

insert into Item values 
('A', 1), 
('B', 1), 
('C', 1), 
('D', 2), 
('E', 2) 

的目標是創建名稱爲每組一個逗號分隔的列表結果。

GroupName ItemNames 
---------- ---------- 
G1   A,B,C 
G2   D,E 

FOR XML用於打開一個查詢結果轉換成XML文檔或XML片段。

這個查詢將創建一個XML片段。

select I.Name 
from Item as I 
for xml path(''), type 

結果:

<Name>A</Name> 
<Name>B</Name> 
<Name>C</Name> 
<Name>D</Name> 
<Name>E</Name> 

上述查詢可以以相互關聯的子查詢可以用來爲每個組創建這樣的XML片段。

select G.Name as GroupName, 
     (
     select I.Name 
     from Item as I 
     where G.GrpID = I.GrpID 
     for xml path(''), type 
     ) as ItemNames 
from Grp as G 

結果:

GroupName ItemNames 
---------- -------------------------------------------- 
G1   <Name>A</Name><Name>B</Name><Name>C</Name> 
G2   <Name>D</Name><Name>E</Name> 

然後,您可以使用value()功能在XML中提取值。

select Name as GroupName, 
     (
     select I.Name 
     from Item as I 
     where G.GrpID = I.GrpID 
     for xml path(''), type 
     ).value('.', 'varchar(max)') as ItemNames 
from Grp as G 

結果:

GroupName ItemNames 
---------- ---------- 
G1   ABC 
G2   DE 

要完成這一點,你需要添加逗號作爲分隔符,並且可以通過子查詢select ','+I.Name加入逗號到每一個項目的名字來完成。它會在第一個值之前留下額外的逗號。您可以使用STUFF函數刪除那個STUFF(Value, 1, 1, '')

最終查詢:

select Name as GroupName, 
     stuff((
     select ','+I.Name 
     from Item as I 
     where G.GrpID = I.GrpID 
     for xml path(''), type 
     ).value('.', 'varchar(max)'), 1, 1, '') as ItemNames 
from Grp as G 
+0

感謝您花時間回覆!我仍然無法得到它......我不斷收到「無效的SELECT語句。意外的標記」爲「...」錯誤消息,無論我嘗試什麼。我檢查了版本類型,它是2005年。我不明白的是「I.Name」和「I.GrpID」之前的「I」正如你在上面看到的,我的字段名已經有了。 ,那可以把它扔掉嗎? –

+0

查詢中的'I'和'G'是表別名,用於限定字段名。您可以使用別名而不是表名。如果您使用表格結構,樣本數據和使用樣本數據的預期輸出來更新您的問題,我可以幫助您處理實際查詢。 –

+0

我相信我已經包括了你要求的@Mikael。感謝您花時間回覆我的評論 - 我通常很擅長計算代碼,但我無法得到它! –