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
209 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
[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
  • 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 eBook: Backup on AWS

Everything you need to know about backup and disaster recovery with AWS, for FREE!

Question has a verified solution.

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

International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

756 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