我有一个表格,以下面的格式存储计划生产。我需要得到本周的工作日期。
+------------+-------------+---------+----------+---------+------+--------------+---------------------------------------------+
| planned_id | per_quarter | per_day | per_week | quarter | year | working_days | working_dates | |
+------------+-------------+---------+----------+---------+------+--------------+---------------------------------------------+
| 1 | 860 | 14.10 | 70.50 | 2 | 2018 | 61 | 02-04-2018,03-04-2018,04-04-2018,05-04-2018 |
| 06-04-2018,09-04-2018,12-04-2018,15-04-2018
+------------+-------------+---------+----------+---------+------+--------------+---------------------------------------------+
我想知道本周的工作日期。我使用的查询如下。我不知道我哪里出错了。
SELECT Value from ( SELECT SUBSTRING_INDEX(SUBSTRING_INDEX(working_dates, ',', n.n), ',', -1) Value
FROM planned_production t CROSS JOIN
(
SELECT a.N + b.N * 10 + 1 n
FROM
(SELECT 0 AS N UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9) a
,(SELECT 0 AS N UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9) b
ORDER BY n
) n
WHERE quarter = quarter(curdate()) and n.n <= 1 + (LENGTH(t.working_dates) - LENGTH(REPLACE(t.working_dates, ',', '')))
) as TEMP_TABLE
WHERE Value between CURRENT_DATE() - INTERVAL
WEEKDAY(CURRENT_DATE()) + 7 DAY
AND
(CURRENT_DATE() - INTERVAL WEEKDAY(CURRENT_DATE()) DAY) - INTERVAL 1
SECOND ORDER BY Value asc;
我需要这样的输出
+------------+
| value |
+------------+
| 17-05-2018 |
| 18-05-2018 |
+------------+
欢迎您提出任何建议,并提前表示感谢。
1条答案
按热度按时间rkue9o1l1#
表结构:
表值:
sql查询:
输出:
逻辑:
1) 首先创建表dim\u date。您可以使用上面查询中所示的查询来创建它,但我建议您在数据库中创建一个表。
2) 使用筛选器仅选择一周
3) 在dim\ u表和test之间使用交叉连接
4) 使用find\ in\ u set函数检查日期是否存在。