我正在研究Postgres 9.3。我有兩個表,第一個支付項目:Postgres:如何加入
Table "public.prescription"
Column | Type | Modifiers
-------------------+-------------------------+--------------------------------------------------------------------
id | integer | not null default nextval('frontend_prescription_id_seq'::regclass)
presentation_code | character varying(15) | not null
presentation_name | character varying(1000) | not null
actual_cost | double precision | not null
pct_id | character varying(3) | not null
,第二個機構:
Table "public.pct"
Column | Type | Modifiers
-------------------+-------------------------+-----------
code | character varying(3) | not null
name | character varying(200) |
我有一個查詢,以獲得特定代碼的所有支付:
SELECT sum(actual_cost) as total_cost, pct_id as row_id
FROM prescription
WHERE presentation_code='1234' GROUP BY pct_id
這是該查詢的query plan。
現在,我想用相關組織的name
屬性對每行進行註釋。這是我想要的:
SELECT sum(prescription.actual_cost) as total_cost, prescription.pct_id, pct.name as row_id
FROM prescription, pct
WHERE prescription.presentation_code='0212000AAAAAAAA'
GROUP BY prescription.pct_id, pct.name;
這是。這非常慢:我做錯了什麼?
我認爲在第一個查詢運行後,必須有一種方法用pct.name
註釋每一行,這會更快。
您還沒有加入的。請參閱有關如何執行此操作的手冊:http://www.postgresql.org/docs/current/static/tutorial-join.html和http://www.postgresql.org/docs/current/static/queries- table-expressions.html#QUERIES-FROM –
Duh!謝謝。 – Richard