473,735 Members | 2,103 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Query to display number count

Hi

I am still new to access. I want to know how i can build a query which
can display results from 4 different columns/fields

Like.

Field1 Field2 Field3 Field4
1 2 1 0
1 1 0 0
2 0 0 0
3 0 0 0

The query should output

Number Count
0 9
1 4
2 2
3 1
(i.e) Query checks for the number of occurences in each of the 4
columns and returns the count.

Please advise

Thanks !!

Dec 6 '05 #1
1 3676
su**********@gm ail.com wrote:
Hi

I am still new to access. I want to know how i can build a query which
can display results from 4 different columns/fields

Like.

Field1 Field2 Field3 Field4
1 2 1 0
1 1 0 0
2 0 0 0
3 0 0 0

The query should output

Number Count
0 9
1 4
2 2
3 1
(i.e) Query checks for the number of occurences in each of the 4
columns and returns the count.

Please advise

Thanks !!


A two query approach assuming your table is called tblMyTable (edit
text if otherwise):

qryAllNumbers:
SELECT Field1 AS AllNumbers FROM tblMyTable WHERE Field1 IS NOT NULL
UNION SELECT Field2 AS AllNumbers FROM tblMyTable WHERE Field2 IS NOT
NULL UNION SELECT Field3 AS AllNumbers FROM tblMyTable WHERE Field3 IS
NOT NULL UNION SELECT Field4 AS AllNumbers FROM tblMyTable WHERE Field4
IS NOT NULL;

!qryAllNumbers:
AllNumbers
0
1
2
3

qryCountAcrossF ields:
SELECT qryAllNumbers.A llNumbers, (SELECT Count(A.Field1) FROM
tblMyTable AS A WHERE A.Field1 = qryAllNumbers.A llNumbers) + (SELECT
Count(A.Field2) FROM tblMyTable AS A WHERE A.Field2 =
qryAllNumbers.A llNumbers) + (SELECT Count(A.Field3) FROM tblMyTable AS
A WHERE A.Field3 = qryAllNumbers.A llNumbers) + (SELECT Count(A.Field4)
FROM tblMyTable AS A WHERE A.Field4 = qryAllNumbers.A llNumbers) AS
theCount FROM qryAllNumbers;

!qryCountAcross Fields:
AllNumbers theCount
0 9
1 4
2 2
3 1

The union query will return all the numbers contained in the four
fields uniquely, sorted by value. The first subquery in
qryCountAcrossF ields counts the number of times the unique number from
qryAllNumbers appears in Field1 in tblMyTable. The second subquery
counts the number of times the unique number from qryAllNumbers appears
in Field2 in tblMyTable, etc. 'Count' is not a good field name since
it's probably a reserved name in SQL so I changed it to theCount. Also
consider whether or not the data in Field1, ..., Field4 should be
separated out into a separate table to obviate counting across fields.
The use of field names like Field1, Field2, etc. often indicates a
non-normalized structure so perhaps your info should be stored like:

tblMyNumbers
IDMN IDMT theNumber theKind
1 1 1 1
2 1 2 2
3 1 1 3
4 1 0 4
5 2 1 1
6 2 1 2
7 2 0 3
8 2 0 4
9 3 2 1
10 3 0 2
11 3 0 3
12 3 0 4
13 4 3 1
14 4 0 2
15 4 0 3
16 4 0 4

Then the data for a given record in tblMyTable, perhaps for a report,
can be found by using the foreign key IDMT. The counting query
simplifies to:

qryCountDown:
SELECT theNumber, Count(theNumber ) AS theCount FROM tblMyNumbers GROUP
BY theNumber HAVING theNumber Is Not Null;

!qryCountDown:
theNumber theCount
0 9
1 4
2 2
3 1

The fact that the fields are similar enough to have their counts
combined seems to indicate that this normalization should be done.
Because of normalization many queries will need more joins and a few
queries will be simpler but if normalization is not done where it
should you will likely encounter some nasty consequences later.

I hope this helps,

James A. Fortune

Dec 6 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
3957
by: Mike Cocker | last post by:
Hello, I'm quite weak at PHP, so I was hoping to get some help understanding the below code. First off, I'm trying to create a "query form" that will allow me to display the results on my screen. I grabbed this code from the net hoping that I could tweak it for my needs. I'm using MySQL, PHP and IIS and they all are running fine. As the code is, it will display the form, but it won't display my result(s). Any suggestions? Cheers,
5
2256
by: Jerome | last post by:
Hi, I've got the following problem: I want my ASP page to display a certain number (based on a COUNT query), it works fine if the result is at least 1! If there are no records to be counted (= the query is empty), I get errors. What I've tried is this:
3
7806
by: krystoffff | last post by:
Hi I would like to paginate the results of a query on several pages. So I use a query with a limit X offset Y to display X results on a page, ok. But for the first page, I need to run the same query with a count(*) to know how many pages I will get (number total of rows/ X). The problem is my query is very slow (maybe 5s) because there is much
1
6582
by: Big Time | last post by:
I've got a crosstab query that counts the number of values in one of my tables. However, rather than giving me the number of instances of each record, what I would like is for each value to display, separated by a comma. For example, in the query is a row is named BC and a column named Vancouver, it currently displays "4" for the number of instances that Vancouver appears combined with the BC field. However, what I would like to display...
11
3144
by: Andy_Khosravi | last post by:
My problem: I'm having trouble with a query taking much too long to run; a query without any criteria evaluating only 650 records takes over 300 seconds to run (over the network. On local drive takes 120 seconds). The Setup: I'm running Access 97 non-developer edition. I have exactly zero other tools to use and no way to change that =(. My database is compiled and resides on a network drive. The database has not been split into a...
4
7776
by: alltechsolutions.net | last post by:
Been at this for two days now... I have a customer database with various fields, two of which are: ContactID (an autonumber field) & ReferralID (long Integer) When a customer refers someone new, the new customer gets the referee's ContactID in their ReferralID field. So someone who refers 10 people will have 10 instances of their ContactID scattered down the ReferralID column.
0
4129
by: RCapps | last post by:
When running the below SQL Query I keep getting the following error: Server: Msg 4924, Level 16, State 1, Line 1 ALTER TABLE DROP COLUMN failed because column 'ContractDef' does not exist in table 'zContractDefault'. For some reason it is only returning the first 11 chars of the column name? Any help would be greatly appreciated... This query searches a DB and determines which columns are 'Invalid' if the column name is >30 and...
3
4875
WyvsEyeView
by: WyvsEyeView | last post by:
This seems like it should be so easy to do. I have a table, called tblTopics. Each topic can have one or more instances, contained in a table called tblTopicInst. tblTopics is bound to a form called frmTopics and tblTopicInst is bound to a form called frmTopicInst. From frmTopics, you access frmTopicInst via a button. I have also created a query, qryCountInst, that counts the number of instances for the topic currently displayed on frmTopics. I...
2
2696
by: }{ | last post by:
Can someone suggest the best way to accomlish the following: I have a query that lists all of the records between two dates, these records contain Machine owner, machine serial number and machine cycle count among other things. I would like to calculate the cycle count for each machine between the two dates. I have used the Min and Max functions against each serial but the problem is
0
8784
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9462
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9248
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9199
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6747
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6049
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4821
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3270
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2187
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.