I have table images with uploaded date field. How can I get the newest image if 2 or more images were uploaded in same day.
1.We check on hours, minutes or seconds.
2.If the H:M:S are same, check on the sequences of inserted ID.
ID | URL | create |
1 | 01.jpg | 2017-02-23 10:24:41 |<<same H:M:s
2 | 02.jpg | 2017-02-23 10:24:41 |<<same H:M:s
3 | 03.jpg | 2017-02-23 10:50:00 |<<same H
4 | 04.jpg | 2017-02-24 21:50:00 |<<others
5 | 05.jpg | 2017-03-28 17:50:00 |<<others
Output: I want to get only
3 | 03.jpg | 2017-02-23 10:50:00 |<< newer than 1, 2
4 | 04.jpg | 2017-02-24 21:50:00 |
5 | 05.jpg | 2017-03-28 17:50:00 |
To get all the rows with the max timestamp on a given day, use
select created,max(id) maxid
from (select t1.*,(select count(distinct created) from t
where created >=t1.created
and cast(created as date)=cast(t1.created as date)) rn
from t t1) x
where rn=1
If there can be ties on the latest timestamp and only the latest id is needed in that case, the above query can be extended to the following.
select y.maxid id,t.url,t.created
from (
select created,max(id) maxid
from (select t1.*,(select count(distinct created) from t
where created >=t1.created
and cast(created as date)=cast(t1.created as date)) rn
from t t1) x
where rn=1
group by created
) y
join t on t.id=y.maxid and t.created=y.created
SELECT t1.*
FROM yourTable t1
INNER JOIN
(
SELECT MAX(create) AS latest, MAX(ID) AS maxID
FROM yourTable
GROUP BY DATE(create)
) t2
ON t1.create = t2.latest AND
t1.ID = t2.maxID
How about using an ORDER BY and a LIMIT?
SELECT *
FROM Images
ORDER BY create DESC, ID DESC
LIMIT 1
The ORDER by sorts the images by create, then ID. The limit selects only 1. This should select only the newest photo.
Related
I have a 'sales' table called phpbb_sold which records each 'sale' as a row.
I am able to use a WHERE clause with the uitemid field to select one particular item in the sales records, as seen below:
SELECT uitemid, locktime, migrated_sold FROM phpbb_sold WHERE uitemid=342;
+---------+------------+---------------+
| uitemid | locktime | migrated_sold |
+---------+------------+---------------+
| 342 | 1632523854 | 1 |
| 342 | 1634239244 | 1 |
| 342 | 1634240072 | 1 |
| 342 | 1636367271 | 1 |
+---------+------------+---------------+
uitemid = number that identifies this as a sale of X item. locktime = UNIX timestamp that shows the datetime that the item was sold. migrated_sold = the quantity of the item sold. So this is nice, I have a table that keeps a record of each sale as it happens.
What I want to achieve though, is a record of the total number of sales of this item type, for each day in a 6 month period spanning back from the current date, and including each day regardless of whether a sale was made or not. So the desired output of my query would be:
SELECT (the query I want goes here) and returns the following rows...;
+------------+------------+
| caldate | sold_total |
+------------+------------+
| 2021-09-23 | 2 |
| 2021-09-24 | 0 |
| 2021-09-25 | 1 |
| 2021-09-26 | 0 |
| 2021-09-27 | 0 |
| 2021-09-28 | 1 |
+------------+------------+
Note that each day is included as a row in the results, even where the sales total for that day is 0. I read that to do this, I would be required to create a calendar table with one column and all the days I want as rows, so I went ahead and did that:
SELECT caldate FROM phpbb_calendar;
+------------+
| caldate |
+------------+
| 2021-09-23 |
| 2021-09-24 |
| 2021-09-25 |
| 2021-09-26 |
| 2021-09-27 |
| 2021-09-28 |
+------------+
Now all that remains is for me to make the query. I need to somehow return all the rows from the phpbb_calendar table, joining the data from sum() (?) of the total migrated_sold for those days where exists, and a 0 where no sales took place.
I anticipated some issues with the UNIX timestamp, but it's okay because I am able to get caldate and locktime fields to be the same format by using from_unixtime(locktime, '%Y-%m-%d'), so both dates will be in the YYYY-MM-DD format for comparison.
Please could someone help me with this. I've gotten so close every time but it seems that everyone else's request is only slightly different from mine, so existing questions and answers have not been able to satisfy my requirements.
End goal is to use a JS chart library (AnyChart) to show a line graph of the number of sales of the item over time. But to get there, I first need to provide it with the query necessary for it to display that data.
Thanks
Update
Using this query:
SELECT c.caldate, u.uitemid, sum(v.migrated_sold) as total_sales
from phpbb_calendar c cross join
(select distinct uitemid from phpbb_sold) u left join
phpbb_sold v
on c.caldate = from_unixtime(v.locktime, '%Y-%m-%d') WHERE u.uitemid = 39 and c.caldate <= curdate() GROUP BY c.caldate ORDER BY c.caldate;
Returns:
But as you can see, it's just tallying up the total number of sales ever made or something - its clearly incrementing in a way I don't understand.
I don't want it to do that - I want it to count the number of total sales on each day individually. The results should look like this:
So that what is returned is basically a 'histogram' of sales, if any occurred, including 'empty' days where there were no sales (so these empty days must still be returned as rows).
SELECT c.caldate, u.uitemid, COALESCE(SUM(v.migrated_sold), 0) AS total_sales
FROM phpbb_calendar c
CROSS JOIN (SELECT DISTINCT uitemid FROM phpbb_sold WHERE uitemid = 37) u
LEFT JOIN phpbb_sold v
ON v.locktime BETWEEN UNIX_TIMESTAMP(TIMESTAMP(c.caldate)) AND UNIX_TIMESTAMP(TIMESTAMP(c.caldate, '23:59:59'))
AND u.uitemid = v.uitemid
WHERE c.caldate BETWEEN CURDATE() - INTERVAL 6 MONTH AND CURDATE()
GROUP BY c.caldate, u.uitemid
ORDER BY c.caldate;
N.B. I have changed your join to use the unix_timestamp as it should be more efficient and it can use any existing index on locktime
check this out:
select id, d, sum(s) from (
select U.id, d, 0 s from (
select adddate(current_date(),-rows.r) d from (
select (#row_number := #row_number + 1) r
from information_schema.columns,
(SELECT #row_number := 0) AS x
limit 200
) rows
) dates,
(SELECT distinct uitemid id FROM `phpbb_sold`) U
where d > adddate(current_date(), interval -6 month)
union
select uitemid, date(from_unixtime(locktime)),sum(migrated_sold)
from `phpbb_sold`
group by uitemid, date(from_unixtime(locktime))
) sales_union
group by id, d
order by id, d;
see dbfiddle
no need for calendar table
Here's what I'm trying to do. Let's say I have this table t:
key_id | id | record_date | other_cols
1 | 18 | 2011-04-03 | x
2 | 18 | 2012-05-19 | y
3 | 18 | 2012-08-09 | z
4 | 19 | 2009-06-01 | a
5 | 19 | 2011-04-03 | b
6 | 19 | 2011-10-25 | c
7 | 19 | 2012-08-09 | d
For each id, I want to select the row containing the minimum record_date. So I'd get:
key_id | id | record_date | other_cols
1 | 18 | 2011-04-03 | x
4 | 19 | 2009-06-01 | a
The only solutions I've seen to this problem assume that all record_date entries are distinct, but that is not this case in my data. Using a subquery and an inner join with two conditions would give me duplicate rows for some ids, which I don't want:
key_id | id | record_date | other_cols
1 | 18 | 2011-04-03 | x
5 | 19 | 2011-04-03 | b
4 | 19 | 2009-06-01 | a
How about something like:
SELECT mt.*
FROM MyTable mt INNER JOIN
(
SELECT id, MIN(record_date) AS MinDate
FROM MyTable
GROUP BY id
) t ON mt.id = t.id AND mt.record_date = t.MinDate
This gets the minimum date per ID, and then gets the values based on those values. The only time you would have duplicates is if there are duplicate minimum record_dates for the same ID.
I could get to your expected result just by doing this in mysql:
SELECT id, min(record_date), other_cols
FROM mytable
GROUP BY id
Does this work for you?
To get the cheapest product in each category, you use the MIN() function in a correlated subquery as follows:
SELECT categoryid,
productid,
productName,
unitprice
FROM products a WHERE unitprice = (
SELECT MIN(unitprice)
FROM products b
WHERE b.categoryid = a.categoryid)
The outer query scans all rows in the products table and returns the products that have unit prices match with the lowest price in each category returned by the correlated subquery.
I would like to add to some of the other answers here, if you don't need the first item but say the second number for example you can use rownumber in a subquery and base your result set off of that.
SELECT * FROM
(
SELECT
ROW_NUM() OVER (PARTITION BY Id ORDER BY record_date, other_cols) as rownum,
*
FROM products P
) INNER
WHERE rownum = 2
This also allows you to order off multiple columns in the subquery which may help if two record_dates have identical values. You can also partition off of multiple columns if needed by delimiting them with a comma
This does it simply:
select t2.id,t2.record_date,t2.other_cols
from (select ROW_NUMBER() over(partition by id order by record_date)as rownum,id,record_date,other_cols from MyTable)t2
where t2.rownum = 1
If record_date has no duplicates within a group:
think of it as of filtering. Simpliy get (WHERE) one (MIN(record_date)) row from the current group:
SELECT * FROM t t1 WHERE record_date = (
select MIN(record_date)
from t t2 where t2.group_id = t1.group_id)
If there could be 2+ min record_date within a group:
filter out non-min rows (see above)
then (AND) pick only one from the 2+ min record_date rows, within the given group_id. E.g. pick the one with the min unique key:
AND key_id = (select MIN(key_id)
from t t3 where t3.record_date = t1.record_date
and t3.group_id = t1.group_id)
so
key_id | group_id | record_date | other_cols
1 | 18 | 2011-04-03 | x
4 | 19 | 2009-06-01 | a
8 | 19 | 2009-06-01 | e
will select key_ids: #1 and #4
SELECT p.* FROM tbl p
INNER JOIN(
SELECT t.id, MIN(record_date) AS MinDate
FROM tbl t
GROUP BY t.id
) t ON p.id = t.id AND p.record_date = t.MinDate
GROUP BY p.id
This code eliminates duplicate record_date in case there are same ids with same record_date.
If you want duplicates, remove the last line GROUP BY p.id.
This a old question, but this can useful for someone
In my case i can't using a sub query because i have a big query and i need using min() on my result, if i use sub query the db need reexecute my big query. i'm using Mysql
select t.*
from (select m.*, #g := 0
from MyTable m --here i have a big query
order by id, record_date) t
where (1 = case when #g = 0 or #g <> id then 1 else 0 end )
and (#g := id) IS NOT NULL
Basically I ordered the result and then put a variable in order to get only the first record in each group.
The below query takes the first date for each work order (in a table of showing all status changes):
SELECT
WORKORDERNUM,
MIN(DATE)
FROM
WORKORDERS
WHERE
DATE >= to_date('2015-01-01','YYYY-MM-DD')
GROUP BY
WORKORDERNUM
select
department,
min_salary,
(select s1.last_name from staff s1 where s1.salary=s3.min_salary ) lastname
from
(select department, min (salary) min_salary from staff s2 group by s2.department) s3
I have a MySQL table that shows the following:
ID DATE FREQUENCY
-- ---------- ---------
1 2017-08-01 1
2 2017-08-02 1
3 2017-08-03 0
4 2017-08-04 1
5 2017-08-05 1
6 2017-08-06 1
I am trying to get the easiest way to group every time there are consecutive 1's on the frequency column. Then I would like to display them.
Example
2 (There are 2 consecutive 1's)
3 (There are also 3 consecutive 1's)
Thank you
This is a typical gaps-and-island problem.
You can solve it by comparing the overal rank of records to their relative ranks in groups of records having the same frequency. The difference between the ranks gives you the group each record belongs to.
The rest is just filtering and aggregating groups that have a frequency of 1.
Query:
select
min(id) min_id,
max(id) max_id,
min(date) min_date,
max(date) max_date,
count(*) streak_length
from (
select
t.*,
row_number() over(order by date) rn1,
row_number() over(partition by frequency order by date) rn2
from mytable t
) t
where frequency = 1
group by rn1 - rn2
order by min_date
Demo on DB Fiddle with your sample data:
min_id | max_id | min_date | max_date | streak_length
-----: | -----: | :--------- | :--------- | ------------:
1 | 2 | 2017-08-01 | 2017-08-02 | 2
4 | 6 | 2017-08-04 | 2017-08-06 | 3
Note: window function row_number() is available starting MySQL 8.0.
I'm making kind of top-ten ranking in my app, and I'm stuck in the SQL query that I'll use for that.
I have 2 tables.
The 'posts' table stores the ID of the post autor(user_id), and the post content(and, of course, the entry ID).
+----+---------+--------------+
| ID | user_id | content |
+----+---------+--------------+
| 1 | 3 | Lorem Ipsum1 |
| 2 | 6 | Lorem Ipsum2 |
| 3 | 3 | Lorem Ipsum3 |
+----+---------+--------------+
The 'likes' table, stores ID of the person who liked the post(user_id), the post ID(post_id) and the like date witch is a timestamp(like_date).
+----+---------+---------+------------+
| ID | user_id | post_id | like_date |
+----+---------+---------+------------+
| 1 | 2 | 1 | 1491484851 |
| 2 | 5 | 1 | 1491484871 |
| 3 | 11 | 2 | 1491484891 |
+----+---------+---------+------------+
Every time a user like a post, an entry is created at the 'likes' table, and if the user unlike it, I just remove the entry.
And here's the deal. I want to grab the top 10 most liked users of the last 30 days. I want the query result to be something like this
+---------+-------+
| user_id | likes |
+---------+-------+
| 3 | 2 |
| 6 | 1 |
+---------+-------+
I've already tried a tons of queries and spent a couple of hours trying to solve that, but I just cant figure out how to.
This one should do the trick
select user_id, count(*)
from posts t1
join likes t2
on t1.ID = t2.post_id
where from_unixtime(t2.like_date) >= now() - interval 30 day
group by user_id
order by count(*) desc
limit 10
Assuming you mean "post_id" not "user_id" in your expected results:
SELECT post_id, count(*) as 'likes'
FROM likes
GROUP BY post_id
WHERE like_date >= DATE(NOW() - INTERVAL 30 DAY)
ORDER By 2 desc
LIMIT 10
Everything's in the likes table, so filter recent posts (WHERE), return/order by the count and onl retrieve the first 10 rows. There are other ways to achieve this...
You don't even need a join for this:
SELECT user_id, count(*) as numlikes
FROM likes l
WHERE like_date >= unix_timestamp() - 30*24*60*60
GROUP BY user_id
ORDER BY numlikes desc
LIMIT 10;
The like_date looks like a Unix timestamp, so we should treat it as one. This gets exactly 30 days from the current time to the second. If you want to count days from midnight:
WHERE like_date >= unix_timestamp(CURDATE() - interval 30 day)
EDIT:
If it is the user_id from the post, then you would use join to get that:
SELECT p.user_id, count(*) as numlikes
FROM likes l join
posts p
on l.post_id = p.id
WHERE l.like_date >= unix_timestamp() - 30*24*60*60
GROUP BY p.user_id
ORDER BY numlikes desc
LIMIT 10;
Hey guys, I've tried to get this right but I can't, maybe you can point me in the right direction
I have 3 columns, 'url_id', 'timestamp' and 'o'. I need to group by 'url_id' and sort by the most current timestamp.
table "example"
timestamp | url_id | o
----------------------------
2000 | 1 | 50
2007 | 1 | 70
2011 | 1 | 90
2001 | 2 | 20
2006 | 2 | 50
2009 | 2 | 40
2011 | 2 | 10
'o' is the value at the end I want. I was trying to do this with a subquery but kept getting the oldest value (tried order by, and had no luck).
What am I doing wrong? Is what I'm looking for actually require a subquery?
SELECT url_id
, MAX(timestamp) AS currentTS
FROM yourTable
GROUP BY url_id
ORDER BY currentTS DESC
Aftre you last explanation, I think you need to JOIN the above query to your original table, like this:
SELECT y.timestamp
, y.url_id
, y.o
FROM yourTable y
JOIN
( SELECT url_id
, MAX(timestamp) AS currentTS
FROM yourTable
GROUP BY url_id
) AS grp
ON grp.url_id = y.url_id
AND grp.currentTS = y.timestamp
ORDER BY y.timestamp DESC
Note: if there are two (or more) rows with same url_id and same timestamp, they'll both (or all) appear at the results.