SQL Server Order by joined with unpivot table with pagination

syqv5f0l  于 2023-05-05  发布在  其他
关注(0)|答案(2)|浏览(95)

I have a 3 tables, I need to apply sorting and pagination.

Create Table Interviews(
Id int primary key,
StartDate datetime2,
LocationName varchar(50),
)

Create Table Questions(
Id int primary key,
Title varchar(500)
)

Create Table Answers(
Id int primary key,
AnswerText varchar(500),
QuestionId int,
InterviewId int,
)

ALTER TABLE Answers ADD FOREIGN KEY (QuestionId) REFERENCES Questions(Id);
ALTER TABLE Answers ADD FOREIGN KEY (InterviewId) REFERENCES Interviews(Id);

Tables Diagram

Questions Table

Interviews Table

Answers Table

I need to sort by (first name, age).

I tried:

SELECT *
FROM
(
    SELECT a.Id,
           a.AnswerText,
           a.QuestionId,
           a.InterviewId,
           i.LocationName,
           ROW_NUMBER() OVER (ORDER BY a.AnswerText) AS InterviewOrderNumber --Not correct
    FROM Interviews i
        LEFT JOIN Answers a
            ON i.Id = a.InterviewId
) t
WHERE InterviewOrderNumber BETWEEN 1 AND 5

I hope this result.

0yg35tkg

0yg35tkg1#

with paginated as (
    SELECT a.Id as AnswerId,
           a.AnswerText,
           a.QuestionId,
           a.InterviewId,
           i.LocationName,
           min(case when q.Title = 'First Name' then a.AnswerText end) over (
               partition by i.Id) as Name,
           min(case when q.Title = 'Age' then cast(a.AnswerText as int) end) over (
               partition by i.Id) as Age
    FROM Interviews i
         INNER JOIN Answers a ON i.Id = a.InterviewId
         INNER JOIN Questions q ON q.id = a.QuestionId
)
select *,
    dense_rank() over (order by Name, Age, InterviewId) as InterviewOrderNumber
from paginated;

https://dbfiddle.uk/LlBpd2x-

6ioyuze2

6ioyuze22#

I hope I understood correctly your question

You wanted to be sorted based on first name and age of the questions and then pageing the data based on that sorting

;with _listQuestion as(

        select *,case 
                     when Title='FirstName' then 1
                     when Title='age' then 2 
                     else 3 end as InterviewOrderNumber  
        from Questions
)
select *
from (
        select a.*,b.LocationName,c.InterviewOrderNumber,c.Id as iq ,c.Title
        , row_number() OVER (  ORDER BY InterviewOrderNumber ) AS rw
         FROM Answers  a
         inner join Interviews b on a.InterviewId=b.Id
         inner join _listQuestion c on  c.Id=a.QuestionId
 )t
 where t.rw between 1 and 5
Create Table Interviews(Id int primary key,StartDate datetime2,LocationName varchar(50),)
Create Table Questions(Id int primary key,Title varchar(500))
Create Table Answers(Id int primary key,AnswerText varchar(500),QuestionId int,InterviewId int,)

ALTER TABLE Answers ADD FOREIGN KEY (QuestionId) REFERENCES Questions(Id);
ALTER TABLE Answers ADD FOREIGN KEY (InterviewId) REFERENCES Interviews(Id);

insert into Questions values(1,'firstname' ),(2,'lastname'),(3,'age')
insert into Interviews values(1,'2022-05-01','usa'),( 2,'2023-05-01','uk') ,(3,'2024-05-01','fr')
insert into Answers values(1,'Mohmoud',1,1),(2,'akr',2,1),(3,'27',3,1),(4,'Ahmad',1,2)
insert into Answers values(5,'zen',2,2),(6,'30',3,2),(7,'obai',1,3),(8,'gh',2,3),(9,'27',3,3)

相关问题