BorlandTalk.com Forum Index BorlandTalk.com
Borland discussion newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Sum Problem with NULL Values

 
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> Delphi Databases (SQL Servers)
View previous topic :: View next topic  
Author Message
Stefan Deutschen
Guest





PostPosted: Wed Sep 10, 2003 9:36 am    Post subject: Sum Problem with NULL Values Reply with quote



Hi folks
(i don't know if this is the right newsgroup for this question, if so,
please forgive me)

I have a table ("ORDERS") which looks like this

Order Serial FromCty ToCty Price
1000 1 washington NY 500
1000 2 washington LA 600
1001 1 washington NY 550
1002 1 washington NY 525

i want to display all the FromCty->ToCty for the user grouped with summed
prices; problem here is that i have to display From-To's in only one row. i
do this with an inner join wich goes (not exactly) like:

SELECT COUNT(a.ORDER), a.FROMCTY, a.TOCTY, b.TOCITY2
FROM ORDERS A
INNER JOIN ORDERS b ON a.ORDER = b.ORDER AND b.SERIAL = 2 AND a.TOCTY <>
b.TOCTY
GROUP BY a.FROMCTY, a.TOCTY, b.TOCTY

with this, i'd get a result set like:
Count FromCty ToCty ToCty2
1 washington NY LA
2 washington NY NULL

so far, so good

but i also need the avg. and the max price for each From-To
For the first one, avg and max would be 1100, as it is only one (i have to
add serial 2)
for the second one, avg = 537,5, max = 550

how can i get this??? i tried different things, but as i have to sum up
a.price and b.price (in case there is a serial 2) i always get SUM = NULL in
case there is no second serial, because SUM(a.PRICE) + SUM(b.PRICE) is
550+525+NULL+NULL and if you have a NULL Value in a SUM statement, the
result will be NULL.

Is there a way to use a CASE within the SUM brackets? Or a completely
different way?

Thx in advance for your help

Stefan Deutschen


Back to top
Stefan Deutschen
Guest





PostPosted: Thu Sep 11, 2003 6:27 am    Post subject: Re: Sum Problem with NULL Values Reply with quote



Microsoft SQL 2000 SP 3

"Alain Quesnel" <alainsansspam (AT) logiquel (DOT) com> schrieb im Newsbeitrag
news:3f5f7f17$1 (AT) newsgroups (DOT) borland.com...
Quote:
What database are you using?

--


Alain Quesnel
[email]alainsansspam (AT) logiquel (DOT) com[/email]

www.logiquel.com


"Stefan Deutschen" <sdeutschen (AT) fixemer (DOT) de> wrote in message
news:3f5ef091 (AT) newsgroups (DOT) borland.com...
Hi folks
(i don't know if this is the right newsgroup for this question, if so,
please forgive me)

I have a table ("ORDERS") which looks like this

Order Serial FromCty ToCty Price
1000 1 washington NY 500
1000 2 washington LA 600
1001 1 washington NY 550
1002 1 washington NY 525

i want to display all the FromCty->ToCty for the user grouped with
summed
prices; problem here is that i have to display From-To's in only one
row.
i
do this with an inner join wich goes (not exactly) like:

SELECT COUNT(a.ORDER), a.FROMCTY, a.TOCTY, b.TOCITY2
FROM ORDERS A
INNER JOIN ORDERS b ON a.ORDER = b.ORDER AND b.SERIAL = 2 AND a.TOCTY
b.TOCTY
GROUP BY a.FROMCTY, a.TOCTY, b.TOCTY

with this, i'd get a result set like:
Count FromCty ToCty ToCty2
1 washington NY LA
2 washington NY NULL

so far, so good

but i also need the avg. and the max price for each From-To
For the first one, avg and max would be 1100, as it is only one (i have
to
add serial 2)
for the second one, avg = 537,5, max = 550

how can i get this??? i tried different things, but as i have to sum up
a.price and b.price (in case there is a serial 2) i always get SUM =
NULL
in
case there is no second serial, because SUM(a.PRICE) + SUM(b.PRICE) is
550+525+NULL+NULL and if you have a NULL Value in a SUM statement, the
result will be NULL.

Is there a way to use a CASE within the SUM brackets? Or a completely
different way?

Thx in advance for your help

Stefan Deutschen








Back to top
Vitali Kalinin
Guest





PostPosted: Thu Sep 11, 2003 8:41 am    Post subject: Re: Sum Problem with NULL Values Reply with quote



Look for IsNull in Books Online
"Stefan Deutschen" <sdeutschen (AT) fixemer (DOT) de> сообщил/сообщила в новостях
следующее: news:3f5ef091 (AT) newsgroups (DOT) borland.com...
Quote:
Hi folks
(i don't know if this is the right newsgroup for this question, if so,
please forgive me)

I have a table ("ORDERS") which looks like this

Order Serial FromCty ToCty Price
1000 1 washington NY 500
1000 2 washington LA 600
1001 1 washington NY 550
1002 1 washington NY 525

