Skip to content
Advertisement

how to get column from tables in priority

I have three table that has priority for select column in SELECT statement.

Suppose table A has some columns like:

Table A:

Table B:

Table C:

Desired result:

Rules for getting value from columns:

1- Table C has a higher priority than Table B, that means if both of them has value in the same column, result picking up from Table C, Except when the value is null

2- Result can be GROUP BY on purchase, type, market

3- Result Has FULL JOIN, that means if a row has an equivalent row on another side, use priority for get value, if not whole of row come in result

4- Priority for choose value for columns (rate | max | min):

  • If column rate in TABLE C has value, without considering value on TABLE B ==> result pick from TABLE C
  • If column rate in TABLE C is null but has value in TABLE B ==> result pick from TABLE B

Advertisement

Answer

This is using sql server syntax, I’m sure you can change as required:

First set up sample data:

Then the query:

Add whatever sorting your require.

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement