Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Database > SQL Language
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
SQL Language SQL Language discussions not specific to a particular RDBMS program or vendor.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Language section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old May 31st, 2005, 02:18 PM
Friend of Wrox
 
Join Date: Jun 2003
Location: , , USA.
Posts: 100
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to ~Bean~
Default Need Count Help

I am trying to return a list of all my customers in "tblCustomers" and also the number of Orders they have, and the Date of the last order. I have 2 tables, table 1 has all Customer info, and table 2 has the Orders...I am stuck on the Count...

Code:
SELECT * ,(SELECT Count(*) FROM tblOrders) As orderCnt FROM tblCustomers
this always returns the same number as "orderCnt"...I havent even gotten to the Last Date part...

help!


-------------------------
Beware of programmers with screwdrivers...
__________________
-------------------------
Beware of programmers with screwdrivers...
Reply With Quote
  #2 (permalink)  
Old May 31st, 2005, 02:24 PM
Friend of Wrox
Points: 7,395, Level: 36
Points: 7,395, Level: 36 Points: 7,395, Level: 36 Points: 7,395, Level: 36
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Capital Federal, , Argentina.
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
Send a message via MSN to gbianchi
Default

select * from
(SELECT customer, Count(*), max(date) FROM tblOrders group by customer) a,
tblcustomers b
where
b.customer = a.customer

try this.. you should check syntaxis b/c i dont know what kind of sql are you using, also i dont have exact names for the fields on the tables...


HTH

Gonzalo
Reply With Quote
  #3 (permalink)  
Old May 31st, 2005, 02:35 PM
Friend of Wrox
 
Join Date: Jun 2003
Location: Naperville, IL, USA.
Posts: 196
Thanks: 0
Thanked 0 Times in 0 Posts
Default

To get the number of orders and the date of the last order:
Code:
SELECT CustomerID, Count( * ) As OrderCnt, MAX(OrderDate) As LastOrder FROM tblOrders
GROUP BY CustomerID
Rand
Reply With Quote
  #4 (permalink)  
Old May 31st, 2005, 02:38 PM
Friend of Wrox
 
Join Date: Jun 2003
Location: , , USA.
Posts: 100
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to ~Bean~
Default

Gonzalo:
What does the "a" and "b" mean?

tables

tblCustomers:
CustomerID
CustomerName
etc.

tblOrders
OrderID
OrderDate
CustomerID
etc.




-------------------------
Beware of programmers with screwdrivers...
Reply With Quote
  #5 (permalink)  
Old May 31st, 2005, 02:43 PM
Friend of Wrox
Points: 7,395, Level: 36
Points: 7,395, Level: 36 Points: 7,395, Level: 36 Points: 7,395, Level: 36
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Capital Federal, , Argentina.
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
Send a message via MSN to gbianchi
Default

as a and as b to avoid using the full name of the tables..

in sqlserver 7 you can avoid the AS...



HTH

Gonzalo
Reply With Quote
  #6 (permalink)  
Old May 31st, 2005, 02:47 PM
Friend of Wrox
 
Join Date: Jun 2003
Location: Naperville, IL, USA.
Posts: 196
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks for more info:
Code:
SELECT c.CustomerName, o.OrderCnt, o.LastOrder FROM tblCustomers c
LEFT OUTER JOIN (SELECT CustomerID, Count( * ) As OrderCnt, MAX(OrderDate) As LastOrder FROM tblOrders
GROUP BY CustomerID) o
ON c.CustomerID = o.CustomerID
The o and the c are aliases for the table name so you don't have to write it out in full every time you reference it.

The LEFT OUTER JOIN will show customers without orders also.

Rand
Reply With Quote
  #7 (permalink)  
Old May 31st, 2005, 02:55 PM
Friend of Wrox
 
Join Date: Jun 2003
Location: , , USA.
Posts: 100
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to ~Bean~
Default

oh...k...gotcha...thx...

so i have:

SELECT * (SELECT CustomerID, Count(*), max(date) FROM tblOrders GROUP BY CustomerID) AS a, tblCustomers AS b WHERE b.CustomerID = a.CustomerID

and I get "Incorrect syntax near the keyword 'AS'." if I remove the 'AS's then I get
"Incorrect syntax near the keyword 'a'."

(I am using SQL Server 7)
??

-------------------------
Beware of programmers with screwdrivers...
Reply With Quote
  #8 (permalink)  
Old May 31st, 2005, 02:58 PM
Friend of Wrox
Points: 7,395, Level: 36
Points: 7,395, Level: 36 Points: 7,395, Level: 36 Points: 7,395, Level: 36
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: Capital Federal, , Argentina.
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
Send a message via MSN to gbianchi
Default

try with rand query.. look more nice than mine ;)

anyway, you forgot a comma and the from!!!

SELECT * FROM (SELECT CustomerID, Count(*), max(date) FROM tblOrders GROUP BY CustomerID) AS a, tblCustomers AS b WHERE b.CustomerID = a.CustomerID



HTH

Gonzalo
Reply With Quote
  #9 (permalink)  
Old May 31st, 2005, 03:18 PM
Friend of Wrox
 
Join Date: Jun 2003
Location: Naperville, IL, USA.
Posts: 196
Thanks: 0
Thanked 0 Times in 0 Posts
Default

You would need to modify your last query as follows:

Code:
SELECT * FROM (SELECT CustomerID, Count(*) AS OrderCnt, max(Orderdate) AS LastOrder 
                    FROM tblOrders 
                    GROUP BY CustomerID) AS a, tboCustomers AS b WHERE b.CustomerID = a.CustomerID
This was tested on the SQL Server Northwind sample database, so I'm certain it's correct.

ps: My earlier posting (query) also worked on Northwind.

Rand
Reply With Quote
  #10 (permalink)  
Old May 31st, 2005, 03:23 PM
Friend of Wrox
 
Join Date: Jun 2003
Location: Naperville, IL, USA.
Posts: 196
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Oops!

That should be tblCustomers instead of tboCustomers.

I mixed up your table and field names with the Northwind table and field names.

Rand
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
XSL: Count = Count + 1 elayaraja.s XSLT 3 July 18th, 2008 03:21 AM
how to count? wkm1925 SQL Server 2000 2 February 26th, 2007 11:57 AM
is there any in built function to count page count g.tamilselvan MySQL 1 February 15th, 2006 07:43 AM
Count, sum, count a value, return records CongoGrey Access 1 April 18th, 2005 02:25 PM
count() crmpicco MySQL 1 January 28th, 2005 01:44 PM



All times are GMT -4. The time now is 10:45 PM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.