sql - Creating a subset of a table using unique columns -


table1:

+-----+---------+---------+-------------+   id  |  val1   |  val2   |   date +-----+---------+---------+-------------+    1  |   10    |   11    |  2015-05-10    2  |   10    |   11    |  2015-05-17    3  |   10    |   11    |  2015-05-23    4  |   9     |   17    |  2015-08-10    5  |   9     |   17    |  2015-08-15 +-----+---------+---------+-------------+ 

table2

+-----+---------+---------+-------------+   id  |  val1   |  val2   |   date +-----+---------+---------+-------------+    3  |   10    |   11    |  2015-05-23    5  |   9     |   17    |  2015-08-15 +-----+---------+---------+-------------+ 

i write sql allow me go table1 table2. table2 subset of table1 recent date unique pair of val1 , val2.

to unique combinations of val1 , val2 need table1 can use:

select distinct val1, val2   table1 

the part stuck on selecting subset table 1 using unique combinations. if following, return of results table1:

select val1, val2, date table1 exists( select distinct val1, val2 table1) 

if select distinct on values including date return them all. have insight problem?

you can try one:

select max(id), val1, val2, max(date) table1 group val1, val2 

Comments