Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
什么';在Postgresql中,多次合并到同一个表的最佳方法是什么?_Postgresql - Fatal编程技术网

什么';在Postgresql中,多次合并到同一个表的最佳方法是什么?

什么';在Postgresql中,多次合并到同一个表的最佳方法是什么?,postgresql,Postgresql,我对Postgresql非常陌生,我正在尝试编写一个相当复杂的select查询,以获取报表所需的数据。我想合并到一个表中,该表包含与度量值所需的所有值相关联的名称(例如,选项“MALE”在主数据表中的值为384)。对于我要替换的一列,连接到items表很容易,但是我有几个列要连接到同一个表,我不确定最好的方法。我知道我可以创建一组临时表用于连接,但我希望在可能的情况下只运行一个查询,而不必担心创建和删除临时表 SELECT helpline_calls.id, helpline_

我对Postgresql非常陌生,我正在尝试编写一个相当复杂的select查询,以获取报表所需的数据。我想合并到一个表中,该表包含与度量值所需的所有值相关联的名称(例如,选项“MALE”在主数据表中的值为384)。对于我要替换的一列,连接到items表很容易,但是我有几个列要连接到同一个表,我不确定最好的方法。我知道我可以创建一组临时表用于连接,但我希望在可能的情况下只运行一个查询,而不必担心创建和删除临时表

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

您可以使用如下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  

谢谢你救了我几个小时的挫折。