I am using SQL Server.
I have a table item_table like this:
item age
--------------
1 1
1 6
2 2
I have the other table price_table like this:
item pricetype price
--------------------------
1 O 5
1 P 6
1 V 7
2 O 8
2 P 9
2 V 10
So, I want to inner join above two tables.
select *
from item_table i
inner join price_table p
on ...
There are some conditions about the on:
- if the average of age of an item is bigger than
3, then I do:inner join price_table on pricetype = 'O' or pricetype = 'P' - If not, then I do:
inner join price_table on pricetype = 'O' or pricetype = 'P' or pricetype = 'V'
So there are conditions for on conditions.
How can I write the select query?
Edit:
I changed the condition to be average of age, instead of type
SQL Fiddle Example
Output: