Click here to Skip to main content
14,643,383 members
Rate this:
Please Sign up or sign in to vote.
See more:
MS SQL SERVER 2008

DECLARE @Request AS TABLE (RESPONSE VARCHAR (30), ATTRIBS DECIMAL(18,2))
INSERT INTO @Request VALUES('HAPPY' ,100)
INSERT INTO @Request VALUES('SATISFIED' , 75)
INSERT INTO @Request VALUES('NEUTRAL' , 50)
INSERT INTO @Request VALUES('UNHAPPY' , 25)
INSERT INTO @Request VALUES('NO-RESPONSE', NULL)


TABLE RESPONSE
----------------------
BRN_NAME
RESPONSE1
RESPONSE2
RESPONSE3
RESPONSE4





SELECT A.BRN_NAME ,
A.RESPONSE1,
A.RESPONSE2,
A.RESPONSE3,
A.RESPONSE4

FROM RESPONSE A
LEFT JOIN @Request C ON A.RESPONSE1 =C.RESPONSE

I have a list of responses

A.RESPONSE1,
A.RESPONSE2,
A.RESPONSE3,
A.RESPONSE4

which I need to get their values by linking to the Lookup table @Request


How do I complete the Select Statement to get the values equivalent of
A.RESPONSE1,
A.RESPONSE2,
A.RESPONSE3,
A.RESPONSE4

What I have tried:

This is a program segment under construction
Posted
Updated 5-Nov-17 0:14am
v2

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

When I suggested that you create a table yesterday: Computing the averages based on counts[^] I didn't suggest a table variable: make it a "permanent" table, just like your existing table is.

Then - as I suggested - use a JOIN:
SELECT a.Brn_name, b.Attribs, c.Attribs, d.Attribs, e.Attribs 
FROM Response a
JOIN Requests b ON a.RESPONSE1 = b.Response
JOIN Requests c ON a.RESPONSE2 = c.Response
JOIN Requests d ON a.RESPONSE3 = d.Response
JOIN Requests e ON a.RESPONSE4 = e.Response
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100