同じ BT_ID を持つすべてのデータを 1 行に表示する必要があるが、別の UF フィールドの下にあるステートメント クエリに取り組んでいます。たとえば、同じ BT_ID を持つ複数の注文があり、それぞれの Invoice_reference 番号を独自の UF フィールドに表示する必要があります。ただし、すべてのデータを UF フィールドに渡すわけではありません。空白であってはならない多くのフィールドは空白です。どんな助けでも感謝します, ありがとう:
DECLARE
@OrgCode varchar(5), @FromDate varchar(15)
Set @OrgCode = 'iho' Set @FromDate = '08/23/2013'
SELECT Invoice.BT_ID,
Name.COMPANY,
Name.COMPANY_RECORD,
Name.COMPANY_SORT,
Name.LAST_FIRST,
Name.FULL_NAME,
Name_Address.PURPOSE,
Name.TITLE,
Name_Address.FULL_ADDRESS,
Invoice.INSTALL_BILL_DATE,
Org_Control.OrgName,
Org_Control.InvoiceAddress,
CONVERT(VARCHAR, Invoice.INVOICE_DATE, 101)AS INVOICE_DATE,
CONVERT(VARCHAR, Trans.TRANSACTION_DATE, 101)AS TRANSACTION_DATE,
Trans.POSTED,
Invoice.CUSTOMER_REFERENCE,
Invoice.INVOICE_TYPE,
Org_Control.OrgCode,
Name.EMAIL,
MAX(CASE WHEN rk = 1 THEN '</td>'+ cast(Invoice.REFERENCE_NUM as varchar(10)) + '</td>' +
cast(Invoice.DESCRIPTION as varchar(50)) + '</td>' + '$' +
cast(Trans.INVOICE_CHARGES as varchar) +'</td>' else '' END) as UF6,
MAX(CASE WHEN rk = 2 THEN '<td>' + cast(Invoice.REFERENCE_NUM as varchar(10)) + '</td>' +
cast(Invoice.DESCRIPTION as varchar(50)) + '</td>' + '$' +
cast(Trans.INVOICE_CHARGES as varchar) +'</td>' else '' END) as UF7,
MAX(CASE WHEN rk = 3 THEN '<td>' + cast(Invoice.REFERENCE_NUM as varchar(10)) + '</td>' +
cast(Invoice.DESCRIPTION as varchar(50)) + '</td>' + '$' +
cast(Trans.INVOICE_CHARGES as varchar) +'</td>' else '' END) as UF8,
MAX(CASE WHEN rk = 4 THEN '<td>' + cast(Invoice.REFERENCE_NUM as varchar(10)) + '</td>' +
cast(Invoice.DESCRIPTION as varchar(50)) + '</td>' + '$' +
cast(Trans.INVOICE_CHARGES as varchar) +'</td>' else '' END) as UF9,
MAX(CASE WHEN rk = 5 THEN '<td>' + cast(Invoice.REFERENCE_NUM as varchar(10)) + '</td>' +
cast(Invoice.DESCRIPTION as varchar(50)) + '</td>' + '$' +
cast(Trans.INVOICE_CHARGES as varchar) +'</td>' else '' END) as UF10,
MAX(CASE WHEN rk = 6 THEN '<td>' + cast(Invoice.REFERENCE_NUM as varchar(10)) + '</td>' +
cast(Invoice.DESCRIPTION as varchar(50)) + '</td>' + '$' +
cast(Trans.INVOICE_CHARGES as varchar) +'</td>' else '' END) as UF11,
MAX(CASE WHEN rk = 7 THEN '<td>' + cast(Invoice.REFERENCE_NUM as varchar(10)) + '</td>' +
cast(Invoice.DESCRIPTION as varchar(50)) + '</td>' + '$' +
cast(Trans.INVOICE_CHARGES as varchar) +'</td>' else '' END) as UF12,
MAX(CASE WHEN rk = 8 THEN '<td>' + cast(Invoice.REFERENCE_NUM as varchar(10)) + '</td>' +
cast(Invoice.DESCRIPTION as varchar(50)) + '</td>' + '$' +
cast(Trans.INVOICE_CHARGES as varchar) +'</td>' else '' END) as UF13,
MAX(CASE WHEN rk = 9 THEN '<td>' + cast(Invoice.REFERENCE_NUM as varchar(10)) + '</td>' +
cast(Invoice.DESCRIPTION as varchar(50)) + '</td>' + '$' +
cast(Trans.INVOICE_CHARGES as varchar) +'</td>' else '' END) as UF14,
MAX(CASE WHEN rk = 10 THEN '<td>' + cast(Invoice.REFERENCE_NUM as varchar(10)) + '</td>' +
cast(Invoice.DESCRIPTION as varchar(50)) + '</td>' + '$' +
cast(Trans.INVOICE_CHARGES as varchar) +'</td>' else '' END) as UF15
FROM (SELECT INVOICE_REFERENCE_NUM,INVOICE_CHARGES,INVOICE_CREDITS,TRANSACTION_DATE,POSTED,
ROW_NUMBER() OVER(PARTITION BY BT_ID ORDER BY BT_ID) AS rk
FROM Trans) AS Trans INNER JOIN
Invoice AS Invoice ON Trans.INVOICE_REFERENCE_NUM = Invoice.REFERENCE_NUM LEFT OUTER JOIN
Name AS Name ON Invoice.BT_ID = Name.ID LEFT OUTER JOIN
Org_Control AS Org_Control ON Invoice.ORG_CODE = Org_Control.OrgCode INNER JOIN
Name_Address AS Name_Address ON Name.BILL_ADDRESS_NUM = Name_Address.ADDRESS_NUM WHERE (Invoice.INVOICE_TYPE <'PP') AND
(Invoice.INVOICE_NUM <0) AND (Trans.TRANSACTION_DATE BETWEEN
@FromDate AND GETDATE()) AND
(Org_Control.OrgCode = @OrgCode) AND (Trans.INVOICE_CHARGES>0)
GROUP BY TRANSACTION_DATE,Trans.posted,Invoice.BT_ID,Name.COMPANY,
Invoice.INVOICE_DATE,Name.COMPANY_RECORD,Name.COMPANY_SORT,Name.LAST_FIRST,Name.FULL_NAME,Name_Address.PURPOSE,Name.TITLE,Name_Address.FULL_ADDRESS,
Invoice.INSTALL_BILL_DATE,Org_Control.OrgName,Org_Control.InvoiceAddress,Invoice.CUSTOMER_REFERENCE,Invoice.INVOICE_TYPE,Org_Control.OrgCode,Name.EMAIL