I have a table:
Type (PK) Year (PK) Value
Default 2000 1
Default 2001 2
UserFooAdjusted 2001 3
UserBarAdjusted 2001 4
What is the fastest SQL query that returns distinct years and if there are two rows with the same year then the user adjusted value is returned? There are multiple users. The query returns data for one user. Example result:
Year Value
2000 1
2001 3
I’ve thougt about A UNION B but how do I always keep the duplicate from B? Maybe a better solution would be to divide the current table into two tables: one would contain default values and the other would contain user adjusted vaules?
Assuming that there are only two types: Default and UserAdjusted, you can use
Common Table ExpressionandWindow Functions, in sql server.