mysql: how to limit to query? - mysql

I select records and group them by date and uid(which is not the primary key), and I need to page those records by date, how can I do it?
For example:
uid date
1 2012-01-10
2 2012-01-10
3 2012-01-09
3 2012-01-09
3 2012-01-11
sql:
SELECT date, uid
FROM users
GROUP by date,`uid`
Results:
uid date
1 2012-01-10
2 2012-01-10
3 2012-01-09
3 2012-01-11
Because I need to page those record by date, if I use sql like:
SELECT date, uid
FROM users
GROUP by date,`uid`
LIMIT 0,2
then I just get the records like this:
uid date
1 2012-01-10
2 2012-01-10
. how can I page the record by date.
The results I want when page size is 2:
uid date
1 2012-01-10
2 2012-01-10
3 2012-01-09

SELECT date, uid
FROM users
WHERE date >= 'your date'
AND date < 'your date' LIMIT 0, 3

Related

MySQL order results by datetime and grouped by user ID

I have a table that looks like this:
user_id
datetime
activity
2
2022-01-10 12:00:00
Logout
1
2022-01-09 12:00:00
Login
3
2022-01-08 12:00:00
Login
3
2022-01-07 12:00:00
Register
2
2022-01-06 12:00:00
Login
1
2022-01-05 12:00:00
Register
If I query the table sorted by datetime DESC I will get the result like the above.
How can I extend the query so that I can get the results grouped by the user_id like below?
user_id
datetime
activity
2
2022-01-10 12:00:00
Logout
2
2022-01-06 12:00:00
Login
1
2022-01-09 12:00:00
Login
1
2022-01-05 12:00:00
Register
3
2022-01-08 12:00:00
Login
3
2022-01-07 12:00:00
Register
The logic is the records will be sorted by datetime DESC at first and when it encounters the user_id for the record, it will aggregate all records belonging to the user_id together and maintaining the datetime DESC sorting within the user_id group.
Use MAX() window function in the ORDER BY clause:
SELECT *
FROM tablename
ORDER BY MAX(datetime) OVER (PARTITION BY user_id) DESC,
user_id, -- just in case there are duplicate datetimes, remove this if the column datetime is unique
datetime DESC;
See the demo.

mysql select 2 table based on date and combine each other

i Need some help.. i have some case like this below
i have 2 table.. call ("in_table" and "out_table")
data "in_table" look like
stock_id item_id date qty_in
-----------------------------------------
1 11 2017-07-11 12
2 11 2017-07-11 10
3 12 2017-07-11 10
4 12 2017-07-19 10
And i have "out_table" is like
id_tr item_id date qty_out
-------------------------------------
1 11 2017-07-19 2
1 12 2017-07-19 1
2 11 2017-07-19 2
2 12 2017-07-19 1
And i want to combine the date and display all the data like this,
Update: the join is by item_id but i want to select by date
date item_id qty_in qty_out
---------------------------------------
2013-07-11 11 22 0
2013-07-11 12 10 0
2013-07-19 11 0 4
2013-07-19 12 10 2
Thank you for your help.
It looks like you need kind of a full outer join of two aggregate subqueries. But in your case I would get item_id and date in a union subquery (derived table) and the sums in correlated subqueries (subselect).
select item_id, date,
(select sum(qty_in) from in_table i where i.item_id = sub.item_id and i.date = sub.date) as qty_in,
(select sum(qty_out) from out_table o where o.item_id = sub.item_id and o.date = sub.date) as qty_out
from (
select item_id, date from in_table
union
select item_id, date from out_table
) sub
order by date, item_id

How to select left table, order by max date of the right table and max date is less than or equal to NOW()?

