I have a table with following structure for about 1000 Posts
SRL Post_Name Name Start_Date End_Date Reason
441a Lecturer Dr (Ms)Prem Kali Sharma 01-11-1974 02-03-1989 Retired
441b Lecturer Post Vacant 03-03-1989 31-07-1989 Vacant
441c Lecturer Dr Aditya Prachandiya 01-08-1989 30-07-1994 Worked1
441d Lecturer Dr Aditya Prachandiya 01-08-1994 31-07-1999 Worked2
441e Lecturer Dr Aditya Prachandiya 01-08-1999 25-08-2000 Worked1
441f Lecturer Dr Aditya Prachandiya 26-08-2000 05-03-2010 Leave
441g Lecturer Dr Aditya Prachandiya 06-03-2010 30-06-2016 Retired
441h Lecturer Post Vacant 01-07-2016 06-11-2017 Vacant
441i Lecturer Dr (Ms) Ranjana Pandey 07-11-2017 06-11-2018 Temporary
442a Reader Dr HC Gupta 01-07-1958 30-06-1990 Retired
442b Reader Post Vacant 01-07-1990 25-10-1990 Vacant
442c Reader Dr Agam Prasad Tyagi 26-10-1990 25-10-1995 Worked5
442d Reader Dr Agam Prasad Tyagi 26-10-1995 25-10-2000 Worked1
442e Reader Dr Agam Prasad Tyagi 26-10-2000 05-12-2003 Worked2
442f Reader Dr Agam Prasad Tyagi 06-12-2003 30-06-2029 Working
Basically what i would like to do is have an access query to find if there is any date gap from '441a to 441i' and '442a to 442f' to check typing error of dates.
Need to extract the numeric prefix as a group identifier. I created a Calculated field in table with expression: Left([SRL],3). Then a nested query to pull the Start_Date of next record:
SELECT *, End_Date + 1 AS NextStart, (SELECT TOP 1 Dupe.Start_Date
FROM Table1 AS Dupe WHERE Dupe.SRL_Group = Table1.SRL_Group AND
Dupe.SRL > Table1.SRL) AS NextRecStart
FROM Table1;
If NextStart and NextRecStart are not equal then there is a date gap.
Related
In MySQL, is it possible in a SELECT statement to iterate over all rows of a subquery and for each row (“r”) return a value that is the result of a further query informed by “r”?
E.g. if I have a table of friend’s dates of birth, how can I get from another table of famous people’s dates of birth the most recent such famous person’s birthday that’s close to each friend’s date of birth?
http://www.sqlfiddle.com/#!9/a5d62f
I hope to generate a matching_birth_dates table with the following rows:
id
famous_name
friend_name
famous_dob
friend_dob
1
David Beckham
Sarah Holland
05/01/1980
07/02/1981
2
David Lynch
John Smith
02/05/1960
02/06/1959
3
David Beckham
Jane Doe
05/01/1980
02/04/1972
You can first find out the closest famous person in a subquery and then get the person and the famous in the outer query:
select friend.id, f.name, friend.name, f.dob, friend.dob
from (
select fa.id, fa.name, fa.dob, min(abs(datediff(fa.dob, fr.dob))) as diff
from famous_birth_dates fa
join friends_birth_dates fr on 1=1
group by fa.id, fa.name, fa.dob
) as f
join friends_birth_dates friend on f.diff = abs(datediff(f.dob, friend.dob))
order by friend.id
See db sqlfiddle
I have two tables:
student (Std id, name, year of study, basic stipend, dept_no)
dept (dept_name, academic_block)
I want to increase the basic stipend of 3rd year computer science department by 5000.00 per month
I tired doing
UPDATE <student>
SET basic stipend = basic stipend + 5000.00
WHERE dept_name IN (SELECT dept_name
FROM dept
WHERE dept_name = 3rd year computer science);
I don't know about the last part as dept_name column is from other relation and there is no common column between two relations.
Could somebody please help me write a MySQL query to update some data I need to recreate from a corrupted source. Due to a programming error I’ve had to delete about 9000 row of corrupt data. I have a copy of the good data but can’t work out the joins to get the good data back into the repaired table.
I have 2 tables from a school with the following relevant columns
Table A – tbl_Lessons
Lesson_id
Lesson_class_id
Lesson_date
Lesson_term
Table B – tbl_Register
Register_id
Register_lesson_id
Register_student_id
Register_status
Register_update_by
Register_update_dat
So for each class_id there is a unique lesson_id for each date it is taught.
And for each lesson_id there is a unique register entry for each student taught.
All lessons from the current term had to be recreated and so the register for all lessons also had to be recreated.
Table C – Backup Data before deletion.
(Create table tbl_tmp_register as
select * from tbl_register inner join tbl_lessons on lesson_id = register_lesson_id
where lesson_term = "autumn 2019")
Lesson_id
Lesson_class_id
Lesson_date
Lesson_term
Register_id
Register_lesson_id
Register_student_id
Register_status
Register_update_by
Register_update_date
So what I would like to do is update Table B (register_status, register_update_by, register_update_date) with the data from the backup table. I know what it should be by looking in the backup table for the row that matches lesson_class_id , lesson_date & register_student_id but I cannot write the SQL to get the data copied across. I have tried and failed many times. I think this is quite close to update one field but it just seems to hang the server even if I narrow the register down to one class on one day.
update tbl_register as r inner join tbl_lessons as l on r.register_lesson_id = l.lesson_id,
(select register_student_status from tbl_tmp_register as TTR inner join tbl_lessons as L on L.lesson_class_id = TTR.lesson_class_id AND l.lesson_Date = TTR.Lesson_date) as tt
Set r.register_student_status = tt.register_student_status
where l.lesson_class_id = 000 and l.lesson_date = '2019-xx-yy';
If I try it another way with a more straight forward query I also hang the server.
update tbl_register as dest
inner join tbl_tmp_register as srce
on srce.register_student_id = dest.register_student_id
inner join tbl_lessons as otsrce
on otsrce.lesson_class_id = srce.lesson_class_id and otsrce.lesson_date = srce.lesson_date
set dest.register_student_status = srce.register_student_status
I know I'm close but not quite getting the joins right.
Any help much appreciated.
Edit with Sample data for NBK:
In the backup data set the register_id and the lesson_id are not identical to the new data set in the register and lesson tables as these have been regenerated so the link between the old and new is the student id, class id, and class date.
Sample data
Table A – lessons
Lesson_id:1
Lesson_class_id: 22
Lesson_date: 2019-09-01
Lesson_term: Autumn 2019
Table B- registers
Register_id:1
Register_lesson_id:1
Register_student_id:33
Register_status: NULL
Register_update_by: NULL
Register_update_dat: NULL
Register_id:2
Register_lesson_id:1
Register_student_id:44
Register_status: NULL
Register_update_by: NULL
Register_update_dat: NULL
…
Table C- Backup
Lesson_id: 555
Lesson_class_id: 1
Lesson_date: 2019-09-01
Lesson_term: Autumn 2019
Register_id: 666
Register_lesson_id: 1
Register_student_id: 33
Register_status: Present
Register_update_by: TeacherX
Register_update_date: 2019-09-02
Lesson_id: 556
Lesson_class_id: 1
Lesson_date: 2019-09-01
Lesson_term: Autumn 2019
Register_id: 667
Register_lesson_id: 1
Register_student_id: 44
Register_status: Late
Register_update_by: TeacherX
Register_update_date: 2019-09-02
I am developing a stable booking system where user can book and update their room bookings and chose stables from interactive map.
My stable registration db structure is like below
event_detail_stable_registrations
| id | accountId | eventId | stableId | checkInDate | checkOutDate |
5 233 55 66 26-06-2017 28-06-2017
6 234 55 66 28-06-2017 29-06-2017
When user updates the booking but do not change the checkInDate and checkOutDate then its an easy scenario which i have implemented already.
In above case if user 234 updates the booking and change checkInDate then the query should return 233 for stableId 66 but my query returns '234' as accountId
Another scenario is when user changes the checkInDate and or checkOutDate of the registration. User A wants to change the booking detail how can i check if any overlapping for updated checkInDate and or checkOutDate for user's booking and if those are already booked then which accountId has booked it.
Right now I am running following query which gives me correct information about overlapping dates but could not get the information of account who has booked it.
Query always returns the user's accountId for overlapping dates as well.
SET #checkInDate = '2017-04-27 14:00:00' , #checkOutDate = '2017-05-01 10:00:00' ;
SELECT a.*,
IF(b.`stableid` IS NULL,"Avalalable","Not Available") as `status`,
IF(NOT b.`stableid` IS NULL,b.`accountId`,"") as `overLapAccount`,
IF(NOT b.`stableid` IS NULL,b.`checkInDate`,"") as `start_overlap`,
IF(NOT b.`stableid` IS NULL,b.`checkOutDate`,"") as `end_overlap`
FROM `event_detail_stable_registrations` b
LEFT JOIN `stables` a
ON a.`id` = b.`stableid` AND
(((`checkInDate` BETWEEN #checkInDate AND #checkOutDate)
OR (`checkOutDate` BETWEEN #checkInDate AND #checkOutDate))
OR ((#checkInDate BETWEEN `checkInDate` AND `checkOutDate`)
OR (#checkOutDate BETWEEN `checkInDate` AND `checkOutDate`))
)
ORDER BY a.`name`
Here is the SQL fiddle where I haven't used the same DB structure but its similar.
The output I get for the same stable booked by multiple account for given period is fine but with the query I am using, I get null in the column name,stableId.
Part 2 of your scenario:
MariaDB-10.5 Application Time Periods - WITHOUT OVERLAPS can enforce the constraints of non-overlapping bookings:
ALTER TABLE event_detail_stable_registrations
ADD period FOR booking(checkInDate, checkOutDate),
ADD PRIMARY KEY (accountId, stableId, booking WITHOUT OVERLAPS)
A user cancelling a day is just:
DELETE
FROM event_detail_stable_registrations
FOR PORTION OF booking
FROM '2017-04-29 14:00:00' TO '2017-04-30 10:00:00'
WHERE accountId=5
AND stableId=866
Which splits the non-deleted dates booking entry over periods.
Any UPDATE of a booking enforces the constraints.
ref: dbfiddle
So I completed a homework assignment (prompt and more details below), and one of my answers looked like this:
SELECT Student.SSN, FirstName, LastName, Section.SectionNo, Section.Instructor
FROM Course Join Enrolls ON Course.CourseNo = Enrolls.CourseNo
JOIN Student ON Student.SSN = Enrolls.SSN
JOIN Section ON Section.SectionNo = Enrolls.SectionNo AND Section.CourseNo = Course.CourseNo
WHERE CourseName = 'Data structure and Algorithms'
I was wondering, is there another way to accomplish the same thing while not making the code look so cluttered? Is there maybe a more efficient or at least short-hand syntax way to do what I did?
Prompt: Get the information (SSN, first name and last name) about students who take the course ‘Data structure and Algorithms’. Also get the section number in which they have enrolled in the course, as well as the instructor of the section.
Here are how my tables look like:
STUDENT
SSN FirstName LastName Street City State Zip
1237456787 Dilly Dob 1233 Revem Court Sacramento CA 56123
1237456788 Filly Fob 1243 Roasm Road Sacramento CA 21234
1237456789 Billy Bob 1234 Random Lane Sacramento CA 12145
SECTION
CourseNo SectionNo Instructor
CSC300 1 Prof Cool
CSC300 2 Prof Cool
CSC300 3 Prof Cool
CSC133 2 Prof SuperCool
CSC133 1 Prof SuperCool
CSC133 3 Prof SuperCool
CSC137 2 Prof NotSoCool
ENROLLS
SSN SectionNo CourseNo
1237456787 1 CSC300
1237456788 1 CSC300
1237456789 1 CSC300
1237456789 2 CSC133
1237456789 2 CSC137
COURSE
CourseNo CourseName Department
CSC300 Advanced Database Management Systems Computer Science
CSC133 Data Structure and Algorithms Computer Science
CSC137 Computer Organization and Digital Circuits Computer Science
RESULT SHOULD BE
SSN FirstName LastName SectionNo Instructor
123456789 Billy Bob 2 Prof SuperCool
Your query is about as minimal as you can get based on the description and qualifiers. A few notes, especially for learning and longer term sanity. whenever dealing with more than one table in a query, get in the habit of always qualifying with table.column (or alias.column) for readability, especially if table names are long, apply a shorter "alias".
Second, and this is more just stylish formatting and comes in nice for joins to know where things are coming from. INDENT... and also, in the joins, I always try to list my first table as the FIRST-side of the join equality. Comes in easier when reading and implementing LEFT JOIN criteria later on in your education...
SELECT
S.SSN,
S.FirstName,
S.LastName,
Section.SectionNo,
Section.Instructor
FROM
Course C
Join Enrolls E
ON C.CourseNo = E.CourseNo
JOIN Student S
ON E.SSN = S.SSN
JOIN Section
ON C.CourseNo = Section.CourseNo
AND E.SectionNo = Section.SectionNo
WHERE
C.CourseName = 'Data structure and Algorithms'
Notice the enrolls table is child relation to course. Student is child relation to Enrolls, but so too is the Section child to Enrolls. But the "ON" clause is looking at both the course and enrolls tables.
Again, personal learning preference style on querying. Also, by having all columns lined up, if you are looking for a specific one, look straight down the list and add/remove as necessary.
Additionally, for tables, I strongly suggest getting auto-increment column for IDs, especially when SSN (private info) could be bad, such as illegal or no such SSN available for students. You could still look up a person by name and have this internal auto-increment the basis of the enrollment table. If someone DOES have an SSN (or Green-Card number, student VISA, whatever), the internal ID won't be any conflict.
SELECT st.SSN, st.FirstName, st.LastName, se.SectionNo, se.Instructor
FROM STUDENT st
INNER JOIN ENROLLS en ON st.SSN = en.SSN
INNER JOIN SECTION se ON en.SectionNo = se.SectionNo and en.CourseNo = se.CourseNo
INNER JOIN COURSE co ON en.CourseNo = co.CourseNo
WHERE CourseName = 'Data structure and Algorithms'
Not unless you change the data source this is the minimum you need to get what is expected.
Unless you could use the CourseNo in the WHERE clause rather than the name?
SELECT st.SSN, st.FirstName, st.LastName, se.SectionNo, se.Instructor
FROM STUDENT st
INNER JOIN ENROLLS en ON st.SSN = en.SSN
INNER JOIN SECTION se ON en.SectionNo = se.SectionNo and en.CourseNo = se.CourseNo
WHERE se.CourseNo = 'CSC133'