2017-04-20 43 views
0

我有7桌,以獲得data.Below列出的表MySQL的一排用不同的頭銜

SELECT 

     visits.policy_id,persons.full_name,visits.person_id,visits.date, 
     (CASE WHEN test_types.name = 'Blood Glucose (Fasting)' THEN visit_test_entries.result ELSE " " END) AS BSL_F, 
     (CASE WHEN test_types.name = 'Blood Glucose (Post Prandial) 2 hrs' THEN visit_test_entries.result ELSE " " END) AS BSL_PP, 
     (CASE WHEN test_types.name = 'Triglycerides (Fasting)' THEN visit_test_entries.result ELSE " " END) AS TGL, 
     (CASE WHEN test_types.name = 'Low Density Lipoprotein (LDL)' THEN visit_test_entries.result ELSE " " END) AS LDL, 
     (CASE WHEN test_types.name = 'HDL (Fasting)' THEN visit_test_entries.result ELSE " " END) AS HDL, 
     (CASE WHEN test_types.name = 'Total Cholesterol (Fasting)' THEN visit_test_entries.result ELSE " " END) AS Total_Cholesterol 
FROM 
visits,provider_locations,providers,visit_test_entries,test_types,persons,households hh 

WHERE 
     visits.provider_location_id=provider_locations.id 
     AND visits.provider_id=providers.id 
     AND visits.id=visit_test_entries.visit_id 
     AND visit_test_entries.test_type_id=test_types.id 
     AND visits.person_id=persons.id 
     AND persons.household_id=hh.id 
     AND visits.date >="2016-06-01" 
     AND visits.provider_location_id=1 
     AND visits.valid_state='valid' 
  1. 訪問
  2. 提供商位置
  3. 提供商
  4. 與人不同的值的列
  5. visit_test_entries
  6. test_types
  7. pers附件

現在,我得到的輸出喜歡這個

--------------------------------------------------------------------------| 
policy_id| full_name|person_id|date  |BSL_F|BSL_PP|TGL|LDL|HDL|Tot_Cho| 
---------|----------|---------|----------|-----|------|---|---|---|-------| 
AN00234 | Rajesh | 2309330 |2017-04-02|123 |  | | | |  | 
---------|----------|---------|----------|-----|------|---|---|---|-------| 
AN00234 | Rajesh | 2309330 |2017-04-02|  |435 | | | |  | 
---------|----------|---------|----------|-----|------|---|---|---|-------| 
AN00234 | Rajesh | 2309330 |2017-04-02|  |  |45 | | |  | 
---------|----------|---------|----------|-----|------|---|---|---|-------| 
AN00234 | Rajesh | 2309330 |2017-04-02|  |  | |78 | |  | 
---------|----------|---------|----------|-----|------|---|---|---|-------| 
AN00234 | Rajesh | 2309330 |2017-04-02|  |  | | |120|  | 
---------|----------|---------|----------|-----|------|---|---|---|-------| 
AN00234 | Rajesh | 2309330 |2017-04-02|  |  | | | |170 | 
--------------------------------------------------------------------------| 

但我需要這樣的

--------------------------------------------------------------------------| 
policy_id| full_name|person_id|date  |BSL_F|BSL_PP|TGL|LDL|HDL|Tot_Cho| 
---------|----------|---------|----------|-----|------|-------|---|-------| 
AN00234 | Rajesh | 2309330 |2017-04-02|123 | 435 |45 |78 |120|170 | 
--------------------------------------------------------------------------| 
+0

放MAX()。 – xQbert

回答

2

環繞輸出每種情況下的最大聲明和組由其他字段不是合計的。

不老風格ancsii的粉絲加入,但...

這工作,因爲我們知道最大(小)將返回,因爲該組由相同的值。

SELECT visits.policy_id 
    , persons.full_name 
    , visits.person_id 
    , visits.date 
    , max(CASE WHEN test_types.name = 'Blood Glucose (Fasting)' THEN visit_test_entries.result ELSE " " END) AS BSL_F 
    , max(CASE WHEN test_types.name = 'Blood Glucose (Post Prandial) 2 hrs' THEN visit_test_entries.result ELSE " " END) AS BSL_PP 
    , max(CASE WHEN test_types.name = 'Triglycerides (Fasting)' THEN visit_test_entries.result ELSE " " END) AS TGL 
    , max(CASE WHEN test_types.name = 'Low Density Lipoprotein (LDL)' THEN visit_test_entries.result ELSE " " END) AS LDL 
    , max(CASE WHEN test_types.name = 'HDL (Fasting)' THEN visit_test_entries.result ELSE " " END) AS HDL 
    , max(CASE WHEN test_types.name = 'Total Cholesterol (Fasting)' THEN visit_test_entries.result ELSE " " END) AS Total_Cholesterol 
FROM visits 
    , provider_locations 
    , providers 
    , visit_test_entries 
    , test_types 
    , persons 
    , households hh 
WHERE visits.provider_location_id=provider_locations.id 
    AND visits.provider_id=providers.id 
    AND visits.id=visit_test_entries.visit_id 
    AND visit_test_entries.test_type_id=test_types.id 
    AND visits.person_id=persons.id 
    AND persons.household_id=hh.id 
    AND visits.date >="2016-06-01" 
    AND visits.provider_location_id=1 
    AND visits.valid_state='valid' 
GROUP BY visits.policy_id 
     , persons.full_name 
     , visits.person_id 
     , visits.date 

也沒有使用「的粉絲」作爲佔位符,我寧願沒有一個else在所有所以價值是<null>

+0

它工作正常,如果所有的列都具有不帶NULL的值。但是如果任何一列具有空值,則它會給出重複的值。 –

0

不求最好,但對你的最簡單的方法是圍繞每一個case語句和group by policy_ID,FULL_NAME,PERSON_ID和日期的子查詢

SELECT 
    visits.policy_id,persons.full_name,visits.person_id,visits.date, 
    (SELECT 
     test_types.result 
    FROM 
     test_types 
    WHERE 1 
     AND visit_test_entries.test_type_id = test_types.id 
     AND test_types.name = 'Low Density Lipoprotein (LDL)' 
    ) AS LDL 
    //... and so on