2012-11-26 51 views
0

im有一個視圖需要包括志願參加活動的成員數。我收到一個錯誤「不是單一羣組功能」。任何想法如何解決這個問題?在創建視圖時使用count on

CREATE VIEW atbyrd.events__view AS 
SELECT e.name, e."DATE", b."LIMIT",b.allocated_amount, COUNT(em.member_id), e.comments 
FROM events e INNER JOIN budgets b ON b.event_id = e.id 
INNER JOIN event_members em ON em.event_id = e.id; 

回答

1
SELECT e.name, 
     e."DATE", 
     b."LIMIT", 
     b.allocated_amount, 
     (select COUNT(member_id) from event_members) as mem_count, 
     e.comments 
FROM events e 
INNER JOIN budgets b ON b.event_id = e.id 
INNER JOIN event_members em ON em.event_id = e.id; 
+0

這會計算所有成員,而不僅僅是給定事件的成員 –

+0

我修改了您的篩選答案。感謝幫助 –

1

您可以使用analytic function count()

SELECT e.name 
    , e."DATE" 
    , b."LIMIT" 
    , b.allocated_amount 
    , COUNT(em.member_id) over (partition by em.event_id) 
    , e.comments 
    FROM events e 
INNER JOIN budgets b 
    ON b.event_id = e.id 
INNER JOIN event_members em 
    ON em.event_id = e.id; 

簡單地把這個計數每event_id成員的數量,但因爲它不是一個聚合函數沒有GROUP BY是必需的。您收到每event_id相同的價值。