i want to display all the FromCty->ToCty for the user grouped with summed
prices; problem here is that i have to display From-To's in only one row.
i
do this with an inner join wich goes (not exactly) like:

SELECT COUNT(a.ORDER), a.FROMCTY, a.TOCTY, b.TOCITY2
FROM ORDERS A
INNER JOIN ORDERS b ON a.ORDER = b.ORDER AND b.SERIAL = 2 AND a.TOCTY
b.TOCTY
GROUP BY a.FROMCTY, a.TOCTY, b.TOCTY

with this, i'd get a result set like:
Count FromCty ToCty ToCty2
1 washington NY LA
2 washington NY NULL

so far, so good

but i also need the avg. and the max price for each From-To
For the first one, avg and max would be 1100, as it is only one (i have to
add serial 2)
for the second one, avg = 537,5, max = 550

how can i get this??? i tried different things, but as i have to sum up
a.price and b.price (in case there is a serial 2) i always get SUM = NULL
in
case there is no second serial, because SUM(a.PRICE) + SUM(b.PRICE) is
550+525+NULL+NULL and if you have a NULL Value in a SUM statement, the
result will be NULL.

Is there a way to use a CASE within the SUM brackets? Or a completely
different way?

Thx in advance for your help

Stefan Deutschen






Back to top
Lester Hanger
Guest





PostPosted: Fri Sep 12, 2003 9:49 pm    Post subject: Re: Sum Problem with NULL Values Reply with quote

Stefan Deutschen <sdeutschen (AT) fixemer (DOT) de> wrote


Quote:
how can i get this??? i tried different things, but as i have to sum up
a.price and b.price (in case there is a serial 2) i always get SUM = NULL in
case there is no second serial, because SUM(a.PRICE) + SUM(b.PRICE) is
550+525+NULL+NULL and if you have a NULL Value in a SUM statement, the
result will be NULL.

Is there a way to use a CASE within the SUM brackets? Or a completely
different way?

You should be able to use SUM(ISNULL(a.PRICE, 0))

ISNULL(x, v) returns x if x is not NULL, else v if x is NULL.




Back to top
Robert Cerny
Guest





PostPosted: Mon Sep 15, 2003 6:20 pm    Post subject: Re: Sum Problem with NULL Values Reply with quote

Your problem is somewhere else.
1. aggregate functions skip null values
2. The query: SELECT * FROM ORDERS A INNER JOIN ORDERS b ON a.ORDER =
b.ORDER AND b.SERIAL = 2 AND a.TOCTY <> b.TOCTY
returns exacly one record, so does the aggregate query, if you remove "AND
b.SERIAL = 2" you get 2 records, aggregate query also, both records having
count 1. This is perfectly normal, since it's INNER JOIN.

3. What's the expression for max and avg? The only way you can get 1100 is
max(a.price+b.price)

You get sum=null only if all values are null.
sum(500+600+null+null)=sum(500+600)=1100
avg(500+600+null+null)=avg(500+600)=550
sum(500+600)+sum(null+null)=1100+null=null

--
Robert Cerny
DelphiShaman

"Stefan Deutschen" <sdeutschen (AT) fixemer (DOT) de> wrote

Quote:
Hi folks
(i don't know if this is the right newsgroup for this question, if so,
please forgive me)

I have a table ("ORDERS") which looks like this

Order Serial FromCty ToCty Price
1000 1 washington NY 500
1000 2 washington LA 600
1001 1 washington NY 550
1002 1 washington NY 525

i want to display all the FromCty->ToCty for the user grouped with summed
prices; problem here is that i have to display From-To's in only one row.
i
do this with an inner join wich goes (not exactly) like:

SELECT COUNT(a.ORDER), a.FROMCTY, a.TOCTY, b.TOCITY2
FROM ORDERS A
INNER JOIN ORDERS b ON a.ORDER = b.ORDER AND b.SERIAL = 2 AND a.TOCTY
b.TOCTY
GROUP BY a.FROMCTY, a.TOCTY, b.TOCTY

with this, i'd get a result set like:
Count FromCty ToCty ToCty2
1 washington NY LA
2 washington NY NULL

so far, so good

but i also need the avg. and the max price for each From-To
For the first one, avg and max would be 1100, as it is only one (i have to
add serial 2)
for the second one, avg = 537,5, max = 550

how can i get this??? i tried different things, but as i have to sum up
a.price and b.price (in case there is a serial 2) i always get SUM = NULL
in
case there is no second serial, because SUM(a.PRICE) + SUM(b.PRICE) is
550+525+NULL+NULL and if you have a NULL Value in a SUM statement, the
result will be NULL.

Is there a way to use a CASE within the SUM brackets? Or a completely
different way?

Thx in advance for your help

Stefan Deutschen







Back to top
Display posts from previous:   
Post new topic   Reply to topic    BorlandTalk.com Forum Index -> Delphi Databases (SQL Servers) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.