Below are my database tables
left_table
id name
1 A
2 B
3 C
4 D
5 E
right_table
id left_table_id size date_time
1 1 xs 2017-06-13 14:20:00
2 3 s 2017-06-13 14:25:00
3 2 xs 2017-06-13 14:27:00
4 1 s 2017-06-13 14:30:00
5 2 m 2017-06-13 14:32:00
6 2 xs 2017-06-13 14:33:00
7 3 xl 2017-06-13 14:40:00
8 4 s 2017-06-13 14:41:00
9 4 m 2017-06-13 14:45:00
10 5 m 2017-06-13 14:46:00
How to write the sql query to get the records of the left_table order by the max(date_time) DESC where date_time <= NOW() as the following result.
result left_table order by max(add_time) DESC
id name max(add_time)
2 B 2017-06-13 14:33:00
1 A 2017-06-13 14:30:00
3 C 2017-06-13 14:25:00
This is because date_time of left_table_id of 4 and 5 is > NOW(). Assume NOW() = 2017-06-13 14::35:00. Note that left_table_id 3 has one add_time > NOW() but it is still being selected because it has other right record, add_time <= NOW().
You can use a query like the following:
SELECT t.id, t.name, r.date_time
FROM left_table AS t
JOIN (
SELECT id, MAX(date_time) AS date_time
FROM right_table
WHERE date_time <= NOW()
GROUP BY id) AS r ON t.id = r.id
ORDER BY r.date_time DESC;
The query uses a derived table which filters out records that have a date_time value that is past the current timestamp. It also group records in order to get the latest date_time value per id. Using this derived table we can easily perform the sorting required using its date_time field.

How to compare date fields of different fields?

id ref_id dates
1 1 2017-01-01 00:00:00
2 1 2017-01-31 00:00:00
3 2 2017-01-01 00:00:00
4 2 2017-01-31 00:00:00
5 3 2016-01-01 00:00:00
6 3 2016-01-31 00:00:00
Query will be like
SELECT * FROM table WHERE dates GROUP By ref_id
I would like to compare ref_id wise Start Date in 1st row & End Date in 2nd row should be between Current Date.
So output will be only those ref_id which are in between current date.
Output
ref_id
1
2
or GROUP_CONCAT of that 1,2
Would like to using single query without UNION.
Try this:
SELECT ref_id
FROM table
GROUP BY ref_id
HAVING NOW() BETWEEN MIN(dates) AND MAX(dates)
The query uses NOW in order to get the current date/time value. If this value lies within the interval defined by the minimum and maximum date value of a ref_id group, then this ref_id is returned by the query.

Mysql Group By Date

I have Table with 3 Columns, Column1 with Date and Column2 stores Points which is nothing but Some random Number between 1 to 9 and column 3 which have some unique email address in every cell.
Now I want to add the points grouped by date for last 5 days.
That is if I have 3 rows for day one, 1 rows for day two, 3 rows for day 3 and 2 rows for day 4 & 5 I should get the sum of points of these 11 rows grouped by date as 5 rows for five days.
Input
Date Points
Email
2012-07-01 5 a#sample.com
2012-07-01 6 b#sample.com
2012-07-01 2 c#sample.com
2012-07-02 5 d#sample.com
2012-07-03 8 e#sample.com
2012-07-03 7
f#sample.com
2012-07-03 1
y#sample.com
2012-07-04 3 x#sample.com
2012-07-04 2 f#sample.com
2012-07-05 3 g#sample.com
2012-07-05 9 b#sample.com
Output
Date Points
2012-07-01 13
2012-07-02 5
2012-07-03 16
2012-07-04 5
2012-07-05 12
Please suggest me a MySQL query for the above.
select `Date`,sum(`Points`) from my_table group by `Date`;
select `Date`,sum(`Points`) from my_table group by `Date` Limit 5;
select Date,sum(Points) from my_table group by Date;
select [Date],SUM(Points)
from myTable1
group by [Date]
You can do:
SELECT date, SUM(points) AS points
FROM tbl
WHERE date > CURDATE() - INTERVAL 5 DAY
GROUP BY date