このクエリをスリム化して、ベンダー ID ごとに 1 つの行を取得し、
-- Insert statements for procedure here
SELECT DISTINCT
mast.VENDOR AS VendorID,
mast.VENDOR_VNAME AS VendorName,
addr.LOCATION_CODE AS 'Remit ID',
mast.TAX_ID AS TIN,
addr.ADDR1 AS Address1,
addr.ADDR2 AS Address2,
addr.CITY_ADDR5 AS City,
addr.STATE_PROV AS St,
addr.COUNTRY AS Country,
addr.POSTAL_CODE AS Zip,
count(inv.TRAN_TOT_TXBL) AS InvoiceCT,
SUM(inv.TRAN_TOT_TXBL) AS InvoiceAMT,
COUNT(inv.TRAN_PAID_AMT) AS PaymentCT,
SUM(inv.TRAN_PAID_AMT) AS PaymentAmt,
inv.CASH_CODE AS PaymentMethod,
inv.TERMS_CD AS PayTerm,
'' AS POCt,
'' AS DPO,
'' AS SourceSystem,
contact.PHONE_NMBR AS 'Phone Number',
contact.E_MAIL_ADDRESS AS 'E-mail',
contact.FAX_NMBR AS 'Fax Number'
FROM APINVOICE inv
INNER JOIN APVENMAST mast ON mast.VENDOR = inv.VENDOR AND mast.VENDOR_GROUP = inv.VENDOR_GROUP AND mast.VENDOR_STATUS ='A'
INNER JOIN APVENADDR addr ON addr.VENDOR = mast.VENDOR AND (addr.CUR_ADDR='Y' AND addr.VENDOR_GROUP = mast.VENDOR_GROUP)
INNER JOIN APCONTACT contact ON contact.VENDOR = mast.VENDOR AND contact.VENDOR_GROUP = mast.VENDOR_GROUP
WHERE inv.INVOICE_DTE BETWEEN '1/1/2012' AND '12/31/2013' AND (inv.VENDOR_GROUP = '2' OR inv.VENDOR_GROUP = '35')
GROUP BY mast.VENDOR,mast.VENDOR_VNAME,mast.TAX_ID,addr.ADDR1,addr.CITY_ADDR5,addr.STATE_PROV,addr.C OUNTRY,addr.POSTAL_CODE,addr.ADDR2,contact.PHONE_NMBR,contact.E_MAIL_ADDRESS,contact.FAX_NMBR,addr.LOCATION_CODE,inv.TERMS_CD,inv.CASH_CODE
ORDER BY mast.VENDOR,mast.VENDOR_VNAME,mast.TAX_ID,addr.ADDR1,addr.CITY_ADDR5,addr.STATE_PROV,addr.COUNTRY,addr.POSTAL_CODE,addr.ADDR2,contact.PHONE_NMBR,contact.E_MAIL_ADDRESS,contact.FAX_NMBR,addr.LOCATION_CODE,inv.TERMS_CD,inv.CASH_CODE
のような結果を得ています
VendorID VendorName RemitID TIN Address1 Address2 City St Country Zip InvoiceCT InvoiceAMT PaymentCT PaymentAmt PaymentMethod PayTerm POCt DPO SourceSystem PhoneNumber E-mail FaxNumber
3 20 Company 1 INC 123456789 EAST STREET WEST SIDE NY 14787 9 0.00 9 30409.39 371
3 20 Company 1 INC 123456789 EAST STREET WEST SIDE NY 14787 7 0.00 7 10128.22 371 PNOW
入力VendorID
される列は、、、、、、、、、、およびです。 VendorName
_ Tin
_ Address1
_City
St
Zip InvoiceCT
InvoiceAmt
PaymentCT
PaymentAmt
Pay Term
この例の他のすべては空白です
結果は、必要な数値とすべてを提供していますが、両方の行を1行に結合し、それぞれの数値を互いに追加して、より大きな合計を得る必要があります。
道に迷いました!