December 8, 2014 at 8:11 am
Can anyone advice how to do this. I am looking to count the account number per zipcode of a state.
December 8, 2014 at 8:25 am
Try:
SELECT
State
,ZipCode
,Count(Account) AS [Accounts]
FROM YourTable
GROUP BY State, ZipCode;
For best practices on asking questions, please read the following article: Forum Etiquette: How to post data/code on a forum to get the best help[/url]
December 8, 2014 at 8:37 am
Window function version of Alvin's code
😎
SELECT
State
,ZipCode
,Count(Account) OVER
(
PARTITION BY State, ZipCode
) AS [Accounts]
FROM YourTable;
December 8, 2014 at 8:42 am
Eirikur Eiriksson (12/8/2014)
Window function version of Alvin's code😎
SELECT
State
,ZipCode
,Count(Account) OVER
(
PARTITION BY State, ZipCode
) AS [Accounts]
FROM YourTable;
That's probably over-complicating it for his homework. 😉
For best practices on asking questions, please read the following article: Forum Etiquette: How to post data/code on a forum to get the best help[/url]
December 8, 2014 at 8:43 am
Thanks guys was what I was looking for.
December 8, 2014 at 8:47 am
cbrammer1219 (12/8/2014)
I have that, I would like to get the totals for example, Meaning that there are that many accounts in that zipcode.ZipCode Count
02149 20
02156 2
01234 10
I don't understand what you are asking for. As I see it, you have what you asked for.
Can you word you request differently?
For best practices on asking questions, please read the following article: Forum Etiquette: How to post data/code on a forum to get the best help[/url]
December 8, 2014 at 8:48 am
If you already have a summary of accounts by zip code, then you don't want to COUNT them, you want to SUM them instead. So use Alvin's query with a SUM instead of a COUNT function.
December 8, 2014 at 9:55 pm
But that way, the teacher will know he was cheating, because he won't be able to explain how it works.
December 10, 2014 at 9:49 am
Or use the ROLLUP option
SELECT
State
,ZipCode
,Count(Account) AS [Accounts]
FROM YourTable
GROUP BY State, ZipCode
WITH ROLLUP;
This will give a total for the State (record will be State, NULL, countTotal) and a total for the whole dataset (record will be NULL,NULL,countTotal)
Viewing 9 posts - 1 through 8 (of 8 total)
You must be logged in to reply to this topic. Login to reply