How do I find the oldest date from the most recent set of records in MySQL?
Consider the below
+--------+-----------+------------+
| ID | PRODUCTID | DATEACTIVE |
+--------+-----------+------------+
| 546502 | 23405 | 2017-07-20 |
| 545075 | 23405 | 2017-07-19 |
| 543651 | 23405 | 2017-07-18 |
| 456783 | 23405 | 2017-01-04 |
| 456782 | 23405 | 2017-01-03 |
| 456781 | 23405 | 2017-01-02 |
| 456780 | 23405 | 2017-01-01 |
| 65453 | 23405 | 2016-07-19 |
| 65452 | 23405 | 2016-07-18 |
+--------+-----------+------------+
I want to be able to find the most recent time a product was activated (18th July 2017)
Pls next time follow what Sloan said in the comments.
If I understood your question, you could use something like this.
The query, in the inner part, "groups" the consecutive dates, giving a consecutive number for each "group".
Then select only the "first" group (the more recent) and numbers the rows in ascending order by date.
Finally, the row with the first "row number" is selected.
SELECT *
FROM (
SELECT #rn:=#rn+1 AS RN,
A.*
FROM (
SELECT #gr:= IF(#prev_date=DATE_ADD(dateactive, INTERVAL +1 DAY), #gr, #gr+1) AS GR
,TN.*
,#prev_date:=dateactive AS PD
FROM TN
CROSS JOIN (SELECT #gr:=0, #prev_date:=(SELECT MAX(dateactive) FROM TN)) R
ORDER BY DATEACTIVE DESC
) A
CROSS JOIN (SELECT #rn:=0) R2
WHERE GR=1
ORDER BY DATEACTIVE
)B
WHERE RN=1;
Output:
RN GR id productid dateactive PD
1 1 543651 23405 18.07.2017 00:00:00 2017-07-18
SELECT MAX(DATEACTIVE) as most_recent_time
FROM `YOUR_TABLE_NAME`
WHERE PRODUCTID = 'YOUR_ID'
You could use ORDER BY to sort the data by date, See code below:
SELECT *
FROM `YOUR_TABLE_NAME`
WHERE `PRODUCTID` = 'YOUR_ID'
ORDER BY `DATEACTIVE` DESC;
Related
I have a MySQL database (also working as MariaDB on some servers) with 2 main tables. One is the tickets and the another is ticket_contents as below:
Tickets
+----+---------+-------------+
|id + subject | Create date |
+----+---------+-------------+
|1 | Ticket1 | 2020-05-24 |
+----+---------+-------------+
|2 | Ticket2 | 2020-05-25 |
+----+---------+-------------+
Ticket Contents
+----+-----------+---------------------+-------------+
|id + ticket_id | update date_time | content |
+----+-----------+---------------------+-------------+
|1 | 1 | 2020-05-24 08:30:15 | msg1 |
+----+-----------+---------------------+-------------+
|1 | 2 | 2020-05-25 10:05:15 | msg2 |
+----+-----------+---------------------+-------------+
|1 | 1 | 2020-05-25 12:15:00 | msg3 |
+----+-----------+---------------------+-------------+
What I need is to have the list of tickets with their latest content record time as this and with the latest update order for whole tickets:
+----------+----------+---------------------+
|ticket_id |subject |last_update |
+----------+---_------+---------------------+
|1 | Ticket1 | 2020-05-25 12:15:00 |
+----------+----------+---------------------+
|2 | Ticket2 | 2020-05-25 10:05:15 |
+----------+----------+---------------------+
This is the code I have written, but it does not provide the correct latest child record information.
SELECT t.id, c.date_time FROM tickets t JOIN
(SELECT ticket_id, date_time FROM tickets_contents GROUP BY ticket_id) c on t.id = c.ticket_id
group by t.id order by date_time desc
Can you help me to correct my SQL code please?
You had it almost right, you must use MAX to get the highest date of a GROUP BY
SELECT t.id,t.subject,t2.maxtime
FROM Tickets t
INNER JOIN (SELECT MAX(`update date_time`) maxtime,ticket_id FROM `Ticket Contents` GROUP BY ticket_id) t2
ON t.id = t2.ticket_id
id | subject | maxtime
-: | :------ | :------------------
1 | Ticket1 | 2020-05-25 12:15:00
2 | Ticket2 | 2020-05-25 10:05:15
db<>fiddle here
Provided that your DBMS version is 10.2+, then window functions such as RANK() might be used to determine the tickets with latest update :
SELECT tt.id AS ticket_id, tt.subject, tt.date_time AS last_update
FROM
(
SELECT t.*, tc.date_time,
RANK() OVER (PARTITION BY tc.ticket_id ORDER BY tc.date_time DESC ) AS rnk
FROM tickets t
JOIN tickets_contents tc
ON tc.ticket_id = t.id
) tt
WHERE rnk = 1
ORDER BY ticket_id;
Demo
where PARTITION BY stands for grouping for each column concerned (in this case : ticket_id), and ORDER BY ... DESC is used to filter the last record (in this case : latest).
I am currently struggling on how to aggregate my daily data in other time aggregations (weeks, months, quarters etc).
Here is how my raw data type looks like:
| date | traffic_type | visits |
|----------|--------------|---------|
| 20180101 | 1 | 1221650 |
| 20180101 | 2 | 411424 |
| 20180101 | 4 | 108407 |
| 20180101 | 5 | 298117 |
| 20180101 | 6 | 26806 |
| 20180101 | 7 | 12033 |
| 20180101 | 8 | 80368 |
| 20180101 | 9 | 69544 |
| 20180101 | 10 | 39919 |
| 20180101 | 11 | 26291 |
| 20180102 | 1 | 1218490 |
| 20180102 | 2 | 410965 |
| 20180102 | 4 | 108037 |
| 20180102 | 5 | 297727 |
| 20180102 | 6 | 26719 |
| 20180102 | 7 | 12019 |
| 20180102 | 8 | 80074 |
First, I would like to check the sum of visits regardless of traffic_type:
SELECT date, SUM(visits) as visits_per_day
FROM visits_tbl
GROUP BY date
Here is the outcome:
| ymd | visits_per_day |
|:--------:|:--------------:|
| 20180101 | 2294563 |
| 20180102 | 2289145 |
| 20180103 | 2300367 |
| 20180104 | 2310256 |
| 20180105 | 2368098 |
| 20180106 | 2372257 |
| 20180107 | 2373863 |
| 20180108 | 2364236 |
However, if I want to check the specific day which the visits_per_day was the highest for each time aggregation (eg.: Month), I am struggling to retrieve the right output.
Here is what I did:
SELECT
(date div 100) as y_month, MAX(visits_per_day) as max_visit_per_day
FROM
(SELECT date, SUM(visits) as visits_per_day
FROM visits_tbl
GROUP BY date) as t1
GROUP BY
y_month
And here is the output of my query:
| y_month | max_visit_per_day |
|:-------:|:-----------------:|
| 201801 | 2435845 |
| 201802 | 2519000 |
| 201803 | 2528097 |
| 201804 | 2550645 |
However, I cannot know what was the exact day where the visits_per_day was the highest.
Desired output:
| y_month | max_visit_per_day | ymd |
|:-------:|:-----------------:|:--------:|
| 201801 | 2435845 | 20180130 |
| 201802 | 2519000 | 20180220 |
| 201803 | 2528097 | 20180325 |
| 201804 | 2550645 | 20180406 |
ymd would represent the day in which the visits_per_day was the highest.
This logic would be used in a dashboard with the help of programming in order to automatically select the time aggregation.
Can someone please help me?
This is a job for the structured part of structured query language. That is, you will write some subqueries and treat them as tables.
You already know how to find the number of visits per day. Let's add the month for each day to that query (http://sqlfiddle.com/#!9/a8455e/13/0).
SELECT date DIV 100 as month, date,
SUM(visits) as visits
FROM visits_tbl
GROUP BY date
Next you need to find the largest number of daily visits in each month. (http://sqlfiddle.com/#!9/a8455e/12/0)
SELECT month, MAX(visits) max_daily_visits
FROM (
SELECT date DIV 100 as month, date,
SUM(visits) as visits
FROM visits_tbl
GROUP BY date
) dayvisits
GROUP BY month
Then, the trick is retrieving the date on which that maximum occurred in each month. That requires a join. Without common table expressions (which MySQL lacks) you need to repeat the first subquery. (http://sqlfiddle.com/#!9/a8455e/11/0)
SELECT detail.*
FROM (
SELECT month, MAX(visits) max_daily_visits
FROM (
SELECT date DIV 100 as month, date,
SUM(visits) as visits
FROM visits_tbl
GROUP BY date
) dayvisits
GROUP BY month
) maxvisits
JOIN (
SELECT date DIV 100 as month, date,
SUM(visits) as visits
FROM visits_tbl
GROUP BY date
) detail ON detail.visits = maxvisits.max_daily_visits
AND detail.month = maxvisits.month
The outline of this rather complex query helps explain it. Instead of that subquery, we'll use an imaginary table called dayvisits.
SELECT detail.*
FROM (
SELECT month, MAX(visits) max_daily_visits
FROM dayvisits
GROUP BY date DIV 100
) maxvisits
JOIN dayvisits detail ON detail.visits = maxvisits.max_daily_visits
AND detail.month = maxvisits.month
You're seeking an extreme value for each month in the subquery. (This is a fairly standard sort of SQL operation.) To do that you find that value with a MAX() ... GROUP BY query. Then you join that to the subquery itself to find the other values corresponding to the extreme value.
If you did have common table expressions, the query would look like this. YOu might consider adopting the MySQL fork called MariaDB, which has CTEs.
WITH dayvisits AS (
SELECT date DIV 100 as month, date,
SUM(visits) as visits
FROM visits_tbl
GROUP BY date
)
SELECT dayvisits.*
FROM (
SELECT month, MAX(visits) max_daily_visits
FROM dayvisits
GROUP BY month
) maxvisits
JOIN dayvisits ON dayvisits.visits = maxvisits.max_daily_visits
AND dayvisits.month = maxvisits.month
[Query Check on MSSQL] its quick and efficient.
select visit_sum_day_wise.date
, visit_sum_day_wise.Max_Visits
, visit_sum_day_wise.traffic_type
, LAST_VALUE(visit_sum_day_wise.visits) OVER(PARTITION BY
visit_sum_day_wise.date ORDER BY visit_sum_day_wise.date ROWS BETWEEN
UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING ) AS max_visit_per_day
from (
select visits_tbl.date , visits_tbl.visits , visits_tbl.traffic_type
,max(visits_tbl.visits ) OVER ( PARTITION BY visits_tbl.date ORDER
BY visits_tbl.date ROWS BETWEEN UNBOUNDED PRECEDING AND 0
PRECEDING) Max_visits
from visits_tbl
) as visit_sum_day_wise
where visit_sum_day_wise.visits = (select max(visits_B.visits ) from
visits_tbl visits_B where visits_B.Date = visit_sum_day_wise.date )
enter image description here
For every ID_Number, there is a bill_date and then two types of bills that happen. I want to return the latest date (max date) for each ID number and then add together the two types of bill amounts. So, based on the table below, it should return:
| 1 | 201604 | 10.00 | |
| 2 | 201701 | 28.00 | |
tbl_charges
+-----------+-----------+-----------+--------+
| ID_Number | Bill_Date | Bill_Type | Amount |
+-----------+-----------+-----------+--------+
| 1 | 201601 | A | 5.00 |
| 1 | 201601 | B | 7.00 |
| 1 | 201604 | A | 4.00 |
| 1 | 201604 | B | 6.00 |
| 2 | 201701 | A | 15.00 |
| 2 | 201701 | B | 13.00 |
+-----------+-----------+-----------+--------+
Then, if possible, I want to be able to do this in a join in another query, using ID_Number as the column for the join. Would that change the query here?
Note: I am initially only wanting to run the query for about 200 distinct ID_Numbers out of about 10 million. I will be adding an 'IN' clause for those IDs. When I do the join for the final product, I will need to know how to get those latest dates out of all the other join possibilities. (ie, how do I get ID_Number 1 to join with 201604 and not 201601?)
I would use NOT EXISTS and GROUP BY
select, t1.id_number, max(t1.bill_date), sum(t1.amount)
from tbl_charges t1
where not exists (
select 1
from tbl_charges t2
where t1.id_number = t2.id_number and
t1.bill_date < t2.bill_date
)
group by t1.id_number
the NOT EXISTS filter out the irrelevant rows and GROUP BY do the sum.
I would be inclined to filter in the where:
select id_number, sum(c.amount)
from tbl_charges c
where c.date = (select max(c2.date)
from tbl_charges c2
where c2.id_number = c.id_number and c2.bill_type = c.bill_type
)
group by id_number;
Or, another fun way is to use in with tuples:
select id_number, sum(c.amount)
from tbl_charges c
where (c.id_number, c.bill_type, c.date) in
(select c2.id_number, c2.bill_type, max(c2.date)
from tbl_charges c2
group by c2.id_number, c2.bill_type
)
group by id_number;
I have a table like this:
+----+---------+---------------------+
| id | user_id | start_date |
+----+---------+---------------------+
| 1 | 1 | 2014-02-01 00:00:00 |
| 2 | 1 | 2014-01-01 00:00:00 |
| 3 | 2 | 2014-01-01 00:00:00 |
| 4 | 2 | 2014-01-01 00:00:00 |
| 5 | 3 | 2015-01-01 00:00:00 |
+----+---------+---------------------+
how can I select all rows that, for each user, have:
start date before NOW() and
maximum start_date
so for sample rows, the output should be:
+----+---------+---------------------+
| id | user_id | start_date |
+----+---------+---------------------+
| 1 | 1 | 2014-02-01 00:00:00 | // this is a single maximum date within that user
| 3 | 2 | 2014-01-01 00:00:00 | // these two share maximum start date
| 4 | 2 | 2014-01-01 00:00:00 |
+----+---------+---------------------+
what I have so far is something like this:
SELECT t.* FROM ticket t
JOIN (
SELECT start_date, MAX(start_date) FROM ticket /* GROUP BY user_id */
) highest
ON t.start_date = highest.start_date
WHERE t.start_date <= NOW();
but this doesn't work as desired. Am I on good path?
You're on the right track, sort of.
In your derived table, you need to get the max date for each user id, so:
SELECT user_id,
MAX(start_date) as MaxDate
FROM ticket
GROUP BY user_id
Then you can join to that on start date and user id:
SELECT t.* FROM ticket t
JOIN (
SELECT user_id,
MAX(start_date) as MaxDate
FROM ticket
GROUP BY user_id
) highest
ON t.start_date = highest.maxdate
and t.user_id = highest.user_id
WHERE t.start_date <= NOW();
SQL Fiddle
_try this:
SELECT T.* FROM ticket AS T
JOIN (SELECT
[User_Id]
,MAX([Start_Date]) AS Start_Date
FROM ticket
WHERE Start_Date <= GETDATE()
GROUP BY User_Id) AS Grouped ON T.User_Id = Grouped.User_Id AND T.Start_Date = Grouped.Start_Date
ORDER BY Id
DROP TABLE #This
I want to join 2 tables:
source_table
----------------------------------
| source_id label |
|----------------------------------|
| 1 Contact Form |
| 2 E-Mail |
| 3 Inbound Call |
| 4 Referral |
----------------------------------
related_table
---------------------------------------
| id created_at source |
|---------------------------------------|
| 1 2013-12-26 2 |
| 2 2013-12-26 2 |
| 3 2013-12-26 4 |
| 4 2013-12-25 1 |
| 5 2013-12-18 2 |
| 6 2013-12-16 4 |
| 7 2013-11-30 2 |
---------------------------------------
So that it looks like this:
---------------------------------------
| created_at source amount |
|---------------------------------------|
| 2013-12-26 E-Mail 2 |
| 2013-12-26 Referral 1 |
| 2013-12-25 Contact Form 1 |
| 2013-12-18 E-Mail 1 |
| 2013-12-16 Referral 1 |
---------------------------------------
I want to count the occurrences of each source in related_table grouped by the source for each date in the range.
But I'm not sure how to write the query.
Here's what I have so far:
SELECT DISTINCT
source_table.source_id,
source_table.label AS source,
related_table.created_at,
COUNT(*) AS amount
FROM source_table
INNER JOIN related_table
ON related_table.source=source_table.source_id AND
related_table.created_at>='2013-12-01' AND
related_table.created_at<='2013-12-31'
GROUP BY `source`
ORDER BY `created_at` ASC
I'm not very good with SQL, so the above query might be far off from what I need to have. All I know is that it doesn't work as expected.
My implementation:
select created_at, s.label, amount
from
(
select count(r.Source) as amount, r.source, r.created_at
from related_table r
group by r.source, r.created_at) a inner join source_table s
on a.source = s.source_id
where created_at between '2013-12-01' and '2013-12-31'
order by amount desc, created_at desc
http://sqlfiddle.com/#!2/841bd/2
adjusted demo to your example...
SELECT
created_at
,label as source
,COUNT(*) AS amount
FROM source_table
INNER JOIN related_table
ON source_table.source_id = related_table.source
GROUP BY label, created_at
ORDER BY created_at DESC