2016-05-02 5 views
2

を数えます。表2 (bday)には、学生のショーが1 time by IDstudent's ethnicityで表示されます。私はLEFT JOINを使用しています。時には、bdayテーブルに不在の生徒がいて、その人種が宣言されていないためにカウントされないためです。サブクエリの学生が1つの以上の時間を示してもよいところ 、第一テーブル<code>(student_schedule)</code>に基づいて民族をカウントする<code>JOIN</code> 2のテーブルにしよう

SELECT bday.Ethnicity, ROUND(COUNT(DISTINCT student_schedule.ID)/(SELECT 
COUNT(DISTINCT student_schedule.ID) FROM student_schedule 
WHERE student_schedule.Course LIKE 'AS%')*100,2) AS "% of AS Population", 
(SELECT COUNT(DISTINCT student_schedule.ID) FROM student_schedule AS 
"Total Student Population") 
FROM student_schedule LEFT JOIN bday ON student_schedule.ID=bday.ID WHERE  
student_schedule.Course LIKE 'AS%' GROUP BY bday.Ethnicity 
ORDER BY COUNT(DISTINCT student_schedule.ID) DESC 

結果は3つの列(エスニシティ、AS集団の%、総学生集団)です。私は私に人種によってグループ化された全体の学校の民族性を与える別の列を追加したいコースLike 'AS%'、 と生徒の割合を比較するために enter image description here

。言い換えれば、白人学生の32%がASを受けたのに対し、白人は30%です。データは表2(bday)から得られます。表2には、すべての生徒の民族性がリストされています。それは

どれ

enter image description here ... COUNTのために必要なGROUP function(White, Black,...).理想的

は、私の結果は次のようになりカテゴリに分離することで、私はこだわっている

SELECT COUNT(bday.Ethnicity) 
FROM student_schedule 
LEFT JOIN bday ON student_schedule.ID=bday.ID. 

のようなものでなければなりません助けに感謝します。

答えて

1

あなたはこのような何かを試みることができる:

<?php 
//NOTE: I AM USING PDO FOR SIMPLICITY... BUT ANY OTHER DBAL WORKS AS WELL... 
//DATABASE CONNECTION CONFIGURATION: 
defined("HOST")  or define("HOST", "localhost");   //REPLACE WITH YOUR DB-HOST 
defined("DBASE") or define("DBASE", "_TEST_");    //REPLACE WITH YOUR DB NAME 
defined("USER")  or define("USER", "root");    //REPLACE WITH YOUR DB-USER 
defined("PASS")  or define("PASS", "root");    //REPLACE WITH YOUR DB-PASS 



try { 
    $dbh   = new PDO('mysql:host='.HOST.';dbname='. DBASE,USER,PASS); 
    $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 


    $sql   = 'SELECT COUNT(SS.ID) AS "Global_Student_Population" FROM student_schedule AS SS '; 
    $stmt   = $dbh->prepare($sql); 
    $stmt->execute(); 
    $result   = $stmt->fetchAll(PDO::FETCH_COLUMN); 
    $intAllStudents = (int)$result[0]; 

    $sql   = " SELECT BD.Ethnicity, {$intAllStudents} AS Global_Student_Population, 
         COUNT(DISTINCT SS.ID) AS Ethnic_Student_Population, 
         ROUND(COUNT(DISTINCT SS.ID)*100/{$intAllStudents}) AS '%_of_AS_Population' 
         FROM student_schedule AS SS 
         LEFT JOIN bday AS BD ON SS.ID=BD.ID 
         WHERE SS.Course LIKE 'AS%' GROUP BY BD.Ethnicity"; 

    $stmt   = $dbh->prepare($sql); 
    $stmt->execute(); 
    $result   = $stmt->fetchAll(); 

     var_dump($result); 
     var_dump($intAllStudents); 

    //GARBAGE COLLECTION 
    $dbh  = null; 
}catch(PDOException $e){ 
    echo $e->getMessage(); 
} 

私のvar_dump()のようなものが表示されます。 - - 表構造:

 array (size=3) 
     0 => 
     array (size=8) 
      'Ethnicity' => string 'Black' (length=5) 
      0 => string 'Black' (length=5) 
      'Global_Student_Population' => string '10' (length=2) 
      1 => string '10' (length=2) 
      'Ethnic_Student_Population' => string '3' (length=1) 
      2 => string '3' (length=1) 
      '%_of_AS_Population' => string '30' (length=2) 
      3 => string '30' (length=2) 
     1 => 
     array (size=8) 
      'Ethnicity' => string 'Hispanic' (length=8) 
      0 => string 'Hispanic' (length=8) 
      'Global_Student_Population' => string '10' (length=2) 
      1 => string '10' (length=2) 
      'Ethnic_Student_Population' => string '1' (length=1) 
      2 => string '1' (length=1) 
      '%_of_AS_Population' => string '10' (length=2) 
      3 => string '10' (length=2) 
     2 => 
     array (size=8) 
      'Ethnicity' => string 'White' (length=5) 
      0 => string 'White' (length=5) 
      'Global_Student_Population' => string '10' (length=2) 
      1 => string '10' (length=2) 
      'Ethnic_Student_Population' => string '2' (length=1) 
      2 => string '2' (length=1) 
      '%_of_AS_Population' => string '20' (length=2) 
      3 => string '20' (length=2) 

そしてここでは、私のテスト表の定義ですテーブルの場合bday -

