Click here to Skip to main content
15,891,708 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Table1

T1_id---Quantity
1----------5
2----------3

Table2
T2_id---T1_id---Quantity
1--------1--------2
2--------1--------1
3--------2--------1

Condition T1_id primary key in Table1 and Foreign key in Table2

Result
T2_id---T1_id---Quantity
1--------1--------2
2--------1--------1
0--------1--------2 //Table1.Quantity(Sum(Table2.Quantity)where Table2.T1_id=Table1.T1_id)
3--------2--------1
0--------2--------2 //Table1.Quantity(Sum(Table2.Quantity)where Table2.T1_id=Table1.T1_id)
Posted
Updated 13-Jun-13 3:54am
v3
Comments
Prasad_Kulkarni 13-Jun-13 9:54am    
..and where's your question?
tushar Vayangankar 13-Jun-13 10:14am    
i have two tables Table1 and Table2
I want output shown as Result
gvprabu 13-Jun-13 10:11am    
T2_id---T1_id---Quantity
1--------1--------2
2--------1--------1
0--------1--------3 //Table1.Quantity(Sum(Table2.Quantity)where Table2.T1_id=Table1.T1_id)
3--------2--------1
0--------2--------1 //Table1.Quantity(Sum(Table2.Quantity)where Table2.T1_id=Table1.T1_id)

Right.... Then what is Table1 Quantity?
tushar Vayangankar 13-Jun-13 10:16am    
Quantity is an integer
in result sum of quantity for t1_id is total quantity for Table1 t1_id

1 solution

SQL
SELECT  T2_id  ,   T1_id  ,   Quantity
FROM (
select 1 AS ORD, T2_id  ,   T1_id  ,   Quantity  from Table2
UNION ALL
select 2 AS ORD,0 AS T2_id  ,   T1_id  ,   SUM(Quantity ) AS Quantity 
from Table2 GROUP BY T1_id
) AS P
ORDER BY T1_id, ORD
 
Share this answer
 
Comments
tushar Vayangankar 14-Jun-13 1:28am    
Thanx dude you make my day

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