Last post Dec 07, 2020 07:15 AM by wmec
Dec 04, 2020 01:00 PM|akhterr|LINK
below is query ,which is not giving correct ouput ,means that inv_ID 224 Received amount should not display in second line,it must display in one row, as in image attached.
Create table #tbl_Receivable (Rec_ID int,Inv_ID int ,Customer_ID int,Rec_Date date,Rec_Amount varchar(50),Inv_type varchar(50),item_Weight int);
Create table #tbl_Customer ( Customer_ID int ,Customer_Name varchar(50));
insert into #tbl_Customer values (1,'Akhter'),(2,'Hussian');
INSERT INTO #tbl_Receivable VALUES
;with cte as (
SELECT a.Inv_ID,item_Weight,b.Customer_ID,min(a.Rec_Date) Rec_Date
,SUM((CASE WHEN a.INV_TYPE= 'Payable' THEN CAST(a.Rec_Amount as int) ELSE 0 END)) as Payable,
SUM((CASE WHEN a.INV_TYPE= 'Received' THEN CAST(a.Rec_Amount as int) ELSE 0 END)) as Received,
SUM((CASE WHEN a.INV_TYPE= 'Payable' THEN CAST(a.Rec_Amount as int) ELSE 0 END)) -
SUM((CASE WHEN a.INV_TYPE= 'Received' THEN CAST(a.Rec_Amount as int) ELSE 0 END)) as Pending
from #tbl_Receivable a
inner join #tbl_Customer b on a.Customer_ID = b.Customer_ID
GROUP BY a.INV_ID,b.Customer_Name,a.item_Weight,b.Customer_ID)
select cast(Inv_ID as varchar(100)) Inv_ID
, cast(item_Weight as varchar(100)) item_Weight
, cast(Customer_ID as varchar(100)) Customer_ID
, cast(Rec_Date as varchar(100)) Rec_Date
Dec 04, 2020 05:43 PM|imapsp|LINK
The item_weight column is preventing the grouping of these 2 rows.
To group them you need to use an aggregate function with that column or remove it from the query.
Hope this help
Dec 07, 2020 07:15 AM|wmec|LINK
It can be data issue. Check relevant query criteria.