SQL multiple occurrences of same variables and max function - mysql

Here is my table
serial Number Job Date
1 12/12/2013
1 1/2/2014
1 2/23/2010
1 3/15/2010
2 11/11/2012
2 6/5/2011
2 1/23/2010
I want only serial number which have job date older that 12/31/2013, in the above table it should be only the serial number 2 and the latest job date which is 11/11/2012
The result table should be
serial Number Job Date
2 11/11/2012
#ohho
using where will not work since it will also display the row,
Serial_number Job Date
1 12/12/2013
2 11/11/2012
select serial_number, max(job_date) from computers
where job_date < '2013-12-31'
Group by serial_number

Question is not very clear. As I understand, you are looking for all the serialnumbers which do not have any jobdate greater than or equal to 12/31/3013.
If so, below query will work.
select serialnumber -- if you want the biggest serialnumber, use max(serialnumber)
from myTable tt
where not exists (select * from myTable where serialnumber = tt.serialnumber
and jobdate > '12/31/3013')

Related

MySql select rows that have multiple records with date differance

I have a MySQL table that stores user operations , in other words it's like operations log that may contains duplicate rows with same user_id.table structured as follow:
id operation_name role operation_type user_id ... creation_date
1 delivery driver 1 2901 2018-05-23 06:06:22
2 ride driver 2 1197 2018-02-23 05:54:22
1 delivery driver 1 2901 2018-08-23 18:06:29
i want to select the rows with same user id and creation date in between a period of time not duplicate.
for example i want to select all the users ids that has did more than n operations between x period of time .
I tried the following query but its not working :
SELECT user_id FROM operations_log where
creation_date between creation_date and DATE_ADD(creation_date, INTERVAL 1 DAY) group by
user_id;
You need the HAVING clause.
SELECT user_id FROM operations_log where
creation_date between creation_date and DATE_ADD(creation_date, INTERVAL 1 DAY) group by
user_id HAVING COUNT(*)>1
Group By requires Having to utilize the aggregate function you'd need, which is count

get the most recent two dates for a customer MySQL

I need to retrieve the last two dates for customers with entries in at least two different dates, implying there are some customer who had purchased only in one date, the table is as follow
client_id date
1 2016-07-02
1 2016-07-02
1 2016-06-01
2 2015-06-01
as a response, I would get
client_id previous_date last_date
1 2016-06-01 2016-07-02
important:
a client can have multiple entries for the same date
a client can have entries only for one date, such customer should be discarded
Try this: group by the client_id column, with a having of count(*) > 1 to find results with more than one result. Then do a check of the min and max date, to ensure they aren't the same. Then just select the date, and order the results by date in desc order, with a limit of 2.
select
date
from
my_table
group by
client_id
having
min(date) <> max(date)
and count(*) > 1
order by
date desc
limit 2

Find average between two date columns for specific rows in a group

I have a table with 2 dates in it and a product, and I need to get the average days difference between them considering just the last 3 rows for each product.
SELECT AVG(DATEDIFF(date2, date1)) FROM table WHERE product = 121
This gives me the average of all the date differences for product 121
SELECT AVG(DATEDIFF(date2, date1)) FROM table WHERE product = 121 LIMIT 3
Still gives me the average off all the records, ignoring the LIMIT argument.
Also when I try a different approach, it also does ignore the last argument and shows the average off all the rows.
SELECT AVG(DATEDIFF(date2, date1)) FROM table WHERE product =121 && date1 > 2015-01-01
Any idea on how to fix this or what I'm doing wrong?
When you have problems like this, I recommend breaking it up and putting it back.
Before doing any calculations, you know that you need the last three rows for each product. So, if you want for example the rows with the latest date2 you can select them by doing the following:
SELECT *
FROM myTable
WHERE product = 121
ORDER BY date2 DESC
LIMIT 3;
That will select the 3 latest rows you want. Then, just use that as a subquery to preform the aggregation. This way, the calculations are only made on the rows you are concerned with:
SELECT product, AVG(DATEDIFF(date2, date1))
FROM(
SELECT product, date1, date2
FROM myTable
WHERE product = 121
ORDER BY date2 DESC
LIMIT 3) tempTable;

sql group by with group condition

I have a product table Like:
id p_name s_date
1 A 2014-10-10
2 B 2014-10-02
3 A 2014-10-08
4 A 2014-10-11
5 B 2014-10-08
I need to group on p_name. query like
SELECT * FROM product GROUP BY p_name;
This query retrieving record 1 and 2. But I need to check p_date with current date when grouping on p_name. If current date is '2014-10-11' I need record 4 for product group 'A' because record 4 matched with current date and record 2 for product group 'B' because no one of p_name 'B' matched with current date. A query to check p_date with current date when grouping on p_name. i need to retrieve the record from group which matched with current date, if no record matched with current date then the record of minimum date.
Maybe you can have a check with the MySQL Having clause.. and modify the query to somewhat like...
SELECT * FROM product GROUP BY p_name HAVING p_date = CURDATE();

SQL Query for columns with a unique value

I have a table which looks like this
courseid session_date title published
1 2012-07-01 Training Course A 0
1 2012-07-02 Training Course A 0
2 2012-07-04 Training Course B 1
2 2012-07-07 Training Course B 1
3 2012-07-05 Training Course C 1
3 2012-07-06 Training Course C 1
4 2012-07-07 Training Course D 1
4 2012-07-10 Training Course D 1
The table has two entries for each ID and Title because the session_date column shows the start date and the end date of the course.
I am trying to create a query that will pull the next five courses without showing any courses in the past.
I have gotten this far
SELECT session_date, title, courseid
FROM table
WHERE published = 1 AND session_date > DATE(NOW())
ORDER BY session_date ASC LIMIT 0,5
This pulls rows from the table for the next five session-dates but it includes both start dates and finish dates whereas I need the next five courses ordered by start date.
I need to create a query that will pull the earliest session_date for each courseid but ignore the row with the latest session_date for that same courseid but I am at a complete loss of how to do this.
Any help or advice would be most gratefully received.
If you group your results by course and select the MAX(session_date), you will get the latest of the dates associated with each course (i.e. the finish date):
SELECT courseid, MIN(session_date) AS start_date
FROM `table`
WHERE published = 1
GROUP BY courseid
HAVING start_date > CURRENT_DATE
ORDER BY start_date ASC
LIMIT 5
See it on sqlfiddle.
What you need to do is retrieve only the rows with the minimum session_date per courseid group and order by that resulting set:
SELECT
b.*
FROM
(
SELECT courseid, MIN(session_date) AS mindate
FROM tbl
GROUP BY courseid
) a
INNER JOIN
tbl b ON a.courseid = b.courseid AND a.mindate = b.session_date
WHERE
b.session_date > NOW() AND
b.published = 1
ORDER BY
b.session_date
LIMIT 5
But a much better design would be to only have one row per courseid and have two columns specifying start and end dates:
tbl
------------------
courseid [PK]
start_date
end_date
title
published
Then you can simply do:
SELECT *
FROM tbl
WHERE start_date > NOW() AND published = 1
ORDER BY start_date
LIMIT 5
Since values of all the columns in your SELECT clause are repeating, just use DISTINCT
SELECT distinct session_date, title, courseid
FROM table
WHERE published = 1 AND session_date > DATE(NOW())
ORDER BY session_date ASC LIMIT 0,5