我已经阅读了反馈,现在了解加入的情况要好得多,但我的结果中仍然遇到重复元组问题。具体来说,从以下sql我收到重复的发票行。结果示例: -
accountnumber Invoice UnitPrice Units GSTIncAmt
10025 MEM010206406 700 1 700
10025 MEM010206406 2475 1 2475
10025 MEM010206406 700 1 700
10025 MEM010206406 2475 1 2475
我需要购买所有销售的活跃产品(来自SNAP_activeproduct)。然后拿起没有产品的任何发票(来自filteredinvoice表。
当我运行以下sql时,我获得了所有必需的信息但是发票行(即两个产品可以在一张发票上)是重复的。任何帮助赞赏
<select distinct
--(select fa.accountnumber where fa.statecodename = 'Active' and ape.statecode = 1) as Accountnumber,
fa.accountnumber
--,fa.name
--,ape.SNAP_Status as 'ProdStatus_tobeadded'
--,fid.snap_pricelistidname
--,fp.productnumber
--,ape.snap_productidName as 'ProductName'
--,fp.producttypecode
--,fp.producttypecodename
--,convert (varchar,ape.CreatedOn,103) as InvoiceDate
,ape.snap_invoiceidName as Invoice
--, ape.SNAP_ProductType as 'ProdTypetobeadded'
--,fi.name as InvoiceNumber
,ape.snap_productidName
,str(fid.priceperunit) as UnitPrice
,str(fid.quantity) as Units
,str(fid.priceperunit*fid.quantity) As GSTIncAmt
,str(fid.snap_otherdiscamount) as 'Discount'
,str(fid.snap_amountafterdiscount) as 'Amt After Discount'
,fi.statecodename as 'Invoice Status'
,fid.baseamount as BaseAmt
,fid.extendedamount
,fid.snap_amountafterdiscount
,SUBSTRING(fid.snap_financedata,4,charindex(':',fid.snap_financedata)) AS Entity
,fid.snap_dbcode as 'Entity Name'
,SUBSTRING(fid.snap_financedata,8,charindex(':',fid.snap_financedata)) AS ReportingUnit
,COALESCE(convert(varchar,fi.snap_paiddate,103),'') as 'Paid Date'
,fi.snap_salespersonidname as Salesperson
,fa.snap_accountmanageridname as AccountManager
,fa.snap_noofemployeesname as NoEmployee
,fa.snap_anzsicdivisionname as DivisionIndustry
,fa.snap_anzsicsubdivisionname as SubDivisionIndustry
,fa.snap_regionidname as Region
FROM SNAP_activeproduct ape
left join FilteredProduct fp
on ape.snap_productid = fp.productid
left join FilteredAccount fa
on fa.accountid = ape.snap_accountid
left join FilteredInvoice fi
on ape.snap_invoiceid = fi.invoiceid
left join FilteredInvoiceDetail fid
on fi.invoiceid = fid.invoiceid
and fi.invoiceid = ape.snap_invoiceid
where ape.snap_status in (1,2,3,5)
--and fa.accountnumber = '10025'
order by fa.accountnumber >
答案 0 :(得分:0)
我无法看到任何数据或任何解释,但我会假设您有一个经典的标题/线型,其中单个标题有多行。
例如,一个标题记录包含header_id = 134
。标头ID在标头表中是唯一的。
Header_ID TotalAmount TotalTax Customer_ID
134 $500 $50 774
这个标题有三行连接。因此,您在行表中还有三个记录header_id=134
,但它们也有行ID。此表在header_id + line_id
Header_ID Line_ID SalePrice Qty LineAmount
134 1 $125 2 $250
134 2 $10 10 $100
134 3 $150 1 $150
现在我们加入这些表格......
SELECT H.Header_ID, H.Customer_ID, H.TotalAmount, L.Line_ID, L.LineAmount
FROM
HeaderTable As H
INNER JOIN
LineTable As L
ON H.Header_ID = L.Header_ID
WHERE Header_ID = 134
我们得到这样的数据:
Header_ID TotalAmount Customer_ID Line_ID LineAmount
134 $500 774 1 $250
134 $500 774 2 $100
134 $500 774 3 $150
看起来TotalAmount
是重复的....但它并没有完全按照您的要求进行操作 - 为您提供一个组合重复表的记录集。
我希望这可以帮助您了解您的问题,但更重要的是让您了解在提问时需要提供的详细程度。
答案 1 :(得分:-1)