Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi All,

If I'm having a table looks like this:
(ID,fName,lName,choice1,choice2)
If I select * myTable where Id=1 it should return 1 row with 2 choices
I would like to select from the table same row twice with one of the choices everytime as follow:
1,John , Doe, choice1
1,John, Doe, choice2

Any ideas how to implement this without using union?
Any help will be appreciated

What I have tried:

I tried using union which should work
SQL
Select Id,fName,lName,choice1
from myTable
WHERE choice1>0
UNION
Select Id,fName,lName,choice2
from myTable
WHERE choice2>0

--I'm looking for a solution that doesn't use union
Posted
Updated 4-Jun-18 14:39pm
Comments
Mohibur Rashid 4-Jun-18 19:51pm    
Why do you hate UNION?

If you are having problem with getting both item, use
UNION ALL
Samira Radwan 4-Jun-18 20:06pm    
it's a requirement not to use UNION or UNION ALL
Mohibur Rashid 4-Jun-18 20:18pm    
It's a homework!
Samira Radwan 4-Jun-18 20:23pm    
no, my boss doesn't like UNION !!
said need to be done in other way, which i don't really know
Mohibur Rashid 4-Jun-18 20:25pm    
that's odd! try UNPIVOT, I am not sure if it would help https://stackoverflow.com/questions/18026236/sql-server-columns-to-rows

1 solution

A sample test for you with UNPIVOT, ran in sqlfiddle.com
SQL
-- SCHEMA


CREATE TABLE geek
    (id int, [choice1] int, [choice2] int)
;

INSERT INTO geek
    ([id], [choice1], [choice2])
VALUES
    (1, 10, 20),
    (2, 30, 0),
    (3, 0, 40)
;


SQL
-- query
SELECT
    id
  , choice
FROM geek
UNPIVOT (
  choice
  FOR value in (choice1, choice2)
) unpiv

WHERE choice > 0
;


SQL
--result
id	choice
1	10
1	20
2	30
3	40


sqlfiddle link[^]
 
Share this answer
 
v2

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900