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 |
javad.nikoo
Starting Member
17 Posts |
Posted - 2010-10-27 : 04:54:17
|
HiI have three table the First is Store(it contains Stuff,stCountInStore),second is Request Table(StuffReq,ReqCount)and the last one is OrderTable(StufOrd,OrdCount)(In This Table OrdCount added to StCountInStore For Each Stuf)the relation between is Stuf and there is no relation bet RequstTable AND OrderTabl,an example of Data :Store :A 100B 150C 15D 90Request:A 10B 7A 13A 12A 11B 9D 17B 25Oreder:A 1B 6A 3A 20A 9B 3D 16B 17OutPut Must Be This:A (OrdCount(A)in Order +(stCountInStore(A))Store) - sum(A)in RequestB (OrdCount(B)in Order +(stCountInStore(B))Store) - sum(B)in RequestC (OrdCount(C)in Order +(stCountInStore(C))Store) - sum(C)in RequestD (OrdCount(D)in Order +(stCountInStore(D))Store) - sum(D)in RequestI have Written a Query For this But it Must Be Faster than this query(I SUM(Count)For Each Item In Each TAble And Compute This Formula but its too Expensive) |
|
pk_bohra
Master Smack Fu Yak Hacker
1182 Posts |
Posted - 2010-10-27 : 04:59:09
|
quote: I have Written a Query For this But it Must Be Faster than this query(I SUM(Count)For Each Item In Each TAble And Compute This Formula but its too Expensive)
Can you show us the query you have tried. |
 |
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2010-10-27 : 05:14:07
|
SELECT s.[Stuff], (s.stCountInStore + isnull(o.OrdCount,0)) - isnull(r.ReqCount,0) as ResultFROM Store as sLEFT JOIN (SELECT StuffOrd, SUM(OrdCount) as OrdCount FROM OrderTable GROUP BY StuffOrd) as oON o.StuffOrd = s.[Stuff]LEFT JOIN (SELECT StuffReq, SUM(ReqCount) as ReqCount FROM RequestTable GROUP BY StuffReq) as rON r.StuffReq = s.Stuff No, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
javad.nikoo
Starting Member
17 Posts |
Posted - 2010-10-27 : 05:16:22
|
what i explained was a sample of my table Each table IS Master\Detail Table BUT I explain this to refuse confusing |
 |
|
webfred
Master Smack Fu Yak Hacker
8781 Posts |
Posted - 2010-10-27 : 05:34:26
|
Did you try my solution? No, you're never too old to Yak'n'Roll if you're too young to die. |
 |
|
|
|
|
|
|