| Author |
Topic |
|
prakashdotc
Starting Member
25 Posts |
Posted - 2009-05-28 : 08:06:10
|
| Hi All,I have "Answers" table with following columns "CustomerID, QuestionID, Answers"CustomerID foreign key to Customer Table which holds customer detailsQuestionID foreign key to Question Table which holds number of QuestionsCustomer is asked to provide answers to 2 Question which is randomly selected from Question TableHow to Compare the 2 Answers provided by the customer with respect to CustomerID and QuestionID in the "Answers" TableIf I'm not clear with the Problem please comment, i'll try to present in a better way.Thanks |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2009-05-28 : 08:09:31
|
You have to explain better was "compare" means to you. E 12°55'05.63"N 56°04'39.26" |
 |
|
|
prakashdotc
Starting Member
25 Posts |
Posted - 2009-05-28 : 08:16:50
|
| Hi Peso,The Answers Table will have data as--------------------------------------------------------------CustomerID - QuestionID - Answers--------------------------------------------------------------12 - 1 - Jack12 - 2 - America12 - 3 - 812 - 4 - Jan Street12 - 5 - Drums12 - 6 - Football13 - 1 - Matt13 - 2 - China... --------------------------------------------------------------Customer is asked to provide answers to any of the 2 questions selected randomly,those answers has to be compared with the data in this table with respect toCustomerID, QuestionID and AnswersWe have CustomerID, Randomly Selected 2 QuestionID and Answers to those QuestionsThanks |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2009-05-28 : 08:20:56
|
so what is the expected result like ? KH[spoiler]Time is always against us[/spoiler] |
 |
|
|
prakashdotc
Starting Member
25 Posts |
Posted - 2009-05-28 : 08:25:33
|
| Hi khtan,Say that the Randomly Selected Question for the Customer is 2 and 4,So Customer will provide answers to these two questions.the Stored Procedure will have Following Input ParametersCustomerIDQuestion1IDAnswer1Question2IDAnswer2Based on this we have to validate the answers with respect to question id's for the customer |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2009-05-28 : 08:34:26
|
What do you want to validate?1. That Question1ID and Question2ID exists in the database?2. That the two ID hasn't been used before for that user?3. See if any other user has made the same answer? E 12°55'05.63"N 56°04'39.26" |
 |
|
|
prakashdotc
Starting Member
25 Posts |
Posted - 2009-05-28 : 08:59:20
|
| Hi Peso,The Where Condition will be asSelect CustomerID from Answers WHERE Question1ID = Answer1 AND Question2ID = Answer2 AND CustomerID = 12 |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2009-05-28 : 09:11:58
|
SELECT CustomerIDFROM AnswersGROUP BY CustomerIDHAVING MAX(CASE WHEN QuestionID = @Question1ID THEN 1 ELSE 0 END) = 1AND MAX(CASE WHEN QuestionID = @Question2ID THEN 1 ELSE 0 END) = 1To get all customers which has answered both @Question1ID and @Question2ID E 12°55'05.63"N 56°04'39.26" |
 |
|
|
prakashdotc
Starting Member
25 Posts |
Posted - 2009-05-28 : 09:32:58
|
| It's not just getting customer who has answered both @Question1ID and @Question2IDThe @Question1ID may the anything between 1 to 10 and same for @Question2ID alsoTwo Questions is Selected Randomly from the list of 10 Questions.Customer will provide appropriate answers to those questions.Those answers provided by the customer has to be compared with the Answers Table for that customer with appropriate questionID. |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2009-05-28 : 09:44:40
|
[code]CREATE PROCEDURE dbo.uspMyAnswerProcedure( @CustomerID INT, @Question1ID INT, @Answer1 VARCHAR(MAX), @Question2ID INT, @Answer2 VARCHAR(MAX))ASSET NOCOUNT ONUPDATE AnswersSET Answer = @Answer1WHERE QuestionID = @Question1IDIF @@ROWCOUNT = 0 INSERT Answers SELECT CustomerID, @Question1ID, @Answer1UPDATE AnswersSET Answer = @Answer2WHERE QuestionID = @Question2IDIF @@ROWCOUNT = 0 INSERT Answers SELECT CustomerID, @Question2ID, @Answer2END[/code] E 12°55'05.63"N 56°04'39.26" |
 |
|
|
prakashdotc
Starting Member
25 Posts |
Posted - 2009-05-28 : 10:43:00
|
| Hi Peso,This is not to insert into the Table, Just to Validate the weather Customers Answer to the Respective Question ID is matching (correct) or not. |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2009-05-28 : 11:01:16
|
[code]CREATE FUNCTION dbo.fnMyAnswerCheck( @CustomerID INT, @Question1ID INT, @Answer1 VARCHAR(MAX), @Question2ID INT, @Answer2 VARCHAR(MAX))RETURNS BITASBEGIN DECLARE @A1 BIT, @A2 BIT SELECT @A1 = 0, @A2 = 0 IF EXISTS (SELECT * FROM Answers WHERE CustomerID = @CustomerID AND QuestionID = @Question1ID AND Answer = @Answer1) SET @A1 = 1 IF EXISTS (SELECT * FROM Answers WHERE CustomerID = @CustomerID AND QuestionID = @Question2ID AND Answer = @Answer2) SET @A2 = 1 RETURN @A1 & @A2END[/code] E 12°55'05.63"N 56°04'39.26" |
 |
|
|
prakashdotc
Starting Member
25 Posts |
Posted - 2009-06-10 : 00:13:37
|
Hi Peso,Thank for u'r Help and sorry for late reply. And here is by SP, which i framed.CREATE PROCEDURE ValidateAnswers @CustomerID INT, @Question1ID INT, @Answer1 NVARCHAR(100), @Question2ID INT, @Answer2 NVARCHAR(100), @Valid INT OUTPUTASBEGIN IF EXISTS(SELECT CustomerID FROM ANSWERS WHERE QuestionID = @Question1ID AND Answers = @Answer1 AND CustomerID = @CustomerID) BEGIN IF EXISTS(SELECT CustomerID FROM ANSWERS WHERE QuestionID = @Question2ID AND Answers = @Answer2 AND CustomerID = @CustomerID) BEGIN SET @Valid = 1 END ELSE BEGIN SET @Valid = 0 END END ELSE BEGIN SET @Valid = 0 ENDEND |
 |
|
|
|