I Want to Write a query Country1 being my priority

15 Views Asked by At

Country1 should be picked sorting by last modified date.

In my table for a particular Id for the latest date(last modified date "11/3/2023, 08:11 PM") the country1 is null, so it should move n pick the next value where the Country1 is not null. how can we do that?

and if all the values in Country1 are null then choose whatever value is present In Country2 and so on.

table

I tried this:

SELECT 
  *, 
  ROW_NUMBER() OVER(
    ORDER BY 
      CASE WHEN Country1 IS NOT NULL THEN LastModifiedDate END DESC
  ) AS RN 
FROM 
  Table

But this would only give me Id's where the Country1 is not null, if the Country1 is null then I would loose the Id. I don't want to lose the Id, I want the SQL to move n choose the country2 if Country1 is null.

0

There are 0 best solutions below