0

im have a view that need to include the count of members volunteering for an event. I'm getting an error 'not a single-group group functions'. Any idea how to resolve this?

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;

2 Answers 2

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;
Sign up to request clarification or add additional context in comments.

2 Comments

This counts all members not just those for a given event
I modified your answer to filter. Thanks for help
1

You could use the 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;

Simply put this counts the number of members per event_id but as it's not an aggregate function no GROUP BY is required. You receive the same value per event_id.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.