Solved

MS SQL: make sure same or similar text values are not selected into a temp table when selecting from multiple tables

Posted on 2011-09-14
3
208 Views
Last Modified: 2012-05-12
Hi,

I have an application that basically works like this:

User One selects her favorite names from a set list of choices (we'll say two, but it changes).  I create a dynamic checkbox list that includes:

a) her correct answers that user two is trying to guess (tblRightAnswers)
b) additional answers from the set list of choices (tblSetChoices)
c) some silly and obviously wrong answers (tblFunnyStuff)

Getting the random table is easy (code attached), but the problem I'm having is that I can have her correct answer (let's say Jen) and also a "wrong" version from the tblSetChoices of Jen (since it was a set choice).  I can also have Jenny as silly answer.  I want to write the queries so that answers cannot be selected into the temp table if they are similar to answers already in the table.  I'm sure there's a way to do it through LEFT JOIN or something, but I'm no SQL specialist.  

Please help (code attached)

kmt
@questionID int = 0, 
	@userID nvarchar(20) = ''

	DECLARE @x int
	DECLARE @dropdown TABLE
	(
		[answerString] nvarchar(MAX),
		[realdeal] bit
	)

    -- Retrieve real values
    	INSERT INTO @dropdown(answerString, realdeal)
	SELECT      answerString, 1 
	FROM        tblRightAnswers
	WHERE     (questionID = @ questionID) AND (userID = @userID)
	
	-- Determine number of fake values
	SELECT @x = COUNT(*) FROM @dropdown
	SET @x = 6 - @x
	SET ROWCOUNT @x
	
	-- Retrieve fake values
	INSERT INTO @dropdown(answerString, realdeal)
	SELECT choice, 0 
	FROM tblSetChoices
WHERE questionID = @questionID	
ORDER BY NEWID()
	SET ROWCOUNT 2
	
	-- Retrieve fun fake values
	INSERT INTO @dropdown(answerString, realdeal)
	SELECT answerString, 0
	FROM      tblFunnyStuff
	WHERE     (questionID = @questionID)
	ORDER BY NEWID()
	SET ROWCOUNT 0
	
	SELECT * FROM @dropdown ORDER BY NEWID()

Open in new window

0
Comment
Question by:kmt333
  • 2
3 Comments
 
LVL 59

Accepted Solution

by:
Kevin Cross earned 500 total points
ID: 36539763
Hi.

In your INSERT statements, you can add a WHERE condition to ensure that no answerString is = to or LIKE your current ones.

for example:
-- Retrieve fake values
INSERT INTO @dropdown(answerString, realdeal)
SELECT choice, 0
FROM tblSetChoices
WHERE questionID = @questionID      
AND NOT EXISTS (
   SELECT 1
   FROM @dropdown
   WHERE answerString = choice
)

ORDER BY NEWID()

OR ...

-- Retrieve fake values
INSERT INTO @dropdown(answerString, realdeal)
SELECT choice, 0
FROM tblSetChoices
WHERE questionID = @questionID      
AND NOT EXISTS (
   SELECT 1
   FROM @dropdown
   WHERE answerString LIKE ('%' + choice + '%')
   OR choice LIKE ('%' + answerString + '%')
)

ORDER BY NEWID()

You may be able to play with something like SOUNDEX() -- http://msdn.microsoft.com/en-us/library/ms187384.aspx -- but see if one of the above works for you.

Kevin
0
 
LVL 59

Assisted Solution

by:Kevin Cross
Kevin Cross earned 500 total points
ID: 36539773
Actually, for completeness, here is the SOUNDEX option.

-- Retrieve fake values
INSERT INTO @dropdown(answerString, realdeal)
SELECT choice, 0
FROM tblSetChoices
WHERE questionID = @questionID      
AND NOT EXISTS (
   SELECT 1
   FROM @dropdown
   WHERE SOUNDEX(answerString) = SOUNDEX(choice)
)

ORDER BY NEWID()

I tested: 'Jen' and 'Jenny' correlated correctly.
0
 
LVL 4

Author Closing Comment

by:kmt333
ID: 36543408
Thank you both!  This did the trick
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
I have a large data set and a SSIS package. How can I load this file in multi threading?
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

840 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