ManiMuthuPandi ManiMuthuPandi - 4 months ago 19
SQL Question

How to use case here

I need to create a SQL query to generate the below output from DB.

enter image description here

I am searching a condition like this


When week_val is 1 then hour_val should come from week_val 2 row.
Otherwise hour_val comes from same row.


If I missed anything for your understanding, please comment, don't down vote

Answer

You may need subquery with CASE Statment

SELECT CASE WHEN week_val =1 THEN (SELECT TOP 1 hour_val FROM TABLE NAME WHERE week_val <> 1)
       ELSE  hour_val
       END
FROM  TableName
Comments