Join MySQL on date range - mysql

I'm trying to query MySQL. Say I have 3 tables and imagine the data looked like this:
apples_sold
+--------+------------+----------+------+
| tranID | date | apple_id | sold |
+--------+------------+----------+------+
| 101 | 2012-07-01 | a01 | 2 |
| 102 | 2012-07-02 | a01 | 5 |
| 103 | 2012-07-03 | a01 | 1 |
| 104 | 2012-07-04 | a01 | 0 |
| 105 | 2012-07-05 | a01 | 2 |
+--------+------------+----------+------+
price_history
+---------+------------+----------+-------+
| priceID | date | apple_id | price |
+---------+------------+----------+-------+
| p01 | 2012-07-01 | a01 | $0.25 |
| p02 | 2012-07-03 | a01 | $0.10 | <- price change
+---------+------------+----------+-------+
apple_name
+----------+----------+
| apple_id | name |
+----------+----------+
| a01 | McIntosh |
+----------+----------+
Trying to create a query that outputs this:
apple_prices
+------------+----------+-------+
| date | name | price |
+------------+----------+-------+
| 2012-07-01 | McIntosh | $0.25 |
| 2012-07-02 | McIntosh | $0.25 |
| 2012-07-03 | McIntosh | $0.10 | <- price change
| 2012-07-04 | McIntosh | $0.10 |
| 2012-07-05 | McIntosh | $0.10 |
+------------+----------+-------+
Didn't want to store the price with the apples_sold record, normalisation and all. The problem is im unsure the best way to join the sold record with the price.

You can use:
SELECT a.date, d.name, c.price
FROM apples_sold a
INNER JOIN
(
SELECT a.date, MAX(b.date) AS maxdate
FROM apples_sold a
INNER JOIN price_history b ON a.date >= b.date
GROUP BY a.date
) b ON a.date = b.date
INNER JOIN price_history c ON b.maxdate = c.date
INNER JOIN apple_name d ON a.apple_id = d.apple_id
SQLFiddle Demo

Related

MySQL Join one table to other two tables

I have a MySQL database including following tables that used to maintain transactions of some documents.
tbl_documents Table
+----+---------+------+---------+
| id | file_no | name | subject |
+----+---------+------+---------+
| 1 | A/10 | F1 | a |
| 2 | A/11 | F2 | b |
| 3 | A/12 | F3 | c |
| 4 | A/13 | F4 | d |
+----+---------+------+---------+
tbl_requests
+----+-------------+----------------+---------------+
| id | document_id | requested_date | approved_date |
+----+-------------+----------------+---------------+
| 1 | 1 | 2019-12-01 | 2019-12-02 |
| 2 | 2 | 2019-12-08 | 2019-12-08 |
+----+-------------+----------------+---------------+
tbl_issues
+----+-------------+------------+
| id | document_id | issue_date |
+----+-------------+------------+
| 1 | 1 | 2019-12-05 |
| 2 | 2 | 2019-12-10 |
+----+-------------+------------+
I want to get the following / Desired output by joining above three tables.
Desired Output
+---------+------+---------+----------------+---------------+------------+
| file_no | name | subject | requested_date | approved_date | issue_date |
+---------+------+---------+----------------+---------------+------------+
| A/10 | F1 | a | 2019-12-01 | 2019-12-02 | 2019-12-05 |
| A/11 | F2 | b | 2019-12-08 | 2019-12-08 | 2019-12-10 |
+---------+------+---------+----------------+---------------+------------+
To do that, I used the following query
select tbl_documents.file_no, tbl_documents.name, tbl_documents.subject, requested_date, approved_date, tbl_issues.issue_date
from tbl_documents
right join tbl_requests on tbl_requests.document_id=tbl_documents.id
right join tbl_issues on tbl_issues.document_id=tbl_documents.id
But didn't get the expected output. Can anyone help ?
Just use inner joins, as in:
select
d.file_no,
d.name,
d.subject,
r.requested_date,
r.approved_date,
i.issue_date
from tbl_documents d
join tbl_requests r on r.document_id = d.id
join tbl_issues i on i.document_id = d.id

MySQL table value difference related to current date and previous date

