psql: copying to table issue - sql

Say I have users.data containing...
Jeff 31 2
Niel 42 15
Chucky 42 15
Paul 48 15
Bert 18 0
Geoff 28 16
Kieth 64 31
Clyde 20 0
Greg 44 3
Bonnie 19 0
Meyer 34 6
Alice 41 3
And in psql, I create a users table like this...
ID | name | age | number
---+-------+-----+----------
1 Jeff 31 2
2 Niel 42 15
3 Chucky 42 15
4 Paul 48 15
5 Bert 18 0
6 Geoff 28 16
7 Kieth 64 31
8 Clyde 20 0
9 Greg 44 3
10 Bonnie 19 0
11 Meyer 34 6
12 Alice 41 3
And then I have events.data containing...
Jeff Party Newtown 2012-02-16
Jeff Work Oldtown 2005-03-30
Chucky Work Oldtown 2008-11-09
Niel Work Newtown 2005-02-28
Paul Party Newtown 2010-02-14
Bert Work Oldtown 2005-03-30
Geoff Work Newtown 2013-04-20
Kieth Sleep Oldtown 2009-08-03
Clyde Party Newtown 2012-02-16
Greg Work Newtown 2013-04-20
Bonnie Sleep Newtown 2009-08-30
Meyer Sleep Newtown 2008-10-30
Alice Work Newtown 2012-03-30
How do I create the table, events, like this?
ID | user_ID | event | city date
---+---------+-------+----------+--------------
1 1 Party Newtown 2012-02-16
2 1 Work Oldtown 2005-03-30
3 2 Work Oldtown 2008-11-09
4 3 Work Newtown 2005-02-28
5 4 Party Newtown 2010-02-14
6 5 Work Oldtown 2005-03-30
7 6 Work Newtown 2013-04-20
8  7 Sleep Oldtown 2009-08-03
9 8 Party Newtown 2012-02-16
10 9 Work Newtown 2013-04-20
11 10 Sleep Newtown 2009-08-30
12 11 Sleep Newtown 2008-10-30
13 12 Work Newtown 2012-03-30
Basically, when I populate the tables I've created and when I am going to copy the data to the files, I need to find the id of the of the user matching the user name attribute of the event record. So when you look at the events table, instead of displaying the name, it is instead showing the id of the user of that same name.
How can I go about doing this? Am I able to do this when I use...
COPY events (user_ID, event, city, date) FROM :dir || 'events.data' WITH (/* ??? */);

Related

fill gaps day with two tables in sql

I have three different ID. id are dynamics
FOR EACH id, i need complete a calendar with last exist value.
example
ID
VALUE
date
1
30
1/1/2020
1
29
3/1/2020
2
65
1/1/2020
3
30
2/1/2020
1
11
6/1/2020
2
40
4/1/2020
3
23
5/1/2020
OUTPUT EXPECTED
ID
VALUE
date
1
30
1/1/2020
1
30
2/1/2020
1
29
3/1/2020
1
29
4/1/2020
1
29
5/1/2020
1
11
6/1/2020
2
65
1/1/2020
2
65
2/1/2020
2
65
3/1/2020
2
40
4/1/2020
2
40
5/1/2020
2
40
6/1/2020
3
30
2/1/2020
3
30
3/1/2020
3
30
4/1/2020
3
23
5/1/2020
3
23
6/1/2020
---Complete fields until today for each id---

SQL replace incomplete days of the week in the month to 0

