SQL - INSERT SELECT two columns without duplicates - mysql

I am trying to do two subqueries to populate my table with two columns. wk_start and wk_end.
Goal is to populate it with a specific range of date.
I've tried cross join but still having duplicates the same as this one
Here is my code
insert into bfs_dw.mpt_calendar (wk_start, wk_end)
select
ter.wk_start,
ter1.wk_end
from
(select * from
(select ADDDATE('2022-01-03', 7*(t4*10000 + t3*1000 + t2*100 + t1*10 + t0)) wk_start from
(select 0 t0 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t0,
(select 0 t1 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t1,
(select 0 t2 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t2,
(select 0 t3 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t3,
(select 0 t4 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t4) v
where wk_start < '2024-01-03'
order by wk_start ) ter
inner JOIN
(select * from
(select ADDDATE('2022-01-09', 7*(t4*10000 + t3*1000 + t2*100 + t1*10 + t0)) wk_end from
(select 0 t0 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t0,
(select 0 t1 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t1,
(select 0 t2 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t2,
(select 0 t3 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t3,
(select 0 t4 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t4) v
where wk_end < '2024-01-03'
order by wk_end ) ter1 ON 1 = 1
Output display below. which has a lot of duplicates after the insert.
What I am trying to do is this expected result, which has no duplicates once inserted:
Note: the two subqueries works well. Its output was the last image posted here.

Use the first subquery to get wk_start, and add 6 days to get the wk_end
insert into bfs_dw.mpt_calendar (wk_start, wk_end)
select wk_start, wk_start + INTERVAL 6 DAY wk_end from
(select ADDDATE('2022-01-03', 7*(t4*10000 + t3*1000 + t2*100 + t1*10 + t0)) wk_start from
(select 0 t0 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t0,
(select 0 t1 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t1,
(select 0 t2 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t2,
(select 0 t3 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t3,
(select 0 t4 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t4) v
where wk_start < '2024-01-03'

Related

Employee attendance calendar report design in iReport [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
I have different tables for track the employee attendance and holidays all things are done and working fine now I want to create a report for employee monthly calendar report facing some issues.
First thing is that
All tables are:
1. hr_employee
2. hr_biometric attendance
1. hr_employee_leave
1. hr_employee_visits
1. hr_holidays
1. Before 15 dates all Saturdays are count as working days after 15 dates all Saturdays are company holidays
2. In holiday records are in the date range from date and to date not single date holidays same for all other tables
I have created a query but it retrieves multiple records, and it's a hard query for me to write. How can I do this?
Here is query:
SET #fromDate = "2019-12-01";
SET #toDate = "2019-12-31";
SET #empId = 2814;
SET #recordStatus = "ATT";
SELECT
att.time_checked AS checkedTIme, #recordStatus
FROM
hr_employee emp
JOIN hr_biometric_attendance att ON att.`employee_id` = emp.id
WHERE emp.id = #empId
AND att.`time_checked` BETWEEN #fromDate AND #toDate
UNION
SELECT v.selected_date, "HD" FROM
(SELECT ADDDATE('1970-01-01',t4.i*10000 + t3.i*1000 + t2.i*100 + t1.i*10 + t0.i) selected_date FROM
(SELECT 0 i UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) t0,
(SELECT 0 i UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) t1,
(SELECT 0 i UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) t2,
(SELECT 0 i UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) t3,
(SELECT 0 i UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) t4) v
,
(SELECT hDay.date_from df, hDay.date_to dt FROM `hr_holiday` hDay WHERE hDay.date_from >= #fromDate AND hDay.date_to <= #toDate) hDayQ
WHERE selected_date BETWEEN hDayQ.df AND hDayQ.dt
UNION
SELECT v1.selected_date1, "EV" FROM
(SELECT ADDDATE('1970-01-01',t41.i*10000 + t31.i*1000 + t21.i*100 + t11.i*10 + t01.i) selected_date1 FROM
(SELECT 0 i UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) t01,
(SELECT 0 i UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) t11,
(SELECT 0 i UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) t21,
(SELECT 0 i UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) t31,
(SELECT 0 i UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) t41) v1
,
(SELECT hEmpV.date_from df, hEmpV.date_to dt FROM `hr_employee_visit` hEmpV WHERE hEmpV.employee_id = #empId) hEmpVQ
WHERE selected_date1 BETWEEN hEmpVQ.df AND hEmpVQ.dt
UNION
SELECT v2.selected_date2, "EL" FROM
(SELECT ADDDATE('1970-01-01',t42.i*10000 + t32.i*1000 + t22.i*100 + t12.i*10 + t02.i) selected_date2 FROM
(SELECT 0 i UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) t02,
(SELECT 0 i UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) t12,
(SELECT 0 i UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) t22,
(SELECT 0 i UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) t32,
(SELECT 0 i UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) t42) v2
,
(SELECT hEmpL.date_from df, hEmpL.date_to dt FROM `hr_employee_leave` hEmpL WHERE hEmpL.employee_id = #empId) hEmpLQ
WHERE selected_date2 BETWEEN hEmpLQ.df AND hEmpLQ.dt
UNION DISTINCT
SELECT v3.selected_date3, (SELECT IF(DAYNAME(v3.selected_date3) ='Sunday' OR (DAYOFMONTH(v3.selected_date3) >= 15 AND DAYNAME(v3.selected_date3) ='saturday'),'H','W'))
FROM
(SELECT ADDDATE('1970-01-01',t43.i*10000 + t33.i*1000 + t23.i*100 + t13.i*10 + t03.i) selected_date3 FROM
(SELECT 0 i UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) t03,
(SELECT 0 i UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) t13,
(SELECT 0 i UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) t23,
(SELECT 0 i UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) t33,
(SELECT 0 i UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) t43) v3
WHERE selected_date3 BETWEEN #fromDate AND #toDate
ORDER BY `checkedTIme`;
Here is the erd tables:
Here is image for expected result report:
Here is Jasper result of above query:
Like I said in the comment, to simplify the query you should create a dedicated calendar table so you can shorten your query. Refer: https://www.db-fiddle.com/f/qVgmfDaVUszABAyFzH7iUS/0
Once that's done, you can re-write your query as following:
SET #fromDate = "2019-12-01";
SET #toDate = "2019-12-31";
SET #empId = 2814;
SET #recordStatus = "ATT";
SELECT att.time_checked AS checkedTIme, #recordStatus FROM hr_employee emp
JOIN hr_biometric_attendance att ON att.`employee_id` = emp.id
WHERE emp.id = #empId
AND att.`time_checked` BETWEEN #fromDate AND #toDate
UNION
SELECT v.selected_dates, "HD" FROM calendar v JOIN
(SELECT hDay.date_from df, hDay.date_to dt FROM `hr_holiday` hDay
WHERE hDay.date_from >= #fromDate AND hDay.date_to <= #toDate) hDayQ
ON selected_dates BETWEEN hDayQ.df AND hDayQ.dt
UNION
SELECT v1.selected_dates, "EV" FROM calendar v1 JOIN
(SELECT hEmpV.date_from df, hEmpV.date_to dt FROM `hr_employee_visit` hEmpV
WHERE hEmpV.employee_id = #empId) hEmpVQ
ON selected_dates BETWEEN hEmpVQ.df AND hEmpVQ.dt
UNION
SELECT v2.selected_dates, "EL" FROM calendar v2 JOIN
(SELECT hEmpL.date_from df, hEmpL.date_to dt FROM `hr_employee_leave` hEmpL
WHERE hEmpL.employee_id = #empId) hEmpLQ
ON selected_dates BETWEEN hEmpLQ.df AND hEmpLQ.dt
UNION DISTINCT
SELECT v3.selected_dates, (SELECT IF(DAYNAME(v3.selected_dates) ='Sunday'
OR (DAYOFMONTH(v3.selected_dates) >= 15 AND DAYNAME(v3.selected_dates) ='saturday'),'H','W'))
FROM calendar v3
WHERE selected_dates BETWEEN #fromDate AND #toDate
ORDER BY `checkedTIme`;
Now it's much easier to read then you can start with building your final query.

MySQL query to get row count when no data available, skip result when add where clause

My following query showing data count even when there is no data for that date.
SELECT c.date AS DATE, COUNT(s.insertTime) AS tot_cnt FROM (SELECT ADDDATE('1970-01-01',t4.i*10000 + t3.i*1000 + t2.i*100 + t1.i*10 + t0.i)
DATE FROM (SELECT 0 i UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) t0,
(SELECT 0 i UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) t1,
(SELECT 0 i UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) t2,
(SELECT 0 i UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) t3,
(SELECT 0 i UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) t4 ) c
LEFT JOIN `access_log` s ON c.date = DATE(s.insertTime)
WHERE UNIX_TIMESTAMP(c.date) BETWEEN 1563733801 AND 1564338599 GROUP BY c.date ORDER BY c.date;
But when I add userId=8, it skips all date range which don't have any value
SELECT c.date AS DATE, COUNT(s.insertTime) AS tot_cnt FROM (SELECT ADDDATE('1970-01-01',t4.i*10000 + t3.i*1000 + t2.i*100 + t1.i*10 + t0.i)
DATE FROM (SELECT 0 i UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) t0,
(SELECT 0 i UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) t1,
(SELECT 0 i UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) t2,
(SELECT 0 i UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) t3,
(SELECT 0 i UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) t4 ) c
LEFT JOIN `access_log` s ON c.date = DATE(s.insertTime)
WHERE UNIX_TIMESTAMP(c.date) BETWEEN 1563733801 AND 1564338599 AND userId=8 GROUP BY c.date ORDER BY c.date;
I need all date even they don't have value and also when apply userId=8 to filter records.
Apply the condition in the ON clause and not the WHERE clause:
ON c.date = DATE(s.insertTime) AND userId=8
so the LEFT JOIN returns the non matching rows.

Select every other day of week using SQL (MySQL)

The goal is to return a set of dates that correspond to the day of week requested.
For example: every other Monday between 2018-08-13 and 2018-12-31.
The following statement returns all dates and works great
select * from
(select adddate('2010-01-01',t4*10000 + t3*1000 + t2*100 + t1*10 + t0) DATES from
(select 0 t0 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t0,
(select 0 t1 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t1,
(select 0 t2 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t2,
(select 0 t3 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t3,
(select 0 t4 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t4) v
where DATES between '2018-08-13' and '2018-12-31'
and dayname(DATES) ='MONDAY'
So to return every other MONDAY I added the following
HAVING DATES % 2 = 0
I assumed that by taking MOD of dates it would, in this case return every MONDAY that met this criteria. Well it does not work. I've tried all sorts of combos but not getting it.
Any ideas?
Dates are a complex data structure and the details of how they are stored and manipulated vary between DBMSs. Most often you can't treat them as numeric data types as you are trying to do here. Instead you should calculate the number of days between the date of interest and a fixed date then you can take the modulus of that number: for example:
select * from
(select adddate('2010-01-01',t4*10000 + t3*1000 + t2*100 + t1*10 + t0) DATES from
(select 0 t0 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t0,
(select 0 t1 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t1,
(select 0 t2 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t2,
(select 0 t3 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t3,
(select 0 t4 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t4) v
where DATES between '2018-08-13' and '2019-01-31'
and dayname(DATES) ='MONDAY'
and datediff(dates,'2018-08-13') % 2 = 0
order by dates
Alternatively since every other Monday is 14 days apart you could use modulus 14 and drop the dayname(DATES) ='MONDAY' predicate:
select * from
(select adddate('2010-01-01',t4*10000 + t3*1000 + t2*100 + t1*10 + t0) DATES from
(select 0 t0 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t0,
(select 0 t1 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t1,
(select 0 t2 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t2,
(select 0 t3 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t3,
(select 0 t4 union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t4) v
where DATES between '2018-08-13' and '2019-01-31'
and datediff(dates,'2018-08-13') % 14 = 0
order by dates
This can be made to work with a few little modifications as follows:
SELECT * FROM
(
SELECT ADDDARE('1970-01-01', t4*10000 + t3*1000 + t2*100 + t1*10 + t0) selected_date FROM
(SELECT 0 t0 UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) t0,
(SELECT 0 t1 UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) t1,
(SELECT 0 t2 UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) t2,
(SELECT 0 t3 UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) t3,
(SELECT 0 t4 UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) t4
) v
WHERE
selected_date between '2018-08-08' AND '2018-10-19'
AND DAYOFWEEK(selected_date) = 2
AND WEEK(selected_date) % 2 = WEEK('2018-08-08') % 2
The idea is that it will include the Monday of the week that the selection period begins from. So, it doesn't matter whether the starting week is odd or even, you'd always get alternate Mondays without having to worry about specifying 0 or 1 for the result of modulo operation.

I cannot find the table I created in the schema

I have a questions when I try to create a table in mysql.
The syntax is below:
create table abc as
(select * from
(select adddate('2010-01-01',t5.i*100000 +t4.i*10000 + t3.i*1000 + t2.i*100 + t1.i*10 + t0.i) selected_date from
(select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t0,
(select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t1,
(select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t2,
(select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t3,
(select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t4,
(select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t5) v
where selected_date between '2010-01-01' and '2020-12-31');
It seems that I create the table successfully, but I cannot find this table in the tables list or even in the schema.
Could someone who has expertise in SQL help me check the syntax?

Display dates based on week() result, except Sunday

So we all know that we can get the week number of a date by using the week() function. My question is how to display the dates of that particular week, except the Sunday date?
To illustrate..
Based from the above example, what I want to display is..
2009-05-17
2009-05-18
2009-05-19
2009-05-20
2009-05-21
2009-05-22
How do I query this?
SET #GivenDate ='2016-05-27';
SET #YearNum = YEAR(#GivenDate);
SET #WeekNum=WEEK(#GivenDate);
select selected_date,weekday(selected_date) AS WeeKDate
from
(select adddate('1970-01-01',t4.i*10000 + t3.i*1000 + t2.i*100 + t1.i*10 + t0.i) selected_date from
(select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t0,
(select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t1,
(select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t2,
(select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t3,
(select 0 i union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select 8 union select 9) t4) v
where YEAR(selected_date)=#YearNum
AND WEEK(selected_date)=#WeekNum
AND WEEKDAY(selected_date)<> 6;