Hi All,
Thank you for your replies so far.
I do apologize again for the vagueness.
I have below a trimmed down version of my script, which can be run to better illustrate my question:
CREATE TABLE #Debtor (DebtorID char(30), DebtorName char(30))
INSERT INTO #Debtor VALUES ('A1', 'A1 Debtor')
CREATE TABLE #Address (DebtorID char(30), Address char(50), Country char(10))
INSERT INTO #Address VALUES ('A1', 'some address', 'US')
CREATE TABLE #Contact (DebtorID char(30), Contactname char (20))
INSERT INTO #Contact VALUES ('A1', 'First Last')
CREATE TABLE #NewTrx (DebtorID char(30), Type int, Amount numeric(10,2))
INSERT INTO #NewTrx VALUES ('A1', 1, 10)
INSERT INTO #NewTrx VALUES ('A1', 1, 20)
INSERT INTO #NewTrx VALUES ('A1', 2, 30)
CREATE TABLE #OldTrx (DebtorID char(30), Type int, Amount numeric(10,2))
INSERT INTO #OldTrx VALUES ('A1', 1, 40)
INSERT INTO #OldTrx VALUES ('A1', 2, 50)
INSERT INTO #OldTrx VALUES ('A1', 2, 60)
SELECT XmlCol.query(' { /* } ')
FROM
(
SELECT
Rtrim(DebtorID) DebtorID, Rtrim(DebtorName) DebtorName,
(
select
Rtrim(DebtorID) DebtorID,
Rtrim([Address]) [Address],
Rtrim(Country) Country
from #Address
where [Debtor].DebtorID = #Address.DebtorID
for xml auto, type
),
(
select
Rtrim(DebtorID) DebtorID,
Rtrim(Contactname) [Contactname]
from #Contact
where [Debtor].DebtorID = #Contact.DebtorID
for xml auto, type
),
(
select
Rtrim(DebtorID) DebtorID,
case [Type]
when 1 then 'Invoice'
when 2 then 'Payment'
end as [DocType],
Amount
from #NewTrx
where [Debtor].DebtorID = #NewTrx.DebtorID
for xml auto, type
),
(
select
Rtrim(DebtorID) DebtorID,
case [Type]
when 1 then 'Invoice'
when 2 then 'Payment'
end as [DocType],
Amount
from #OldTrx
where [Debtor].DebtorID = #OldTrx.DebtorID
for xml auto, type
)
FROM #Debtor [Debtor]
FOR XML AUTO,TYPE
) as T(XmlCol)
Drop Table #Debtor
Drop Table #Address
Drop Table #Contact
Drop Table #NewTrx
Drop Table #OldTrx
The resulting XML file looks like:
FileSummary
Debtor DebtorID="A1" DebtorName="A1 Debtor"
_x0023_Address DebtorID="A1" Address="some address" Country="US" /
_x0023_Contact DebtorID="A1" Contactname="First Last" /
_x0023_NewTrx DebtorID="A1" DocType="Invoice" Amount="10.00" /
_x0023_NewTrx DebtorID="A1" DocType="Invoice" Amount="20.00" /
_x0023_NewTrx DebtorID="A1" DocType="Payment" Amount="30.00" /
_x0023_OldTrx DebtorID="A1" DocType="Invoice" Amount="40.00" /
_x0023_OldTrx DebtorID="A1" DocType="Payment" Amount="50.00" /
_x0023_OldTrx DebtorID="A1" DocType="Payment" Amount="60.00" /
/Debtor
/FileSummary
At this stage, I need assistance to populate the FileSummary node above with a summation for invoices and payments.
I hope that makes sense?
Any assistance would be appreciated.
Thank you