So the problem is that I have a dataset where each row contains the following data:
ID, data, year, meta
The data contained in different year varies a bit, but some data is there every year. I want to query data that only exists in some years, but I also want to query the data that exists every year in the query.
For example:
- DATA1 can be in years 2011-2013,
- while DATA2 can be in years 2009-2015.
I want to query for both of them, where they have a matching ID and both exist only in that year. So far a working solution that I have is this.
SELECT ID, data, year, meta
FROM table1 WHERE year IN
(
SELECT year
FROM table1
WHERE meta LIKE 'DATA1'
)
AND meta LIKE 'DATA2'
UNION ALL
(
SELECT ID, data, year, meta
FROM table1
WHERE meta LIKE 'DATA1'
)
ORDER BY year, ID
the problem here is that the query
SELECT ID, data, year, meta
FROM table1
WHERE meta LIKE 'DATA1'
is repeated once (it is used as a condition in the first query, and then it is also queried for itself. I was wondering if there's a way to query for what I want, without repeating the above query. I.e. i want to do a query, return the results, then do another query that only has data that overlaps within the first query while only doing two queries in total. example input:
122, DATA1_datapoint, 2010, DATA2
122, DATA1_datapoint, 2009, DATA2
123, DATA1_datapoint, 2011, DATA2
123, DATA2_datapoint, 2011, DATA1
124, DATA1_datapoint, 2012, DATA2
124, DATA2_datapoint, 2012, DATA1
Expected output (id is 3-digit number, e.g. 123):
123, DATA1_datapoint, 2011, DATA1
123, DATA2_datapoint, 2011, DATA2
124, DATA1_datapoint, 2012, DATA1
124, DATA2_datapoint, 2012, DATA2