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
206 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

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Question has a verified solution.

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

Load balancing is the method of dividing the total amount of work performed by one computer between two or more computers. Its aim is to get more work done in the same amount of time, ensuring that all the users get served faster.
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.
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed

867 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now