dbTalk Databases Forums  

[Help!] A birthdate selection that's to make more precise..

comp.databases.mysql comp.databases.mysql


Discuss [Help!] A birthdate selection that's to make more precise.. in the comp.databases.mysql forum.



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

Default [Help!] A birthdate selection that's to make more precise.. - 07-06-2010 , 11:37 AM






Hello to all,

I'm in trouble

I've prepared a sql statement extracting the typical ranges of dates
from a table of users: 20-25 years old, 25-30 etc.
After, I've realized that the range is not precise. In fact I use the
YEAR and CURDATE(): this way, persons born in 1980 figure out as 30-
years old, but if - in example - we've a person born in december 1980,
*now* this person will be 29 and not 30 years old.

So, I must use also months, and to be perfect, also days.

This is the sql, I'm in trouble to find a way to transform the
statement in the right way. Any help will be very appreciated! Thanks
in advance

$sql = " (SELECT
SUM(CASE WHEN YEAR(data_nascita) BETWEEN YEAR(CURDATE())-25 AND
YEAR(CURDATE())-21 THEN 1 ELSE 0 END) AS 20_25anni,
SUM(CASE WHEN YEAR(data_nascita) BETWEEN YEAR(CURDATE())-30 AND
YEAR(CURDATE())-26 THEN 1 ELSE 0 END) AS 25_30anni,
SUM(CASE WHEN YEAR(data_nascita) BETWEEN YEAR(CURDATE())-35 AND
YEAR(CURDATE())-31 THEN 1 ELSE 0 END) AS 30_35anni,
SUM(CASE WHEN YEAR(data_nascita) BETWEEN YEAR(CURDATE())-40 AND
YEAR(CURDATE())-36 THEN 1 ELSE 0 END) AS 35_40anni,
SUM(CASE WHEN YEAR(data_nascita) BETWEEN YEAR(CURDATE())-45 AND
YEAR(CURDATE())-41 THEN 1 ELSE 0 END) AS 40_45anni,
SUM(CASE WHEN YEAR(data_nascita) <= YEAR(CURDATE())-46 THEN 1
ELSE 0 END) AS over_45
FROM wd_utenti
WHERE attivo='1')";

Luk

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

Default Re: [Help!] A birthdate selection that's to make more precise.. - 07-06-2010 , 01:00 PM






On 2010-07-06 18:37, Luk wrote:
Quote:
Hello to all,

I'm in trouble

I've prepared a sql statement extracting the typical ranges of dates
from a table of users: 20-25 years old, 25-30 etc.
After, I've realized that the range is not precise. In fact I use the
YEAR and CURDATE(): this way, persons born in 1980 figure out as 30-
years old, but if - in example - we've a person born in december 1980,
*now* this person will be 29 and not 30 years old.

So, I must use also months, and to be perfect, also days.

This is the sql, I'm in trouble to find a way to transform the
statement in the right way. Any help will be very appreciated! Thanks
in advance

$sql = " (SELECT
SUM(CASE WHEN YEAR(data_nascita) BETWEEN YEAR(CURDATE())-25 AND
YEAR(CURDATE())-21 THEN 1 ELSE 0 END) AS 20_25anni,
SUM(CASE WHEN YEAR(data_nascita) BETWEEN YEAR(CURDATE())-30 AND
YEAR(CURDATE())-26 THEN 1 ELSE 0 END) AS 25_30anni,
SUM(CASE WHEN YEAR(data_nascita) BETWEEN YEAR(CURDATE())-35 AND
YEAR(CURDATE())-31 THEN 1 ELSE 0 END) AS 30_35anni,
SUM(CASE WHEN YEAR(data_nascita) BETWEEN YEAR(CURDATE())-40 AND
YEAR(CURDATE())-36 THEN 1 ELSE 0 END) AS 35_40anni,
SUM(CASE WHEN YEAR(data_nascita) BETWEEN YEAR(CURDATE())-45 AND
YEAR(CURDATE())-41 THEN 1 ELSE 0 END) AS 40_45anni,
SUM(CASE WHEN YEAR(data_nascita) <= YEAR(CURDATE())-46 THEN 1
ELSE 0 END) AS over_45
FROM wd_utenti
WHERE attivo='1')";

Have you tried the date_sub function?

select
count(case when data_nascita
between date_sub(curdate(), interval '25' YEAR)
and date_sub(curdate(), interval '21' YEAR)
then 1 end) as 20_25anni,
count(case when data_nascita
between date_sub(curdate(), interval '30' YEAR)
and date_sub(curdate(), interval '26' YEAR)
then 1 end) as 25_30anni,
...
from luk;


/Lennart

Reply With Quote
  #3  
Old   
Erick T. Barkhuis
 
Posts: n/a

Default Re: [Help!] A birthdate selection that's to make more precise.. - 07-06-2010 , 01:11 PM



Luk:

Hi Luk!

Quote:
I've prepared a sql statement extracting the typical ranges of dates
from a table of users: 20-25 years old, 25-30 etc.

$sql = " (SELECT
SUM(CASE WHEN YEAR(data_nascita) BETWEEN YEAR(CURDATE())-25 AND
YEAR(CURDATE())-21 THEN 1 ELSE 0 END) AS 20_25anni,

Perhaps use:

WHEN NOW()> DATE_ADD(data_nascita, INTERVAL 20 YEARS) AND
NOW()<=DATE_ADD(data_nascita, INTERVAL 25 YEARS)
THEN 1 ELSE 0


--
Erick

Reply With Quote
  #4  
Old   
Erick T. Barkhuis
 
Posts: n/a

Default Re: [Help!] A birthdate selection that's to make more precise.. - 07-06-2010 , 01:12 PM



Luk:

Hi Luk!

Quote:
I've prepared a sql statement extracting the typical ranges of dates
from a table of users: 20-25 years old, 25-30 etc.

$sql = " (SELECT
SUM(CASE WHEN YEAR(data_nascita) BETWEEN YEAR(CURDATE())-25 AND
YEAR(CURDATE())-21 THEN 1 ELSE 0 END) AS 20_25anni,

Perhaps use:

WHEN NOW()> DATE_ADD(data_nascita, INTERVAL 20 YEAR) AND
NOW()<=DATE_ADD(data_nascita, INTERVAL 25 YEAR)
THEN 1 ELSE 0


--
Erick

Reply With Quote
  #5  
Old   
Luk
 
Posts: n/a

Default Re: A birthdate selection that's to make more precise.. - 07-07-2010 , 03:09 PM



On 6 Lug, 20:12, "Erick T. Barkhuis" <erick.use-... (AT) ardane (DOT) c.o.m>
wrote:
Quote:
Luk:

Hi Luk!

I've prepared a sql statement extracting the typical ranges of dates
from a table of users: 20-25 years old, 25-30 etc.

$sql = " * * * (SELECT
* * * * * * * * * * * * * *SUM(CASE WHEN YEAR(data_nascita) BETWEEN YEAR(CURDATE())-25 AND
YEAR(CURDATE())-21 THEN 1 ELSE 0 END) AS 20_25anni,

Perhaps use:

WHEN NOW()> DATE_ADD(data_nascita, INTERVAL 20 YEAR) AND
* * *NOW()<=DATE_ADD(data_nascita, INTERVAL 25 YEAR)
THEN 1 ELSE 0

--
Erick
Many Thanks Erick, your solution worked! :-)

Luk

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.