Click here to Skip to main content
15,888,984 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
In sql server i have two tables. One table for questions and another table for options. Both tables are having questionId. Based on questionid get the options for the particular question. If i use join query i dont get the expected result. I want result like the first row contains question with first option, the remaining rows are only optioons. Then next question, how can i achieve this? I need to call this through api then return to MVC views.

What I have tried:

I write the query and changed it. But i am not get the expected result.
Posted
Updated 12-Oct-16 21:37pm

1 solution

If I understand your question correctly, you would like an output like:
Question 1   Option 1
             Option 2
             Option 3
Question 2   Option 1
             Option 2
             Option 3
             Option 4
...

If this is the case, then I believe that you're doing formatting in the wrong layer. SQL is not designed to format data, only to fetch and manipulate it. So if you want to do formatting you should use client side tools which are designed for this or for example reporting services in SQL Server.

Having that said, if you really want to eliminate the repeating value, you can achieve this using LAG/LEAD. Have a look at How to fetch data from the previous or next rows in the resultset[^]

ADDED:

Taken that I understood the requirement correctly, have a look at How to format repeating values in SQL result set[^]
 
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