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 |
|
maya_zakry
Constraint Violating Yak Guru
379 Posts |
Posted - 2007-05-07 : 23:51:50
|
hi experts,im in a progress to calculate this and get stuck.. my code with sample data:--- Prepare sample dataDECLARE @tbljobDoc TABLE (jobID varchar(12), DocRefID varchar(10), PickComplete char(1) )INSERT @tbljobDocSELECT 'A', 'docA', 'Y' UNION ALLSELECT 'A', 'docA2', 'N' DECLARE @tblItemPicked TABLE (ItemStorageID varchar(12), DocRefID varchar(10), jobID varchar(12), PickQty float )INSERT @tblItemPickedSELECT 'ISO1', 'docA', 'A', 10 UNION ALLSELECT 'ISO1', 'docA2', 'A', 3 UNION ALLSELECT 'ISO1', 'docA2', 'A', 4 UNION ALLSELECT 'ISO2', 'docA', 'A', 10 UNION ALLSELECT 'ISO2', 'docA2', 'A', 3 UNION ALLSELECT 'ISO2', 'docA2', 'A', 4 DECLARE @tblItemDetail TABLE (ItemStorageID varchar(12), OriginQty float, PickQty float, own char(3), client varchar(10), itemid varchar(20) )INSERT @tblItemDetailSELECT 'ISO1', 50, 17, 'OWN', 'jabpb', 'rainbow' UNION ALLSELECT 'ISO2', 50, 17, 'VMI', 'jabpb', 'rainbow'select * from @tbljobDocselect * from @tblItemPickedselect * from @tblItemDetaildeclare @AVA float, @book float, @pick float, @VMIAVA float, @VMIBook floatSELECT @AVA= ISNULL(SUM(d.OriginQty), 0) - ISNULL(SUM(d.PickQty), 0), @Book=sum(coalesce(p.pickqty,0)), @Pick=case when (sum(coalesce(p.pickqty,0))) > 0 Then ISNULL(SUM(d.PickQty), 0) - sum(coalesce(p.pickqty,0)) Else ISNULL(SUM(d.PickQty), 0) end, @VMIAVA = SUM(CASE WHEN own = 'VMI' THEN d.OriginQty ELSE 0 END) - SUM(CASE WHEN own = 'VMI' THEN d.PickQty ELSE 0 END)--, --@VMIBook = SUM(CASE WHEN OwnerStatus = 'VMI' THEN SUM(COALESCE(p.PickQty,0)) ELSE 0 END) FROM @tblItemDetail dLEFT JOIn @tblitempicked p ON p.itemstorageid=d.itemstorageidleft JOIn @tblJobDoc j ON j.Docrefid=p.Docrefid AND j.jobID=p.JobID AND j.Pickcomplete='N'WHERE d.ItemID = 'rainbow' and d.client='jabpb'select @AVA as ava, @book as book, @pick as pick, @VMIAVA as vmiAva, @VMIBook as vmiBook--expected resultselect 66 as ava, 14 as book, 20 as pick, 33 as vmiAVA, 7 As vmiBook, 10 as vmiPick SPECSava : sum originQty - PickQty in @tblItemdetailbook : total of PickQty in @tblItemPicked when @PickComplete in @tbljobDoc='N'pick : if book > 0 then pick = pickQty in @tblItemdetail - book else take pickQty with vmi : same calculation except own='vmi'~~~Focus on problem, not solution ¯\(º_o)/¯ ~~~ |
|
|
maya_zakry
Constraint Violating Yak Guru
379 Posts |
Posted - 2007-05-08 : 00:29:27
|
| [code]select @AVA=sum(d.originqty)-sum(d.pickqty), @book=sum(x.pickqty), @pick=sum(d.pickqty)- sum(x.pickqty), @VMIAVA= sum(case when d.own='vmi' then d.originqty-d.pickqty else '0' end), @VMIBook= sum(case when d.own='vmi' then x.pickqty else '0' end), @vmiPick= sum(case when d.own='vmi' then d.pickqty-x.pickqty else '0' end) from @tblitemdetail dleft join (select sum(coalesce(p.pickqty,0))as pickqty, p.itemstorageid as itemstorageid from @tblItempicked p left join @tbljobdoc j on p.docrefid=j.docrefid and p.jobid=j.jobid where j.pickcomplete='N' group by p.itemstorageid ) as x on x.itemstorageid=d.itemstorageidWHERE d.ItemID = 'rainbow' and d.client='jabpb'select @AVA as ava, @book as book, @pick as pick, @VMIAVA as vmiAva, @VMIBook as vmiBook, @vmiPick as vmiPick[/code]so far this worked~~~Focus on problem, not solution ¯\(º_o)/¯ ~~~ |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-05-08 : 00:52:50
|
| No need for variablesselect sum(d.originqty) - sum(d.pickqty) AS AVA, sum(x.pickqty) AS Book, sum(d.pickqty) - sum(x.pickqty) AS PICK, sum(case when d.own='vmi' then d.originqty - d.pickqty else '0' end) AS VMIAVA, sum(case when d.own='vmi' then x.pickqty else '0' end) AS VMIBOOK, sum(case when d.own='vmi' then d.pickqty - x.pickqty else '0' end) AS VMIPICK from @tblitemdetail AS dleft join (select sum(coalesce(p.pickqty,0)) as pickqty, p.itemstorageid as itemstorageid from @tblItempicked as p left join @tbljobdoc as j on p.docrefid = j.docrefid and p.jobid = j.jobid where j.pickcomplete = 'N' group by p.itemstorageid ) as x on x.itemstorageid = d.itemstorageidWHERE d.ItemID = 'rainbow' and d.client = 'jabpb'Peter LarssonHelsingborg, Sweden |
 |
