2016-04-26 11 views
4

對於令人困惑的標題感到抱歉,我試圖實現的是通過下表獲取工作的總申請:試圖獲得適用於年齡範圍工作的男性/女性的比例

CREATE TABLE IF NOT EXISTS `applications` (
    `application_id` int(11) NOT NULL AUTO_INCREMENT, 
    `application_user` varchar(100) NOT NULL, 
    `application_date` datetime NOT NULL, 
    `application_job` int(11) NOT NULL, 
    `application_status` varchar(10) DEFAULT 'pending', 
    `application_enabled` int(2) NOT NULL DEFAULT '1', 
    `application_resume` int(11) NOT NULL, 
    `application_description` text NOT NULL, 
    PRIMARY KEY (`application_id`) 
) ENGINE=InnoDB DEFAULT CHARSET=latin1; 

爲了獲得他們的年齡,我離開user_personal_information加入application_user,因爲他們是申請工作的用戶。我查詢:

SELECT count(*) as total, 
     user_gender as gender, 
     TIMESTAMPDIFF(YEAR, user_birthdate, CURDATE()) AS age, 
     application_date 
FROM applications 
LEFT JOIN user_personal_information 
    ON user_personal_information_user = application_user 
WHERE application_job = ? 

我的用戶與user_gender表可以等於male/femaleuser_birthdate這在上面的語句,我將其轉換爲一個時代。

我試圖將所有的應用程序具有的年齡範圍,例如:

16 - 21 
22 - 30 
31 - 45 
45 - 64 
65+ 

而且男性和女性的比例爲年齡。要使用需要像這樣的數據的數據圖表:

"dataProvider": [ 
{ 
    "age": "85+", 
    "male": 25, // 
    "female": 25 
}, { 
    "age": "80-54", 
    "male": 25,//percentage 
    "female": 25//percentage 
}] 
從上面

所以,有男性的25%,已申請85歲以上,女性25%。你明白了,所以這就是我想讓我的選擇陳述起作用。

,這將創建一個圖表,像這樣:

enter image description here

所以只是爲了澄清,我要計算總的應用程序,並制定了基於性別和年齡組申請的百分比。我如何用上面的select語句來做到這一點?

回答

1

幾個嵌套的分組子查詢可以爲你做。需要注意的是計算出的比例是特定性別:

select a.age, a.gender, a.cnt, 100*a.cnt/b.sm as percentage from 
    (
     SELECT user_gender as gender, 
       TIMESTAMPDIFF(YEAR, user_birthdate, CURDATE()) AS age, 
       count(*) as cnt, 
     FROM applications 
     LEFT JOIN user_personal_information 
     ON user_personal_information_user = application_user 
     WHERE application_job = ? 
     GROUP BY user_gender, TIMESTAMPDIFF(YEAR, user_birthdate, CURDATE()) 
    ) a, 
    (
     SELECT TIMESTAMPDIFF(YEAR, user_birthdate, CURDATE()) AS age, 
       count(*) as sm 
     FROM applications 
     LEFT JOIN user_personal_information 
     ON user_personal_information_user = application_user 
     WHERE application_job = ? 
     GROUP BY TIMESTAMPDIFF(YEAR, user_birthdate, CURDATE()) 
    ) b 
    where a.age = b.age; 

如果您正在尋找特定的申請總數的百分比,你需要的東西,如:

select a.age, 
     a.gender, 
     a.cnt, 
100*a.cnt/(
      select count(TIMESTAMPDIFF(YEAR, user_birthdate, CURDATE())) 
      from 
      applications 
      LEFT JOIN user_personal_information 
      ON user_personal_information_user = application_user 
      WHERE application_job = ? 
     ) as percentage 
from 
    (
     SELECT user_gender as gender, 
       TIMESTAMPDIFF(YEAR, user_birthdate, CURDATE()) AS age, 
       count(*) as cnt, 
     FROM applications 
     LEFT JOIN user_personal_information 
     ON user_personal_information_user = application_user 
     WHERE application_job = ? 
     GROUP BY user_gender, TIMESTAMPDIFF(YEAR, user_birthdate, CURDATE()) 
    ) a; 
+0

對不起,我試圖得到的百分比太多,並顯示年齡範圍,即(40-50) – 4334738290

+0

需要使用一個'case when'聲明計算年齡限制他們組(如16-21等)然後gro由此以及性別 –

+0

我選擇嵌套兩個小組,並查詢他們的加入戰利品。我認爲它會起作用。 – Spade

0

這應該做的工作:

 
select 
sum(case when age between 16 and 21 and gender='male' then 1 end) as '[Male 16-21]', 
sum(case when age between 16 and 21 and gender='female' then 1 end) as 'Female [16-21]', 
sum(case when age between 22 and 30 and gender='male' then 1 end) as '[Male 22-30]', 
sum(case when age between 22 and 30 and gender='female' then 1 end) as '[Female 22-30]', 
sum(case when age between 31 and 45 and gender='male' then 1 end) as '[Male 31-45]', 
sum(case when age between 31 and 45 and gender='female' then 1 end) as '[Female 31-45]', 
sum(case when age between 46 and 64 and gender='male' then 1 end) as '[Male 46-64]', 
sum(case when age between 46 and 64 and gender='female' then 1 end) as '[Female 46-64]', 
sum(case when age > 64    and gender='male' then 1 end) as '[Male Over 64]', 
sum(case when age > 64    and gender='female' then 1 end) as '[Female Over 64]', 
sum(case when 1=1     and gender='female' then 1 end) as '[Male TOTAL]', 
sum(case when 1=1     and gender='male' then 1 end) as '[Female TOTAL]' 

FROM applications 
相關問題