I am very much confused about this GROUP BY & Union statements.
Right Now I am getting this O/P.
But I want my output to look like this:
Vendor Name Purchase Order Req Qty Inward Qty
----------------------------------------------------------------------
Philips Lighting PO20140918133011 350 350
(I) Pvt Ltd
Requirement :
If my vendor is null then also I have to group by on the basis of purchase order. But the output which is I am getting in which all the requirement is not satisfied Is there any way that I can do group by only on the basis of purchase order.
**Query : **
SELECT
vendor_name,
purchase_order_no,
SUM(ISNULL(InwardQty,0)) AS InwardQty,
SUM(ISNULL(ReqQty,0)) ReqQty
FROM
(
SELECT
vendor_name,
pm.purchase_order_no,
ISNULL(SUM(PIIM.qty),0) AS ReqQty,
0 InwardQty
FROM
RS_Purchase_Order_Master AS PM
LEFT OUTER JOIN RS_Purchase_Invoice_Info_Master AS PIIM ON PIIM.purchase_order_no = PM.purchase_order_no
LEFT OUTER JOIN RS_Vendor_Master AS VM ON VM.vendor_id = PM.vendor_id
WHERE
IsPicreated = 1
GROUP BY
vendor_name,
pm.purchase_order_no
UNION
SELECT
NULL AS vendor_name,
purchase_order_no,
0 AS ReqQty,
ISNULL(SUM(qty),0) AS InwardQty
FROM
RS_GIN_Master
GROUP BY
purchase_order_no
)
AS A
GROUP BY
vendor_name,
purchase_order_no
Any help or suggestion will appreciated.
Thank you in advance