I have a query that when executed gives me the result like this
speciaid | title | date
one | xxx | 2013-10-03 02:13:54
one | xxx | 2013-10-03 03:13:54
two | yyy | 2013-10-02 02:13:54
Now if I add GROUP BY specialid, it gets grouped but the date i get is the old date and not the new date for the 'date' column like this
speciaid | title | date
one | xxx | 2013-10-03 02:13:54 // old date selected for entry one
two | yyy | 2013-10-02 02:13:54
and I want it like this
speciaid | title | date
one | xxx | 2013-10-03 03:13:54 // new date for entry one needed
two | yyy | 2013-10-02 02:13:54
I have tried many group by's - I get the same result. I would appreciate it very much if someone help with the group by statement for query.
You're looking for a greatest-n-per-group solution.
Usually solved this way (assuming your result is the table, due to the lack of information in the question).
Option 1:
SELECT * FROM t JOIN (
SELECT title, max(`date`) `date` FROM t
GROUP BY title
) s ON t.title = s.title AND t.`date` = s.`date`
Option 2:
SELECT t1.* FROM t t1
LEFT JOIN t t2
ON t1.title = t2.title AND t1.`date` < t2.`date`
WHERE t2.`date` IS NULL
Working fiddle: http://sqlfiddle.com/#!2/76f2d3/1
If you want to get first/last date from grouped column you need to use max() or min() grouped function.
SELECT speciaid, title, MAX(`date`) FROM table GROUP BY title;
select speciaid, title , date
from
(select * from table_name order by date desc) d
group by title
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
Ok, so I have a table something like this
+----+-------+------------+
| id | title | date |
+----+-------+------------+
| 1 | aaa | 2018-08-13 |
| 2 | bbb | 2018-08-02 |
| 3 | ccc | 2018-07-06 |
| 4 | ddd | 2018-07-16 |
| 5 | fff | 2018-07-13 |
+----+-------+------------+
and I want to sort it by date column ascending, but only until a specific date, after that I want to sort it descending, like this
+----+-------+------------+
| id | title | date |
+----+-------+------------+
| 4 | ddd | 2018-07-16 |
| 2 | bbb | 2018-08-02 |
| 1 | aaa | 2018-08-13 |
| 5 | fff | 2018-07-13 |
| 3 | ccc | 2018-07-06 |
+----+-------+------------+
I thought I could use UNION, but either I don't know how to use it, or it doesn't accept ORDER BY clauses specified in each SELECT.
EDIT: I probably didn't explain myself so good. In my query I would need this to sort ascending by date all of the rows after a specific date (>= some date, in this case let's use 2018-07-15) and all of the rows before to be sorted descending.
You should do this with a single order by and no subquery:
order by (case when date >= #date then 1 else 0 end), -- put more recent first
(case when date >= #date then date end) asc, -- order the first group ascending
date desc -- order by second group descending
With reference to the groundwork laid down by D-Shih...
SELECT id
, title
, date
FROM
( SELECT *
, CASE WHEN date <'2018-07-15' THEN #i:=0 ELSE #i:=#i+1 END i
FROM t
, (SELECT #i:=0) vars
ORDER
BY date
) x
ORDER
BY i=0, i, date DESC;
http://sqlfiddle.com/#!9/58a8ac/20
You probably don't really need all the techniques shown above, but I'm just making this stuff up as I go along...
Try this union with subquery:
select * from
(select *,ord=0 from tablename where id=2
union
select *,ord=1 from tablename where id<>2)a
order by ord asc, date desc
#D-Shih has nearly got it right...
SELECT *
FROM yourtable
ORDER BY (CASE
WHEN `date`>"2018-07-06" THEN 'date'
ELSE DATE("2018-07-06")
) ASC,
`date` DESC;
You could do it with a UNION but this gets messy quickly.
You can also implement the CASE statement as LEAST()
Knowing exactly the dates you want to use, you could do it like this:
SELECT * FROM (
SELECT
concat(1,'-',#firstquery := #firstquery + 1) as rownum, table_name.*
FROM
table_name,
(SELECT #firstquery:=0) fq
WHERE
date_column <= '2018-08-05'
ORDER BY
date_column ASC) a
UNION ALL
SELECT * FROM (
SELECT
concat(2,'-',#secondquery := #secondquery + 1) as rownum, table_name.*
FROM
table_name,
(SELECT #secondquery:=0) sq
WHERE
date_column > '2018-08-05'
ORDER BY
date_column DESC) b
ORDER BY rownum ASC
It is quite big, but in this case I'm emulating the rownumber based on each subquery and concatenating that for ordering later. Meaning you could order backwards also at some point.
What I like about this one is that we have the order specified in the rownum based on each item ordered previous to the union.
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.
i have database in which user device id and device registerd date (cr_date) stored i want to fetch list of total count of registerd devices on same day with date wise
Here below display table structure
device_ID | cr_date
--------------------
1 | 2016-06-02 18:02:13
2 | 2016-06-02 18:03:58
3 | 2016-06-02 18:04:11
4 | 2016-06-03 18:04:33
5 | 2016-06-03 18:04:33
6 | 2016-06-04 18:04:44
and i want below result
count | date
---------------------------------
3 | 2016-06-02
2 | 2016-06-03
1 | 2016-06-04
i used below query but dosent get success
SELECT device_type, count(*) AS duplicate_count
FROM (
SELECT device_type FROM tbl_app_deviceregister
GROUP BY device_type HAVING COUNT(device_type) > 0
) AS t
Did you want this;)
SELECT count(1) as `count`, date_format(cr_date, '%Y-%m-%d') as `date`
FROM tbl_app_deviceregister
GROUP BY `date`
ORDER BY `count` DESC
You should GROUP BY date value, not by device:
SELECT CAST(cr_date AS DATE) AS `date`, COUNT(device_id)
FROM tbl_app_deviceregister
GROUP BY CAST(cr_date AS DATE)
Your query doesn't seem to reference the same table structure that you've described, but from what I understand, would this work?
SELECT count(id), date_registered FROM (
SELECT
id,
DATE(FROM_UNIXTIME(cr_date)) as date_registered
FROM
tbl_app_deviceregister
) A GROUP BY date_registered
you group it by device_type which is wrong. try to group by date.
select COUNT(*) as count,CONVERT(date,cr_date) as date
from tbl_app_deviceregister
group by CONVERT(date,cr_date)