我对Postgresql还是很陌生,我正在尝试编写一个相当复杂的select查询以获取报告所需的数据。我要合并到一个表,其中包含与我们需要进行度量的所有值相关的名称(例如,选项“ MALE”在主数据表中的值为384)。加入我要替换的列之一的项目表很容易,但是我有几列想加入同一表,因此我不确定执行此操作的最佳方法。我知道我可以创建一堆临时表以便在联接中使用,但是理想情况下,我希望只运行一个查询,而不必担心创建和删除临时表。
SELECT helpline_calls.id,
helpline_calls.call_id,
fam_id,
family,
high_priority,
sent_fam_nav,
clear_fam_nav,
call_back_count,
start_time,
end_time,
phone,
helpline_calls.NAME,
address_1,
address_2,
city,
zip_code,
helpline_calls.email,
age,
approximate,
is_spanish,
other_translation,
is_professional,
followup_date,
followup_time,
market_agency,
market_school,
contracted,
res_referrals,
cps,
emergency,
conference,
no_referrals,
send_fam_nav,
post_adopt,
crisis_response,
requested_materials,
third_party,
complete,
pact,
mst,
connections,
helpline_calls.created_at,
helpline_calls.updated_at,
state_id,
whos_calling_id,
gate_id,
helpline_calls.call_reason_id,
tmptblcallreason2.call_reason,
(SELECT l_list_items.NAME
FROM helpline_calls
JOIN l_list_items
ON helpline_calls.call_reason_id = l_list_items.id
WHERE l_list_items.list_code = 'cc') AS Call_Reason,
l_list_items.NAME AS Call_type,
market_categories.cat_code AS MarketCategory_Code,
market_categories.text AS MarketCategory_Code_Text,
market_codes.cat_code AS Market_Code,
market_codes.text AS Market_codes_text,
problem_categories.code AS Problemcategory_code,
problem_categories.text AS probelmcategory_text,
problem_codes.cat_code AS problem_code,
problem_codes.text AS Problem_code_text,
CASE
WHEN helpline_calls.pact_offered_id = 603 THEN 'Offered'
WHEN helpline_calls.pact_offered_id = 604 THEN 'Accepted'
END AS pact_offered,
CASE
WHEN helpline_calls.mst_offered_id = 603 THEN 'Offered'
WHEN helpline_calls.mst_offered_id = 604 THEN 'Accepted'
END AS mst_offered,
CASE
WHEN helpline_calls.connections_offered_id = 603 THEN 'Offered'
WHEN helpline_calls.connections_offered_id = 604 THEN 'Accepted'
END AS connections_offered,
CASE
WHEN helpline_calls.crisis_response_offered_id = 603 THEN 'Offered'
WHEN helpline_calls.crisis_response_offered_id = 604 THEN 'Accepted'
END AS crisis_response_offered,
CASE
WHEN helpline_calls.sex_id = 384 THEN 'Male'
WHEN helpline_calls.sex_id = 385 THEN 'Female'
WHEN helpline_calls.sex_id = 386 THEN 'Unknown'
END AS Sex,
counties.NAME AS County,
helpline_calls.region_id,
family_phone_id,
include_on_fam_nav_rpt,
users.username,
CASE
WHEN helpline_calls.is_callback_id = 598 THEN 'Yes'
WHEN helpline_calls.is_callback_id = 599 THEN 'No'
END AS Call_back_id,
CASE
WHEN helpline_calls.is_callback_90_days_id = 578 THEN 'Yes'
WHEN helpline_calls.is_callback_90_days_id = 579 THEN 'No'
WHEN helpline_calls.is_callback_90_days_id = 580 THEN 'Declined'
END AS callback_90_days_id,
needs_callback,
needs_callback_90_days,
con_call_type,
review,
adult_only_prevention,
complete_message,
sent_fam_nav_date_time,
fam_nav_email_flag
FROM PUBLIC.helpline_calls
JOIN users
ON helpline_calls.user_id = users.id
LEFT JOIN counties
ON helpline_calls.county_id = counties.id
LEFT JOIN problem_categories
ON helpline_calls.problem_category_id = problem_categories.id
LEFT JOIN problem_codes
ON helpline_calls.problem_code_id = problem_codes.id
LEFT JOIN market_codes
ON helpline_calls.market_code_id = market_codes.id
LEFT JOIN market_categories
ON helpline_calls.market_category_id = market_categories.id
LEFT JOIN l_list_items
ON helpline_calls.call_type_id = l_list_items.id
LEFT JOIN tmptblcallreason2
ON helpline_calls.call_id = tmptblcallreason2.call_id
WHERE ( start_time BETWEEN '1/1/2019' AND '10/31/2019 11:59:59 PM' )
SELECT helpline_calls.call_id,
call_reason_id,
l_list_items.NAME AS call_reason
INTO temp table tmptblcallreason2
FROM helpline_calls
JOIN l_list_items
ON helpline_calls.call_reason_id = l_list_items.id
答案 0 :(得分:0)
您可以像这样使用CTE:
WITH tmptblcallreason2 AS (
SELECT helpline_calls.call_id,
call_reason_id,
l_list_items.NAME AS call_reason
FROM helpline_calls
JOIN l_list_items
ON helpline_calls.call_reason_id = l_list_items.id
),
main_cte AS (
SELECT helpline_calls.id,
helpline_calls.call_id,
fam_id,
family,
high_priority,
sent_fam_nav,
clear_fam_nav,
call_back_count,
start_time,
end_time,
phone,
helpline_calls.NAME,
address_1,
address_2,
city,
zip_code,
helpline_calls.email,
age,
approximate,
is_spanish,
other_translation,
is_professional,
followup_date,
followup_time,
market_agency,
market_school,
contracted,
res_referrals,
cps,
emergency,
conference,
no_referrals,
send_fam_nav,
post_adopt,
crisis_response,
requested_materials,
third_party,
complete,
pact,
mst,
connections,
helpline_calls.created_at,
helpline_calls.updated_at,
state_id,
whos_calling_id,
gate_id,
helpline_calls.call_reason_id,
tmptblcallreason2.call_reason,
(SELECT l_list_items.NAME
FROM helpline_calls
JOIN l_list_items
ON helpline_calls.call_reason_id = l_list_items.id
WHERE l_list_items.list_code = 'cc') AS Call_Reason,
l_list_items.NAME AS Call_type,
market_categories.cat_code AS MarketCategory_Code,
market_categories.text AS MarketCategory_Code_Text,
market_codes.cat_code AS Market_Code,
market_codes.text AS Market_codes_text,
problem_categories.code AS Problemcategory_code,
problem_categories.text AS probelmcategory_text,
problem_codes.cat_code AS problem_code,
problem_codes.text AS Problem_code_text,
CASE
WHEN helpline_calls.pact_offered_id = 603 THEN 'Offered'
WHEN helpline_calls.pact_offered_id = 604 THEN 'Accepted'
END AS pact_offered,
CASE
WHEN helpline_calls.mst_offered_id = 603 THEN 'Offered'
WHEN helpline_calls.mst_offered_id = 604 THEN 'Accepted'
END AS mst_offered,
CASE
WHEN helpline_calls.connections_offered_id = 603 THEN 'Offered'
WHEN helpline_calls.connections_offered_id = 604 THEN 'Accepted'
END AS connections_offered,
CASE
WHEN helpline_calls.crisis_response_offered_id = 603 THEN 'Offered'
WHEN helpline_calls.crisis_response_offered_id = 604 THEN 'Accepted'
END AS crisis_response_offered,
CASE
WHEN helpline_calls.sex_id = 384 THEN 'Male'
WHEN helpline_calls.sex_id = 385 THEN 'Female'
WHEN helpline_calls.sex_id = 386 THEN 'Unknown'
END AS Sex,
counties.NAME AS County,
helpline_calls.region_id,
family_phone_id,
include_on_fam_nav_rpt,
users.username,
CASE
WHEN helpline_calls.is_callback_id = 598 THEN 'Yes'
WHEN helpline_calls.is_callback_id = 599 THEN 'No'
END AS Call_back_id,
CASE
WHEN helpline_calls.is_callback_90_days_id = 578 THEN 'Yes'
WHEN helpline_calls.is_callback_90_days_id = 579 THEN 'No'
WHEN helpline_calls.is_callback_90_days_id = 580 THEN 'Declined'
END AS callback_90_days_id,
needs_callback,
needs_callback_90_days,
con_call_type,
review,
adult_only_prevention,
complete_message,
sent_fam_nav_date_time,
fam_nav_email_flag
FROM PUBLIC.helpline_calls
JOIN users
ON helpline_calls.user_id = users.id
LEFT JOIN counties
ON helpline_calls.county_id = counties.id
LEFT JOIN problem_categories
ON helpline_calls.problem_category_id = problem_categories.id
LEFT JOIN problem_codes
ON helpline_calls.problem_code_id = problem_codes.id
LEFT JOIN market_codes
ON helpline_calls.market_code_id = market_codes.id
LEFT JOIN market_categories
ON helpline_calls.market_category_id = market_categories.id
LEFT JOIN l_list_items
ON helpline_calls.call_type_id = l_list_items.id
LEFT JOIN tmptblcallreason2
ON helpline_calls.call_id = tmptblcallreason2.call_id
WHERE ( start_time BETWEEN '1/1/2019' AND '10/31/2019 11:59:59 PM' )
)
SELECT * FROM main_cte