 |
| 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
|
|
|
|

December 8th, 2003, 04:58 PM
|
|
Authorized User
|
|
Join Date: Dec 2003
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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
|
|

December 8th, 2003, 05:05 PM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 196
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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
|
|

December 8th, 2003, 05:06 PM
|
|
Authorized User
|
|
Join Date: Dec 2003
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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.
|
|
|

December 8th, 2003, 05:14 PM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 196
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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
|
|

December 8th, 2003, 05:54 PM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 196
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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
|
|

December 8th, 2003, 06:10 PM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 839
Thanks: 0
Thanked 1 Time in 1 Post
|
|
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
|
|

December 9th, 2003, 02:41 AM
|
|
Registered User
|
|
Join Date: Nov 2003
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
SELECT ZipCode, COUNT(*) FROM view GROUP BY Zipcode
|
|

December 9th, 2003, 11:17 AM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 196
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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
|
|

December 9th, 2003, 11:39 AM
|
|
Friend of Wrox
|
|
Join Date: Jun 2003
Posts: 839
Thanks: 0
Thanked 1 Time in 1 Post
|
|
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
|
|
 |