Pergunta

I have a table where each data has a date when it was inserted in form of two columns, year and week:

+------+------+-------+
| Week | Year | Value |
+------+------+-------+
| 1    | 2014 | 5     |
| 5    | 2014 | 23    |
| 6    | 2014 | 12    |
| 7    | 2014 | 43    |
| 8    | 2014 | 4     |
| 9    | 2014 | 2     |
| 26   | 2013 | 21    |
| 27   | 2013 | 17    |
| 28   | 2013 | 42    |
| 31   | 2013 | 5     |
| ...  | ...  | ..    |
+------+------+-------+

I need a query to get data that was inserted between two dates (year, week). I guess that it should be alternative to intersection of two queries, one with a starting date and the second with ending data, but I can't get it to work. Any help or sugestion?

Here is my shot but INTERSECT is not supported in MySql:

(SELECT SUM(Duration), Week, Type, Year 
 FROM UP26rotordowntime 
 WHERE (Year>=2013) 
 AND (Week >=01) 
 GROUP BY Week)

INTERSECT

(SELECT SUM(Duration), Week, Type, Year 
 FROM UP26rotordowntime 
 WHERE (Year<=2014) 
 AND (Week <=14) 
 GROUP BY Week)
Foi útil?

Solução

You can put simple logic in WHERE conditions and use (year,week) pairs for GROUP BY:

SELECT SUM(Duration), Week, Type, Year 
FROM UP26rotordowntime 
WHERE Year = 2005 AND Week >= 10
   OR Year BETWEEN 2006 AND 2013
   OR Year = 2014 AND Week <= 14
GROUP BY Year,Week

Outras dicas

If you have id the query is very simple:

SELECT SUM(Duration), Week, Type, Year FROM UP26rotordowntime 
WHERE ID IN (
  SELECT ID FROM UP26rotordowntime WHERE (Year>=2013) AND (Week >=01) )
OR ID IN (
  SELECT ID FROM UP26rotordowntime WHERE (Year<=2014) AND (Week <=14))
GROUP BY Week

This should return you the intersect you need

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top