CREATE TABLE `bday` (
    `ID` int(11) NOT NULL, 
     `Ethnicity` varchar(255) NOT NULL 
    ) ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=utf8; 

    -- 
    -- Dumping data for table `bday` 
    -- 

    INSERT INTO `bday` (`ID`, `Ethnicity`) VALUES 
    (1, 'Black'), 
    (2, 'Black'), 
    (3, 'Black'), 
    (4, 'White'), 
    (5, 'Hispanic'), 
    (6, 'White'), 
    (7, 'Asian'), 
    (8, 'Hispanic'), 
    (9, 'White'), 
    (10, 'Black'); 

    -- 
    -- Indexes for dumped tables 
    -- 

    -- 
    -- Indexes for table `bday` 
    -- 
    ALTER TABLE `bday` 
     ADD PRIMARY KEY (`ID`); 

    -- 
    -- AUTO_INCREMENT for dumped tables 
    -- 

    -- 
    -- AUTO_INCREMENT for table `bday` 
    -- 
    ALTER TABLE `bday` 
     MODIFY `ID` int(11) NOT NULL AUTO_INCREMENT,AUTO_INCREMENT=11; 




    -- 
    -- Table structure for table `student_schedule` 
    -- 

    CREATE TABLE `student_schedule` (
    `ID` int(11) unsigned NOT NULL, 
     `Course` varchar(255) NOT NULL 
    ) ENGINE=InnoDB AUTO_INCREMENT=11 DEFAULT CHARSET=utf8; 

    -- 
    -- Dumping data for table `student_schedule` 
    -- 

    INSERT INTO `student_schedule` (`ID`, `Course`) VALUES 
    (1, 'AS'), 
    (2, 'AS'), 
    (3, 'EN'), 
    (4, 'EN'), 
    (5, 'AS'), 
    (6, 'AS'), 
    (7, 'EN'), 
    (8, 'EN'), 
    (9, 'AS'), 
    (10, 'AS'); 

    -- 
    -- Indexes for dumped tables 
    -- 

    -- 
    -- Indexes for table `student_schedule` 
    -- 
    ALTER TABLE `student_schedule` 
     ADD PRIMARY KEY (`ID`); 

    -- 
    -- AUTO_INCREMENT for dumped tables 
    -- 

    -- 
    -- AUTO_INCREMENT for table `student_schedule` 
    -- 
    ALTER TABLE `student_schedule` 
     MODIFY `ID` int(11) unsigned NOT NULL AUTO_INCREMENT,AUTO_INCREMENT=11; 
1

あなたSQLは、少し厄介である第一それがより明確で読みやすくするためにそれを再構築することができます:

SELECT bday.Ethnicity 
     , ROUND(
      COUNT(*)/(SELECT COUNT(DISTINCT ID) FROM student_schedule 
               WHERE Course LIKE 'AS%') 
      *100,2) AS "% of AS Population" 
FROM bday INNER JOIN student_schedule on bday.ID=student_schedule.ID 
WHERE student_schedule.Course LIKE 'AS%' 
GROUP BY bday.Ethnicity 

お知らせここでは、ここで良いでしょうとして宣言されていないEthnicityと一部の学生があった場合、私は、INNER JOINを使用しましたあなたは今、私たちは単にSub-Queryを追加することを行うには、テーブルbdayから各Ethnicityの学生の(count)を追加したい

理にかなってその割合を取得します(とWHERE句で除外することができます)

SELECTリスト:

SELECT bday.Ethnicity 
     , ROUND(
      COUNT(*)/(SELECT COUNT(DISTINCT ID) FROM student_schedule 
               WHERE Course LIKE 'AS%') 
      *100,2) AS "% of AS Population" 

     , ROUND(
      (SELECT COUNT(*) FROM bday a WHERE a.Ethnicity=bday.Ethnicity) 
      /(SELECT COUNT(*) FROM bday) 
      *100,2) AS "Ethnicity of School"   
FROM bday INNER JOIN student_schedule on bday.ID=student_schedule.ID 
WHERE student_schedule.Course LIKE 'AS%' 
GROUP BY bday.Ethnicity 

注ここでは、私は、サブクエリ内のWHERE句でそれを使用するときに、外側bdayテーブルとの競合を避けるためにaとしてサブクエリのテーブルbdayaliasを設定します。

0

時には、あなたがハングアップしたときに最初から始める必要があります。私は、グローバルCOUNTSから始め、CASEステートメントを使ってWHEREカウントを行うことにしました。私は最終的にそれは私が私が思いついた解決策をneeded.Theように動作するようになっている:

SELECT bday.Ethnicity, ROUND(COUNT(DISTINCT CASE WHEN 
student_schedule.Course LIKE 'AS%' THEN student_schedule.ID END)/(SELECT 
COUNT(DISTINCT student_schedule.ID) FROM student_schedule WHERE 
student_schedule.Course LIKE 'AS%')*100,2) AS '% of AS Population', 
ROUND(COUNT(DISTINCT student_schedule.ID)/(SELECT COUNT(DISTINCT 
student_schedule.ID) FROM student_schedule)*100,2) AS '% of Student 
Population' 
FROM student_schedule INNER JOIN bday ON student_schedule.ID=bday.ID 
GROUP BY bday.Ethnicity 
ORDER BY COUNT(DISTINCT student_schedule.ID) DESC 

結果は次のとおりです。 enter image description here

関連する問題