[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
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
Medium Priority
?
215 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 60

Accepted Solution

by:
Kevin Cross earned 2000 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 60

Assisted Solution

by:Kevin Cross
Kevin Cross earned 2000 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

What’s Wrong with Your Cloud Strategy ?

Even as many CIOs are embracing a cloud-first strategy, the reality is that moving to the cloud is a lengthy process and the end-state is likely to be a blend of multiple clouds—public and private. Learn why multicloud solutions matter in this webinar by Nimble Storage.

Question has a verified solution.

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

In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
This month, Experts Exchange sat down with resident SQL expert, Jim Horn, for an in-depth look into the makings of a successful career in SQL.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

649 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