|
|
maya_zakry
Constraint Violating Yak Guru
379 Posts |
Posted - 2007-05-08 : 01:35:28
|
| hi peterwell.. i need it peter.. later ill update some tables with these values .. like usual this is sample dat a~~~Focus on problem, not solution ¯\(º_o)/¯ ~~~ |
 |
|
|
maya_zakry
Constraint Violating Yak Guru
379 Posts |
Posted - 2007-05-08 : 01:47:31
|
| oh no.. problem arise.. help help..im having problem when im trying to replace null wif 0 (AGAIN):-@book=isnull(sum(x.pickqty),0), Warning: Null value is eliminated by an aggregate or other SET operation.so the updating failed when this error occuredthis also didnt work:-@book=sum(coalesce(x.pickqty,0)),~~~Focus on problem, not solution ¯\(º_o)/¯ ~~~ |
 |
|
|
harsh_athalye
Master Smack Fu Yak Hacker
5581 Posts |
Posted - 2007-05-08 : 02:03:05
|
| This is not an error and should not cause update to fail. It is just a warning caused by SET ANSI_WARNINGS ON option.Harsh AthalyeIndia."The IMPOSSIBLE is often UNTRIED" |
 |
|
|
maya_zakry
Constraint Violating Yak Guru
379 Posts |
Posted - 2007-05-08 : 02:41:02
|
| ya rite.. its my problem in another SP.. arghh arghhh... wasting my time checking on warning.. :P~~~Focus on problem, not solution ¯\(º_o)/¯ ~~~ |
 |
|
|
maya_zakry
Constraint Violating Yak Guru
379 Posts |
Posted - 2007-05-08 : 04:52:08
|
| hi ..i need some opinion coz this run 3:45 mins for 364 records... any faster waythanks~~~Focus on problem, not solution ¯\(º_o)/¯ ~~~ |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2007-05-08 : 05:25:33
|
| Add some proper indexes.Peter LarssonHelsingborg, Sweden |
 |
|
|
maya_zakry
Constraint Violating Yak Guru
379 Posts |
Posted - 2007-05-08 : 06:03:18
|
| peter .. how?~~~Focus on problem, not solution ¯\(º_o)/¯ ~~~ |
 |
|
|
maya_zakry
Constraint Violating Yak Guru
379 Posts |
Posted - 2007-05-08 : 08:13:46
|
| i guess it's becoz the group by p.itemstorageidplus it's quite tough to sum every id that exists in other table according to flag column in another table..and how to add proper indexes? :( peter peter.. parker..~~~Focus on problem, not solution ¯\(º_o)/¯ ~~~ |
 |
|
|
|
|
|
|
|