473,394 Members | 1,802 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,394 software developers and data experts.

Getting a count on one table using data from another table.

Firstly, thanks in advance for any assistance, it is much appriciated.

I have two tables, structure as follows:

TBL_FANS

Column | Example
--------------------------------------
Fan_ID | 256
Code_Prefix | ABC
Email | email@hotmail.com
Postcode | 2015
--------------------------------------

TBL_POSTCODES

Column | Example
--------------------------------------
Postcode | 2015
State | NSW
--------------------------------------

I need to get a count of what state all of the fans are in so that I can use this data in a charting package to show the location of the fans. The data will need to be in this format

State | Count
--------------------------------------
NSW | 125
QLD | 75
VIC | 100
Other | 20

If the query does not find a match to the Fans Postcode in TBL_POSTCODES then it should be added to a new row named other.

Any ideas on how I go about accomplishing this?
Apr 2 '09 #1
5 2807
ck9663
2,878 Expert 2GB
What do you have so far?

---- CK
Apr 2 '09 #2
I am only new to SQL & I have tried a million different things, nothing that even comes close to working :(
Apr 3 '09 #3
Ok I have made some progress with the following query:
----
Select State, Count(*) AS Count
From tbl_postcodes
INNER JOIN
tbl_fans ON tbl_postcodes.Postcode = tbl_fans.Fan_Postcode

WHERE Code_Prefix = 'WHATEVER'

Group By State
----

This 'seems' to work, however I need to add the postcodes that do not return a match to a count called 'Other'. as per below:

State | Count
--------------------------------------
NSW | 125
QLD | 75
VIC | 100
*Other | 20
Apr 3 '09 #4
You should be able to get what you are after with a LEFT JOIN rather than INNER JOIN

Something Like:

Expand|Select|Wrap|Line Numbers
  1. Select isnull(State,'Other'), Count(*) AS Count
  2. From tbl_fans
  3. LEFT JOIN
  4. tbl_postcodes ON tbl_postcodes.Postcode = tbl_fans.Fan_Postcode
  5.  
  6. WHERE Code_Prefix = 'WHATEVER'
  7.  
  8. Group By State
Apr 3 '09 #5
Thanks for the info, I have tested the above and cannot seem to get it to work.

If the postcode field contains an overseas Postcode/ZIP (say 90210) for example this record is not returned in the count, even though there is definetely no match in tbl_postcodes. The rest of the count remains correct however.

Sample Output:

NSW 7
QLD 20
SA 2
VIC 2
WA 1
Apr 3 '09 #6

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

Similar topics

2
by: Gregory.Spencer | last post by:
Help, I have a query in MySQL which gets the details of members of a club. e.g. Select * from members. however, in the same query I want to return the amount of "functions" the member has...
15
by: U N Me | last post by:
I have a continuous form that lists records from a query. One of the columns is an amount field, In the footer of the form I place a text box that displays the total (sum) of the amount. The...
2
by: Joe | last post by:
Hi All, I am new to using the Access DB and I need some help if someone is able to give it to me. What I want to do is get the names of the columns of certain tables. Not the data in the table...
4
by: m_houllier | last post by:
STUDENT TABLE StudentReference Student Name etc ATTENDANCE TABLE AttendanceID CourseID StudentReference
6
by: tshad | last post by:
I need to get to a status label I have on my footer section of my datalist. There is no event happening that would go to the footer. I am just doing some processing and want to update the label...
1
by: Gunjan Garg | last post by:
Hello All, I am working to create a generic datagrid which accepts a datasource(ListData - This is our own datatype) and depending on the calling program customizes itself for sorting,...
4
by: Sean Shanny | last post by:
To all, Running into an out of memory error on our data warehouse server. This occurs only with our data from the 'September' section of a large fact table. The exact same query running over...
7
by: No bother | last post by:
I have a table which has, among other fields, a date field. I want to get a count of records where certain criteria are met for, say, three days in a row. For example: NumWidgets Date...
1
by: raghuvendra | last post by:
Hi I have a jsp page with 4 columns: namely Category name , Category order, Input field and a submit button. All these are aligned in a row. And Each Category Name has its corresponding Category...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.