get the most recent two dates for a customer MySQL - 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

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 closest value lower than a specific value and group by

Is there a possibility to get the closest value lower than a specific value with a group function without a join?
date productId stock
2014-12-27 1 10
2014-12-31 1 20
2015-01-05 1 30
2014-12-28 2 10
2015-01-04 2 20
The value is for example the date and should be lower than 2015-01-01 but the highest date value and the result should be ordered by the stock sac, so the result should be:
date productId stock
2014-12-28 2 10
2014-12-31 1 20
Of course, this could be solves with a join, but a join is slower in large tables, isn't it?
You're looking for the last day of 2014, it seems, for each distinct product id.
You do that with
SELECT MAX(date) date, product_id
FROM yourtable
WHERE date < '2015-01-01'
GROUP BY product_id
That gives you a collection of date, product_id. A compound index on (date, product_id) will make this query very efficient to evaluate.
Then you join that to your main table, like so.
SELECT a.*
FROM yourtable AS a
JOIN (
SELECT MAX(date) date, product_id
FROM yourtable
WHERE date < '2015-01-01'
GROUP BY product_id
) AS b USING(date,product_id)
ORDER BY a.product_id, a.date
and that retrieves the detail records for the last item in 2014. The same compound index will accelerate the JOIN.
You're worried about JOIN performance, and that's legitimate. But it can be improved with proper indexing. There really isn't a better way to do it.

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: max(date) returns wrong matching column

IF I run below query
select prod_id, date, price
from product_price;
I get these correct results:
3 2011-02-25 14:30:24 24000
3 2013-04-12 15:58:40 28000
3 2011-04-01 00:00:00 25000
3 2012-04-13 14:53:01 26500
3 2012-05-08 12:31:01 26500
But when I would like to get back a row with the max date with matching price, I do get back the max date column, BUT with the wrong price (24000 column), which should be actually 28000
SQL I've tried
select max(date), price from product_price group by prod_id;
select max(date), price
from product_price
group by prod_id having max(date);
Both sql returns the below wrong price result:
3 2013-04-12 15:58:40 24000
I should in fact get.
3 2013-04-12 15:58:40 28000
Any idea how to get back the matching price with the max date?
Thanks
Nic
You can ORDER BY date DESC with LIMIT 1 to get the prod_id with the max date, like this:
SELECT prod_id, date, price
FROM product_price
ORDER BY date DESC
LIMIT 1;
See it in action here:
SQL Fiddle Demo

Grouping with Nulls T-SQL

I have this problem with a SQL query (SQL Server 2008)
SELECT id, client, SUM(debt), date
FROM Table GROUP BY id, client, date
Returned from the query is
id client debt date
1 jim x 500 05/05/2012
2 jack a 900 06/06/2012
2 jack a 500 null
Is there a way to add in this scenario Jack a's debt (1400) and display the non null date i.e. 06/06/2012.
A person can only have 2 records max and 1 record is always date null so is there a way to do the sum and use the date that is not null?
Thanks
To group by client you have to remove id and date from your GROUP BY:
SELECT
MAX(id) AS newest_id, -- or MIN(id) if you prefer
client,
SUM(debt) AS total_debt,
MAX(date) AS most_recent -- or MIN(date) if you prefer
FROM YourTable
GROUP BY client