473,473 Members | 1,894 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

count and group by with OR

Hi!

I am wondering if this query is possible somehow:

I have a table with many fields that all can have a value from 1 to 5.

if I wanna see the count of each value from one field, then this is
easy:
SELECT field1, count(field1) as cntnr FROM table group by field1

But the thing is that I need to see the count of each possible value
(still 1 to 5), but
two or more fileds are to be considered. How is this done? Is it
possible at all? I am
sorry if this is tooooooo stupid question, but I haven't found the
solution during all the
day. :(

Or in other words, I need to know how many times all possible values
are present in field1
OR field2. And if one value is present in one row in both field1 and
field2 then it should
be counted only once. Like 'OR' you know...

Please advice.
Nov 13 '05 #1
3 2118
Not quite sure I follow.

Are you saying that if there are four rows with 3 in field1 and two with 3
in field2, you want to report 6? (In other words, you don't care which field
it comes from)

If so, try a Union query, and then do your counts on that query:

SELECT Field1 As TheField FROM MyTable
UNION ALL
SELECT Field2 FROM MyTable

--
Doug Steele, Microsoft Access MVP
http://I.Am/DougSteele
(no e-mails, please!)

"Abhi" <go****@megadelfi.com> wrote in message
news:1d**************************@posting.google.c om...
Hi!

I am wondering if this query is possible somehow:

I have a table with many fields that all can have a value from 1 to 5.

if I wanna see the count of each value from one field, then this is
easy:
SELECT field1, count(field1) as cntnr FROM table group by field1

But the thing is that I need to see the count of each possible value
(still 1 to 5), but
two or more fileds are to be considered. How is this done? Is it
possible at all? I am
sorry if this is tooooooo stupid question, but I haven't found the
solution during all the
day. :(

Or in other words, I need to know how many times all possible values
are present in field1
OR field2. And if one value is present in one row in both field1 and
field2 then it should
be counted only once. Like 'OR' you know...

Please advice.

Nov 13 '05 #2
"Douglas J. Steele" <NOSPAM_djsteele@NOSPAM_canada.com> wrote in message news:<eP********************@rogers.com>...
Not quite sure I follow.

Are you saying that if there are four rows with 3 in field1 and two with 3
in field2, you want to report 6? (In other words, you don't care which field
it comes from)
Yes, I don't care which field it comes from. It should indeed return 6
in this case, but if there are 10 rows and EACH row contains 3 in both
field1 and field2 then it should report 10, not 20. And of course I
need all the possible values counts reported.

If so, try a Union query, and then do your counts on that query:

SELECT Field1 As TheField FROM MyTable
UNION ALL
SELECT Field2 FROM MyTable


How to do counts on that query? I can only think of creating a
temporary table with results of this UNION query and then querying
that temp table. Is there any more elegant way?

P.S. Doesn't counting this UNION query return 20 and not 10 in a case
described above?

Abhi
Nov 13 '05 #3
"Abhi" <go****@megadelfi.com> wrote in message
news:1d**************************@posting.google.c om...
"Douglas J. Steele" <NOSPAM_djsteele@NOSPAM_canada.com> wrote in message
news:<eP********************@rogers.com>...
Not quite sure I follow.

Are you saying that if there are four rows with 3 in field1 and two with
3
in field2, you want to report 6? (In other words, you don't care which
field
it comes from)


Yes, I don't care which field it comes from. It should indeed return 6
in this case, but if there are 10 rows and EACH row contains 3 in both
field1 and field2 then it should report 10, not 20. And of course I
need all the possible values counts reported.

If so, try a Union query, and then do your counts on that query:

SELECT Field1 As TheField FROM MyTable
UNION ALL
SELECT Field2 FROM MyTable


How to do counts on that query? I can only think of creating a
temporary table with results of this UNION query and then querying
that temp table. Is there any more elegant way?

P.S. Doesn't counting this UNION query return 20 and not 10 in a case
described above?


Yes, you're correct that the union would return 20, not 10, in the case you
described. That's why we needed more information from you!

Okay, so you've got the following:

Id Field1 Field2
1 5 10
2 10 10
3 10 15
4 15 5

and you want to return

FieldValue Count
5 2
10 3
15 2

Correct?

Create a query that returns all of the unique values in the table and name
it qryUniqueValues:

SELECT Field1 AS LookupField
FROM MyTable
UNION
SELECT Field2 AS LookupField
FROM MyTable

Create a 2nd query that joins qryUniqueValues to your table and name it
qryIntermediate

SELECT MyTable.Id, MyTable.Field1, MyTable.Field2,
qryNewsgroupQuestionUniqueValues.LookupField
FROM MyTable
INNER JOIN qryValues
ON (MyTable.Field1 = qryUniqueValues.LookupField)
OR (MyTable.Field2 = qryUniqueValues.LookupField)

Create a 3rd query base that figures out the counts using qryIntermediate:

SELECT LookupField, Count(Id) As Total
FROM qryIntermediate
GROUP BY LookupField

Now, assuming you're using Access 2000 or newer, it's possible to combine
all of that into a single query, but I wanted to explain the steps:

SELECT MyData.LookupField, Count(MyData.Id) AS Total
FROM [SELECT UV.LookupField, MyTable.Id
FROM MyTable
INNER JOIN
(SELECT Field1 AS LookupField FROM MyTable UNION SELECT Field2 FROM MyTable)
AS UV
ON (MyTable.Field1 = UV.LookupField) OR (MyTable.Field2 = UV.LookupField)].
AS MyData
GROUP BY MyData.LookupField;
--
Doug Steele, Microsoft Access MVP
http://I.Am/DougSteele
(no e-mails, please!)

Nov 13 '05 #4

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

Similar topics

1
by: Joseph Santaniello | last post by:
Hello all, I have a table with among other things a timestamp field. To get the count of records per hour I do: select count(*) as c, date_trunc('hour', timestmp) as h from mytable
2
by: Deamond | last post by:
Hi I have a Table with Automatic ID numbers... In access I delete All records by hand When I add new record with delphi (SQL) the number ID of record count begin with last record+1 and not with...
5
by: vknid | last post by:
Hello everyone! I'm new to this newsgroup and also to C++. I've just started looking into learning C++ so I know very little about it. I'm hoping with this group and the books I have on...
2
by: Chris | last post by:
Hello all, I'm having some trouble setting up a query. Background: The table TBLSCREEN stores data about screenings of patients/subjects for eligibility to participate in a health study. A...
8
by: Thomas | last post by:
Hi! I´m a newbie with this and I´m trying to build a forum of my own but have stumbled on my first problem as early as the opening page. To the problem: I want to show a simple forum layout...
4
by: Igor | last post by:
I have one SELECT statement that needs to return one ntext field from one table and count something from other table, problem is that all fileds that are not in count have to be in group by and...
1
by: zafm86 | last post by:
Hi everyone! I'm sure my problem is pretty easy to solve but I've been working on it for a long and my my brain is not working correctly anymore. I'm working with an AS400 and I mhave to do an...
2
by: ArcticCircuits | last post by:
Greetings, As part of my Highschool project I had to come up with a themed database with a bunch of tables to query from, so I came up with a 'police' database. I am trying to get a count of all...
1
by: rjlorenzo | last post by:
Good Day Just wanted to ask for any help on how could I count a record depending on the value of the two column. Example below table i need to count the productivity of the agent for march 1 for...
1
by: Charles Quamina | last post by:
In the footer section of a report where summary data is presented for each group of Position Titles I want to count the number of records that have an 'X' in a particular field. I have a textbox in...
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
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,...
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...
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...
1
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
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 ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
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...

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.