*I figured out a more efficient and elgegant solution to this* SELECT CASE WHEN (select max(id) from Seat)%2 = 1 and id = (SELECT max(id) FROM Seat) THEN id WHEN id%2=1 THEN id+1 WHEN id%2=0 THEN id-1 END AS id, student FROM Seat ORDER BY id
with cte as ( select student, case when id%2 = 1 then id+1 else id-1 end as new_id from seat order by new_id) select ROW_NUMBER() over()id ,student from cte
with cte1 as( select case when id = (select max(id) from Seat) then id when id%2 = 0 then id-1 when id%2 = 1 then id+1 else 'null' end as id , student from Seat where (select max(id) from Seat)%2 = 1 order by id asc) , cte2 as ( select case when id%2 = 0 then id-1 when id%2 = 1 then id+1 else 'null' end as id , student from Seat where (select max(id) from Seat)%2 = 0 order by id asc ) select * from cte1 union all select * from cte2
Simple way:/* Write your T-SQL query statement below */ with cte as (select *, lag(student) over(order by id) as prev, lead(student) over(order by id) as nextt from seat) select id,case when id%2=0 then prev when id%2!=0 and nextt is not null then nextt else student end as student from cte
with cte as ( select id, student as original_student, case when id % 2 = 1 then lead(student,1) over(order by id) else lag(student,1) over(order by id) end as student from seat) select id, case when student is null then original_student else student end as student from cte
How about this? SELECT id, CASE WHEN id % 2 = 1 THEN LEAD(student,1, student) OVER (ORDER BY id) WHEN id % 2 = 0 THEN LAG(student) OVER (ORDER BY id) END AS student FROM Seat;
select a.id , CASE WHEN B.STUDENT IS NULL THEN a.STUDENT WHEN a.id%2 =0 THEN c.STUDENT else b.student end as student From Seat a left join Seat b on a.id+1 = b.id left join Seat c on a.id = c.id + 1
with cte1 as ( select id,student,lead(id) over(order by id) as next, lag(id) over(order by id) as prev from seat), cte2 AS ( SELECT id, student, CASE WHEN id = (SELECT COUNT(*) FROM seat) THEN CASE WHEN id % 2 != 0 THEN id ELSE prev END ELSE CASE WHEN id % 2 = 0 THEN prev ELSE next END END AS swap_id FROM cte1 ) select swap_id as id,student from cte2 order by id;
WITH cte AS ( SELECT id, student, LAG(student) OVER (ORDER BY id) AS prev_name, LEAD(student) OVER (ORDER BY id) AS next_name FROM Seat ) SELECT id, CASE WHEN id % 2 = 1 AND next_name IS NOT NULL THEN next_name WHEN id % 2 = 0 THEN prev_name ELSE student END AS student FROM cte ORDER BY id;
Best approach, getting previous and next is easier to understand than, let's say id+1 and id-1
Glad that you found the video useful 😊
*I figured out a more efficient and elgegant solution to this*
SELECT
CASE WHEN (select max(id) from Seat)%2 = 1 and id = (SELECT max(id) FROM Seat) THEN id
WHEN id%2=1 THEN id+1
WHEN id%2=0 THEN id-1 END AS id,
student
FROM Seat
ORDER BY id
I too the got the same way
Your videos are very helpful. Thanks
Good Explanation Sir !
The approach is really good 👍
Glad that you found the video useful, Snehal.
hi !! Great video . I just want to ask how to develop logic building skills for SQL Queries
Nice approach and well explained. Thank you!
Glad that you found the videos useful.
with cte as (
select student,
case when id%2 = 1 then id+1 else id-1 end as new_id
from seat
order by new_id)
select ROW_NUMBER() over()id ,student from cte
with cte1 as(
select case when id = (select max(id) from Seat) then id
when id%2 = 0 then id-1
when id%2 = 1 then id+1 else 'null' end as id , student
from Seat
where (select max(id) from Seat)%2 = 1
order by id asc) , cte2 as
(
select case when id%2 = 0 then id-1
when id%2 = 1 then id+1 else 'null' end as id , student
from Seat
where (select max(id) from Seat)%2 = 0
order by id asc )
select * from cte1
union all
select * from cte2
Simple way:/* Write your T-SQL query statement below */
with cte as
(select *,
lag(student) over(order by id) as prev,
lead(student) over(order by id) as nextt
from seat)
select id,case when id%2=0 then prev
when id%2!=0 and nextt is not null then nextt
else student end as student
from cte
with cte as (
select id, student as original_student, case when id % 2 = 1 then lead(student,1) over(order by id) else
lag(student,1) over(order by id) end as student
from seat)
select id, case when student is null then original_student else student end
as student
from cte
How about this?
SELECT
id,
CASE
WHEN id % 2 = 1 THEN LEAD(student,1, student) OVER (ORDER BY id)
WHEN id % 2 = 0 THEN LAG(student) OVER (ORDER BY id)
END AS student
FROM
Seat;
how are you handling the last column case where the id can be odd?
select a.id , CASE WHEN B.STUDENT IS NULL THEN a.STUDENT
WHEN a.id%2 =0 THEN c.STUDENT else b.student end as student
From Seat a left join Seat b
on a.id+1 = b.id
left join Seat c
on a.id = c.id + 1
with cte1 as (
select id,student,lead(id) over(order by id) as next,
lag(id) over(order by id) as prev
from seat),
cte2 AS
( SELECT id, student,
CASE
WHEN id = (SELECT COUNT(*) FROM seat) THEN
CASE
WHEN id % 2 != 0 THEN id
ELSE prev
END
ELSE
CASE
WHEN id % 2 = 0 THEN prev
ELSE next
END
END AS swap_id FROM cte1 )
select swap_id as id,student from cte2 order by id;
WITH cte AS (
SELECT id, student,
LAG(student) OVER (ORDER BY id) AS prev_name,
LEAD(student) OVER (ORDER BY id) AS next_name
FROM Seat
)
SELECT id,
CASE
WHEN id % 2 = 1 AND next_name IS NOT NULL THEN next_name
WHEN id % 2 = 0 THEN prev_name
ELSE student
END AS student
FROM cte
ORDER BY id;