Wrox Programmer Forums
|
SQL Server 2000 General discussion of Microsoft SQL Server -- for topics that don't fit in one of the more specific SQL Server forums. version 2000 only. There's a new forum for SQL Server 2005.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the SQL Server 2000 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 8th, 2003, 04:58 PM
Authorized User
 
Join Date: Dec 2003
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
Default query of a query

Hello, i am stuck on a query and i have been searching non stop and cant seem to put my finger on what i need.

i have a view that is this

AcctNum Code Date Zipcode
1 32.2 010101 12345

i want to find out how many of those accounts are in each zipcode. there are 545 unique zips in the table. how would i take that list of zips and pipe it back through and get a count for each zip.

daniel

 
Old December 8th, 2003, 05:05 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 196
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Try the following:

Code:
SELECT COUNT(AcctNum), ZIPCode FROM <view name> GROUP BY ZIPCode
You will need to substitute the name of the view for <view name>.

Rand
 
Old December 8th, 2003, 05:06 PM
Authorized User
 
Join Date: Dec 2003
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
Default

thanks!!! gosh that is so embarrasing.. i need to get back into sql..thanks!!!

Quote:
quote:Originally posted by planoie
 SELECT COUNT(Zipcode) FROM view GROUP BY Zipcode

Peter
------------------------------------------------------
Work smarter, not harder.
 
Old December 8th, 2003, 05:14 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 196
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Even more readable as:

Code:
SELECT ZIPCode, COUNT(AcctNum) AS NumOfAccounts FROM <view name> GROUP BY ZIPCode
See - we are all in too much of a hurry!





Rand
 
Old December 8th, 2003, 05:54 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 196
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Since you may only wish to see and count by the first five characters in the ZIPCode

SELECT LEFT(ZIPCode, 5) AS ZIP5, COUNT(AcctNum) AS NumOfAccts FROM <view name> GROUP BY LEFT(ZIPCode , 5) ORDER BY LEFT(ZIPCode, 5)


Rand
 
Old December 8th, 2003, 06:10 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 839
Thanks: 0
Thanked 1 Time in 1 Post
Default

Quote:
quote:SELECT LEFT(ZIPCode, 5) AS ZIP5, COUNT(AcctNum) AS NumOfAccts
    FROM <view name> GROUP BY LEFT(ZIPCode , 5) ORDER BY LEFT(ZIPCode, 5)
For what it's worth, you don't need to repeat the expression in the ORDER BY clause; since you've given the column expression in the SELECT clause an alias, you can refer to it by the alias in the ORDER BY clause (note that you DO need to repeat it in the GROUP BY clause, though). Thus:
Code:
SELECT LEFT(ZIPCode, 5) AS ZIP5, COUNT(AcctNum) AS NumOfAccts
    FROM <view name> GROUP BY LEFT(ZIPCode , 5) ORDER BY ZIP5;
Jeff Mason
Custom Apps, Inc.
www.custom-apps.com
 
Old December 9th, 2003, 02:41 AM
Registered User
 
Join Date: Nov 2003
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to Ramanuj Send a message via Yahoo to Ramanuj
Default

SELECT ZipCode, COUNT(*) FROM view GROUP BY Zipcode
 
Old December 9th, 2003, 11:17 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 196
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Jeff:

It strikes me as inconsistent (and confusing) that it is acceptable to use the alias in the ORDER BY clause but that it is not acceptable to use the alias in the GROUP BY clause.

Rand
 
Old December 9th, 2003, 11:39 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 839
Thanks: 0
Thanked 1 Time in 1 Post
Default

Quote:
quote:It strikes me as inconsistent (and confusing) that it is acceptable to use the alias in the ORDER BY clause but that it is not acceptable to use the alias in the GROUP BY clause.
You won't get any argument from me about that. :)

The reason that it works that way is that column aliases don't come into scope until the resultset is constructed. This is right before the cursor that performs the ORDER BY clause is executed. The columns of the resultset are given their names either from their source as a 'table.column' reference or from the alias if it is provided. The columns as named are then passed to the cursor which performs the ordering operation. The output of the ordering is then returned to the client provider as the final resultset.

The execution of the FROM, WHERE, GROUP BY and HAVING clauses occur before the aliases come into scope, so you are forced to refer to the columns only by their 'table.column' syntax. This is also why you have to repeat any expressions (like 'LEFT(ZIPCode,5)') in those clauses.

Jeff Mason
Custom Apps, Inc.
www.custom-apps.com





Similar Threads
Thread Thread Starter Forum Replies Last Post
Output Query to txt file from SQL Query everest SQL Server 2005 4 November 22nd, 2007 01:49 AM
how to make a query from an existing query raport SQL Language 3 November 13th, 2006 08:59 PM
I solved insert query.now see this Update Query. [email protected] VB.NET 2002/2003 Basics 2 September 21st, 2006 12:48 AM
Syntax error in query. Incomplete query clause. dispickle ADO.NET 3 April 16th, 2004 01:04 PM
Error on Make-Table Query In Union Query rylemer Access 1 August 20th, 2003 07:42 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.