我有一个表格,其中每个数据都有一个日期,它以两列,一年和一周的形式插入:
+------+------+-------+
| 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 |
| ... | ... | .. |
+------+------+-------+
我需要一个查询来获取在两个日期(年,周)之间插入的数据。我猜它应该是两个查询的交集的替代方案,一个具有开始日期,第二个具有结束数据,但我无法使其工作。任何帮助或消化?
这是我的镜头,但MySql不支持INTERSECT:
(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)
答案 0 :(得分:1)
您可以在WHERE条件中放置简单逻辑,并为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
答案 1 :(得分:0)
如果您有id,则查询非常简单:
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
这应该返回你需要的交叉点