I am getting the same result pandey, I might have left that of by accident.
I
have tried it many ways.
case when sum (dids.supplier_cost) = '0'
then '0'
when sum (dids.supplier_cost) IS NULL
then '0'
when sum (dihs.qty_total) = '0'
then '0'
when sum (dihs.qty_total) IS NULL
then '0'
WHEN SUM (dids.revenue) = '0'
THEN '0'
WHEN SUM (dids.revenue) IS NULL
THEN '0'
else sum (dids.revenue/((dihs.qty_total) * dids.supplier_cost))
end [dollar_turns],
"Abhishek Pandey" wrote:
> in your case statement you r not checking for dids.supplier_cost = 0
> and in your else clause you are divinding by
> ( (dihs.qty_total) * dids.supplier_cost )
> hope thishelps
> rgds
> abhishek
> "ITDUDE27" wrote:
>i am not sure what exactly are you trying to do here..
in your case statement you are checking "sum(dids.supplier_cost)" and other
things for 0 and handling it accordingly but in else clause you are divindin
g
by dids.supplier_cost.
say for example
dids.supplier_cost has three values
1
0
3
while sum(dids.supplier_cost) <> 0 but this doest garuntee that individual
values are also not zero.
so in this case when it goes to else clause sql server experience a divide
by zero error coz what it will try to do is this
sum( somethingvalue / somenonzerovalue + somevalue/ 0 + somevalue/somevalue)
i hope this is clear enough.
You gotta design your query in a better way. if you are having a hard time
try to post your FULL query and some1 would be able to help you out.
regards
Abhishek
"ITDUDE27" wrote:
> I am getting the same result pandey, I might have left that of by accident
. I
> have tried it many ways.
> case when sum (dids.supplier_cost) = '0'
> then '0'
> when sum (dids.supplier_cost) IS NULL
> then '0'
> when sum (dihs.qty_total) = '0'
> then '0'
> when sum (dihs.qty_total) IS NULL
> then '0'
> WHEN SUM (dids.revenue) = '0'
> THEN '0'
> WHEN SUM (dids.revenue) IS NULL
> THEN '0'
> else sum (dids.revenue/((dihs.qty_total) * dids.supplier_cost))
> end [dollar_turns],
> "Abhishek Pandey" wrote:
>
Showing posts with label sum. Show all posts
Showing posts with label sum. Show all posts
Tuesday, March 27, 2012
Friday, March 23, 2012
Help with accessing data from a webform please...
Ok,
I have a web app that calls several very complex (at least to me) sql
queries like this for example:
SELECT t1.MemberId, t1.PeriodID,
SUM(CASE WHEN t2.amountTypeId = 7 THEN t2.amount WHEN
t2.amountTypeId = 23 THEN -t2.amount END) AS Purchase,
SUM(CASE WHEN t2.amountTypeId = 8 THEN t2.amount WHEN
t2.amountTypeId = 24 THEN -t2.amount END) AS Matrix,
SUM(CASE WHEN t2.amountTypeId = 20 THEN t2.amount WHEN
t2.amountTypeId = 21 THEN -t2.amount END) AS QualiFly,
SUM(CASE WHEN t2.amountTypeId = 9 THEN t2.amount
WHEN t2.amountTypeId = 25 THEN -t2.amount END) AS Dist,
SUM(CASE WHEN t2.amountTypeId = 10 THEN t2.amount WHEN
t2.amountTypeId = 26 THEN -t2.amount END) AS SM,
SUM(CASE WHEN t2.amountTypeId = 11 THEN t2.amount
WHEN t2.amountTypeId = 27 THEN -t2.amount END) AS BreakAway,
SUM(CASE WHEN t2.amountTypeId = 13 THEN t2.amount WHEN
t2.amountTypeId = 14 THEN -t2.amount END) AS Transfer,
SUM(CASE WHEN t2.amountTypeId = 28 THEN t2.amount WHEN
t2.amountTypeId = 15 THEN -t2.amount END) AS Spent
FROM tblTravelDetail t1 INNER JOIN
tblTravelDetailAmount t2 ON t1.TravelDetailId =
t2.TravelDetailId INNER JOIN
tblTravelDetail t3 ON t2.TravelDetailId =
t3.TravelDetailId INNER JOIN
tblTravelDetailMember t4 ON t3.TravelDetailId =
t4.TravelDetailId INNER JOIN
tblTravelEvent t5 ON t1.TravelEventId =
t5.TravelEventId INNER JOIN
amount_type t6 ON t2.amountTypeId =
t6.amount_type_id
WHERE (t1.MemberId = 12391) AND (t2.amount <> 0)
GROUP BY t1.MemberId, t1.PeriodID...
It is so slow on our very fast servers. Would an XML connection with
the "SQL XML Support in IIS" be an option? How can I get this view to
be faster for users to get over the net?
Thanks for any help,
Trinttrint
Have you defined any indexes on the table?
Have you seen an execution plan of the query?
"trint" <trinity.smith@.gmail.com> wrote in message
news:1123496340.977863.196110@.g47g2000cwa.googlegroups.com...
> Ok,
> I have a web app that calls several very complex (at least to me) sql
> queries like this for example:
> SELECT t1.MemberId, t1.PeriodID,
> SUM(CASE WHEN t2.amountTypeId = 7 THEN t2.amount WHEN
> t2.amountTypeId = 23 THEN -t2.amount END) AS Purchase,
> SUM(CASE WHEN t2.amountTypeId = 8 THEN t2.amount WHEN
> t2.amountTypeId = 24 THEN -t2.amount END) AS Matrix,
> SUM(CASE WHEN t2.amountTypeId = 20 THEN t2.amount WHEN
> t2.amountTypeId = 21 THEN -t2.amount END) AS QualiFly,
> SUM(CASE WHEN t2.amountTypeId = 9 THEN t2.amount
> WHEN t2.amountTypeId = 25 THEN -t2.amount END) AS Dist,
> SUM(CASE WHEN t2.amountTypeId = 10 THEN t2.amount WHEN
> t2.amountTypeId = 26 THEN -t2.amount END) AS SM,
> SUM(CASE WHEN t2.amountTypeId = 11 THEN t2.amount
> WHEN t2.amountTypeId = 27 THEN -t2.amount END) AS BreakAway,
> SUM(CASE WHEN t2.amountTypeId = 13 THEN t2.amount WHEN
> t2.amountTypeId = 14 THEN -t2.amount END) AS Transfer,
> SUM(CASE WHEN t2.amountTypeId = 28 THEN t2.amount WHEN
> t2.amountTypeId = 15 THEN -t2.amount END) AS Spent
> FROM tblTravelDetail t1 INNER JOIN
> tblTravelDetailAmount t2 ON t1.TravelDetailId =
> t2.TravelDetailId INNER JOIN
> tblTravelDetail t3 ON t2.TravelDetailId =
> t3.TravelDetailId INNER JOIN
> tblTravelDetailMember t4 ON t3.TravelDetailId =
> t4.TravelDetailId INNER JOIN
> tblTravelEvent t5 ON t1.TravelEventId =
> t5.TravelEventId INNER JOIN
> amount_type t6 ON t2.amountTypeId =
> t6.amount_type_id
> WHERE (t1.MemberId = 12391) AND (t2.amount <> 0)
> GROUP BY t1.MemberId, t1.PeriodID...
> It is so slow on our very fast servers. Would an XML connection with
> the "SQL XML Support in IIS" be an option? How can I get this view to
> be faster for users to get over the net?
> Thanks for any help,
> Trint
>|||I have not defined any indexes on the table ( none are defined ). The
tables are created by another department, but I have exported all of
them as 'copies' to my local machine for my c# app development. I am
willing to go for it though and then present this, if faster, to my
boss to get the changes in the real instances of SQL Server. I will
research on how to do this now.
Any help on making this faster (or easier) is appreciated.
Thanks,
Trintsql
I have a web app that calls several very complex (at least to me) sql
queries like this for example:
SELECT t1.MemberId, t1.PeriodID,
SUM(CASE WHEN t2.amountTypeId = 7 THEN t2.amount WHEN
t2.amountTypeId = 23 THEN -t2.amount END) AS Purchase,
SUM(CASE WHEN t2.amountTypeId = 8 THEN t2.amount WHEN
t2.amountTypeId = 24 THEN -t2.amount END) AS Matrix,
SUM(CASE WHEN t2.amountTypeId = 20 THEN t2.amount WHEN
t2.amountTypeId = 21 THEN -t2.amount END) AS QualiFly,
SUM(CASE WHEN t2.amountTypeId = 9 THEN t2.amount
WHEN t2.amountTypeId = 25 THEN -t2.amount END) AS Dist,
SUM(CASE WHEN t2.amountTypeId = 10 THEN t2.amount WHEN
t2.amountTypeId = 26 THEN -t2.amount END) AS SM,
SUM(CASE WHEN t2.amountTypeId = 11 THEN t2.amount
WHEN t2.amountTypeId = 27 THEN -t2.amount END) AS BreakAway,
SUM(CASE WHEN t2.amountTypeId = 13 THEN t2.amount WHEN
t2.amountTypeId = 14 THEN -t2.amount END) AS Transfer,
SUM(CASE WHEN t2.amountTypeId = 28 THEN t2.amount WHEN
t2.amountTypeId = 15 THEN -t2.amount END) AS Spent
FROM tblTravelDetail t1 INNER JOIN
tblTravelDetailAmount t2 ON t1.TravelDetailId =
t2.TravelDetailId INNER JOIN
tblTravelDetail t3 ON t2.TravelDetailId =
t3.TravelDetailId INNER JOIN
tblTravelDetailMember t4 ON t3.TravelDetailId =
t4.TravelDetailId INNER JOIN
tblTravelEvent t5 ON t1.TravelEventId =
t5.TravelEventId INNER JOIN
amount_type t6 ON t2.amountTypeId =
t6.amount_type_id
WHERE (t1.MemberId = 12391) AND (t2.amount <> 0)
GROUP BY t1.MemberId, t1.PeriodID...
It is so slow on our very fast servers. Would an XML connection with
the "SQL XML Support in IIS" be an option? How can I get this view to
be faster for users to get over the net?
Thanks for any help,
Trinttrint
Have you defined any indexes on the table?
Have you seen an execution plan of the query?
"trint" <trinity.smith@.gmail.com> wrote in message
news:1123496340.977863.196110@.g47g2000cwa.googlegroups.com...
> Ok,
> I have a web app that calls several very complex (at least to me) sql
> queries like this for example:
> SELECT t1.MemberId, t1.PeriodID,
> SUM(CASE WHEN t2.amountTypeId = 7 THEN t2.amount WHEN
> t2.amountTypeId = 23 THEN -t2.amount END) AS Purchase,
> SUM(CASE WHEN t2.amountTypeId = 8 THEN t2.amount WHEN
> t2.amountTypeId = 24 THEN -t2.amount END) AS Matrix,
> SUM(CASE WHEN t2.amountTypeId = 20 THEN t2.amount WHEN
> t2.amountTypeId = 21 THEN -t2.amount END) AS QualiFly,
> SUM(CASE WHEN t2.amountTypeId = 9 THEN t2.amount
> WHEN t2.amountTypeId = 25 THEN -t2.amount END) AS Dist,
> SUM(CASE WHEN t2.amountTypeId = 10 THEN t2.amount WHEN
> t2.amountTypeId = 26 THEN -t2.amount END) AS SM,
> SUM(CASE WHEN t2.amountTypeId = 11 THEN t2.amount
> WHEN t2.amountTypeId = 27 THEN -t2.amount END) AS BreakAway,
> SUM(CASE WHEN t2.amountTypeId = 13 THEN t2.amount WHEN
> t2.amountTypeId = 14 THEN -t2.amount END) AS Transfer,
> SUM(CASE WHEN t2.amountTypeId = 28 THEN t2.amount WHEN
> t2.amountTypeId = 15 THEN -t2.amount END) AS Spent
> FROM tblTravelDetail t1 INNER JOIN
> tblTravelDetailAmount t2 ON t1.TravelDetailId =
> t2.TravelDetailId INNER JOIN
> tblTravelDetail t3 ON t2.TravelDetailId =
> t3.TravelDetailId INNER JOIN
> tblTravelDetailMember t4 ON t3.TravelDetailId =
> t4.TravelDetailId INNER JOIN
> tblTravelEvent t5 ON t1.TravelEventId =
> t5.TravelEventId INNER JOIN
> amount_type t6 ON t2.amountTypeId =
> t6.amount_type_id
> WHERE (t1.MemberId = 12391) AND (t2.amount <> 0)
> GROUP BY t1.MemberId, t1.PeriodID...
> It is so slow on our very fast servers. Would an XML connection with
> the "SQL XML Support in IIS" be an option? How can I get this view to
> be faster for users to get over the net?
> Thanks for any help,
> Trint
>|||I have not defined any indexes on the table ( none are defined ). The
tables are created by another department, but I have exported all of
them as 'copies' to my local machine for my c# app development. I am
willing to go for it though and then present this, if faster, to my
boss to get the changes in the real instances of SQL Server. I will
research on how to do this now.
Any help on making this faster (or easier) is appreciated.
Thanks,
Trintsql
Monday, March 12, 2012
Help w/sum pls
Hi,
I'd like to add the results of T1 and T2 in the query below but I just
cannot see to get it rt.
I tried Sum(T1) and Sum(cast(T1 as int)) but no go.
What's the trick?
TIA
Mike
DROP TABLE #testtb
CREATE TABLE #testtb (day_in datetime, day_out datetime)
INSERT INTO #testtb (day_in, day_out) VALUES ('3/30/2006', '3/31/2006')
INSERT INTO #testtb (day_in, day_out) VALUES ('2/28/2006', '3/3/2006')
INSERT INTO #testtb (day_in, day_out) VALUES ('1/2/2006', '5/5/2006')
INSERT INTO #testtb (day_in, day_out) VALUES ('3/30/2006','4/5/2006')
SELECT day_in, day_out,
CASE WHEN datediff(day, day_in, day_out) between 0 and 3 THEN 1 ELSE 0 END
AS T1,
CASE WHEN datediff(day, day_in, day_out) between 3 and 7 THEN 1 ELSE 0 END
AS T2
FROM #testtb
Results should be:
2 periods meet the condition in T1
1 period meet the conditions in T2
Therefore:
Sum of T1 = 2
Sum of T2 = 1Mike
I'm
. I got the output four rows as
t1 t2
1 1
1 0
0 1
0 0
What is supposed to be? I think for the t2 should be 2 periods as well ,
isn't?
"Mike_B" <nospam@.yahoo.com> wrote in message
news:eL%23JBQUWGHA.752@.TK2MSFTNGP02.phx.gbl...
> Hi,
> I'd like to add the results of T1 and T2 in the query below but I just
> cannot see to get it rt.
> I tried Sum(T1) and Sum(cast(T1 as int)) but no go.
> What's the trick?
> TIA
> Mike
> DROP TABLE #testtb
> CREATE TABLE #testtb (day_in datetime, day_out datetime)
> INSERT INTO #testtb (day_in, day_out) VALUES ('3/30/2006', '3/31/2006')
> INSERT INTO #testtb (day_in, day_out) VALUES ('2/28/2006', '3/3/2006')
> INSERT INTO #testtb (day_in, day_out) VALUES ('1/2/2006', '5/5/2006')
> INSERT INTO #testtb (day_in, day_out) VALUES ('3/30/2006','4/5/2006')
>
> SELECT day_in, day_out,
> CASE WHEN datediff(day, day_in, day_out) between 0 and 3 THEN 1 ELSE 0 END
> AS T1,
> CASE WHEN datediff(day, day_in, day_out) between 3 and 7 THEN 1 ELSE 0 END
> AS T2
> FROM #testtb
> --
> Results should be:
> 2 periods meet the condition in T1
> 1 period meet the conditions in T2
> Therefore:
> Sum of T1 = 2
> Sum of T2 = 1
>|||Is this what you want?
Note that BETWEEN is inclusive so the second between should
be from 4 to 7.
SELECT
SUM(CASE WHEN datediff(day, day_in, day_out) between 0 and 3 THEN 1
ELSE 0 END)
AS T1,
SUM(CASE WHEN datediff(day, day_in, day_out) between 4 and 7 THEN 1
ELSE 0 END)
AS T2
FROM #testtb|||Hi Mike,
First I think you have a logical bug. The second line you insert to the
table, has a difference of 3 days, which means it is evaluated both by T1 an
d
T2 thouse cousing the sum results of them to be T1=2 and T2=2.
Second:
you can get the sum only if you do not return the dates (otherwise you will
get a sum only for dates that are alike both for the in_day and the out_day.
the query should be like this:
SELECT
SUM(CASE WHEN datediff(day, day_in, day_out) between 0 and 3 THEN 1 ELSE 0
END)
AS T1,
SUM(CASE WHEN datediff(day, day_in, day_out) between 3 and 7 THEN 1 ELSE 0
END)
AS T2
FROM #testtb
GuyBar
"Mike_B" wrote:
> Hi,
> I'd like to add the results of T1 and T2 in the query below but I just
> cannot see to get it rt.
> I tried Sum(T1) and Sum(cast(T1 as int)) but no go.
> What's the trick?
> TIA
> Mike
> DROP TABLE #testtb
> CREATE TABLE #testtb (day_in datetime, day_out datetime)
> INSERT INTO #testtb (day_in, day_out) VALUES ('3/30/2006', '3/31/2006')
> INSERT INTO #testtb (day_in, day_out) VALUES ('2/28/2006', '3/3/2006')
> INSERT INTO #testtb (day_in, day_out) VALUES ('1/2/2006', '5/5/2006')
> INSERT INTO #testtb (day_in, day_out) VALUES ('3/30/2006','4/5/2006')
>
> SELECT day_in, day_out,
> CASE WHEN datediff(day, day_in, day_out) between 0 and 3 THEN 1 ELSE 0 EN
D
> AS T1,
> CASE WHEN datediff(day, day_in, day_out) between 3 and 7 THEN 1 ELSE 0 EN
D
> AS T2
> FROM #testtb
> --
> Results should be:
> 2 periods meet the condition in T1
> 1 period meet the conditions in T2
> Therefore:
> Sum of T1 = 2
> Sum of T2 = 1
>
>|||Between includes both start and end limit.
so fot t2 it should be 4 and 7.
Regards
Amish Shah
I'd like to add the results of T1 and T2 in the query below but I just
cannot see to get it rt.
I tried Sum(T1) and Sum(cast(T1 as int)) but no go.
What's the trick?
TIA
Mike
DROP TABLE #testtb
CREATE TABLE #testtb (day_in datetime, day_out datetime)
INSERT INTO #testtb (day_in, day_out) VALUES ('3/30/2006', '3/31/2006')
INSERT INTO #testtb (day_in, day_out) VALUES ('2/28/2006', '3/3/2006')
INSERT INTO #testtb (day_in, day_out) VALUES ('1/2/2006', '5/5/2006')
INSERT INTO #testtb (day_in, day_out) VALUES ('3/30/2006','4/5/2006')
SELECT day_in, day_out,
CASE WHEN datediff(day, day_in, day_out) between 0 and 3 THEN 1 ELSE 0 END
AS T1,
CASE WHEN datediff(day, day_in, day_out) between 3 and 7 THEN 1 ELSE 0 END
AS T2
FROM #testtb
Results should be:
2 periods meet the condition in T1
1 period meet the conditions in T2
Therefore:
Sum of T1 = 2
Sum of T2 = 1Mike
I'm
. I got the output four rows ast1 t2
1 1
1 0
0 1
0 0
What is supposed to be? I think for the t2 should be 2 periods as well ,
isn't?
"Mike_B" <nospam@.yahoo.com> wrote in message
news:eL%23JBQUWGHA.752@.TK2MSFTNGP02.phx.gbl...
> Hi,
> I'd like to add the results of T1 and T2 in the query below but I just
> cannot see to get it rt.
> I tried Sum(T1) and Sum(cast(T1 as int)) but no go.
> What's the trick?
> TIA
> Mike
> DROP TABLE #testtb
> CREATE TABLE #testtb (day_in datetime, day_out datetime)
> INSERT INTO #testtb (day_in, day_out) VALUES ('3/30/2006', '3/31/2006')
> INSERT INTO #testtb (day_in, day_out) VALUES ('2/28/2006', '3/3/2006')
> INSERT INTO #testtb (day_in, day_out) VALUES ('1/2/2006', '5/5/2006')
> INSERT INTO #testtb (day_in, day_out) VALUES ('3/30/2006','4/5/2006')
>
> SELECT day_in, day_out,
> CASE WHEN datediff(day, day_in, day_out) between 0 and 3 THEN 1 ELSE 0 END
> AS T1,
> CASE WHEN datediff(day, day_in, day_out) between 3 and 7 THEN 1 ELSE 0 END
> AS T2
> FROM #testtb
> --
> Results should be:
> 2 periods meet the condition in T1
> 1 period meet the conditions in T2
> Therefore:
> Sum of T1 = 2
> Sum of T2 = 1
>|||Is this what you want?
Note that BETWEEN is inclusive so the second between should
be from 4 to 7.
SELECT
SUM(CASE WHEN datediff(day, day_in, day_out) between 0 and 3 THEN 1
ELSE 0 END)
AS T1,
SUM(CASE WHEN datediff(day, day_in, day_out) between 4 and 7 THEN 1
ELSE 0 END)
AS T2
FROM #testtb|||Hi Mike,
First I think you have a logical bug. The second line you insert to the
table, has a difference of 3 days, which means it is evaluated both by T1 an
d
T2 thouse cousing the sum results of them to be T1=2 and T2=2.
Second:
you can get the sum only if you do not return the dates (otherwise you will
get a sum only for dates that are alike both for the in_day and the out_day.
the query should be like this:
SELECT
SUM(CASE WHEN datediff(day, day_in, day_out) between 0 and 3 THEN 1 ELSE 0
END)
AS T1,
SUM(CASE WHEN datediff(day, day_in, day_out) between 3 and 7 THEN 1 ELSE 0
END)
AS T2
FROM #testtb
GuyBar
"Mike_B" wrote:
> Hi,
> I'd like to add the results of T1 and T2 in the query below but I just
> cannot see to get it rt.
> I tried Sum(T1) and Sum(cast(T1 as int)) but no go.
> What's the trick?
> TIA
> Mike
> DROP TABLE #testtb
> CREATE TABLE #testtb (day_in datetime, day_out datetime)
> INSERT INTO #testtb (day_in, day_out) VALUES ('3/30/2006', '3/31/2006')
> INSERT INTO #testtb (day_in, day_out) VALUES ('2/28/2006', '3/3/2006')
> INSERT INTO #testtb (day_in, day_out) VALUES ('1/2/2006', '5/5/2006')
> INSERT INTO #testtb (day_in, day_out) VALUES ('3/30/2006','4/5/2006')
>
> SELECT day_in, day_out,
> CASE WHEN datediff(day, day_in, day_out) between 0 and 3 THEN 1 ELSE 0 EN
D
> AS T1,
> CASE WHEN datediff(day, day_in, day_out) between 3 and 7 THEN 1 ELSE 0 EN
D
> AS T2
> FROM #testtb
> --
> Results should be:
> 2 periods meet the condition in T1
> 1 period meet the conditions in T2
> Therefore:
> Sum of T1 = 2
> Sum of T2 = 1
>
>|||Between includes both start and end limit.
so fot t2 it should be 4 and 7.
Regards
Amish Shah
Friday, February 24, 2012
Help Setting Sum of Column
This seems like this should be easy but I'm struggling. I have an
Items_Sold table with columns for Loc_id, Item_no, Qty_sold, and
Date_sold. I also have an Item_Inv table with columns of Loc_id,
Item_no, Max_qty, and many other columns. I need to update the
Item_Inv table and set the Max_qty column to represent the total
Qty_sold for each Loc_id, Item_no in the Items_sold table. So I'll
have multiple rows of Items_sold for a location/item combo. The
Max_qty column in the Item_Inv table should equal to sum of all the
Qty_sold columns for that location/item combo. Hopefully that makes
sense. Thanks.UPDATE Item_Inv
SET Max_qty = COALESCE(
(select sum(Qty_sold) from Items_Sold as S
where Item_Inv.Loc_id = S.Loc_id
and Item_Inv.Item_no = S.Item_no), 0)
Note that the COALESCE prevents Max_qty from being assigned a NULL
when there is no matching (Loc_id, Item_no).
Roy Harvey
Beacon Falls, CT
On 28 Apr 2006 15:27:42 -0700, tony@.acslhome.com wrote:
>This seems like this should be easy but I'm struggling. I have an
>Items_Sold table with columns for Loc_id, Item_no, Qty_sold, and
>Date_sold. I also have an Item_Inv table with columns of Loc_id,
>Item_no, Max_qty, and many other columns. I need to update the
>Item_Inv table and set the Max_qty column to represent the total
>Qty_sold for each Loc_id, Item_no in the Items_sold table. So I'll
>have multiple rows of Items_sold for a location/item combo. The
>Max_qty column in the Item_Inv table should equal to sum of all the
>Qty_sold columns for that location/item combo. Hopefully that makes
>sense. Thanks.|||Thanks very much, Roy. It's perfect.
-Tony
Roy Harvey wrote:
> UPDATE Item_Inv
> SET Max_qty = COALESCE(
> (select sum(Qty_sold) from Items_Sold as S
> where Item_Inv.Loc_id = S.Loc_id
> and Item_Inv.Item_no = S.Item_no), 0)
> Note that the COALESCE prevents Max_qty from being assigned a NULL
> when there is no matching (Loc_id, Item_no).
> Roy Harvey
> Beacon Falls, CT
> On 28 Apr 2006 15:27:42 -0700, tony@.acslhome.com wrote:
>|||Here is a guess as to the DDL you did not bother to post.
CREATE TABLE Sales
(location_id INTEGER NOT NULL,
item_nbr INTEGER NOT NULL,
FOREIGN KEY (location_id, item_nbr)
REFERENCES Inventory (location_id, item_nbr),
sale_qty INTEGER NOT NULL,
sale_date DATETIME NOT NULL,
PRIMARY KEY (location_id, item_nbr, sale_date));
CREATE TABLE Inventory
(location_id INTEGER NOT NULL,
item_nbr INTEGER NOT NULL,
PRIMARY KEY (location_id, item_nbr),
max_qty INTEGER NOT NULL, -- computable!!
etc.);
Do you know the rule in RDBMS about not storing computed data? It is
redundant and such computations should be done with a VIEW.
max_qty column in the Inventory table should equal to sum of all the
sales_qty columns for that location/item combo. <<
CREATE VIEW (location_id, item_nbr, max_qty)
AS
SELECT location_id, item_nbr, SUM(sales_qty)
FROM Sales
GROUP BY location_id, item_nbr;
I think that you are thinking in terms an old file system in which the
Sales would be in one file and then merged into the Inventory file in a
batch. In an RDBMS, the tables are part of the WHOLE schema, and not
disjoint islands of data, like a file system. Think in relational
terms, not sequential processing and tape file merging.
Items_Sold table with columns for Loc_id, Item_no, Qty_sold, and
Date_sold. I also have an Item_Inv table with columns of Loc_id,
Item_no, Max_qty, and many other columns. I need to update the
Item_Inv table and set the Max_qty column to represent the total
Qty_sold for each Loc_id, Item_no in the Items_sold table. So I'll
have multiple rows of Items_sold for a location/item combo. The
Max_qty column in the Item_Inv table should equal to sum of all the
Qty_sold columns for that location/item combo. Hopefully that makes
sense. Thanks.UPDATE Item_Inv
SET Max_qty = COALESCE(
(select sum(Qty_sold) from Items_Sold as S
where Item_Inv.Loc_id = S.Loc_id
and Item_Inv.Item_no = S.Item_no), 0)
Note that the COALESCE prevents Max_qty from being assigned a NULL
when there is no matching (Loc_id, Item_no).
Roy Harvey
Beacon Falls, CT
On 28 Apr 2006 15:27:42 -0700, tony@.acslhome.com wrote:
>This seems like this should be easy but I'm struggling. I have an
>Items_Sold table with columns for Loc_id, Item_no, Qty_sold, and
>Date_sold. I also have an Item_Inv table with columns of Loc_id,
>Item_no, Max_qty, and many other columns. I need to update the
>Item_Inv table and set the Max_qty column to represent the total
>Qty_sold for each Loc_id, Item_no in the Items_sold table. So I'll
>have multiple rows of Items_sold for a location/item combo. The
>Max_qty column in the Item_Inv table should equal to sum of all the
>Qty_sold columns for that location/item combo. Hopefully that makes
>sense. Thanks.|||Thanks very much, Roy. It's perfect.
-Tony
Roy Harvey wrote:
> UPDATE Item_Inv
> SET Max_qty = COALESCE(
> (select sum(Qty_sold) from Items_Sold as S
> where Item_Inv.Loc_id = S.Loc_id
> and Item_Inv.Item_no = S.Item_no), 0)
> Note that the COALESCE prevents Max_qty from being assigned a NULL
> when there is no matching (Loc_id, Item_no).
> Roy Harvey
> Beacon Falls, CT
> On 28 Apr 2006 15:27:42 -0700, tony@.acslhome.com wrote:
>|||Here is a guess as to the DDL you did not bother to post.
CREATE TABLE Sales
(location_id INTEGER NOT NULL,
item_nbr INTEGER NOT NULL,
FOREIGN KEY (location_id, item_nbr)
REFERENCES Inventory (location_id, item_nbr),
sale_qty INTEGER NOT NULL,
sale_date DATETIME NOT NULL,
PRIMARY KEY (location_id, item_nbr, sale_date));
CREATE TABLE Inventory
(location_id INTEGER NOT NULL,
item_nbr INTEGER NOT NULL,
PRIMARY KEY (location_id, item_nbr),
max_qty INTEGER NOT NULL, -- computable!!
etc.);
Do you know the rule in RDBMS about not storing computed data? It is
redundant and such computations should be done with a VIEW.
max_qty column in the Inventory table should equal to sum of all the
sales_qty columns for that location/item combo. <<
CREATE VIEW (location_id, item_nbr, max_qty)
AS
SELECT location_id, item_nbr, SUM(sales_qty)
FROM Sales
GROUP BY location_id, item_nbr;
I think that you are thinking in terms an old file system in which the
Sales would be in one file and then merged into the Inventory file in a
batch. In an RDBMS, the tables are part of the WHOLE schema, and not
disjoint islands of data, like a file system. Think in relational
terms, not sequential processing and tape file merging.
Subscribe to:
Posts (Atom)