Wednesday, March 28, 2012

Query

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

idCol1Col2maxValues
1aabb10
2ccdd10
3eeff10
4gghh10
5jjkk10

table 2

idCol1Col2CurrentValue
1ccdd7
2gghh3
3jjkk5

table 3

idCol1Col2AvailableEntries
1aabb10
2ccdd7
3eeff10
4gghh3
5jjkk5

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 youSmile [:)]

No comments:

Post a Comment