我有7个表来获取数据。列出了列表
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'
现在我得到了这样的输出
--------------------------------------------------------------------------|
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 :(得分:2)
将每个案例包含在max语句中,并由其他不在聚合中的字段组合。
不是ancsii加入旧风格的粉丝,但......
这是有效的,因为我们知道max(或min)将返回相同的值,因为group by。
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
也不喜欢使用&#34; &#34;作为占位符,我根本没有其他内容,因此价值为<null>
。
答案 1 :(得分:0)
不是最好的,但最简单的方法是子选择
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