I have some tables that used to manage vehicle details. Related tables as follows:
store_item
+---------+-----------+--------+
| item_id | item_name | status |
+---------+-----------+--------+
| 1 | Diesel | 1 |
| 2 | Petrol | 1 |
+---------+-----------+--------+
tbl_vehicle
+------------+---------------+
| vehicle_id | registered_no |
+------------+---------------+
| 1 | LE-7476 |
| 2 | 270-0523 |
+------------+---------------+
tbl_direct_fuel
+----------------+----------+---------+------------+------------+
| direct_fuel_id | vehicle | orderNo | issue_date | milo_meter |
+----------------+----------+---------+------------+------------+
| 1 | LE-7476 | 173072 | 2019-11-12 | 30,000 |
| 2 | LE-7476 | 173069 | 2019-11-08 | 29,600 |
| 3 | LE-7476 | 173059 | 2019-11-05 | 29,000 |
| 4 | LE-7476 | 173055 | 2019-10-08 | 25,000 |
| 5 | 270-0523 | 173068 | 2019-11-02 | 10,000 |
| 6 | 270-0523 | 173067 | 2019-10-02 | 8,500 |
+----------------+----------+---------+------------+------------+
tbl_direct_fuel_details
+------------------------+----------------+------+----------+------------+
| direct_fuel_details_id | direct_fuel_id | item | fuel_qty | fuel_price |
+------------------------+----------------+------+----------+------------+
| 100 | 1 | 1 | 20 | 105 |
| 101 | 2 | 1 | 15 | 105 |
| 102 | 3 | 1 | 12 | 105 |
| 103 | 4 | 2 | 50 | 165 |
| 104 | 5 | 1 | 25 | 100 |
| 105 | 6 | 2 | 18 | 165 |
+------------------------+----------------+------+----------+------------+
Desired Output
I want to get no_of_kms, vs issued fuel quantities using issue_date & milo_meter (current milo_meter - previous milo_meter). The output as follows :
+----------+-----------+
| vehicle | no_of_kms |
+----------+-----------+
| LE-7476 | 400 |
| LE-7476 | 600 |
| LE-7476 | 4,000 |
| 270-0523 | 1500 |
+----------+-----------+
I used the following query :
select v1.registered_no as vehicle, si.item_name as fuel, df.milo_meter - df.milo_meter as no_of_kms
from (select dfd.item, sum(dfd.fuel_qty) AS qty
from tbl_direct_fuel df
join tbl_direct_fuel_details dfd on df.direct_fuel_id = dfd.direct_fuel_id
join tbl_vehicle v1 on df.vehicle = v1.vehicle_id
where df.status = 1
group by registered_no) dfd
join store_item si on dfd.item = si.item_id
join (select item, sum(dfd.fuel_qty) AS fuel_qty
from tbl_direct_fuel_details
group by item) dfd on si.item_id=dfd.item
But the above query didn't working fine. What may be going wrong ? Can anyone help me ?
If you are running MySQL 8.0, you can do this simply with window function lag():
select *
from (
select
issue_date,
vehicle,
milo_meter
- lag(milo_meter) over(partition by vehicle order by issue_date) no_of_kms
from tbl_direct_fuel
) t
where no_of_kms is not null
order by vehicle desc, issue_date desc
I added the issue_date to the output columns, since this seems like a sensible information to understand the results.
In earlier versions, I think that an inline query might do the trick:
select *
from (
select
issue_date,
vehicle,
milo_meter - (
select max(milo_meter)
from tbl_direct_fuel d1
where d1.vehicle = d.vehicle and d1.issue_date < d.issue_date
) no_of_kms
from tbl_direct_fuel d
) t
where no_of_kms is not null
order by vehicle desc, issue_date desc
This assumes that the milo_meter of a given vehicule can only increase, which seems like a reasonable assumption.
Demo on DB Fiddle
Both queries return:
issue_date | vehicle | no_of_kms
:--------- | :------- | --------:
2019-11-12 | LE-7476 | 400
2019-11-08 | LE-7476 | 600
2019-11-05 | LE-7476 | 4000
2019-11-02 | 270-0523 | 1500

sql inner join on more than 2 tables and aggregate function

