Hi there,
I have a DB with 90,000 records and I want to count the number of distinct postcodes (fieldname=postcode) and then group them by area(fieldname=Spare2).
The ideal output would be two columns, one column with area(Spare2), and the other column a count of the unique postcodes for that area.
Any help would be appreciated, as I can get a list of distinct postcodes (around 7,500), but can't work out how to then group them by area, and display them.
Many thanks, and Happy Christmas by the way!
11 3448
Use your distinct query in another query.
Sorry - I am a total newbie - how do you run a query within a query?
To embed a query within a query, just use parentheses.
SELECT This, That, (SELECT TheOtherThing FROM Here WHERE This = 'That') As 'Result'
FROM ThisPlace
WHERE Something = 'Nothing'
You can place an entire query within another query that way, as long as it only returns one value.
Thanks for that BrightShadow - it's close but I want the second column to be simply a total of the distinct postcodes within that area, so Area 1 has 1200 distinct postcodes, Area 2 has 1100 etc.
I tried your query and as you say it lists all the distinct postcodes and then has a column listing what area it is in - therefore there are 7500 records.
The query I need would only have 5 records - a total of the distinct postcodes for each of the 5 areas.
Thanks for your help once again, it is appreciated.
[Completely insane post edited out!]
See post below. [Mods, feel free to remove this one!]
Oh...Wait a minute. I think -maybe- I was overcomplicating things.
How about this?
SELECT DISTINCT Spare2, COUNT(postcode)
FROM Table1
GROUP BY Spare2
We're close now, but the figures don't add up - there are now 5 areas listed in the first column (from spare2) but the totals in the second column (titled Expr1001) are incorrect.
It says Area one has 12977 distinct postcodes, but there are only 7500 distinct postcodes in total in the DB.
Any ideas?
Ohh! Yeah, I see what it's doing there. It's counting the records per area but not the distinct records.
What you'd need is something more along the lines of a subquery that returns a distinct list of areas and postcodes (like the original one I gave you) and put that inside a query that just counts the postcodes grouped by area.
I'm honestly better at this with MS SQL than Access; Access's SQL implementation is a bit less flexible and I'm totally unfamiliar with the "design view" query method, as it really doesn't make a lick of sense to me. Can anyone that's a bit better with Access SQL chime in and point out how this should be structured?
You can either do: -
SELECT Count(PostCode) AS CountOfPostCode, Spare2
-
FROM (SELECT DISTINCT PostCode, Spare2 FROM TableName) AS x
-
GROUP BY Spare2;
-
Or
Query1: -
SELECT DISTINCT Postcode, Spare2
-
FROM TableName;
-
Query2: -
SELECT Count(PostCode) As CountOfPostCode, Spare2
-
FROM Query1
-
GROUP BY Spare2;
-
Thanks Rabbit
That was exactly what I was looking for. I used the first combined query and got the right results.
I'm glad I found this forum - it's really helpful.
Happy Christmas folks!
Thanks also to brightshadow for the help!
Thanks Rabbit
That was exactly what I was looking for. I used the first combined query and got the right results.
I'm glad I found this forum - it's really helpful.
Happy Christmas folks!
Thanks also to brightshadow for the help!
Not a problem.
They're both basically the same thing. The second approach was what I was suggesting the first time around because it's easier to understand the two queries separately. Most people have trouble with subqueries at first. It's a bit of a learning curve.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Paxton |
last post by:
Hi,
I'm trying to display the total page views per page within a given date
range, but the correct SQL is seemingly beyond me. I get the correct result
with a straightforward Group By and Count...
|
by: Sean C. |
last post by:
Helpful folks,
Most of my previous experience with DB2 was on s390 mainframe systems
and the optimizer on this platform always seemed very predictable and
consistent. Since moving to a WinNT/UDB...
|
by: Michael John |
last post by:
Dear Oracle Developers,
my task is to make up a Oracle View from a Pivot table in MS Access.
Given are two tables to join:
T_FIRM:
FIRM_ABBR VARCHAR2(3 BYTE),
FIRM_LONG ...
|
by: orekinbck |
last post by:
Hi There
Our test database has duplicate data:
COMPANYID COMPANYNAME
1 Grupple Group
2 Grupple Group
5 Grupple Group
3 Yada Inc
4 Yada...
|
by: Larry B via DotNetMonster.com |
last post by:
Hello,
I seem to be having a problem with inserting data, from a web form, into an
access database. I have tried
to do the same thing in a regular windows app, there it works fine, but when
I...
|
by: s_wadhwa |
last post by:
SELECT DISTINCTROW "01C" AS dummy, Buildings.BuildingNumber,
UCASE(Buildings.BuildingName) AS BuildingName,
Buildings.MasterPlanCode, Buildings.UniformBuildingCode,...
|
by: MMcCarthy |
last post by:
To view Access queries in SQL rather than Access query design - open the query design window and change the view to SQL:
Select Statement
SELECT FROM ;
Append Statement
INSERT INTO (, , )...
|
by: SQL Learner |
last post by:
Hi all,
I have an Access db with two large tables - 3,100,000 (tblA) and 7,000
(tblB) records. I created a select query using Inner Join by partial
matching two fields (X from tblA and Y from...
|
by: Halimat |
last post by:
I have a table in MS Access and ran a query to return some distinct record, it works fine but a similar table in Oracle 9i using similar query (sql) failed. Find below the MS Access Orignal table,...
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
| | |