2

I need to generate a report based on the following tables:

CALLS_FOR_PROPOSALS ID|Name --+------ 1|Call 1 2|Call 2 3|Call 3 PROPOSALS ID|Call ID|Title --+-------+---------- 1| 1|Proposal 1 2| 2|Proposal 2 3| 2|Proposal 3 PROPOSAL_STATUSES ID|Proposal ID|Status ID --+-----------+--------- 1| 1| 1 2| 2| 1 3| 3| 1 4| 3| 2 STATUSES ID|NAME --+------------ 1|Not Reviewed 2|Processing 3|Accepted 4|Rejected 

With this sample data, there are 3 Calls for Proposals. There are three Proposals; one for Call 1, and two for Call 2. (Call 3 does not have any proposals.) Each proposal has at least one status assigned to it. When a row is inserted into the PROPOSALS table, a corresponding row is inserted into PROPOSAL_STATUSES, giving the Proposal an initial default status of 1 (Not Reviewed). Each time the status is changed, a new row is inserted into the PROPOSAL_STATUSES table, so that the history of status changes is preserved. I need to generate a report that shows for each Call, the number of Proposals submitted, and the number of Proposals that have had more than one status (i.e. the status has been changed from the default at least once.) For the sample data above, the results would look like this:

Call Name|Proposals Submitted|Proposals Reviewed| ---------+-------------------+------------------+ Call 1 | 1| 0| Call 2 | 2| 1| Call 3 | 0| 0| 

How would I write the SQL query to generate this report based on the above table structure? Thanks for your help.

1 Answer 1

2

something like that should do the trick : Demo

 SELECT Name as 'Call name', submitted as 'Proposals Submitted', SUM(CASE WHEN maxStatus > 1 THEN 1 ELSE 0 END) as 'Proposals Reviewed' FROM (SELECT cfp.Name, sum(case when ps.Status_ID = 1 then 1 else 0 end) as submitted, MAX(ps.Status_ID) as maxStatus FROM CALLS_FOR_PROPOSALS cfp LEFT JOIN PROPOSALS p on cfp.ID = p.CALL_ID LEFT JOIN PROPOSAL_STATUSES ps on ps.PROPOSAL_ID = p.ID GROUP BY cfp.Name) AS s GROUP BY Name, submitted 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.