我一直在尝试减少这个查询,以便每个供应商ID获得一行并且汇款我
-- 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
,{ {1}},InvoiceAmt
,PaymentCT
和PaymentAmt
。
此示例中的其他所有内容均为空白
结果是给出了我需要的数字和所有内容,但是我需要将两行合并为一行,并且每个行的数量应该相互加入以给我一个更大的总和。
我迷路了!
答案 0 :(得分:1)
两条线的区别在于TERMS_CD,一条是PNOW而另一条是空白的,因为您在该字段上进行分组,返回两行。如果你取该字段的MIN / MAX,你将得到你想要的一行。
您必须决定哪些字段会对您的情况产生不同的值。
答案 1 :(得分:1)
您有两行,因为它们在PayTerm列中具有不同的值
PayTerm blank / PNOW
将此列包含为某种聚合而不是原始列,例如最大(PayTerm)或 将它从查询中完全排除。