Database Management
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsDatabasesDatabase Management

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Stop making mediocre tutorials.The best tutorials are video! Camtasia Studio makes it easy to create engaging, buzz-building screen videos at any size, in any popular format. Download the free trial!
  #1  
Old July 29th, 2003, 06:25 PM
Jeffy the Body Jeffy the Body is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Posts: 6 Jeffy the Body User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
SQL statement on SQL Server 2000

Can anyone figure out why these statements return totally different results?

'This Query Returns 0
SELECT SUM(LIFE_GDC) AS Expr1
FROM sysdba.C_WBY_TMAGICPROD
WHERE (PROD_DAY > '12/31/2002') AND (UPPER(CONTACTID) = 'C6UJ9A00347P')

'This Query Returns -1.81898940354586E-12
SELECT SUM(LIFE_GDC) AS Expr1
FROM sysdba.C_WBY_TMAGICPROD
WHERE (PROD_DAY > '12/31/2002') AND (CONTACTID = 'C6UJ9A00347P')

Running either one of these queries with a SELECT * in place of line 1 shows you the same 31 Records in the table. The data in the LIFE_GDC column is all zeroes with the exception of 4 records with 6895.46, -22233.2, 6815.64, and 8522.1 as the only non zero values. Clearly they add up to zero, yet if I don't have that UPPER applied to my CONTACTID I get erroneous results. Is this a Microsoft bug?

Reply With Quote
  #2  
Old July 29th, 2003, 10:32 PM
thedude thedude is offline
The Dude Abides
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Feb 2000
Location: grass valley,ca
Posts: 1,063 thedude User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 9 h 4 m 29 sec
Reputation Power: 10
You might have missed that this is a forum for MySQL

Reply With Quote
  #3  
Old July 30th, 2003, 08:43 AM
Jeffy the Body Jeffy the Body is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Posts: 6 Jeffy the Body User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Does that matter?

I am a little slow but why does that matter? Wouldn't queries in MySQL and SQL 2000 be similiar?
Thanks!

Reply With Quote
  #4  
Old July 30th, 2003, 09:26 AM
Moose Man Moose Man is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Posts: 4 Moose Man User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
The addition of 'upper' will convert any contact IDs that are in lower case to upper case. Check if any are in lower as the bottom query will exclude those in lower case.

Not sure if that resolves it or not.

Reply With Quote
  #5  
Old July 30th, 2003, 09:45 AM
karsh44's Avatar
karsh44 karsh44 is offline
Just another guy
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Jun 2003
Location: Wisconsin
Posts: 2,915 karsh44 User rank is Second Lieutenant (5000 - 10000 Reputation Level)karsh44 User rank is Second Lieutenant (5000 - 10000 Reputation Level)karsh44 User rank is Second Lieutenant (5000 - 10000 Reputation Level)karsh44 User rank is Second Lieutenant (5000 - 10000 Reputation Level)karsh44 User rank is Second Lieutenant (5000 - 10000 Reputation Level)karsh44 User rank is Second Lieutenant (5000 - 10000 Reputation Level)karsh44 User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 13 h 6 m 22 sec
Reputation Power: 76
Well, for one, those are not totally different results.
-1.81898940354586E-12 is basically 0.
I'm not sure why the difference occurs. The way you have written your query shows that the id is all uppercase anyway, which is all that the Upper() function will do for you. Purely speculating now, you often find that computers don't really store '0' as '0', which is why you get those little tiny fractions, which are functionally equivalent to 0. When you add the Upper() to the query, the program may truncate the numbers in the table for some reason, before running the sum. This could explain the difference, since without the Upper(), the raw numbers are used without any prior work being done.
Anyway, thats just my conjecture.
HTH
Dave
__________________
--Dave--

U2kgSG9jIExlZ2VyZSBTY2lzLCBOaW1pdW0gRXJ1ZGl0aW9uaXMgSGFiZXM=

Reply With Quote
  #6  
Old July 30th, 2003, 09:48 AM
Jeffy the Body Jeffy the Body is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Posts: 6 Jeffy the Body User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
UPPER ContactIDs

Thanks Mooseman.
It doesn't appear that any of the ContactIDs are anything but a mixture of Upper Case Characters and Numerals. The wierd thing though is that when you run the queries the order of the returned records are different. It appears that SQL thinks the Contact IDs are different. But what doesn't make sense to me is why would summing be different on the exact same record set depending on which order you sum them?

Reply With Quote
  #7  
Old July 30th, 2003, 09:55 AM
Jeffy the Body Jeffy the Body is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Posts: 6 Jeffy the Body User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Karsh Reply

Thanks Karsh,
The only flaw I can see with your thoughts are that the Upper is being performed on the ContactID, not in the actual sum. Basically, I see it mentally as "Find these records with UPPER(CONTACTID) =..." and now sum them. It seems like the order in which the records are summed makes a difference when it shouldn't.

Reply With Quote
  #8  
