Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

Join

$
0
0
Hi Everyone,
Just want to output my result in a better way. Below is the query I am using where I am a doing a join and a union. I am compiling all purchases and issues for all items during a specific period. 

SELECT Indetails.Category, Indetails.SerialNo, Indetails.Item, 'issue' as Code, isnull(OutDetails.Qty,0) as Qty, isnull(CONVERT(varchar,OutDetails.date,102),'NA') as Date, isnull(outdetails.sfno,'NA') as RefNo
FROM (SELECT Barcode, Item, Category FROM Item_Definition)
InDetails(SerialNo, Item, Category)
lEFT OUTER JOIN
(SELECT SerialNo, Qty, sfno, date, time,poffice from Store_Out_Details join Store_Out on Store_Out.SessionID=Store_Out_Details.SessionID where Store_Out.date between '2013-10-31' and '2014-01-01')
OutDetails(SerialNo, Qty, sfno, date, time, poffice)
ON Indetails.SerialNo=OutDetails.SerialNo
UNION
SELECT Indetails.Category, Indetails.SerialNo, Indetails.Item, 'purchase' as Code, isnull(OutDetails.Qty,0) as Qty, isnull(CONVERT(varchar,OutDetails.date,102),'NA') as Date, isnull(outdetails.PoNo,'NA') as RefNo
FROM (SELECT Barcode, Item, Category FROM Item_Definition)
InDetails(SerialNo, Item, Category)
lEFT OUTER JOIN
(SELECT SerialNo, Qty, store_po.PONo, store_po.Date, store_po.Time from Store_PO_Details join Store_PO on Store_PO.PONo=Store_PO_Details.PONo and Store_PO.Date = Store_PO_Details.date where Store_PO.date between '2013-10-31' and '2014-01-01')
OutDetails(SerialNo, Qty, PoNo, date, time)
ON Indetails.SerialNo=OutDetails.SerialNo
order by Indetails.Category, Indetails.Item, date ASC


Below is a sample of my output.

Category SerialNo Item Code Qty Date RefNo
Binding BIN-COV-CAR Binding back cover-carton A4 issue 100 2013.11.26 17656
Binding BIN-COV-CAR Binding back cover-carton A4 purchase 0 NA NA

Thing is where there is no purchase, it is showing up a record where purchase is 0 and same goes for issues if there is no issue for that period. How can I modify my query so as to not show this additional record when there is either no purchase or issue?

Thanks to advise.

Ashley

Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>