Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
jonasalbert20
Constraint Violating Yak Guru
300 Posts |
Posted - 2006-03-19 : 04:11:58
|
| [code]SELECT *INTO #tempFROM(SELECT 1 as id, '11' as st_type,'return excess' as descrp , 'NK-S180001' as stk_code, '1' as qtyunion allSELECT 2 as id, 12, 'return nc', 'NK-S180001', 2union allSELECT 3 as id, 3, 'sales', 'NK-S180001', '1'union allSELECT 4 as id, 11, 'return excess', 'NK-S180002', 1union allSELECT 5 as id, 12, 'return nc', 'NK-S180002', 2union allSELECT 6 as id, 3, 'sales', 'NK-S180002', 10) as tblTransselect * from #temp as t1select t1.st_type ,t1.descrp ,t1.stk_code ,(select qty from #temp where st_type = 3 and stk_code = t1.stk_code) as sales ,t1.qty as returnedfrom #temp as t1where t1.st_type <> 3group by st_type ,t1.descrp ,t1.stk_code ,t1.qtyorder by t1.stk_code ,st_typedrop table #temp[/code]RESULTS....I.[code]ID st_type descrp stk_code qty-----------------------------------------------------1 11 return excess NK-S180001 12 12 return nc NK-S180001 23 3 sales NK-S180001 14 11 return excess NK-S180002 15 12 return nc NK-S180002 26 3 sales NK-S180002 10[/code]DESIRED RESULTII.[code]ID st_type descrp stk_code SALES RETURNED-----------------------------------------------------11 return excess NK-S180001 1 112 return nc NK-S180001 1 211 return excess NK-S180002 10 112 return nc NK-S180002 10 2[/code]Any approach that will elimanate the use of subquery?Tnx in advance!Want Philippines to become 1st World COuntry? Go for World War 3... |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2006-03-19 : 04:20:57
|
Like this ?select t1.st_type, t1.descrp, t1.stk_code, t2.qty as sales, t1.qty as returnedfrom #temp as t1inner join( select stk_code, qty from #temp where st_type = 3) as t2on t1.stk_code = t2.stk_codewhere t1.st_type <> 3order by t1.stk_code, st_type KHChoice is an illusion, created between those with power, and those without.Concordantly, while your first question may be the most pertinent, you may or may not realize it is also the most irrelevant |
 |
|
|
jonasalbert20
Constraint Violating Yak Guru
300 Posts |
Posted - 2006-03-19 : 04:33:33
|
| thanks khtan!yup! how about not using a subquery?Want Philippines to become 1st World COuntry? Go for World War 3... |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2006-03-19 : 04:57:34
|
quote: Originally posted by jonasalbert20 thanks khtan!yup! how about not using a subquery?Want Philippines to become 1st World COuntry? Go for World War 3...
It isn't using subquery. It is derived table KHChoice is an illusion, created between those with power, and those without.Concordantly, while your first question may be the most pertinent, you may or may not realize it is also the most irrelevant |
 |
|
|
jonasalbert20
Constraint Violating Yak Guru
300 Posts |
Posted - 2006-03-19 : 05:01:21
|
| Sorry for the term i used. :DYup! how about not using a derived table?Want Philippines to become 1st World COuntry? Go for World War 3... |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2006-03-19 : 07:12:20
|
What is your requirement ? What's wrong with using sub query or derived table in your query ? KHChoice is an illusion, created between those with power, and those without.Concordantly, while your first question may be the most pertinent, you may or may not realize it is also the most irrelevant |
 |
|
|
jonasalbert20
Constraint Violating Yak Guru
300 Posts |
Posted - 2006-03-19 : 07:21:59
|
Im making a query plan to speed-up my query.Just getting idea what would be the best approach. Using a sub query or a derived table is a speed factor. I have read a post here which I recall with same requirement and im having a whole day finding that thread. Want Philippines to become 1st World COuntry? Go for World War 3... |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2006-03-19 : 07:40:38
|
run both method of the query against your table and compare the execution plan & timing. It should gives you an idea which is better. Maybe somebody else have more efficient method. KHChoice is an illusion, created between those with power, and those without.Concordantly, while your first question may be the most pertinent, you may or may not realize it is also the most irrelevant |
 |
|
|
RyanRandall
Master Smack Fu Yak Hacker
1074 Posts |
Posted - 2006-03-20 : 06:02:57
|
| Hi all,Jonas - khtan's method is the best way to do this, and I don't think you'll find a better method. Put an index on st_type though (if you don't have one already) - that will speed things up.Ryan Randallwww.monsoonmalabar.comIdeas are easy. Choosing between them is the hard part. |
 |
|
|
jonasalbert20
Constraint Violating Yak Guru
300 Posts |
Posted - 2006-03-20 : 07:14:26
|
Thnx RyanRandall, but i just want to find that thread first coz im sure it was a good approach. I'll just post it again, once ive seen that thread.But for the meantime, I've used the derived table method. Tnx for the reply guys.Want Philippines to become 1st World COuntry? Go for World War 3... |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2006-03-20 : 08:00:52
|
It will be interesting to see a better approach to this. Do post if you ever found it KHChoice is an illusion, created between those with power, and those without.Concordantly, while your first question may be the most pertinent, you may or may not realize it is also the most irrelevant |
 |
|
|
|
|
|
|
|