1

XML PATH がどのように機能するかを理解することは言うまでもなく、XML PATH をコードに追加していくつかの情報を連結する方法を見つけようとして、ひどい時間を過ごしています。過去 2 日間の大部分をこの作業に費やしてきました。助けていただければ幸いです!!

ここに私が取り組んでいるコードがあります:

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                    |

誰かが私を助けてくれれば、とても感謝しています。

前もって感謝します!!

ジェイミー S

4

3 に答える 3

3

XML PATH の仕組み

このセットアップを使用してそれを説明しようとします:

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
于 2012-08-29T07:47:07.897 に答える
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 フィドル

于 2012-08-30T05:28:20.663 に答える
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

これにより、次のように出力されます (UPPER は、労働の説明を強調するためのものです)。

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

うまくいけば、私はあなたの質問を理解し、これがうまくいくでしょう.

于 2012-08-29T03:38:31.463 に答える