Hello,
I have a bit of code that obviously doesn't work, but I need
help creating a query that would have the same effect as if this
query was working.
SELECT * FROM
(SELECT Count(*) AS numMarried FROM tblClients WHERE mID = 1),
(SELECT Count(*) AS numSingle FROM tblClients WHERE mID = 2),
(SELECT Count(*) AS numDivorced FROM tblClients WHERE mID = 3),
(SELECT Count(*) AS numWidowed FROM tblClients WHERE mID = 4);
Right now I have broken each count into a separate query, and
then SELECT * from all of the count queries, but there has to
be a better way.
---
Chad Reid
Strategic Tech Solutions
Email: ch******@gaebel.ca
ICQ: 2319732 4 4669
Chad Reid <ch******@gaebel.ca> wrote in
news:Pine.WNT.4.58.0309261920300.-895817@tesko: Hello,
I have a bit of code that obviously doesn't work, but I need help creating a query that would have the same effect as if this query was working.
SELECT * FROM (SELECT Count(*) AS numMarried FROM tblClients WHERE mID = 1), (SELECT Count(*) AS numSingle FROM tblClients WHERE mID = 2), (SELECT Count(*) AS numDivorced FROM tblClients WHERE mID = 3), (SELECT Count(*) AS numWidowed FROM tblClients WHERE mID = 4);
Right now I have broken each count into a separate query, and then SELECT * from all of the count queries, but there has to be a better way.
Instead of counting, you should be summing.
SELECT
Sum(iif(mID=1,1,0) as numMarried,
Sum(iif(mID=2,1,0) as numSingle,
Sum(iif(mID=3,1,0) as numDivorced,
Sum(iif(mID=4,1,0) as numWidowed,
from tblClients
Theory: the iif(test,truevalue,falsevalue) function will test your
mID=x and return the truevalue or falsevalue based on whether the
test is true or false.
You can google up the thread on counting boolean fields, and
simplify the equations even more.
e.g. Sum(abs(mID=1)) will be about four times faster, important if
you have a hundred thousand clients.
Bob Q.
--- Chad Reid Strategic Tech Solutions Email: ch******@gaebel.ca ICQ: 2319732
Bob Quintal wrote: Chad Reid <ch******@gaebel.ca> wrote in news:Pine.WNT.4.58.0309261920300.-895817@tesko:
Hello,
I have a bit of code that obviously doesn't work, but I need help creating a query that would have the same effect as if this query was working.
SELECT * FROM (SELECT Count(*) AS numMarried FROM tblClients WHERE mID = 1), (SELECT Count(*) AS numSingle FROM tblClients WHERE mID = 2), (SELECT Count(*) AS numDivorced FROM tblClients WHERE mID = 3), (SELECT Count(*) AS numWidowed FROM tblClients WHERE mID = 4);
Right now I have broken each count into a separate query, and then SELECT * from all of the count queries, but there has to be a better way.
Instead of counting, you should be summing.
SELECT Sum(iif(mID=1,1,0) as numMarried, Sum(iif(mID=2,1,0) as numSingle, Sum(iif(mID=3,1,0) as numDivorced, Sum(iif(mID=4,1,0) as numWidowed, from tblClients
Theory: the iif(test,truevalue,falsevalue) function will test your mID=x and return the truevalue or falsevalue based on whether the test is true or false.
You can google up the thread on counting boolean fields, and simplify the equations even more.
e.g. Sum(abs(mID=1)) will be about four times faster, important if you have a hundred thousand clients.
Bob Q. --- Chad Reid Strategic Tech Solutions Email: ch******@gaebel.ca ICQ: 2319732
Ideally, you would have a lookup table that has a string description of
the mID code. E.g.:
Lookup table "RelationTypes":
mID Description
--- -----------
1 Married
2 Single
3 Divorced
4 Widowed
Then you could create a query like this:
SELECT R.Description, Count(*) As NumberOfPersons
FROM tblClients AS C INNER JOIN RelationTypes AS R
ON C.mID = R.mID
ORDER BY R.Description
Then whenever you added a new Description to the RelationTypes table you
would not have to change your query to include that new Description.
MGFoster:::mgf
Oakland, CA (USA)
I forgot to put in the GROUP BY clause:
SELECT R.Description, Count(*) As NumberOfPersons
FROM tblClients AS C INNER JOIN RelationTypes AS R
ON C.mID = R.mID
GROUP BY R.Description
ORDER BY R.Description
MGFoster:::mgf
Oakland, CA (USA)
MGFoster wrote: Ideally, you would have a lookup table that has a string description of the mID code. E.g.:
Lookup table "RelationTypes":
mID Description --- ----------- 1 Married 2 Single 3 Divorced 4 Widowed
Then you could create a query like this:
SELECT R.Description, Count(*) As NumberOfPersons FROM tblClients AS C INNER JOIN RelationTypes AS R ON C.mID = R.mID ORDER BY R.Description
Then whenever you added a new Description to the RelationTypes table you would not have to change your query to include that new Description.
MGFoster:::mgf Oakland, CA (USA)
MGFoster <me@privacy.com> wrote in
news:5u*****************@newsread3.news.pas.earthl ink.net: I forgot to put in the GROUP BY clause:
You also forgot the crosstab query to move your four rows into the
one the OP wanted. ;)
Bob Q. SELECT R.Description, Count(*) As NumberOfPersons FROM tblClients AS C INNER JOIN RelationTypes AS R ON C.mID = R.mID GROUP BY R.Description ORDER BY R.Description
MGFoster:::mgf Oakland, CA (USA)
MGFoster wrote: Ideally, you would have a lookup table that has a string description of the mID code. E.g.:
Lookup table "RelationTypes":
mID Description --- ----------- 1 Married 2 Single 3 Divorced 4 Widowed
Then you could create a query like this:
SELECT R.Description, Count(*) As NumberOfPersons FROM tblClients AS C INNER JOIN RelationTypes AS R ON C.mID = R.mID ORDER BY R.Description
Then whenever you added a new Description to the RelationTypes table you would not have to change your query to include that new Description.
MGFoster:::mgf Oakland, CA (USA)
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Brenda J. |
last post by:
Hello all
I'm sure this is a relatively simple query to create, but I can't seem
to get my head around it.
Here is an example of the two tables I am using:
tblAccountTypes
...
|
by: Megan |
last post by:
hi everybody-
i'm having a counting problem i hope you guys and gals could give me
some help with. i have a query that retrieves a bevy of information
from several different tables.
first let...
|
by: svdh |
last post by:
I have posed a question last saturday and have advanced alot in the
meantime. But I am still not there
Problem is that I try to merging various fields from various tables in
one document in Word...
|
by: ChadDiesel |
last post by:
I appreciate the help on this group. I know I've posted a lot here the last
couple of weeks, but I was thrown into a database project at my work with
very little Access experience. No other...
|
by: mattytee123 |
last post by:
I have about 20 tables, of which I would like to do a union query and
count of how many of each different code there is?
The simplified verson of the table is structured like this.
Code ...
|
by: RussCRM |
last post by:
I need some help getting unique records from our database! I work for a small non-profit homeless shelter. We keep track of guest information as well as what services we have offered for...
|
by: mwang |
last post by:
I have sql like this:
select count(*) from TRANSACTION transaction0_ where transaction0_.CREATED > sysdate - 10 and exists (select lineItem.TRANSACTION_ID from LINEITEM lineItem where...
|
by: crassostrea |
last post by:
Hello and Happy New Year,
I have two tables in Access 2003 (Windows XP) with similar, but different, information. We’ll call them table A and table B. I want to count the number of records in...
|
by: jasone |
last post by:
hey, this is what ive got so far:
("SELECT (Select count(*) from tbl_flight_details) + (select count(*) FROM tbl_flight_departures) as grandtotal")
i need to count all the records in the...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
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...
|
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
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
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,...
|
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...
| |