I have the following tables,
select * from department;
+--------------+--------------+--------+------------+---------------+
| departmentid | name | budget | startdate | administrator |
+==============+==============+========+============+===============+
| 101 | Computer Sci | 1000 | 2010-12-26 | XYZ |
| 102 | ECE | 500 | 2015-02-15 | ABC |
| 103 | EEE | 1500 | 2016-08-25 | PQR |
| 104 | Mech | 2500 | 2017-08-22 | LMN |
+--------------+--------------+--------+------------+---------------+
select * from course;
+----------+-------------------+---------+--------------+
| courseid | title | credits | departmentid |
+==========+===================+=========+==============+
| 1001 | Data structures | 12 | 101 |
| 1002 | Algorithms | 12 | 101 |
| 1003 | Graphics | 20 | 101 |
| 2001 | DSP | 20 | 102 |
| 2002 | Matlab | 20 | 102 |
| 2003 | Maths | 10 | 102 |
| 3001 | CAD | 10 | 104 |
| 4001 | Power electronics | 10 | 103 |
| 4002 | Semi conductors | 20 | 103 |
+----------+-------------------+---------+--------------+
select * from student_grade;
+--------------+----------+----------+-------+
| enrollmentid | courseid | personid | grade |
+==============+==========+==========+=======+
| 1 | 1001 | 1 | A |
| 2 | 1002 | 1 | B |
| 3 | 1003 | 1 | A |
| 4 | 3001 | 3 | A |
| 5 | 3001 | 2 | B |
| 6 | 4001 | 4 | A |
| 7 | 4002 | 4 | A |
+--------------+----------+----------+-------+
select * from person;
+----------+----------+-----------+------------+----------------+
| personid | lastname | firstname | hiredate | enrollmentdate |
+==========+==========+===========+============+================+
| 1 | Goudar | Anil | 2016-08-16 | 2016-08-17 |
| 2 | Goudar | Sunil | 2018-09-16 | 2018-09-27 |
| 3 | Dambal | Abhi | 2018-05-07 | 2018-06-17 |
| 4 | Desai | Arun | 2018-05-07 | 2018-06-17 |
| 5 | Xam | Sam | 2018-12-08 | 2018-12-08 |
| 6 | Chatpati | Mangal | 2018-10-10 | 2018-10-08 |
| 9 | Shankar | Dev | 2018-10-10 | 2018-10-08 |
| 10 | Shankar | Mahadev | 2018-08-10 | 2018-08-11 |
+----------+----------+-----------+------------+----------------+
Now I am trying to get the department details with number of students belonging to the department.
And here is my query,
select d.departmentid, d.name, sg.personid from department d inner join course c on c.departmentid = d.departmentid inner join student_grade sg on sg.courseid = c.courseid;
+--------------+--------------+----------+
| departmentid | name | personid |
+==============+==============+==========+
| 101 | Computer Sci | 1 |
| 101 | Computer Sci | 1 |
| 101 | Computer Sci | 1 |
| 104 | Mech | 3 |
| 104 | Mech | 2 |
| 103 | EEE | 4 |
| 103 | EEE | 4 |
+--------------+--------------+----------+
But I want to get the count(distinct(personid)) for each department like group by clause. But I am getting the error with the following query.
select d.departmentid, d.name, count(distinct(sg.personid)) from department d inner join course c on c.departmentid = d.departmentid inner join student_grade sg on sg.courseid = c.courseid;
Cannot use non GROUP BY column 'departmentid' in query results without an aggregate function
Please help me, where I am going wrong.
you have to use group by as you used aggregate funtion
select d.departmentid, d.name,
count(distinct(sg.personid))
from department d inner join course c on c.departmentid = d.departmentid inner join student_grade sg on sg.courseid = c.courseid;
group by d.departmentid, d.name
Use this:-
select d.departmentid, d.name, count(distinct(sg.personid)) from department d inner join course c on c.departmentid = d.departmentid inner join student_grade sg on sg.courseid = c.courseid group by d.departmentid, d.name;

Join table to itself to find products bought one after the other

I have a table which contains the following columns:
+------------------+----------+
| date_of_purchase | product |
+------------------+----------+
| 2013-06-18 | A |
| 2013-07-18 | A |
| 2013-08-24 | A |
| 2013-10-21 | A |
| 2013-11-20 | A |
| 2013-12-20 | A |
| 2014-01-20 | A |
| 2014-03-24 | A |
| 2014-03-24 | B |
| 2014-04-23 | B |
| 2014-04-23 | A |
| 2014-05-16 | B |
| 2014-05-23 | A |
+------------------+----------+
And I want to get something like this:
+----------+----------+------------+------------+
| product1 | product2 | date_a | date_b |
+----------+----------+------------+------------+
| A | B | 2014-01-20 | 2014-03-24 |
| A | B | 2014-03-24 | 2014-04-23 |
| B | A | 2014-05-16 | 2014-05-23 |
| A | B | 2014-04-23 | 2014-05-16 |
+----------+----------+------------+------------+
What I am trying to do is to check which product "B" is bought after product "A".
I am new to stack-overflow, so the way I am asking this question might seem vague. But need help on this one.
The derived table keeps track of the next purchase date using a variable which is then used to check whether a different product was purchased on the next purchase date
select t1.product, t2.product, t1.date_of_purchase, t2.date_of_purchase from (
select date_of_purchase, product,
#nextDateOfPurchase nextDateOfPurchase,
#nextDateOfPurchase := date_of_purchase
from Table1 t
order by date_of_purchase desc
) t1 join Table1 t2 on t2.date_of_purchase = t1.nextDateOfPurchase
and t2.product <> t1.product
and t2.date_of_purchase <> t1.date_of_purchase
order by t1.date_of_purchase
http://sqlfiddle.com/#!9/44b24/3

