CREATE PROCEDURE [dbo].[spGetLatestOpenPollSubjectID]
AS
BEGIN
	DECLARE @subject_id	INT;
	SELECT @subject_id = MAX (polls.subject_id)
	FROM 
	(SELECT s.subject_id
	FROM tblPollSubject s 
	LEFT JOIN tblPollOption o ON (o.subject_id = s.subject_id)
	WHERE s.closed = 0
	GROUP BY s.subject_id
	HAVING COUNT(o.option_id) > 0) AS polls;
	SELECT @subject_id;
END
