473,326 Members | 2,076 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Access newbie needs help - Distinct, Group By Query.

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!
Dec 21 '07 #1
11 3448
Rabbit
12,516 Expert Mod 8TB
Use your distinct query in another query.
Dec 21 '07 #2
Sorry - I am a total newbie - how do you run a query within a query?
Dec 21 '07 #3
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.
Dec 21 '07 #4
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.
Dec 21 '07 #5
[Completely insane post edited out!]

See post below. [Mods, feel free to remove this one!]
Dec 21 '07 #6
Oh...Wait a minute. I think -maybe- I was overcomplicating things.

How about this?

SELECT DISTINCT Spare2, COUNT(postcode)
FROM Table1
GROUP BY Spare2
Dec 21 '07 #7
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?
Dec 21 '07 #8
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?
Dec 21 '07 #9
Rabbit
12,516 Expert Mod 8TB
You can either do:
Expand|Select|Wrap|Line Numbers
  1. SELECT Count(PostCode) AS CountOfPostCode, Spare2
  2. FROM (SELECT DISTINCT PostCode, Spare2 FROM TableName) AS x
  3. GROUP BY Spare2;
  4.  
Or

Query1:
Expand|Select|Wrap|Line Numbers
  1. SELECT DISTINCT Postcode, Spare2
  2. FROM TableName;
  3.  
Query2:
Expand|Select|Wrap|Line Numbers
  1. SELECT Count(PostCode) As CountOfPostCode, Spare2
  2. FROM Query1
  3. GROUP BY Spare2;
  4.  
Dec 21 '07 #10
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!
Dec 21 '07 #11
Rabbit
12,516 Expert Mod 8TB
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.
Dec 21 '07 #12

Sign in to post your reply or Sign up for a free account.

Similar topics

2
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...
14
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...
4
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 ...
3
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...
7
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...
8
by: s_wadhwa | last post by:
SELECT DISTINCTROW "01C" AS dummy, Buildings.BuildingNumber, UCASE(Buildings.BuildingName) AS BuildingName, Buildings.MasterPlanCode, Buildings.UniformBuildingCode,...
4
MMcCarthy
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 (, , )...
27
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...
3
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,...
0
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...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
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...
1
isladogs
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...
0
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...
0
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...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
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...
0
isladogs
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...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.