Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Get count plus detail in single SQL?
Message
General information
Forum:
Visual FoxPro
Category:
Databases,Tables, Views, Indexing and SQL syntax
Miscellaneous
Thread ID:
00663662
Message ID:
00663791
Views:
8
This message has been marked as the solution to the initial question of the thread.
Try
SELECT Questions.*, COUNT(UserCode) AS VoteCount, ;
          SUM( IIF(UserCode = lcUserCode, 1, 0)) AS VoteCountCurentUser ;
	FROM Questions ;
	LEFT JOIN Votes ;
	WHERE Questions.SurveyPK=Votes.SurveyPK ;
	ORDER BY CloseDate DESC ;
	GROUP BY SurveyPK ;
	INTO CURSOR TQuery
>I'm doing a survey module and want to run a single query that gives me the total number of votes in a survey (easy with COUNT and GROUP BY), PLUS whether the logged in user voted in each one or not.
>
>One table is Questions, the second is Votes. Here's what I have - it gives the vote count for each question, but not whether the current user (lcUserCode) voted in each one:
>
>
SELECT Questions.*, UserCode, COUNT(UserCode) AS VoteCount ;
>	FROM Questions ;
>	LEFT JOIN Votes ;
>	WHERE Questions.SurveyPK=Votes.SurveyPK ;
>	ORDER BY CloseDate DESC ;
>	GROUP BY SurveyPK ;
>	INTO CURSOR TQuery
>
>SurveyPK is the PK in the Questions table. Of course, I'm interested in listing all questions.
>
>Any ideas?
--sb--
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform