Solved

SQL QUery issue

Posted on 2009-05-12
5
181 Views
Last Modified: 2012-05-06
I am not sure how i could perform a sub query within a query.

Typically, i am trying to compute the Score for all correct answer and score for all wrong answer and finally calculate the total score.

I put up a SQL but need some guidance. Thanks in advance.
CREATE PROCEDURE [dbo].[ExamMode_PostTestScores]
(
	@EXAM_REFERENCE_CODE nvarchar(50),
	@USER_ID int,
	@REQUESTOR_ID int
)
AS
 
BEGIN
 
INSERT INTO TEST_SCORE_CARD
(
	EXAM_REF_CODE,
	USER_ID,
	REQUESTOR_ID,
	SCORE_YES,
	SCORE_NO,
	SCORE_FINAL,
	CREATE_DATE
)
SELECT
 
	@EXAM_REFERENCE_CODE,
	@USER_ID,
	@REQUESTOR_ID, 
	SELECT SUM(MARK_ALLOCATED) FROM CESV WHERE CORRECT_ANSWER_ID=QUESTION_RESPONSE_ID AND EXAM_REFERENCE_CODE=@EXAM_REFERENCE_CODE AS SCORE_YES ,
	SELECT SUM(MARK_ALLOCATED) FROM CESV WHERE CORRECT_ANSWER_ID <>QUESTION_RESPONSE_ID AND EXAM_REFERENCE_CODE=@EXAM_REFERENCE_CODE AS SCORE_NO,
	SUM(SCORE_YES, SCORE_NO) AS SCORE_TOTAL
	getdate()
 
FROM
	
	EXAMINATION_SCHEDULER_VIEW CESV

Open in new window

0
Comment
Question by:TECH_NET
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 8

Expert Comment

by:Bobaran98
ID: 24370932
Okay, I see a few errors in your code, but they're all minor:
  1. Your subqueries need to be enclosed in parentheses (the ending parenthesis before the AS clause).
  2. You can't use SUM() to determine your SCORE_TOTAL... instead, simply add the values together like this:  (SCORE_YES+SCORE_NO) AS SCORE_TOTAL.
  3. You need a comma after SCORE_TOTAL and before getdate().
  4. Not withstanding what I said in point #2, I don't think SCORE_TOTAL will calculate properly as is since it's totaling the values of two other columns from the same select statement... you may need to add another level of subquery to make it work, but that's simple.
In short, I'd rewrite your code as follows:

CREATE PROCEDURE [dbo].[ExamMode_PostTestScores]
(
        @EXAM_REFERENCE_CODE nvarchar(50),
        @USER_ID int,
        @REQUESTOR_ID int
)
AS
 
BEGIN
 
INSERT INTO TEST_SCORE_CARD
(
        EXAM_REF_CODE,
        USER_ID,
        REQUESTOR_ID,
        SCORE_YES,
        SCORE_NO,
        SCORE_FINAL,
        CREATE_DATE
)
SELECT
 
        @EXAM_REFERENCE_CODE,
        @USER_ID,
        @REQUESTOR_ID,
	TT.SCORE_YES,
	TT.SCORE_NO,
	(TT.SCORE_YES + TT.SCORE_NO) AS SCORE_TOTAL,
	GETDATE()
 
FROM (
 
	SELECT
		(SELECT SUM(MARK_ALLOCATED) FROM CESV WHERE CORRECT_ANSWER_ID=QUESTION_RESPONSE_ID AND EXAM_REFERENCE_CODE=@EXAM_REFERENCE_CODE) AS SCORE_YES,
		(SELECT SUM(MARK_ALLOCATED) FROM CESV WHERE CORRECT_ANSWER_ID<>QUESTION_RESPONSE_ID AND EXAM_REFERENCE_CODE=@EXAM_REFERENCE_CODE) AS SCORE_NO
	 
	FROM	
		EXAMINATION_SCHEDULER_VIEW CESV
 
) TT

Open in new window

0
 
LVL 8

Accepted Solution

by:
Bobaran98 earned 500 total points
ID: 24370965
Actually, looking at your code a bit closer, I suddenly realize you're only inserting one record here.  If that's true, I think you might find the following code a bit simpler:

(Mind you I think the following should work, but I'm a bit rusty, I couldn't test it, and I don't currently have access to any of my old stored procedures for comparison.)

:-)


CREATE PROCEDURE [dbo].[ExamMode_PostTestScores]
(
        @EXAM_REFERENCE_CODE nvarchar(50),
        @USER_ID int,
        @REQUESTOR_ID int
)
AS
 
BEGIN
 
DECLARE @SCORE_YES int,
	@SCORE_NO int,
	@SCORE_FINAL int
 
SET @SCORE_YES = (SELECT SUM(MARK_ALLOCATED) FROM CESV WHERE CORRECT_ANSWER_ID=QUESTION_RESPONSE_ID AND EXAM_REFERENCE_CODE=@EXAM_REFERENCE_CODE)
SET @SCORE_NO = (SELECT SUM(MARK_ALLOCATED) FROM CESV WHERE CORRECT_ANSWER_ID<>QUESTION_RESPONSE_ID AND EXAM_REFERENCE_CODE=@EXAM_REFERENCE_CODE)
SET @SCORE_FINAL = @SCORE_YES + @SCORE_NO
 
INSERT INTO TEST_SCORE_CARD VALUES
(
	@EXAM_REFERENCE_CODE,
        @USER_ID,
        @REQUESTOR_ID,
	@SCORE_YES,
	@SCORE_NO,
	@SCORE_FINAL,
	GETDATE()
)

Open in new window

0
 

Author Comment

by:TECH_NET
ID: 24371092
I get the following error
 Incorrect syntax near ')'.
0
 

Author Closing Comment

by:TECH_NET
ID: 31580819
The final query was missing a END. (BEGIN END BLOCK). Thanks for everything.

The explaination was simple and easy to follow.
0
 
LVL 8

Expert Comment

by:Bobaran98
ID: 24371166
Great!  Sorry about that END block... trying to go from memory here! :-)
0

Featured Post

Webinar: Aligning, Automating, Winning

Join Dan Russo, Senior Manager of Operations Intelligence, for an in-depth discussion on how Dealertrack, leading provider of integrated digital solutions for the automotive industry, transformed their DevOps processes to increase collaboration and move with greater velocity.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

There are some very powerful Data Management Views (DMV's) introduced with SQL 2005. The two in particular that we are going to discuss are sys.dm_db_index_usage_stats and sys.dm_db_index_operational_stats.   Recently, I was involved in a discu…
by Mark Wills Attending one of Rob Farley's seminars the other day, I heard the phrase "The Accidental DBA" and fell in love with it. It got me thinking about the plight of the newcomer to SQL Server...  So if you are the accidental DBA, or, simp…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question