dbTalk Databases Forums  

Access Sql Query

comp.databases.mysql comp.databases.mysql


Discuss Access Sql Query in the comp.databases.mysql forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Giovanni Militano
 
Posts: n/a

Default Access Sql Query - 03-04-2011 , 09:38 AM






Hi there,

i don't know if i'm in right Ng, but i'd ask you something bout access
database query.

I have got a db with a table so

ID
ITALY (boolean)
GREECE boolean

Ok. I have to create a query to calculate how many records are true for
ITALY and the same for Greece. I have already written a double query
that is this:
select count(Italy) as IT from XXX where Italy = True
and the same for greece.
Can someone tell me how write this query one time for both?

Thanks
(i hope to be clear!)

Reply With Quote
  #2  
Old   
Robert Hairgrove
 
Posts: n/a

Default Re: Access Sql Query - 03-04-2011 , 04:18 PM






On 03/04/2011 04:38 PM, Giovanni Militano wrote:
Quote:
Hi there,

i don't know if i'm in right Ng, but i'd ask you something bout access
database query.
Have you tried "microsoft.public.access.queries" yet?

Quote:
I have got a db with a table so

ID
ITALY (boolean)
GREECE boolean

Ok. I have to create a query to calculate how many records are true for
ITALY and the same for Greece. I have already written a double query
that is this:
select count(Italy) as IT from XXX where Italy = True
and the same for greece.
Can someone tell me how write this query one time for both?

Thanks
(i hope to be clear!)
Look up the SQL clause "GROUP BY" from the documentation.

Reply With Quote
  #3  
Old   
Lennart Jonsson
 
Posts: n/a

Default Re: Access Sql Query - 03-05-2011 , 01:35 AM



On 2011-03-04 16:38, Giovanni Militano wrote:
Quote:
Hi there,

i don't know if i'm in right Ng, but i'd ask you something bout access
database query.

Some access group or a general database group such as comp.databases
would probably have been a better NG

Quote:
I have got a db with a table so

ID
ITALY (boolean)
GREECE boolean

Ok. I have to create a query to calculate how many records are true for
ITALY and the same for Greece.
[...]

First, consider redesigning your data model. It looks as if your table
is some kind of pivot table.

select Country, count(*) from (
select case when Italy then 'Italy' end as Country from IT
union all
select case when Greece then 'Greece' end as Country from IT
) as T
where Country is not null
group by Country;

/Lennart

Reply With Quote
  #4  
Old   
Álvaro G. Vicario
 
Posts: n/a

Default Re: Access Sql Query - 03-07-2011 , 02:28 AM



El 04/03/2011 16:38, Giovanni Militano escribió/wrote:
Quote:
i don't know if i'm in right Ng, but i'd ask you something bout access
database query.

I have got a db with a table so

ID
ITALY (boolean)
GREECE boolean

Ok. I have to create a query to calculate how many records are true for
ITALY and the same for Greece. I have already written a double query
that is this:
select count(Italy) as IT from XXX where Italy = True
and the same for greece.
Can someone tell me how write this query one time for both?
In MySQL you can just use a CASE statement, supposing you have
TINYINT(1) columns:

SELECT
COUNT(CASE italy WHEN TRUE THEN 1 END) AS num_true_italy,
COUNT(CASE greece WHEN TRUE THEN 1 END) AS num_true_greece
FROM xxx

Whether MS Access supports this is something I don't know.


--
-- http://alvaro.es - Álvaro G. Vicario - Burgos, Spain
-- Mi sitio sobre programación web: http://borrame.com
-- Mi web de humor satinado: http://www.demogracia.com
--

Reply With Quote
  #5  
Old   
Álvaro G. Vicario
 
Posts: n/a

Default Re: Access Sql Query - 03-07-2011 , 02:31 AM



El 07/03/2011 9:28, "Álvaro G. Vicario" escribió/wrote:
Quote:
El 04/03/2011 16:38, Giovanni Militano escribió/wrote:
i don't know if i'm in right Ng, but i'd ask you something bout access
database query.

I have got a db with a table so

ID
ITALY (boolean)
GREECE boolean

Ok. I have to create a query to calculate how many records are true for
ITALY and the same for Greece. I have already written a double query
that is this:
select count(Italy) as IT from XXX where Italy = True
and the same for greece.
Can someone tell me how write this query one time for both?

In MySQL you can just use a CASE statement, supposing you have
TINYINT(1) columns:

SELECT
COUNT(CASE italy WHEN TRUE THEN 1 END) AS num_true_italy,
COUNT(CASE greece WHEN TRUE THEN 1 END) AS num_true_greece
FROM xxx
I'm sleepy... I you have have TINYINT(1) columns with values 0 and 1,
you can simply make a sum:

SELECT
SUM(italy) AS num_true_italy,
SUM(greece) AS num_true_greece
FROM xxx

Of course, the CASE approach tends to be better since MySQL won't
prevent you from inserting values greater than 1...




--
-- http://alvaro.es - Álvaro G. Vicario - Burgos, Spain
-- Mi sitio sobre programación web: http://borrame.com
-- Mi web de humor satinado: http://www.demogracia.com
--

Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.