SQL Database - query to produce a list of doctor IDs and their names with the number of appointments they have?

I have three tables; doctor, person, and appointment.
doctor table:
+-----------+----------+---------+----------------+----------------+
| doctor_id | phone_no | room_no | date_qualified | date_appointed |
+-----------+----------+---------+----------------+----------------+
| 50 | 1234 | 1 | 1963-09-01 | 1991-05-10 |
| 51 | 1235 | 2 | 1973-09-12 | 1991-05-10 |
| 52 | 1236 | 3 | 1990-10-02 | 1993-04-01 |
| 53 | 1237 | 4 | 1965-06-30 | 1994-03-01 |
+-----------+----------+---------+----------------+----------------+
person table
+-----------+----------+-----------+---------------+------+
| person_id | initials | last_name | date_of_birth | sex |
+-----------+----------+-----------+---------------+------+
| 100 | T | Williams | 1972-01-12 | m |
| 101 | J | Garcia | 1981-03-18 | f |
| 102 | W | Fisher | 1950-10-22 | m |
| 103 | K | Waldon | 1942-06-01 | m |
| 104 | P | Timms | 1928-06-03 | m |
| 105 | A | Dryden | 1944-06-23 | m |
| 106 | F | Fogg | 1955-10-16 | f |
| 150 | T | Saj | 1994-06-17 | m |
| 50 | A | Cameron | 1937-04-04 | m |
| 51 | B | Finlay | 1948-12-01 | m |
| 52 | C | King | 1965-06-06 | f |
| 53 | D | Waldon | 1938-07-08 | f |
+-----------+----------+-----------+---------------+------+
appointment table
+-----------+------------+------------+-----------+---------------+
| doctor_id | patient_id | appt_date | appt_time | appt_duration |
+-----------+------------+------------+-----------+---------------+
| 50 | 100 | 1994-08-10 | 10:00:00 | 10 |
| 50 | 100 | 1994-08-16 | 10:50:00 | 10 |
| 50 | 102 | 1994-08-21 | 11:20:00 | 20 |
| 50 | 103 | 1994-08-10 | 10:10:00 | 10 |
| 50 | 104 | 1994-08-10 | 10:20:00 | 20 |
| 52 | 102 | 1994-08-10 | 10:00:00 | 10 |
| 52 | 105 | 1994-08-10 | 10:10:00 | 10 |
| 52 | 150 | 2014-03-10 | 12:00:00 | 15 |
| 53 | 106 | 1994-08-10 | 11:30:00 | 10 |
+-----------+------------+------------+-----------+---------------+
I need to create a query to produce a list of doctor IDs and their names with the number of appointments they have.
I have already created a statement to produce a list of doctor IDs with the number of appointments they have but im not sure how to produce a list with doctor IDs and their names.
The statement that I have now is:
select doctor.doctor_id, count(appointment.appt_time) as no_appt
from doctor
left join appointment
on doctor.doctor_id = appointment.doctor_id
group by doctor.doctor_id;
Please Help.
You need an additional join to the person table. Apparently, the doctor_id is the link. Yuck. This should be an explicit column rather than a re-use of the id.
select d.doctor_id, p.initials, p.last_name, count(appointment.appt_time) as no_appt
from doctor d left join
appointment a
on d.doctor_id = a.doctor_id left join
person p
on d.doctor_id = p.person_id
group by d.doctor_id, p.initials, p.last_name;
In MySQL, you don't actually need to add the two columns to the group by, but it is good practice to do so.
select doctor.doctor_id, person.initials, person.last_name, count(appointment.appt_time) as no_appt
from doctor
left join appointment on doctor.doctor_id = appointment.doctor_id
left join person on person.person_id = appointment.patient_id
group by doctor.doctor_id;
your SQL is nearly there - you just need to add a JOIN to the Person table to get the initial and last_name of the doctors - like this:
SELECT
d.doctor_id,
p.initials,
p.last_name,
COUNT(a.*)
FROM [person] p
JOIN [doctor] d ON p.person_id = d.doctor_id
LEFT JOIN [appointment] a ON a.doctor_id = d.doctor_id
GROUP BY d.doctor_id, p.initials, p.last_name
Hope this helps