I have this table
date week_day
1-02-2018 4
2-02-2018 5
3-02-2018 6
4-02-2018 7
5-02-2018 1
6-02-2018 2
7-02-2018 3
................
26-02-2018 1
27-02-2018 2
28-02-2018 3
I need to get in SQL incomplete weeks to next in the following form:
date week_day
0 1
0 2
0 3
1-02-2018 4
2-02-2018 5
3-02-2018 6
4-02-2018 7
5-02-2018 1
6-02-2018 2
7-02-2018 3
................
26-02-2018 1
27-02-2018 2
28-02-2018 3
0 4
0 5
0 6
0 7
Is something like this what you're looking for:
DECLARE #FirstDate date =
(
SELECT MIN(DATEADD(DAY, (week_day - 1) * -1, [date]))
FROM YourTable
)
DECLARE #LastDate date =
(
SELECT MAX(DATEADD(DAY, 7 - week_day, [date]))
FROM YourTable
)
;
WITH cteDates
AS
(
SELECT
#FirstDate [date]
, 1 week_day
UNION ALL
SELECT
DATEADD(DAY, 1, [date])
, CASE WHEN week_day + 1 > 7 THEN 1 ELSE week_day + 1 END
FROM cteDates
WHERE [date] < #LastDate
)
SELECT
T.[date]
, C.week_day
FROM
cteDates C
LEFT JOIN YourTable T ON
C.[date] = T.[date]
AND C.week_day = T.week_day
This query seems to solve your query. I assume that your minimal date is over 1900-01-01. If not put any other date that is under than your minimal date and is monday
Also there may be better solution with recursive cte
with cte as (
select cast(a as datetime) date, b week_day from (values ('20180201',4)
,('20180202',5)
,('20180203',6)
,('20180204',7)
,('20180205',1)
,('20180206',2)
,('20180207',3)
,('20180226',1)
,('20180227',2)
,('20180228',3)
,('20181231',1)
,('20190101',2)
,('20190102',3)
,('20190103',4)
,('20190104',5)
) t(a,b)
)
select
iif((rn = 1 and charindex(k, st) > 0) or k is null, convert(varchar(10), date, 120), '0')
, isnull(k, week_day)
from (
select
date, week_day, ww
, concat(max(iif(week_day = 1, 1, null)) over (partition by ww)
, max(iif(week_day = 2, 2, null)) over (partition by ww)
, max(iif(week_day = 3, 3, null)) over (partition by ww)
, max(iif(week_day = 4, 4, null)) over (partition by ww)
, max(iif(week_day = 5, 5, null)) over (partition by ww)
, max(iif(week_day = 6, 6, null)) over (partition by ww)
, max(iif(week_day = 7, 7, null)) over (partition by ww)) st
, row_number() over (partition by ww order by week_day) rn
from
cte
cross apply (select datediff(dd, cast('19000101' as datetime), date) / 7 ww) q
) t
left join (values ('1'),('2'),('3'),('4'),('5'),('6'),('7')) z(k)
on
rn = 1
and (charindex(k, st) = 0 or iif(rn = 1, week_day, 0) = k)
The main key is to get the cartesian join of the (year, month, week) and weekdays then do a left join on the dates in question. The second key is to adjust for Monday being to the start of the week.
Here is an example that works in MySQL. It has one edge case which I have not yet resolved. The case of a month's last day ending on a Sunday. In that case, there are 7 zeros between the end of that month and the start of the next.
select * from (
select cartesian.`year`
, cartesian.`month`
, cartesian.`week`
, coalesce(dates.`date`,0) `date`, cartesian.weekday+1 week_day from (
/* get distinct cartesian results for the (year, month, week) and weekday */
select `year`
, `month`
, case weekday when 6 then `week` - 1 else `week` end `week`
, weekday
from (
select distinct year(`date`) `year`
, month(`date`) `month`
, week(`date`) `week`
, weeks.weekday
from date_48361641, (
select 0 weekday union
select 1 weekday union
select 2 weekday union
select 3 weekday union
select 4 weekday union
select 5 weekday union
select 6 weekday
) weeks
) weeks
-- order by `year`, `month`, `week`, `weekday`
) cartesian
left join (
/* adjust for edge case of week 0 */
select `date`
, `year`
, `month`
, `week`
, weekday
from (
/* adjust the week to allow for Monday start of week*/
select `date`
, `year`
, `month`
, case weekday when 6 then `week` - 1 else `week` end `week`
, weekday
from (
select `date`
, year(`date`) `year`
, month(`date`) `month`
, week(`date`) `week`
, weekday(`date`) weekday
from date_48361641 dates
-- order by `date`
) dates
) dates
) dates
on dates.`year` = cartesian.`year`
and dates.`month` = cartesian.`month`
and dates.`week` = cartesian.`week`
and dates.`weekday` = cartesian.`weekday`
) results
where results.`week` > -1
order by
results.`year`
, results.`month`
, results.`week`
, results.`week_day`
-- results
year month week date week_day
2016 1 0 0 1
2016 1 0 0 2
2016 1 0 0 3
2016 1 0 0 4
2016 1 0 2016-01-01 5
2016 1 0 2016-01-02 6
2016 1 0 2016-01-03 7
...
2016 1 4 2016-01-25 1
2016 1 4 2016-01-26 2
2016 1 4 2016-01-27 3
2016 1 4 2016-01-28 4
2016 1 4 2016-01-29 5
2016 1 4 2016-01-30 6
2016 1 4 2016-01-31 7
2016 1 5 0 1
2016 1 5 0 2
2016 1 5 0 3
2016 1 5 0 4
2016 1 5 0 5
2016 1 5 0 6
2016 2 4 0 7
2016 2 5 2016-02-01 1
2016 2 5 2016-02-02 2
2016 2 5 2016-02-03 3
2016 2 5 2016-02-04 4
2016 2 5 2016-02-05 5
2016 2 5 2016-02-06 6
2016 2 5 2016-02-07 7
...
2016 2 9 2016-02-29 1
2016 2 9 0 2
2016 2 9 0 3
2016 2 9 0 4
2016 2 9 0 5
2016 2 9 0 6
2016 3 8 0 7
2016 3 9 0 1
2016 3 9 2016-03-01 2
2016 3 9 2016-03-02 3
2016 3 9 2016-03-03 4
2016 3 9 2016-03-04 5
2016 3 9 2016-03-05 6
2016 3 9 2016-03-06 7
...
2016 3 13 2016-03-28 1
2016 3 13 2016-03-29 2
2016 3 13 2016-03-30 3
2016 3 13 2016-03-31 4
2016 3 13 0 5
2016 3 13 0 6
2016 4 12 0 7
2016 4 13 0 1
2016 4 13 0 2
2016 4 13 0 3
2016 4 13 0 4
2016 4 13 2016-04-01 5
2016 4 13 2016-04-02 6
2016 4 13 2016-04-03 7
...
2016 5 22 2016-05-30 1
2016 5 22 2016-05-31 2
2016 5 22 0 3
2016 5 22 0 4
2016 5 22 0 5
2016 5 22 0 6
2016 6 21 0 7
2016 6 22 0 1
2016 6 22 0 2
2016 6 22 2016-06-01 3
2016 6 22 2016-06-02 4
2016 6 22 2016-06-03 5
2016 6 22 2016-06-04 6
2016 6 22 2016-06-05 7
...
2016 6 26 2016-06-27 1
2016 6 26 2016-06-28 2
2016 6 26 2016-06-29 3
2016 6 26 2016-06-30 4
2016 6 26 0 5
2016 6 26 0 6
2016 7 25 0 7
2016 7 26 0 1
2016 7 26 0 2
2016 7 26 0 3
2016 7 26 0 4
2016 7 26 2016-07-01 5
2016 7 26 2016-07-02 6
2016 7 26 2016-07-03 7
...
2016 7 30 2016-07-25 1
2016 7 30 2016-07-26 2
2016 7 30 2016-07-27 3
2016 7 30 2016-07-28 4
2016 7 30 2016-07-29 5
2016 7 30 2016-07-30 6
2016 7 30 2016-07-31 7
2016 7 31 0 1
2016 7 31 0 2
2016 7 31 0 3
2016 7 31 0 4
2016 7 31 0 5
2016 7 31 0 6
2016 8 30 0 7
2016 8 31 2016-08-01 1
2016 8 31 2016-08-02 2
2016 8 31 2016-08-03 3
2016 8 31 2016-08-04 4
2016 8 31 2016-08-05 5
2016 8 31 2016-08-06 6
2016 8 31 2016-08-07 7
...
2016 8 35 2016-08-29 1
2016 8 35 2016-08-30 2
2016 8 35 2016-08-31 3
2016 8 35 0 4
2016 8 35 0 5
2016 8 35 0 6
2016 9 34 0 7
2016 9 35 0 1
2016 9 35 0 2
2016 9 35 0 3
2016 9 35 2016-09-01 4
2016 9 35 2016-09-02 5
2016 9 35 2016-09-03 6
2016 9 35 2016-09-04 7
...
2016 9 39 2016-09-26 1
2016 9 39 2016-09-27 2
2016 9 39 2016-09-28 3
2016 9 39 2016-09-29 4
2016 9 39 2016-09-30 5
2016 9 39 0 6
2016 10 38 0 7
2016 10 39 0 1
2016 10 39 0 2
2016 10 39 0 3
2016 10 39 0 4
2016 10 39 0 5
2016 10 39 2016-10-01 6
2016 10 39 2016-10-02 7
...
2016 10 44 2016-10-31 1
2016 10 44 0 2
2016 10 44 0 3
2016 10 44 0 4
2016 10 44 0 5
2016 10 44 0 6
2016 11 43 0 7
2016 11 44 0 1
2016 11 44 2016-11-01 2
2016 11 44 2016-11-02 3
2016 11 44 2016-11-03 4
2016 11 44 2016-11-04 5
2016 11 44 2016-11-05 6
2016 11 44 2016-11-06 7
...
2016 11 48 2016-11-28 1
2016 11 48 2016-11-29 2
2016 11 48 2016-11-30 3
2016 11 48 0 4
2016 11 48 0 5
2016 11 48 0 6
2016 12 47 0 7
2016 12 48 0 1
2016 12 48 0 2
2016 12 48 0 3
2016 12 48 2016-12-01 4
2016 12 48 2016-12-02 5
2016 12 48 2016-12-03 6
2016 12 48 2016-12-04 7
2016 12 49 2016-12-05 1
2016 12 49 2016-12-06 2
2016 12 49 2016-12-07 3
2016 12 49 2016-12-08 4
2016 12 49 2016-12-09 5
2016 12 49 2016-12-10 6
2016 12 49 2016-12-11 7
2016 12 50 2016-12-12 1
2016 12 50 2016-12-13 2
2016 12 50 2016-12-14 3
2016 12 50 2016-12-15 4
2016 12 50 2016-12-16 5
2016 12 50 2016-12-17 6
2016 12 50 2016-12-18 7
2016 12 51 2016-12-19 1
2016 12 51 2016-12-20 2
2016 12 51 2016-12-21 3
2016 12 51 2016-12-22 4
2016 12 51 2016-12-23 5
2016 12 51 2016-12-24 6
2016 12 51 2016-12-25 7
2016 12 52 2016-12-26 1
2016 12 52 2016-12-27 2
2016 12 52 2016-12-28 3
2016 12 52 2016-12-29 4
2016 12 52 2016-12-30 5
2016 12 52 2016-12-31 6
2017 1 0 2017-01-01 7
2017 1 1 2017-01-02 1
2017 1 1 2017-01-03 2
2017 1 1 2017-01-04 3
2017 1 1 2017-01-05 4
2017 1 1 2017-01-06 5
2017 1 1 2017-01-07 6
2017 1 1 2017-01-08 7
2017 1 2 2017-01-09 1
2017 1 2 2017-01-10 2
2017 1 2 2017-01-11 3
2017 1 2 2017-01-12 4
2017 1 2 2017-01-13 5
2017 1 2 2017-01-14 6
2017 1 2 2017-01-15 7
2017 1 3 2017-01-16 1
2017 1 3 2017-01-17 2
2017 1 3 2017-01-18 3
2017 1 3 2017-01-19 4
2017 1 3 2017-01-20 5
2017 1 3 2017-01-21 6
2017 1 3 2017-01-22 7
2017 1 4 2017-01-23 1
2017 1 4 2017-01-24 2
2017 1 4 2017-01-25 3
2017 1 4 2017-01-26 4
2017 1 4 2017-01-27 5
2017 1 4 2017-01-28 6
2017 1 4 2017-01-29 7
2017 1 5 2017-01-30 1
2017 1 5 2017-01-31 2
2017 1 5 0 3
2017 1 5 0 4
2017 1 5 0 5
2017 1 5 0 6
2017 2 4 0 7
2017 2 5 0 1
2017 2 5 0 2
2017 2 5 2017-02-01 3
2017 2 5 2017-02-02 4
2017 2 5 2017-02-03 5
2017 2 5 2017-02-04 6
2017 2 5 2017-02-05 7
2017 2 6 2017-02-06 1
2017 2 6 2017-02-07 2
2017 2 6 2017-02-08 3
2017 2 6 2017-02-09 4
2017 2 6 2017-02-10 5
2017 2 6 2017-02-11 6
2017 2 6 2017-02-12 7
2017 2 7 2017-02-13 1
2017 2 7 2017-02-14 2
2017 2 7 2017-02-15 3
2017 2 7 2017-02-16 4
2017 2 7 2017-02-17 5
2017 2 7 2017-02-18 6
2017 2 7 2017-02-19 7
2017 2 8 2017-02-20 1
2017 2 8 2017-02-21 2
2017 2 8 2017-02-22 3
2017 2 8 2017-02-23 4
2017 2 8 2017-02-24 5
2017 2 8 2017-02-25 6
2017 2 8 2017-02-26 7
2017 2 9 2017-02-27 1
2017 2 9 2017-02-28 2
2017 2 9 0 3
2017 2 9 0 4
2017 2 9 0 5
2017 2 9 0 6
2017 3 8 0 7
2017 3 9 0 1
2017 3 9 0 2
2017 3 9 2017-03-01 3
2017 3 9 2017-03-02 4
2017 3 9 2017-03-03 5
2017 3 9 2017-03-04 6
2017 3 9 2017-03-05 7
2017 3 10 2017-03-06 1
2017 3 10 2017-03-07 2
2017 3 10 2017-03-08 3
2017 3 10 2017-03-09 4
2017 3 10 2017-03-10 5
2017 3 10 2017-03-11 6
2017 3 10 2017-03-12 7
2017 3 11 2017-03-13 1
2017 3 11 2017-03-14 2
2017 3 11 2017-03-15 3
2017 3 11 2017-03-16 4
2017 3 11 2017-03-17 5
2017 3 11 2017-03-18 6
2017 3 11 2017-03-19 7
2017 3 12 2017-03-20 1
2017 3 12 2017-03-21 2
2017 3 12 2017-03-22 3
2017 3 12 2017-03-23 4
2017 3 12 2017-03-24 5
2017 3 12 2017-03-25 6
2017 3 12 2017-03-26 7
2017 3 13 2017-03-27 1
2017 3 13 2017-03-28 2
2017 3 13 2017-03-29 3
2017 3 13 2017-03-30 4
2017 3 13 2017-03-31 5
2017 3 13 0 6
2017 4 12 0 7
2017 4 13 0 1
2017 4 13 0 2
2017 4 13 0 3
2017 4 13 0 4
2017 4 13 0 5
2017 4 13 2017-04-01 6
2017 4 13 2017-04-02 7
2017 4 14 2017-04-03 1
2017 4 14 2017-04-04 2
2017 4 14 2017-04-05 3
2017 4 14 2017-04-06 4
2017 4 14 2017-04-07 5
2017 4 14 2017-04-08 6
2017 4 14 2017-04-09 7
2017 4 15 2017-04-10 1
2017 4 15 2017-04-11 2
2017 4 15 2017-04-12 3
2017 4 15 2017-04-13 4
2017 4 15 2017-04-14 5
2017 4 15 2017-04-15 6
2017 4 15 2017-04-16 7
2017 4 16 2017-04-17 1
2017 4 16 2017-04-18 2
2017 4 16 2017-04-19 3
2017 4 16 2017-04-20 4
2017 4 16 2017-04-21 5
2017 4 16 2017-04-22 6
2017 4 16 2017-04-23 7
2017 4 17 2017-04-24 1
2017 4 17 2017-04-25 2
2017 4 17 2017-04-26 3
2017 4 17 2017-04-27 4
2017 4 17 2017-04-28 5
2017 4 17 2017-04-29 6
2017 4 17 2017-04-30 7
2017 4 18 0 1
2017 4 18 0 2
2017 4 18 0 3
2017 4 18 0 4
2017 4 18 0 5
2017 4 18 0 6
2017 5 17 0 7
2017 5 18 2017-05-01 1
2017 5 18 2017-05-02 2
2017 5 18 2017-05-03 3
2017 5 18 2017-05-04 4
2017 5 18 2017-05-05 5
2017 5 18 2017-05-06 6
2017 5 18 2017-05-07 7
2017 5 19 2017-05-08 1
2017 5 19 2017-05-09 2
2017 5 19 2017-05-10 3
2017 5 19 2017-05-11 4
2017 5 19 2017-05-12 5
2017 5 19 2017-05-13 6
2017 5 19 2017-05-14 7
2017 5 20 2017-05-15 1
2017 5 20 2017-05-16 2
2017 5 20 2017-05-17 3
2017 5 20 2017-05-18 4
2017 5 20 2017-05-19 5
2017 5 20 2017-05-20 6
2017 5 20 2017-05-21 7
2017 5 21 2017-05-22 1
2017 5 21 2017-05-23 2
2017 5 21 2017-05-24 3
2017 5 21 2017-05-25 4
2017 5 21 2017-05-26 5
2017 5 21 2017-05-27 6
2017 5 21 2017-05-28 7
2017 5 22 2017-05-29 1
2017 5 22 2017-05-30 2
2017 5 22 2017-05-31 3
2017 5 22 0 4
2017 5 22 0 5
2017 5 22 0 6
2017 6 21 0 7
2017 6 22 0 1
2017 6 22 0 2
2017 6 22 0 3
2017 6 22 2017-06-01 4
2017 6 22 2017-06-02 5
2017 6 22 2017-06-03 6
2017 6 22 2017-06-04 7
2017 6 23 2017-06-05 1
2017 6 23 2017-06-06 2
2017 6 23 2017-06-07 3
2017 6 23 2017-06-08 4
2017 6 23 2017-06-09 5
2017 6 23 2017-06-10 6
2017 6 23 2017-06-11 7
2017 6 24 2017-06-12 1
2017 6 24 2017-06-13 2
2017 6 24 2017-06-14 3
2017 6 24 2017-06-15 4
2017 6 24 2017-06-16 5
2017 6 24 2017-06-17 6
2017 6 24 2017-06-18 7
2017 6 25 2017-06-19 1
2017 6 25 2017-06-20 2
2017 6 25 2017-06-21 3
2017 6 25 2017-06-22 4
2017 6 25 2017-06-23 5
2017 6 25 2017-06-24 6
2017 6 25 2017-06-25 7
2017 6 26 2017-06-26 1
2017 6 26 2017-06-27 2
2017 6 26 2017-06-28 3
2017 6 26 2017-06-29 4
2017 6 26 2017-06-30 5
2017 6 26 0 6
2017 7 25 0 7
2017 7 26 0 1
2017 7 26 0 2
2017 7 26 0 3
2017 7 26 0 4
2017 7 26 0 5
2017 7 26 2017-07-01 6
2017 7 26 2017-07-02 7
2017 7 27 2017-07-03 1
2017 7 27 2017-07-04 2
2017 7 27 2017-07-05 3
2017 7 27 2017-07-06 4
2017 7 27 2017-07-07 5
2017 7 27 2017-07-08 6
2017 7 27 2017-07-09 7
2017 7 28 2017-07-10 1
2017 7 28 2017-07-11 2
2017 7 28 2017-07-12 3
2017 7 28 2017-07-13 4
2017 7 28 2017-07-14 5
2017 7 28 2017-07-15 6
2017 7 28 2017-07-16 7
2017 7 29 2017-07-17 1
2017 7 29 2017-07-18 2
2017 7 29 2017-07-19 3
2017 7 29 2017-07-20 4
2017 7 29 2017-07-21 5
2017 7 29 2017-07-22 6
2017 7 29 2017-07-23 7
2017 7 30 2017-07-24 1
2017 7 30 2017-07-25 2
2017 7 30 2017-07-26 3
2017 7 30 2017-07-27 4
2017 7 30 2017-07-28 5
2017 7 30 2017-07-29 6
2017 7 30 2017-07-30 7
2017 7 31 2017-07-31 1
2017 7 31 0 2
2017 7 31 0 3
2017 7 31 0 4
2017 7 31 0 5
2017 7 31 0 6
2017 8 30 0 7
2017 8 31 0 1
2017 8 31 2017-08-01 2
2017 8 31 2017-08-02 3
2017 8 31 2017-08-03 4
2017 8 31 2017-08-04 5
2017 8 31 2017-08-05 6
2017 8 31 2017-08-06 7
2017 8 32 2017-08-07 1
2017 8 32 2017-08-08 2
2017 8 32 2017-08-09 3
2017 8 32 2017-08-10 4
2017 8 32 2017-08-11 5
2017 8 32 2017-08-12 6
2017 8 32 2017-08-13 7
2017 8 33 2017-08-14 1
2017 8 33 2017-08-15 2
2017 8 33 2017-08-16 3
2017 8 33 2017-08-17 4
2017 8 33 2017-08-18 5
2017 8 33 2017-08-19 6
2017 8 33 2017-08-20 7
2017 8 34 2017-08-21 1
2017 8 34 2017-08-22 2
2017 8 34 2017-08-23 3
2017 8 34 2017-08-24 4
2017 8 34 2017-08-25 5
2017 8 34 2017-08-26 6
2017 8 34 2017-08-27 7
2017 8 35 2017-08-28 1
2017 8 35 2017-08-29 2
2017 8 35 2017-08-30 3
2017 8 35 2017-08-31 4
2017 8 35 0 5
2017 8 35 0 6
2017 9 34 0 7
2017 9 35 0 1
2017 9 35 0 2
2017 9 35 0 3
2017 9 35 0 4
2017 9 35 2017-09-01 5
2017 9 35 2017-09-02 6
2017 9 35 2017-09-03 7
2017 9 36 2017-09-04 1
2017 9 36 2017-09-05 2
2017 9 36 2017-09-06 3
2017 9 36 2017-09-07 4
2017 9 36 2017-09-08 5
2017 9 36 2017-09-09 6
2017 9 36 2017-09-10 7
2017 9 37 2017-09-11 1
2017 9 37 2017-09-12 2
2017 9 37 2017-09-13 3
2017 9 37 2017-09-14 4
2017 9 37 2017-09-15 5
2017 9 37 2017-09-16 6
2017 9 37 2017-09-17 7
2017 9 38 2017-09-18 1
2017 9 38 2017-09-19 2
2017 9 38 2017-09-20 3
2017 9 38 2017-09-21 4
2017 9 38 2017-09-22 5
2017 9 38 2017-09-23 6
2017 9 38 2017-09-24 7
2017 9 39 2017-09-25 1
2017 9 39 2017-09-26 2
2017 9 39 2017-09-27 3
2017 9 39 2017-09-28 4
2017 9 39 2017-09-29 5
2017 9 39 2017-09-30 6
2017 10 39 2017-10-01 7
2017 10 40 2017-10-02 1
2017 10 40 2017-10-03 2
2017 10 40 2017-10-04 3
2017 10 40 2017-10-05 4
2017 10 40 2017-10-06 5
2017 10 40 2017-10-07 6
2017 10 40 2017-10-08 7
2017 10 41 2017-10-09 1
2017 10 41 2017-10-10 2
2017 10 41 2017-10-11 3
2017 10 41 2017-10-12 4
2017 10 41 2017-10-13 5
2017 10 41 2017-10-14 6
2017 10 41 2017-10-15 7
2017 10 42 2017-10-16 1
2017 10 42 2017-10-17 2
2017 10 42 2017-10-18 3
2017 10 42 2017-10-19 4
2017 10 42 2017-10-20 5
2017 10 42 2017-10-21 6
2017 10 42 2017-10-22 7
2017 10 43 2017-10-23 1
2017 10 43 2017-10-24 2
2017 10 43 2017-10-25 3
2017 10 43 2017-10-26 4
2017 10 43 2017-10-27 5
2017 10 43 2017-10-28 6
2017 10 43 2017-10-29 7
2017 10 44 2017-10-30 1
2017 10 44 2017-10-31 2
2017 10 44 0 3
2017 10 44 0 4
2017 10 44 0 5
2017 10 44 0 6
2017 11 43 0 7
2017 11 44 0 1
2017 11 44 0 2
2017 11 44 2017-11-01 3
2017 11 44 2017-11-02 4
2017 11 44 2017-11-03 5
2017 11 44 2017-11-04 6
2017 11 44 2017-11-05 7
2017 11 45 2017-11-06 1
2017 11 45 2017-11-07 2
2017 11 45 2017-11-08 3
2017 11 45 2017-11-09 4
2017 11 45 2017-11-10 5
2017 11 45 2017-11-11 6
2017 11 45 2017-11-12 7
2017 11 46 2017-11-13 1
2017 11 46 2017-11-14 2
2017 11 46 2017-11-15 3
2017 11 46 2017-11-16 4
2017 11 46 2017-11-17 5
2017 11 46 2017-11-18 6
2017 11 46 2017-11-19 7
2017 11 47 2017-11-20 1
2017 11 47 2017-11-21 2
2017 11 47 2017-11-22 3
2017 11 47 2017-11-23 4
2017 11 47 2017-11-24 5
2017 11 47 2017-11-25 6
2017 11 47 2017-11-26 7
2017 11 48 2017-11-27 1
2017 11 48 2017-11-28 2
2017 11 48 2017-11-29 3
2017 11 48 2017-11-30 4
2017 11 48 0 5
2017 11 48 0 6
2017 12 47 0 7
2017 12 48 0 1
2017 12 48 0 2
2017 12 48 0 3
2017 12 48 0 4
2017 12 48 2017-12-01 5
2017 12 48 2017-12-02 6
2017 12 48 2017-12-03 7
2017 12 49 2017-12-04 1
2017 12 49 2017-12-05 2
2017 12 49 2017-12-06 3
2017 12 49 2017-12-07 4
2017 12 49 2017-12-08 5
2017 12 49 2017-12-09 6
2017 12 49 2017-12-10 7
2017 12 50 2017-12-11 1
2017 12 50 2017-12-12 2
2017 12 50 2017-12-13 3
2017 12 50 2017-12-14 4
2017 12 50 2017-12-15 5
2017 12 50 2017-12-16 6
2017 12 50 2017-12-17 7
2017 12 51 2017-12-18 1
2017 12 51 2017-12-19 2
2017 12 51 2017-12-20 3
2017 12 51 2017-12-21 4
2017 12 51 2017-12-22 5
2017 12 51 2017-12-23 6
2017 12 51 2017-12-24 7
2017 12 52 2017-12-25 1
2017 12 52 2017-12-26 2
2017 12 52 2017-12-27 3
2017 12 52 2017-12-28 4
2017 12 52 2017-12-29 5
2017 12 52 2017-12-30 6
2017 12 52 2017-12-31 7
2017 12 53 0 1
2017 12 53 0 2
2017 12 53 0 3
2017 12 53 0 4
2017 12 53 0 5
2017 12 53 0 6
2018 1 0 2018-01-01 1
2018 1 0 2018-01-02 2
2018 1 0 2018-01-03 3
2018 1 0 2018-01-04 4
2018 1 0 2018-01-05 5
2018 1 0 2018-01-06 6
2018 1 0 2018-01-07 7
2018 1 1 2018-01-08 1
2018 1 1 2018-01-09 2
2018 1 1 2018-01-10 3
2018 1 1 2018-01-11 4
2018 1 1 2018-01-12 5
2018 1 1 2018-01-13 6
2018 1 1 2018-01-14 7
2018 1 2 2018-01-15 1
2018 1 2 2018-01-16 2
2018 1 2 2018-01-17 3
2018 1 2 2018-01-18 4
2018 1 2 2018-01-19 5
2018 1 2 2018-01-20 6
2018 1 2 2018-01-21 7
2018 1 3 2018-01-22 1
2018 1 3 2018-01-23 2
2018 1 3 2018-01-24 3
2018 1 3 2018-01-25 4
2018 1 3 2018-01-26 5
2018 1 3 2018-01-27 6
2018 1 3 2018-01-28 7
2018 1 4 2018-01-29 1
2018 1 4 2018-01-30 2
2018 1 4 2018-01-31 3
2018 1 4 0 4
2018 1 4 0 5
2018 1 4 0 6
2018 2 3 0 7
2018 2 4 0 1
2018 2 4 0 2
2018 2 4 0 3
2018 2 4 2018-02-01 4
2018 2 4 2018-02-02 5
2018 2 4 2018-02-03 6
2018 2 4 2018-02-04 7
2018 2 5 2018-02-05 1
2018 2 5 2018-02-06 2
2018 2 5 2018-02-07 3
2018 2 5 2018-02-08 4
2018 2 5 2018-02-09 5
2018 2 5 2018-02-10 6
2018 2 5 2018-02-11 7
2018 2 6 2018-02-12 1
2018 2 6 2018-02-13 2
2018 2 6 2018-02-14 3
2018 2 6 2018-02-15 4
2018 2 6 2018-02-16 5
2018 2 6 2018-02-17 6
2018 2 6 2018-02-18 7
2018 2 7 2018-02-19 1
2018 2 7 2018-02-20 2
2018 2 7 2018-02-21 3
2018 2 7 2018-02-22 4
2018 2 7 2018-02-23 5
2018 2 7 2018-02-24 6
2018 2 7 2018-02-25 7
2018 2 8 2018-02-26 1
2018 2 8 2018-02-27 2
2018 2 8 2018-02-28 3
2018 2 8 0 4
2018 2 8 0 5
2018 2 8 0 6
2018 3 7 0 7
2018 3 8 0 1
2018 3 8 0 2
2018 3 8 0 3
2018 3 8 2018-03-01 4
2018 3 8 2018-03-02 5
2018 3 8 2018-03-03 6
2018 3 8 2018-03-04 7
2018 3 9 2018-03-05 1
2018 3 9 2018-03-06 2
2018 3 9 2018-03-07 3
2018 3 9 2018-03-08 4
2018 3 9 2018-03-09 5
2018 3 9 2018-03-10 6
2018 3 9 2018-03-11 7
2018 3 10 2018-03-12 1
2018 3 10 2018-03-13 2
2018 3 10 2018-03-14 3
2018 3 10 2018-03-15 4
2018 3 10 2018-03-16 5
2018 3 10 2018-03-17 6
2018 3 10 2018-03-18 7
2018 3 11 2018-03-19 1
2018 3 11 2018-03-20 2
2018 3 11 2018-03-21 3
2018 3 11 2018-03-22 4
2018 3 11 2018-03-23 5
2018 3 11 2018-03-24 6
2018 3 11 2018-03-25 7
2018 3 12 2018-03-26 1
2018 3 12 2018-03-27 2
2018 3 12 2018-03-28 3
2018 3 12 2018-03-29 4
2018 3 12 2018-03-30 5
2018 3 12 2018-03-31 6
2018 4 12 2018-04-01 7
2018 4 13 2018-04-02 1
2018 4 13 2018-04-03 2
2018 4 13 2018-04-04 3
2018 4 13 2018-04-05 4
2018 4 13 2018-04-06 5
2018 4 13 2018-04-07 6
2018 4 13 2018-04-08 7
2018 4 14 2018-04-09 1
2018 4 14 2018-04-10 2
2018 4 14 2018-04-11 3
2018 4 14 2018-04-12 4
2018 4 14 2018-04-13 5
2018 4 14 2018-04-14 6
2018 4 14 2018-04-15 7
2018 4 15 2018-04-16 1
2018 4 15 2018-04-17 2
2018 4 15 2018-04-18 3
2018 4 15 2018-04-19 4
2018 4 15 2018-04-20 5
2018 4 15 2018-04-21 6
2018 4 15 2018-04-22 7
2018 4 16 2018-04-23 1
2018 4 16 2018-04-24 2
2018 4 16 2018-04-25 3
2018 4 16 2018-04-26 4
2018 4 16 2018-04-27 5
2018 4 16 2018-04-28 6
2018 4 16 2018-04-29 7
2018 4 17 2018-04-30 1
2018 4 17 0 2
2018 4 17 0 3
2018 4 17 0 4
2018 4 17 0 5
2018 4 17 0 6
2018 5 16 0 7
2018 5 17 0 1
2018 5 17 2018-05-01 2
2018 5 17 2018-05-02 3
2018 5 17 2018-05-03 4
2018 5 17 2018-05-04 5
2018 5 17 2018-05-05 6
2018 5 17 2018-05-06 7
2018 5 18 2018-05-07 1
2018 5 18 2018-05-08 2
2018 5 18 2018-05-09 3
2018 5 18 2018-05-10 4
2018 5 18 2018-05-11 5
2018 5 18 2018-05-12 6
2018 5 18 2018-05-13 7
2018 5 19 2018-05-14 1
2018 5 19 2018-05-15 2
2018 5 19 2018-05-16 3
2018 5 19 2018-05-17 4
2018 5 19 2018-05-18 5
2018 5 19 2018-05-19 6
2018 5 19 2018-05-20 7
2018 5 20 2018-05-21 1
2018 5 20 2018-05-22 2
2018 5 20 2018-05-23 3
2018 5 20 2018-05-24 4
2018 5 20 2018-05-25 5
2018 5 20 2018-05-26 6
2018 5 20 2018-05-27 7
2018 5 21 2018-05-28 1
2018 5 21 2018-05-29 2
2018 5 21 2018-05-30 3
2018 5 21 2018-05-31 4
2018 5 21 0 5
2018 5 21 0 6
2018 6 20 0 7
2018 6 21 0 1
2018 6 21 0 2
2018 6 21 0 3
2018 6 21 0 4
2018 6 21 2018-06-01 5
2018 6 21 2018-06-02 6
2018 6 21 2018-06-03 7
2018 6 22 2018-06-04 1
2018 6 22 2018-06-05 2
2018 6 22 2018-06-06 3
2018 6 22 2018-06-07 4
2018 6 22 2018-06-08 5
2018 6 22 2018-06-09 6
2018 6 22 2018-06-10 7
2018 6 23 2018-06-11 1
2018 6 23 2018-06-12 2
2018 6 23 2018-06-13 3
2018 6 23 2018-06-14 4
2018 6 23 2018-06-15 5
2018 6 23 2018-06-16 6
2018 6 23 2018-06-17 7
2018 6 24 2018-06-18 1
2018 6 24 2018-06-19 2
2018 6 24 2018-06-20 3
2018 6 24 2018-06-21 4
2018 6 24 2018-06-22 5
2018 6 24 2018-06-23 6
2018 6 24 2018-06-24 7
2018 6 25 2018-06-25 1
2018 6 25 2018-06-26 2
2018 6 25 2018-06-27 3
2018 6 25 2018-06-28 4
2018 6 25 2018-06-29 5
2018 6 25 2018-06-30 6
2018 7 25 2018-07-01 7
2018 7 26 2018-07-02 1
2018 7 26 2018-07-03 2
2018 7 26 2018-07-04 3
2018 7 26 2018-07-05 4
2018 7 26 2018-07-06 5
2018 7 26 2018-07-07 6
2018 7 26 2018-07-08 7
2018 7 27 2018-07-09 1
2018 7 27 2018-07-10 2
2018 7 27 2018-07-11 3
2018 7 27 2018-07-12 4
2018 7 27 2018-07-13 5
2018 7 27 2018-07-14 6
2018 7 27 2018-07-15 7
2018 7 28 2018-07-16 1
2018 7 28 2018-07-17 2
2018 7 28 2018-07-18 3
2018 7 28 2018-07-19 4
2018 7 28 2018-07-20 5
2018 7 28 2018-07-21 6
2018 7 28 2018-07-22 7
2018 7 29 2018-07-23 1
2018 7 29 2018-07-24 2
2018 7 29 2018-07-25 3
2018 7 29 2018-07-26 4
2018 7 29 2018-07-27 5
2018 7 29 2018-07-28 6
2018 7 29 2018-07-29 7
2018 7 30 2018-07-30 1
2018 7 30 2018-07-31 2
2018 7 30 0 3
2018 7 30 0 4
2018 7 30 0 5
2018 7 30 0 6
2018 8 29 0 7
2018 8 30 0 1
2018 8 30 0 2
2018 8 30 2018-08-01 3
2018 8 30 2018-08-02 4
2018 8 30 2018-08-03 5
2018 8 30 2018-08-04 6
2018 8 30 2018-08-05 7
2018 8 31 2018-08-06 1
2018 8 31 2018-08-07 2
2018 8 31 2018-08-08 3
2018 8 31 2018-08-09 4
2018 8 31 2018-08-10 5
2018 8 31 2018-08-11 6
2018 8 31 2018-08-12 7
2018 8 32 2018-08-13 1
2018 8 32 2018-08-14 2
2018 8 32 2018-08-15 3
2018 8 32 2018-08-16 4
2018 8 32 2018-08-17 5
2018 8 32 2018-08-18 6
2018 8 32 2018-08-19 7
2018 8 33 2018-08-20 1
2018 8 33 2018-08-21 2
2018 8 33 2018-08-22 3
2018 8 33 2018-08-23 4
2018 8 33 2018-08-24 5
2018 8 33 2018-08-25 6
2018 8 33 2018-08-26 7
2018 8 34 2018-08-27 1
2018 8 34 2018-08-28 2
2018 8 34 2018-08-29 3
2018 8 34 2018-08-30 4
2018 8 34 2018-08-31 5
2018 8 34 0 6
2018 9 33 0 7
2018 9 34 0 1
2018 9 34 0 2
2018 9 34 0 3
2018 9 34 0 4
2018 9 34 0 5
2018 9 34 2018-09-01 6
2018 9 34 2018-09-02 7
2018 9 35 2018-09-03 1
2018 9 35 2018-09-04 2
2018 9 35 2018-09-05 3
2018 9 35 2018-09-06 4
2018 9 35 2018-09-07 5
2018 9 35 2018-09-08 6
2018 9 35 2018-09-09 7
2018 9 36 2018-09-10 1
2018 9 36 2018-09-11 2
2018 9 36 2018-09-12 3
2018 9 36 2018-09-13 4
2018 9 36 2018-09-14 5
2018 9 36 2018-09-15 6
2018 9 36 2018-09-16 7
2018 9 37 2018-09-17 1
2018 9 37 2018-09-18 2
2018 9 37 2018-09-19 3
2018 9 37 2018-09-20 4
2018 9 37 2018-09-21 5
2018 9 37 2018-09-22 6
2018 9 37 2018-09-23 7
2018 9 38 2018-09-24 1
2018 9 38 2018-09-25 2
2018 9 38 2018-09-26 3
2018 9 38 2018-09-27 4
2018 9 38 2018-09-28 5
2018 9 38 2018-09-29 6
2018 9 38 2018-09-30 7
2018 9 39 0 1
2018 9 39 0 2
2018 9 39 0 3
2018 9 39 0 4
2018 9 39 0 5
2018 9 39 0 6
2018 10 38 0 7
2018 10 39 2018-10-01 1
2018 10 39 2018-10-02 2
2018 10 39 2018-10-03 3
2018 10 39 2018-10-04 4
2018 10 39 2018-10-05 5
2018 10 39 2018-10-06 6
2018 10 39 2018-10-07 7
2018 10 40 2018-10-08 1
2018 10 40 2018-10-09 2
2018 10 40 2018-10-10 3
2018 10 40 2018-10-11 4
2018 10 40 2018-10-12 5
2018 10 40 2018-10-13 6
2018 10 40 2018-10-14 7
2018 10 41 2018-10-15 1
2018 10 41 2018-10-16 2
2018 10 41 2018-10-17 3
2018 10 41 2018-10-18 4
2018 10 41 2018-10-19 5
2018 10 41 2018-10-20 6
2018 10 41 2018-10-21 7
2018 10 42 2018-10-22 1
2018 10 42 2018-10-23 2
2018 10 42 2018-10-24 3
2018 10 42 2018-10-25 4
2018 10 42 2018-10-26 5
2018 10 42 2018-10-27 6
2018 10 42 2018-10-28 7
2018 10 43 2018-10-29 1
2018 10 43 2018-10-30 2
2018 10 43 2018-10-31 3
2018 10 43 0 4
2018 10 43 0 5
2018 10 43 0 6
2018 11 42 0 7
2018 11 43 0 1
2018 11 43 0 2
2018 11 43 0 3
2018 11 43 2018-11-01 4
2018 11 43 2018-11-02 5
2018 11 43 2018-11-03 6
2018 11 43 2018-11-04 7
2018 11 44 2018-11-05 1
2018 11 44 2018-11-06 2
2018 11 44 2018-11-07 3
2018 11 44 2018-11-08 4
2018 11 44 2018-11-09 5
2018 11 44 2018-11-10 6
2018 11 44 2018-11-11 7
2018 11 45 2018-11-12 1
2018 11 45 2018-11-13 2
2018 11 45 2018-11-14 3
2018 11 45 2018-11-15 4
2018 11 45 2018-11-16 5
2018 11 45 2018-11-17 6
2018 11 45 2018-11-18 7
2018 11 46 2018-11-19 1
2018 11 46 2018-11-20 2
2018 11 46 2018-11-21 3
2018 11 46 2018-11-22 4
2018 11 46 2018-11-23 5
2018 11 46 2018-11-24 6
2018 11 46 2018-11-25 7
2018 11 47 2018-11-26 1
2018 11 47 2018-11-27 2
2018 11 47 2018-11-28 3
2018 11 47 2018-11-29 4
2018 11 47 2018-11-30 5
2018 11 47 0 6
2018 12 46 0 7
2018 12 47 0 1
2018 12 47 0 2
2018 12 47 0 3
2018 12 47 0 4
2018 12 47 0 5
2018 12 47 2018-12-01 6
2018 12 47 2018-12-02 7
2018 12 48 2018-12-03 1
2018 12 48 2018-12-04 2
2018 12 48 2018-12-05 3
2018 12 48 2018-12-06 4
2018 12 48 2018-12-07 5
2018 12 48 2018-12-08 6
2018 12 48 2018-12-09 7
2018 12 49 2018-12-10 1
2018 12 49 2018-12-11 2
2018 12 49 2018-12-12 3
2018 12 49 2018-12-13 4
2018 12 49 2018-12-14 5
2018 12 49 2018-12-15 6
2018 12 49 2018-12-16 7
2018 12 50 2018-12-17 1
2018 12 50 2018-12-18 2
2018 12 50 2018-12-19 3
2018 12 50 2018-12-20 4
2018 12 50 2018-12-21 5
2018 12 50 2018-12-22 6
2018 12 50 2018-12-23 7
2018 12 51 2018-12-24 1
2018 12 51 2018-12-25 2
2018 12 51 2018-12-26 3
2018 12 51 2018-12-27 4
2018 12 51 2018-12-28 5
2018 12 51 2018-12-29 6
2018 12 51 2018-12-30 7
2018 12 52 2018-12-31 1
2018 12 52 0 2
2018 12 52 0 3
2018 12 52 0 4
2018 12 52 0 5
2018 12 52 0 6
-- supporting SQL Schema
CREATE TABLE `date_48361641` (
`date` date DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
truncate date_48361641 ;
insert into date_48361641 ( `date` )
select * 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 selected_date between '2016-01-01' and '2018-12-31' ;
Something like this should work
with allDates as (
see link below to finish this part
)
select DateFieldFromYourTable
, isnull(WeekDay, 0) DayOfWeek
from allDates left join YourTable on allDates.Something = DateFieldFromYourTable
Read this to get the code for your subquery.

How to print lines based on values in multiple columns

I'd like to print every line in my file that satisfies the following:
print line if column 3 or column 4 or column 5 is less than 10
Example
Emma A 10 4 7
Sally A 4 4 7
Jack B 15 19 2
Jeff C 15 20 25
Mary A 15 20 25
Meg C 2 7 9
Output
Emma A 10 4 7
Sally A 4 4 7
Jack B 15 19 2
Meg C 2 7 9
It's pretty simple with awk:
awk '$3<10 || $4<10 || $5<10' file
The output:
Emma A 10 4 7
Sally A 4 4 7
Jack B 15 19 2
Meg C 2 7 9

display the rows based on the last occurrence of an element in a column in Pandas dataframe

display the rows based on last occurrence of row based on matching values in qty and name column. I would like to drop the rows which does not match the criteria.
before:
name qty price
0 Adam 10 1
1 Rose 11 9
2 Jack 10 12
3 Jack 5 11
4 Rose 15 4
5 Jack 12 17
6 Adam 10 8
7 Rose 11 4
8 Jack 6 23
5 Jack 12 9
Jack 10 4
after:
name qty price
0 Jack 5 11
1 Rose 15 4
2 Adam 10 8
3 Rose 11 4
4 Jack 6 23
5 Jack 12 9
6 Jack 10 4
I believe you are trying to get the last occurrence of each name, qty grouping.
df.groupby(['name', 'qty'], as_index=False).last()
name qty price
0 Adam 10 8
1 Jack 5 11
2 Jack 6 23
3 Jack 10 4
4 Jack 12 9
5 Rose 11 4
6 Rose 15 4

SQL server select from 3 tables

I have three tables in my database Books, Borrowers and Movement:
Books
BookID Title Author Category Published
----------- ------------------------------ ------------------------- --------------- ----------
101 Ulysses James Joyce Fiction 1922-06-16
102 Huckleberry Finn Mark Twain Fiction 1884-03-24
103 The Great Gatsby F. Scott Fitzgerald Fiction 1925-06-17
104 1984 George Orwell Fiction 1949-04-19
105 War and Peace Leo Tolstoy Fiction 1869-08-01
106 Gullivers Travels Jonathan Swift Fiction 1726-07-01
107 Moby Dick Herman Melville Fiction 1851-08-01
108 Pride and Prejudice Jane Austen Fiction 1813-08-13
110 The Second World War Winston Churchill NonFiction 1953-09-01
111 Relativity Albert Einstein NonFiction 1917-01-09
112 The Right Stuff Tom Wolfe NonFiction 1979-09-07
121 Hitchhikers Guide to Galaxy Douglas Adams Humour 1975-10-27
122 Dad Is Fat Jim Gaffigan Humour 2013-03-01
131 Kick-Ass 2 Mark Millar Comic 2012-03-03
133 Beautiful Creatures: The Manga Kami Garcia Comic 2014-07-01
Borrowers
BorrowerID Name Birthday
----------- ------------------------- ----------
2 Bugs Bunny 1938-09-08
3 Homer Simpson 1992-09-09
5 Mickey Mouse 1928-02-08
7 Fred Flintstone 1960-06-09
11 Charlie Brown 1965-06-05
13 Popeye 1933-03-03
17 Donald Duck 1937-07-27
19 Mr. Magoo 1949-09-14
23 George Jetson 1948-04-08
29 SpongeBob SquarePants 1984-08-04
31 Stewie Griffin 1971-11-17
Movement
MoveID BookID BorrowerID DateOut DateIn ReturnCondition
----------- ----------- ----------- ---------- ---------- ---------------
1 131 31 2012-06-01 2013-05-24 good
2 101 23 2012-02-10 2012-03-24 good
3 102 29 2012-02-01 2012-04-01 good
4 105 7 2012-03-23 2012-05-11 good
5 103 7 2012-03-22 2012-04-22 good
6 108 7 2012-01-23 2012-02-12 good
7 112 19 2012-01-12 2012-02-10 good
8 122 11 2012-04-14 2013-05-01 poor
9 106 17 2013-01-24 2013-02-01 good
10 104 2 2013-02-24 2013-03-10 bitten
11 121 3 2013-03-01 2013-04-01 good
12 131 19 2013-04-11 2013-05-23 good
13 111 5 2013-05-22 2013-06-22 poor
14 131 2 2013-06-12 2013-07-23 bitten
15 122 23 2013-07-10 2013-08-12 good
16 107 29 2014-01-01 2014-02-14 good
17 110 7 2014-01-11 2014-02-01 good
18 105 2 2014-02-22 2014-03-02 bitten
What is a query I can use to find out which book was borrowed by the oldest borrower?
I am new to SQL and am using Microsoft SQL Server 2014
Here are two different solutions:
First using two sub querys and one equi-join:
select Title
from Books b , Movement m
where b.BookID = m.BookID and m.BorrowerID = (select BorrowerID
from Borrowers
where Birthday = (select MIN(Birthday)
from Borrowers))
Using two equi-joins and one sub query:
select Title
from Books b, Borrowers r, Movement m
where b.BookID = m.BookID
and m.BorrowerID = r.BorrowerID
and Birthday = (select MIN(Birthday) from Borrowers)
Both above queries give the following answer:
Title
------------------------------
Relativity