Click here to Skip to main content
15,912,756 members
Home / Discussions / Database
   

Database

 
GeneralRe: What Query i have to used Pin
khan201021-Oct-09 7:20
khan201021-Oct-09 7:20 
GeneralRe: What Query i have to used Pin
dan!sh 21-Oct-09 7:46
professional dan!sh 21-Oct-09 7:46 
GeneralRe: What Query i have to used Pin
J4amieC21-Oct-09 8:20
J4amieC21-Oct-09 8:20 
GeneralRe: What Query i have to used Pin
khan201028-Oct-09 21:38
khan201028-Oct-09 21:38 
GeneralRe: What Query i have to used Pin
J4amieC29-Oct-09 4:15
J4amieC29-Oct-09 4:15 
GeneralRe: What Query i have to used Pin
khan201028-Oct-09 23:38
khan201028-Oct-09 23:38 
GeneralRe: What Query i have to used Pin
Rob Graham29-Oct-09 4:37
Rob Graham29-Oct-09 4:37 
AnswerRe: What Query i have to used Pin
Niladri_Biswas24-Oct-09 20:28
Niladri_Biswas24-Oct-09 20:28 
Try this

declare @tbl1 table(productname varchar(20))
insert into @tbl1 values('abc_123a')
insert into @tbl1 values('cfl_523j')
insert into @tbl1 values('jpm_111k')

declare @tbl2 table(productname varchar(20),purqty int)
insert into @tbl2 values('abc_123a',50)
insert into @tbl2 values('abc_123a',30)
insert into @tbl2 values('cfl_523j',100)
insert into @tbl2 values('cfl_523j',20)
insert into @tbl2 values('cfl_523j',10)
insert into @tbl2 values('jpm_111k',105)
insert into @tbl2 values('jpm_111k',10)
insert into @tbl2 values('jpm_111k',25)

declare @tbl3 table(productname varchar(20),soldqty int)
insert into @tbl3 values('abc_123a',10)
insert into @tbl3 values('abc_123a',05)
insert into @tbl3 values('cfl_523j',20)
insert into @tbl3 values('cfl_523j',05)
insert into @tbl3 values('cfl_523j',10)
insert into @tbl3 values('jpm_111k',15)
insert into @tbl3 values('jpm_111k',05)
insert into @tbl3 values('jpm_111k',10)

declare @tbl4 table(productname varchar(20),refqty int)
insert into @tbl4 values('abc_123a',2)
insert into @tbl4 values('abc_123a',6)
insert into @tbl4 values('cfl_523j',4)
insert into @tbl4 values('cfl_523j',3)
insert into @tbl4 values('cfl_523j',9)
insert into @tbl4 values('jpm_111k',3)
insert into @tbl4 values('jpm_111k',8)
insert into @tbl4 values('jpm_111k',2)


;with cte1 as(
select productname,sum(purqty)as purqty  from @tbl2 
group by productname)
,cte2 as(
select productname,sum(soldqty)as soldqty  from @tbl3 
group by productname)
,cte3 as(
select productname,sum(refqty)as refqty  from @tbl4 
group by productname)

select c1.productname,c1.purqty,c2.soldqty,c3.refqty,c1.purqty- c2.soldqty-c3.refqty as STOCK
from cte1 c1 inner join cte2 c2 on c1.productname = c2.productname 
inner join cte3 c3
on c1.productname = c3.productname


Let me know in case of any concern
Smile | :)

Niladri Biswas

GeneralRe: What Query i have to used Pin
khan201028-Oct-09 21:42
khan201028-Oct-09 21:42 
QuestionJoining tables and get column with comma seperated values out of the join result Pin
nainakarri21-Oct-09 0:21
nainakarri21-Oct-09 0:21 
AnswerRe: Joining tables and get column with comma seperated values out of the join result [modified] Pin
J4amieC21-Oct-09 1:06
J4amieC21-Oct-09 1:06 
AnswerRe: Joining tables and get column with comma seperated values out of the join result Pin
i.j.russell21-Oct-09 1:51
i.j.russell21-Oct-09 1:51 
GeneralRe: Joining tables and get column with comma seperated values out of the join result Pin
Mycroft Holmes21-Oct-09 13:10
professionalMycroft Holmes21-Oct-09 13:10 
GeneralRe: Joining tables and get column with comma seperated values out of the join result Pin
i.j.russell21-Oct-09 21:41
i.j.russell21-Oct-09 21:41 
GeneralRe: Joining tables and get column with comma seperated values out of the join result Pin
Mycroft Holmes21-Oct-09 22:08
professionalMycroft Holmes21-Oct-09 22:08 
AnswerRe: Joining tables and get column with comma seperated values out of the join result Pin
Niladri_Biswas24-Oct-09 23:08
Niladri_Biswas24-Oct-09 23:08 
GeneralRe: Joining tables and get column with comma seperated values out of the join result Pin
nainakarri25-Oct-09 19:12
nainakarri25-Oct-09 19:12 
GeneralRe: Joining tables and get column with comma seperated values out of the join result Pin
Niladri_Biswas26-Oct-09 15:54
Niladri_Biswas26-Oct-09 15:54 
QuestionAn Extraordinarily Dumb Question Pin
Roger Wright20-Oct-09 20:15
professionalRoger Wright20-Oct-09 20:15 
AnswerRe: An Extraordinarily Dumb Question Pin
SeMartens20-Oct-09 20:41
SeMartens20-Oct-09 20:41 
GeneralRe: An Extraordinarily Dumb Question Pin
Roger Wright21-Oct-09 3:12
professionalRoger Wright21-Oct-09 3:12 
AnswerRe: An Extraordinarily Dumb Question Pin
i.j.russell21-Oct-09 3:09
i.j.russell21-Oct-09 3:09 
GeneralRe: An Extraordinarily Dumb Question Pin
Roger Wright25-Oct-09 21:40
professionalRoger Wright25-Oct-09 21:40 
GeneralRe: An Extraordinarily Dumb Question Pin
Gary R. Wheeler29-Oct-09 13:04
Gary R. Wheeler29-Oct-09 13:04 
GeneralRe: An Extraordinarily Dumb Question Pin
Roger Wright29-Oct-09 18:04
professionalRoger Wright29-Oct-09 18:04 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.