Old July 30th, 2003, 10:11 AM
karsh44's Avatar
karsh44 karsh44 is offline
Just another guy
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Jun 2003
Location: Wisconsin
Posts: 2,915 karsh44 User rank is Second Lieutenant (5000 - 10000 Reputation Level)karsh44 User rank is Second Lieutenant (5000 - 10000 Reputation Level)karsh44 User rank is Second Lieutenant (5000 - 10000 Reputation Level)karsh44 User rank is Second Lieutenant (5000 - 10000 Reputation Level)karsh44 User rank is Second Lieutenant (5000 - 10000 Reputation Level)karsh44 User rank is Second Lieutenant (5000 - 10000 Reputation Level)karsh44 User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 13 h 6 m 22 sec
Reputation Power: 76
The Upper is being done before the Sum is calculated, making a sort of temporary table where the Sum is calculated. I'm guessing thats where the truncating occurs. For the statement without the Upper, the Sum is calculated from the original table.

Reply With Quote
  #9  
Old July 30th, 2003, 10:23 AM
Jeffy the Body Jeffy the Body is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Posts: 6 Jeffy the Body User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Karsh Reply, again

Thanks Karsh,
That makes sense now. I guess I need to find out what's getting into my base table. I am importing decimal data up to 2 places as far as I know. Therefore, the sum being close to zero but not zero doesn't make sense. I'll check further.

Reply With Quote
  #10  
Old July 30th, 2003, 12:47 PM
karsh44's Avatar
karsh44 karsh44 is offline
Just another guy
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Jun 2003
Location: Wisconsin
Posts: 2,915 karsh44 User rank is Second Lieutenant (5000 - 10000 Reputation Level)karsh44 User rank is Second Lieutenant (5000 - 10000 Reputation Level)karsh44 User rank is Second Lieutenant (5000 - 10000 Reputation Level)karsh44 User rank is Second Lieutenant (5000 - 10000 Reputation Level)karsh44 User rank is Second Lieutenant (5000 - 10000 Reputation Level)karsh44 User rank is Second Lieutenant (5000 - 10000 Reputation Level)karsh44 User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 13 h 6 m 22 sec
Reputation Power: 76
Checking is good, but you just need to remember that because you think/see 2 decimal places, the computer actually has more stored. If it doesn't cause problems, I would say just leave the Upper() statement in, and accept the 0 you get that way.
Good luck

Reply With Quote
  #11  
Old July 30th, 2003, 10:41 PM
r937's Avatar
r937 r937 is offline
SQL Consultant
Dev Shed God 25th Plane (17000 - 17499 posts)
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 17,343 r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 3 Weeks 5 Days 8 h 5 m 22 sec
Reputation Power: 891
i should like to see the 31 records for myself

surely that's not too many for you to paste here?

that would eliminate a lot of guessing

i'm wondering if thedude has noticed this isn't the mysql forum...

Reply With Quote
  #12  
Old July 31st, 2003, 06:29 AM
karsh44's Avatar
karsh44 karsh44 is offline
Just another guy
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Jun 2003
Location: Wisconsin
Posts: 2,915 karsh44 User rank is Second Lieutenant (5000 - 10000 Reputation Level)karsh44 User rank is Second Lieutenant (5000 - 10000 Reputation Level)karsh44 User rank is Second Lieutenant (5000 - 10000 Reputation Level)karsh44 User rank is Second Lieutenant (5000 - 10000 Reputation Level)karsh44 User rank is Second Lieutenant (5000 - 10000 Reputation Level)karsh44 User rank is Second Lieutenant (5000 - 10000 Reputation Level)karsh44 User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 13 h 6 m 22 sec
Reputation Power: 76
Posting the records definitely could help.
(And it was in the mysql forum, it was moved)

Reply With Quote
  #13  
Old July 31st, 2003, 06:38 AM
r937's Avatar
r937 r937 is offline
SQL Consultant
Dev Shed God 25th Plane (17000 - 17499 posts)
 
Join Date: Feb 2003
Location: Toronto Canada
Posts: 17,343 r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level)r937 User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 3 Weeks 5 Days 8 h 5 m 22 sec
Reputation Power: 891
doh!

sorry, that certainly would explain thedude's remark

sorry

Reply With Quote
  #14  
Old July 31st, 2003, 09:33 AM
Jeffy the Body Jeffy the Body is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Posts: 6 Jeffy the Body User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Wierd Sum Results

Here is the final outcome. We are using SalesLogix 5.2 on SQL Server 2000. We have a table, TMAGICPROD, that stores production data. The guy who helped us set up the database recommended Long Integer fields for the sales data initially. Then, when rounding and decimals were an issue, had us change to Float data. Turns out that with Float data, you can enter a value as 6895.46 and SQL Server puts it in the fields something like 6895.4600000000181898940354586
Go figure. (Please read next post for additional info.)
Anyway, the fix was to change the Float fields to what SalesLogix calls Currency fields, same as Numeric(15,2), which should handle our needs for Sales fields that are actually dollars and cents anyway, with plenty of accuracy.
The reason the upper worked was as Karsh said, that function truncates the value in a temp table, therefore chopping of the non zero data.
Thanks for your help everyone!

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesDatabase Management > SQL statement on SQL Server 2000


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off