Click here to Skip to main content
15,885,757 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have 2 tables
in one table one UID has its value
in second table one uid has multiple value.
i want to data like this.

Table 1
SQL
UID	Bill
U123	5000
U125	4500



Table 2
SQL
UID	Collection
U123	2000
U123	2500
U125    1250
U123    200




I want result
SQL
UID   Bill       Coll      Outstd
U123  5000       2000        0
U123  5000       2500         0
U123  5000        200        300
U125   4500       1250       2250
Posted
Updated 22-Sep-14 0:29am
v2
Comments
Gihan Liyanage 22-Sep-14 6:33am    
What is the outstanding value ? Cant Buildup the query for you because the outstanding value is not clear ..
Gihan Liyanage 22-Sep-14 7:31am    
Do You have any response?? I think you want the concept of running total. But still I am not clear about the outstanding values, If this related to running total, then you need to have date or any other sequential ID for that, rather than UID

I am not sure if your results are correct. However, you can start something like this:

SQL
--Get Collections as Sum
SELECT
    #Table1.UId,
    SUM(#Table2.Collection) AS Collection
INTO
    #Temp
FROM
    #Table1 INNER JOIN #Table2 ON
        #Table1.UId = #Table2.UId
GROUP BY #Table1.UId

--Get Actual Results
SELECT
    #Table1.UId,
    #Table1.Bill,
    #Table2.Collection,
    (#Table1.Bill - #Temp.Collection) AS OutStanding
FROM
    #Table1 INNER JOIN #Table2 ON
        #Table1.UId = #Table2.UId
    INNER JOIN #Temp ON
        #Table2.UId = #Temp.UId
ORDER BY
    #Table1.UId

DROP TABLE #Temp
 
Share this answer
 
Try among these, live examples http://www.sqlfiddle.com/#!3/c3e84/2[^]

SQL
/*Soution 1: As wanted*/
WITH [Collection]
AS
(
	SELECT ROW_NUMBER() OVER(ORDER BY UID) AS RowId, Coll.*
		FROM Table2 AS Coll
),
LastCollection
AS
(
	SELECT MAX(RowId)AS RowId, UID AS LastId
		FROM [Collection]
		GROUP BY UID
)
SELECT 
	Bill.*,
	Coll.[Collection] AS Coll,
	CASE
		WHEN(LastColl.RowId IS NULL) THEN(SELECT 0)
		ELSE(
			-- total bill - (sum of previous collections and current row collection)
			COALESCE(Bill.Bill, 0) - COALESCE((SELECT SUM(PreColl.Collection)
													FROM [Collection] AS PreColl
													WHERE PreColl.UID = Coll.UID 
														AND PreColl.RowId <= Coll.RowId), 0)
		)
	END AS Outstd
	FROM Table1 AS Bill
	INNER JOIN [Collection] AS Coll ON Bill.UID = Coll.UID
	LEFT JOIN LastCollection AS LastColl ON Coll.RowId = LastColl.RowId;


or little more interesting
SQL
/*Soution 2: improved*/
WITH [Collection]
AS
(
	SELECT ROW_NUMBER() OVER(ORDER BY UID) AS RowId, Coll.*
		FROM Table2 AS Coll
)
SELECT Bill.*, Coll.Collection AS Coll,
	-- total bill - (sum of previous collections and current row collection)
	COALESCE(Bill.Bill, 0) - COALESCE((SELECT SUM(PreColl.Collection)
											FROM [Collection] AS PreColl
											WHERE PreColl.UID = Coll.UID 
												AND PreColl.RowId <= Coll.RowId), 0)AS Outstd
	FROM Table1 AS Bill
	INNER JOIN [Collection] AS Coll ON Bill.UID = Coll.UID;



But it would be better if
SQL
/*Soution 3: I like it*/
SELECT AllUser.UID,
	COALESCE(TtlBill.Bill, 0) AS Bill,
	COALESCE(TtlCollection.Coll, 0) AS Coll,
	COALESCE(TtlBill.Bill, 0) - COALESCE(TtlCollection.Coll, 0) AS Outstd
	--total usser
	FROM(
		SELECT *
			FROM(
				SELECT UID FROM Table1
				UNION ALL
				SELECT UID FROM Table2
			) AS Users
			GROUP BY UID
	) AS AllUser
	
	--total Bill
	LEFT JOIN(
		SELECT UID, SUM(Bill) AS Bill
			FROM Table1
			GROUP BY UID
	) AS TtlBill
	ON AllUser.UID = TtlBill.UID
	
	--total Collection
	LEFT JOIN(
	SELECT UID, SUM(Collection) AS Coll
		FROM Table2
		GROUP BY UID
	) AS TtlCollection
	ON AllUser.UID = TtlCollection.UID;
 
Share this answer
 
Comments
DiponRoy 22-Sep-14 16:28pm    
if you are not using sql server than just let me know
SQL
select *, OutStd --(put ur logic here) 
from tbl1
left outer join tbl2 on tbl.UID =UID
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900