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.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 count sum

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 data
DECLARE @tbljobDoc TABLE (jobID varchar(12), DocRefID varchar(10), PickComplete char(1) )

INSERT @tbljobDoc
SELECT 'A', 'docA', 'Y' UNION ALL
SELECT 'A', 'docA2', 'N'

DECLARE @tblItemPicked TABLE (ItemStorageID varchar(12), DocRefID varchar(10), jobID varchar(12), PickQty float )

INSERT @tblItemPicked
SELECT 'ISO1', 'docA', 'A', 10 UNION ALL
SELECT 'ISO1', 'docA2', 'A', 3 UNION ALL
SELECT 'ISO1', 'docA2', 'A', 4 UNION ALL
SELECT 'ISO2', 'docA', 'A', 10 UNION ALL
SELECT 'ISO2', 'docA2', 'A', 3 UNION ALL
SELECT 'ISO2', 'docA2', 'A', 4

DECLARE @tblItemDetail TABLE (ItemStorageID varchar(12), OriginQty float, PickQty float, own char(3), client varchar(10), itemid varchar(20) )

INSERT @tblItemDetail
SELECT 'ISO1', 50, 17, 'OWN', 'jabpb', 'rainbow' UNION ALL
SELECT 'ISO2', 50, 17, 'VMI', 'jabpb', 'rainbow'

select * from @tbljobDoc
select * from @tblItemPicked
select * from @tblItemDetail

declare @AVA float, @book float, @pick float, @VMIAVA float, @VMIBook float
SELECT @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 d
LEFT JOIn @tblitempicked p ON p.itemstorageid=d.itemstorageid
left 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 result
select 66 as ava, 14 as book, 20 as pick, 33 as vmiAVA, 7 As vmiBook, 10 as vmiPick

SPECS
ava : sum originQty - PickQty in @tblItemdetail
book : 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 d

left 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.itemstorageid

WHERE 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)/¯ ~~~
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2007-05-08 : 00:52:50
No need for variables

select 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 d
left 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.itemstorageid
WHERE d.ItemID = 'rainbow'
and d.client = 'jabpb'


Peter Larsson
Helsingborg, Sweden
Go to Top of Page

maya_zakry
Constraint Violating Yak Guru

379 Posts

Posted - 2007-05-08 : 01:35:28
hi peter
well.. 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)/¯ ~~~
Go to Top of Page

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 occured

this also didnt work:-
@book=sum(coalesce(x.pickqty,0)),

~~~Focus on problem, not solution ¯\(º_o)/¯ ~~~
Go to Top of Page

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 Athalye
India.
"The IMPOSSIBLE is often UNTRIED"
Go to Top of Page

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)/¯ ~~~
Go to Top of Page

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 way
thanks

~~~Focus on problem, not solution ¯\(º_o)/¯ ~~~
Go to Top of Page

SwePeso
Patron Saint of Lost Yaks

30421 Posts

Posted - 2007-05-08 : 05:25:33
Add some proper indexes.


Peter Larsson
Helsingborg, Sweden
Go to Top of Page

maya_zakry
Constraint Violating Yak Guru

379 Posts

Posted - 2007-05-08 : 06:03:18
peter .. how?

~~~Focus on problem, not solution ¯\(º_o)/¯ ~~~
Go to Top of Page

maya_zakry
Constraint Violating Yak Guru

379 Posts

Posted - 2007-05-08 : 08:13:46
i guess it's becoz the group by p.itemstorageid
plus 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)/¯ ~~~
Go to Top of Page
   

- Advertisement -