ManiMuthuPandi ManiMuthuPandi - 1 year ago 101
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 Source

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
FROM  TableName
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download