MS SQL Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsDatabasesMS SQL Development

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:
  #1  
Old July 16th, 2004, 11:42 AM
Gnosis Gnosis is offline
Sellout
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2003
Posts: 205 Gnosis User rank is Corporal (100 - 500 Reputation Level)Gnosis User rank is Corporal (100 - 500 Reputation Level)Gnosis User rank is Corporal (100 - 500 Reputation Level)Gnosis User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 2 Days 10 h 14 m 58 sec
Reputation Power: 6
Record count should be null. Why 1?

I have a table that contains, among other things, customerid and orderid.

I need to get the last order that a customer made, if any. It's quite possible that they have never ordered.

I'm using the following SQL:
select max(orderid) as lastOrder from orders where customerid='#userid#';

If the customerid isn't in the table (never ordered before), I would expect a record count of 0 or null. However, I'm getting a record count of 1 with a value of [empty string] in lastOrder.

What gives?

I need to run another query if the customer has never ordered, but the other query isn't being run because the first one is finding a result.

Any ideas how I can do this differently or better?

Thanks,
G

Reply With Quote
  #2  
Old July 16th, 2004, 12:38 PM
victorpendleton victorpendleton is offline
Contributing User
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Jan 2003
Location: No es importante
Posts: 2,065 victorpendleton User rank is Private First Class (20 - 50 Reputation Level)victorpendleton User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 6 h 44 m 30 sec
Reputation Power: 8
Max and Min will return one row regards. You should try using count instead.
__________________
El éxito consiste en una serie de pequeñas victorias día a día

MySQL, MS SQL, MS ACCESS, Oracle Database Manager - http://victorpendleton.net/products/psdviewer.html

Reply With Quote
  #3  
Old July 16th, 2004, 12:58 PM
dcaillouet's Avatar
dcaillouet dcaillouet is offline
Big Endian
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: May 2001
Location: Fly-over country
Posts: 1,173 dcaillouet User rank is Sergeant (500 - 2000 Reputation Level)dcaillouet User rank is Sergeant (500 - 2000 Reputation Level)dcaillouet User rank is Sergeant (500 - 2000 Reputation Level)dcaillouet User rank is Sergeant (500 - 2000 Reputation Level)dcaillouet User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 16 h 29 m 5 sec
Reputation Power: 24
The following should return a single record with the maximum orderid or an empty recordset:

SELECT TOP 1 orderid AS lastOrder
FROM orders
WHERE customerid='#userid#'
ORDER BY orderid DESC

Reply With Quote
  #4  
Old July 16th, 2004, 05:35 PM
Gnosis Gnosis is offline
Sellout
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2003
Posts: 205 Gnosis User rank is Corporal (100 - 500 Reputation Level)Gnosis User rank is Corporal (100 - 500 Reputation Level)Gnosis User rank is Corporal (100 - 500 Reputation Level)Gnosis User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 2 Days 10 h 14 m 58 sec
Reputation Power: 6
Ok. I like that solution.

My current solution is to test the result to make sure it's an INT, set a boolean, and switch on that.

It'll save a couple steps to just have a proper query.

Thanks all who answered!

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesMS SQL Development > Record count should be null. Why 1?


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
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway