Hi!
I need to create a query(stored procedure) that will select all rows from the second table and only the rows from table 1 where the maxValues for Col1 and Col2 do not exist in table 2 so that the result would look something like table 3
table 1
id | Col1 | Col2 | maxValues |
1 | aa | bb | 10 |
2 | cc | dd | 10 |
3 | ee | ff | 10 |
4 | gg | hh | 10 |
5 | jj | kk | 10 |
table 2
id | Col1 | Col2 | CurrentValue |
1 | cc | dd | 7 |
2 | gg | hh | 3 |
3 | jj | kk | 5 |
table 3
id | Col1 | Col2 | AvailableEntries |
1 | aa | bb | 10 |
2 | cc | dd | 7 |
3 | ee | ff | 10 |
4 | gg | hh | 3 |
5 | jj | kk | 5 |
Thanks for your help.
If ALL values in table2 are also in table1, and maxValue always >= currentValue, then this should work:select col1, col2, MIN(theValues)
from (
select col1, col2, maxValues as 'theValues'
from table1
UNION
select col1, col2, currentValue as 'theValues'
from table2
) as tempTable
group by col1, col2
|||It is perfect, thank you
No comments:
Post a Comment