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

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 3653
su**********@gmail.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

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

!qryCountAcrossFields:
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
qryCountAcrossFields 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
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...
5
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...
3
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...
1
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...
11
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...
4
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...
0
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...
3
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...
2
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...
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
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
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,...
1
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...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.