I have a table called asset_usages which records the viewing of an asset by a viewer. The relevant fields are
id (int)
asset_id (int)
viewer_type (string)
viewer_id (int)
viewed_at (datetime)
I have a new field i just added called time_between_viewings, which is an int field representing seconds. I want to set this to the time, in seconds, since that asset was last viewed. So, if i had these four records:
+-----+----------+-----------+-------------+---------------------+-----------------------+
| id | asset_id | viewer_id | viewer_type | viewed_at | time_between_viewings |
+-----+----------+-----------+-------------+---------------------+-----------------------+
| 506 | 7342 | 1182 | User | 2009-01-05 11:10:01 | NULL |
| 509 | 7342 | 1182 | User | 2009-01-05 11:12:47 | NULL |
| 514 | 6185 | 1182 | User | 2009-01-05 11:14:28 | NULL |
| 524 | 6185 | 1182 | User | 2009-01-05 11:28:18 | NULL |
| 618 | 1234 | 1182 | User | 2009-01-05 11:29:03 | NULL |
| 729 | 1234 | 1182 | User | 2009-01-05 11:29:01 | NULL |
+-----+----------+-----------+-------------+---------------------+-----------------------+
then time_between_viewings should be set as follows:
+-----+----------+-----------+-------------+---------------------+-----------------------+
| id | asset_id | viewer_id | viewer_type | viewed_at | time_between_viewings |
+-----+----------+-----------+-------------+---------------------+-----------------------+
| 506 | 7342 | 1182 | User | 2009-01-05 11:10:01 | NULL |
| 509 | 7342 | 1182 | User | 2009-01-05 11:12:47 | 166 |
| 514 | 6185 | 1182 | User | 2009-01-05 11:14:28 | NULL |
| 524 | 6185 | 1182 | User | 2009-01-05 11:28:18 | 830 |
| 618 | 1234 | 1182 | User | 2009-01-05 11:29:03 | 2 |
| 729 | 1234 | 1182 | User | 2009-01-05 11:29:01 | NULL |
+-----+----------+-----------+-------------+---------------------+-----------------------+
where 166 and 830 are the time difference between each pair, in seconds.
What would be the sql to populate this field? I can't quite figure it out.
IMPORTANT NOTE: the data is not always inserted into the db in chronological order. Ie, you could have two records A and B, where B has a higher id but A has a later value for viewed_at. So, looking for the first matching record with a lower id would not necesarily give you the previous viewing by the same person - you'll need to examine all the records in the database.
thanks! max
EDIT - stated that time_between_viewings is an int field representing seconds.
EDIT - added a couple of rows as an example of a row with a higher id but earlier timestamp
EDIT - i just realised that i didn't stipulate the question properly. The time_between_viewings should be equal to the time since the asset was last viewed by the same viewer, ie the time between the record and the previous (based on viewed_at) record that has the same asset_id, viewer_id and viewer_type. The example data i gave still holds, but i could have put in some different viewer_id and viewer_type values to flesh the example out a bit.
If would be helpful if you prepared sample table and data inserts.
Read this link to learn why it is so important if you want to get help : http://tkyte.blogspot.com/2005/06/how-to-ask-questions.html
This time I created it for you, click on this link: http://sqlfiddle.com/#!2/9719a/2
And try this query (you will find this query together with sample data under the above link) :
select alias1.*,
timestampdiff( second, previous_viewed_at, viewed_at )
as time_between_viewings
from (
select alias.*,
(
select viewed_at from (
select
( select count(*) from asset_usages y
where x.asset_id = y.asset_id
and y.viewed_at < x.viewed_at
) as rn,
x.*
from asset_usages x
) xyz
where xyz.asset_id = alias.asset_id
and xyz.rn = alias.rn - 1
) previous_viewed_at
from (
select
( select count(*) from asset_usages y
where x.asset_id = y.asset_id
and y.viewed_at < x.viewed_at
) as rn,
x.*
from asset_usages x
) alias
) alias1;
This SELECT statement will give you the right data. You might need to do the update in chunks, though.
You can drop the ORDER BY clause for the UPDATE statement. As written, the derived data doesn't depend on the order of rows in the outer SELECT statement.
select asset_id, viewer_id, viewer_type, viewed_at,
prev_viewed_at, timestampdiff(second, prev_viewed_at, viewed_at) elapsed_sec
from (select asset_id, viewer_id, viewer_type, viewed_at,
(select max(t2.viewed_at)
from Table1 t2
where t2.viewed_at < Table1.viewed_at
and t2.asset_id = Table1.asset_id
and t2.viewer_id = Table1.viewer_id
) prev_viewed_at
from Table1
)t3
order by asset_id, viewer_id, viewed_at;
Related
I have two queries that pull data from two different tables, but I need them to pull in the same report. I have a shared key between them, and the first table has one entry that corresponds to many entries in the second table.
My first query:
SELECT Proposal_ID,
substr(Proposal_Name, 1, 3) AS Prefix,
substr(Proposal_Name, 4, 6) AS `Number`,
Institution,
CollegeCode,
DepartmentCode,
Proposer_FirstName,
Proposer_LastName
FROM proposals.proposal
WHERE Institution = 'T';
Sample Data:
+----+--------+--------+-------+----------+----------+-----------+----------+
| ID | Prefix | Number | Inst. | CollCode | DeptCode | FirstName | LastName |
+----+--------+--------+-------+----------+----------+-----------+----------+
| 18 | SYP | 4675 | T | AS | SOC | Linda | McGaff |
+----+--------+--------+-------+----------+----------+-----------+----------+
| 20 | GEO | 4340 | T | AS | SGS | Teddy | Graham |
+----+--------+--------+-------+----------+----------+-----------+----------+
My second query:
SELECT Parent_Proposal,
SUBSTRING_INDEX(GROUP_CONCAT(`status`.`Status_Code` ORDER BY `status`.`Status_Time` DESC), ',', 1) AS status_code,
SUBSTRING_INDEX(GROUP_CONCAT(`status`.`Status_Time` ORDER BY `status`.`Status_Time` DESC), ',', 1) AS status_timestamp
FROM proposals.`status`
GROUP BY `status`.Parent_Proposal
Sample Data:
+-----------------+-------------+----------------------+
| Parent_Proposal | Status_Code | Status_Time |
+-----------------+-------------+----------------------+
| 18 | 40 | 2016-11-09 06:30:35 |
+-----------------+-------------+----------------------+
| 20 | 11 | 2017-03-20 10:26:31 |
+-----------------+-------------+----------------------+
I basically need to pull the most recent Status_Code and Status_Timestamp based on the Status_Timestamp and then relate that to the first table with the Parent_Proposal column.
Is there a way to group a subset of results without grouping all of the data together?
Expected Result:
+----+--------+--------+-------+----------+----------+-------+--------+-------------+----------------------+
| ID | Prefix | Number | Inst. | CollCode | DeptCode | FName | LName | Status_Code | Status_Time |
+----+--------+--------+-------+----------+----------+-------+--------+-------------+----------------------+
| 18 | SYP | 4675 | T | AS | SOC | Linda | McGaff | 40 | 2016-11-09 06:30:35 |
+----+--------+--------+-------+----------+----------+-------+--------+-------------+----------------------+
| 20 | 11 | GEO | 4340 | AS | SGS | Teddy | Graham | 11 | 2017-03-20 10:26:31 |
+----+--------+--------+-------+----------+----------+-------+--------+-------------+----------------------+
Thanks for any help and insight!
I think you want this. Just join your two tables together, and then do an additional join to a subquery on the status table to find the latest record for each parent proposal.
SELECT
p.Proposal_ID,
SUBSTR(p.Proposal_Name, 1, 3) AS Prefix,
SUBSTR(p.Proposal_Name, 4, 6) AS Number,
p.Institution,
p.CollegeCode,
p.DepartmentCode,
p.Proposer_FirstName,
p.Proposer_LastName,
s1.Status_Code,
s1.Status_Time
FROM proposals.proposal p
LEFT JOIN proposals.status s1
ON p.ID = s1.Parent_Proposal
INNER JOIN
(
SELECT Parent_Proposal, MAX(Status_Time) AS Max_Status_Time
FROM proposals.status
GROUP BY Parent_Proposal
) s2
ON s1.Parent_Proposal = s2.Parent_Proposal AND s1.Status_Time = s2.Max_Status_Time
WHERE
p.Institution = 'T';
I have a problem with a SQL select query, I can't figure out what it needs to be.
This is what my items table look like:
| id | i_id | last_seen | spot |
----------------------------------------------------
| 1 | ls100 | 2017-03-10 15:30:40 | spot800 |
| 2 | ls100 | 2017-03-10 16:20:15 | spot753 |
| 3 | ls200 | 2017-03-10 16:33:10 | spot800 |
| 4 | ls300 | 2017-03-10 15:30:40 | spot800 |
| 5 | ls300 | 2017-03-10 12:10:30 | spot800 |
| 6 | ls400 | 2017-03-10 10:30:10 | spot800 |
This is what I'm trying to obtain:
| id | i_id | last_seen | spot |
----------------------------------------------------
| 3 | ls200 | 2017-03-10 16:33:10 | spot800 |
| 5 | ls300 | 2017-03-10 12:10:30 | spot800 |
So I need to have the rows where spot= 'spot800', last_seen = MAX(but only if the DateTime is the newest compared to all spots with the samei_id`), and at last the DateTime must be bigger than '2017-03-10 11:00:00'.
This is what I have so far:
SELECT *
FROM items
WHERE spot = 'spot800'
HAVING MAX(`last_seen`)
AND `last_seen` > '2017-03-10 11:00:00'
E.g.:
DROP TABLE IF EXISTS my_table;
CREATE TABLE my_table
(id INT NOT NULL AUTO_INCREMENT PRIMARY KEY
,i_id INT NOT NULL
,last_seen DATETIME NOT NULL
,spot INT NOT NULL
);
INSERT INTO my_table VALUES
(1,100,'2017-03-10 15:30:40',800),
(2,100,'2017-03-10 14:20:15',753),
(3,200,'2017-03-10 16:33:10',800),
(4,300,'2017-03-10 15:30:40',800),
(5,300,'2017-03-10 12:10:30',800),
(6,400,'2017-03-10 10:30:10',800);
SELECT [DISTINCT] x.*
FROM my_table x
LEFT
JOIN my_table y
ON y.i_id = x.i_id
AND y.last_seen < x.last_seen
WHERE x.last_seen > '2017-03-10 11:00:00'
AND x.spot = 800
AND y.id IS NULL;
----+------+---------------------+------+
| id | i_id | last_seen | spot |
+----+------+---------------------+------+
| 3 | 200 | 2017-03-10 16:33:10 | 800 |
| 5 | 300 | 2017-03-10 12:10:30 | 800 |
+----+------+---------------------+------+
2 rows in set (0.00 sec)
Use MAX and GROUP BY.
SELECT id, i_id, MAX(last_seen), spot
FROM items
WHERE spot = 'spot800'
AND last_seen > '2017-03-10 11:00:00'
GROUP BY id, i_id, spot
There is several things wrng with your statement.
Firstly, HAVING must be accompanied with a GROUP BY clause, so it's not what you are looking for.
Also, MAX is an aggregate, not a boolean, function. That is, it cannot be used in filters, such as a where clause or a having clause. Also, if it did work, MAX would only return the entry that contains the time as '2017-03-10 16:33:10'. Not what you expected.
Try this instead:
SELECT * FROM items WHERE (spot='spot800' AND last_seen > '2017-03-10 11:00:00');
I am working on a product sample inventory system where I track the movement of the products. The status of each product can have a status of "IN" or "OUT" or "REMOVED". Each row of the table represents a new entry, where ID, status and date are unique. Each product also has a serial number.
I need help with a SQL query that will return all products that are currently "OUT". If I simply just select SELECT * FROM table WHERE status = "IN", it will return all products that ever had status IN.
Every time product comes in and out, I duplicate the last row of that specific product and change the status and update the date and it will get a new ID automatically.
Here is the table that I have:
id | serial_number | product | color | date | status
------------------------------------------------------------
1 | K0T4N | XYZ | silver | 2016-07-01 | IN
2 | X56Z7 | ABC | silver | 2016-07-01 | IN
3 | 96T4F | PQR | silver | 2016-07-01 | IN
4 | K0T4N | XYZ | silver | 2016-07-02 | OUT
5 | 96T4F | PQR | silver | 2016-07-03 | OUT
6 | F0P22 | DEF | silver | 2016-07-04 | OUT
7 | X56Z7 | ABC | silver | 2016-07-05 | OUT
8 | F0P22 | DEF | silver | 2016-07-06 | IN
9 | K0T4N | XYZ | silver | 2016-07-07 | IN
10 | X56Z7 | ABC | silver | 2016-07-08 | IN
11 | X56Z7 | ABC | silver | 2016-07-09 | REMOVED
12 | K0T4N | XYZ | silver | 2016-07-10 | OUT
13 | 96T4F | PQR | silver | 2016-07-11 | IN
14 | F0P22 | DEF | silver | 2016-07-12 | OUT
This query will give you all the latest records for each serial_number
SELECT a.* FROM your_table a
LEFT JOIN your_table b ON a.serial_number = b.serial_number AND a.id < b.id
WHERE b.serial_number IS NULL
Below query will give your expected result
SELECT a.* FROM your_table a
LEFT JOIN your_table b ON a.serial_number = b.serial_number AND a.id < b.id
WHERE b.serial_number IS NULL AND a.status LIKE 'OUT'
There are two good ways to do this. Which way is best,in terms of performance, can depend on various factors, so try both.
SELECT
t1.*
FROM table t
LEFT OUTER JOIN table later_t
ON later_t.serial_number = t.serial_number
AND later_t.date > t.date
WHERE later_t.id IS NULL
AND t.status = "OUT"
Which column you check from later_t for IS NULL does not matter, so long as that column is declared NOT NULL in the table definition.
The other logically equivalent method is:
SELECT
t.*
FROM table t
INNER JOIN (
SELECT
serial_number,
MAX(date) AS date
FROM table
GROUP BY serial_number
) latest_t
ON later_t.serial_number = t.serial_number
AND latest_t.date = t.date
WHERE t.status = "OUT"
For each of these queries, I strongly suggest the following index:
ALTER TABLE table
ADD INDEX `LatestSerialStatus` (serial_number,date)
I use this type of query a lot in my own work, and have the above index as the primary key on tables. Query performance is extremely fast in such cases, for these type of queries.
See also the documentation on this query type.
I have a table called tbl_chat and tbl_post.
The tbl_chat is given as follows.
|--------------------------------------------------------------------------------|
| chat_id | message | from_user | to_user | post_id |send_date |
|--------------------------------------------------------------------------------|
| 1 | Hi | 23 | A | 35 | 2016-04-01 17:35|
| 2 | Test | 24 | A | 35 | 2016-04-02 01:35|
| 3 | Thut | A | 23 | 35 | 2016-04-02 03:35|
| 4 | test | A | 24 | 35 | 2016-04-02 12:35|
| 5 | Hi | 23 | A | 35 | 2016-04-03 17:35|
|--------------------------------------------------------------------------------|
Now, in the chat table we can see three users are interacting with each other. The Admin (A), user with id = 23 and user = 24.
So there is basically two chat thread.
One between A and 23
Another between A and 24.
I want a query which will show the two chat threads, with the last chat message. Just like in case of facebook chat list showing all the chat-threads with mentioning the last chat.
I am writing a query like this.
SELECT * FROM tbl_chat, tbl_post
WHERE tbl_post.post_id = tbl_chat.post_id
AND tbl_post.post_id = '39'
GROUP BY tbl_chat.chat_from
ORDER BY date DESC
The query has a problem. It is first retrieving all the chats and grouping it w.r.t. chat_from and then ORDERING it Descending-wise.
So first it's creating the group, and then ordering the group.
Also, the first query produces three group, taking the reply message from Admin as a separate group. Since GROUP BY chat_from.
How can I solve this issue?
EDIT:-
I would be grateful if someone can build the query in Active-Records of Codeigniter.
DROP TABLE IF EXISTS my_table;
CREATE TABLE my_table
(chat_id INT NOT NULL AUTO_INCREMENT PRIMARY KEY
,message VARCHAR(20) NOT NULL
,from_user VARCHAR(12)
,to_user VARCHAR(12)
,post_id INT NOT NULL
,send_date DATETIME NOT NULL
);
INSERT INTO my_table VALUES
(1,'Hi' ,'23','A' ,35,'2016-04-01 17:35:00'),
(2,'Test','24','A' ,35,'2016-04-02 01:35:00'),
(3,'Thut','A' ,'23',35,'2016-04-02 03:35:00'),
(4,'test','A' ,'24',35,'2016-04-02 12:35:00'),
(5,'Hi' ,'23','A' ,35,'2016-04-03 17:35:00');
SELECT a.*
FROM my_table a
JOIN
( SELECT LEAST(from_user,to_user) user1
, GREATEST(from_user,to_user) user2
, MAX(send_date) send_date
FROM my_table
GROUP
BY user1
, user2
) b
ON b.user1 = LEAST(a.from_user,a.to_user)
AND b.user2 = GREATEST(a.from_user,a.to_user)
AND b.send_date = a.send_date;
+---------+---------+-----------+---------+---------+---------------------+
| chat_id | message | from_user | to_user | post_id | send_date |
+---------+---------+-----------+---------+---------+---------------------+
| 4 | test | A | 24 | 35 | 2016-04-02 12:35:00 |
| 5 | Hi | 23 | A | 35 | 2016-04-03 17:35:00 |
+---------+---------+-----------+---------+---------+---------------------+
You can use NOT EXISTS() :
SELECT * FROM tbl_chat
INNER JOIN tbl_post
ON tbl_post.post_id = tbl_chat.post_id
WHERE NOT EXISTS(SELECT 1 FROM tbl_chat s
WHERE tbl_chat.from_user IN(s.from_user,s.to_user)
AND tbl_chat.to_user IN(s.from_user,s.to_user)
AND tbl_chat.date < s.date)
Although date field looks like DATE type, which is curious - how would you find the difference between two messages at the same day?
I'm new to MySQL and I'm having trouble with the problem below;
Let's call this table runners_table
+---------+-----------+
| race_id | runner_id |
+---------+-----------+
| 10 | A |
| 10 | E |
| 10 | V |
| 23 | G |
| 23 | J |
| 23 | A |
| 67 | E |
| 67 | G |
| 67 | X |
+---------+-----------+
And I want to add a new column like this;
+---------+-----------+--------------+
| race_id | runner_id | prev_race_id |
+---------+-----------+--------------+
| 10 | A | - |
| 10 | E | - |
| 10 | V | - |
| 23 | G | - |
| 23 | J | - |
| 23 | A | 10 |
| 67 | E | 10 |
| 67 | G | 23 |
| 67 | X | - |
+---------+-----------+--------------+
Where prev_race_id looks back and gets the previous race_id for the same runner_id.
To illustrate;
What I'd like to do is, say a runner took part in 6 races out of 10 races that year, in the row relating to his 5th race I want to know the race_id of his 4th race.
I guess I could make a new table for every runner that was a record of the races they took part in, but this would stretch to hundreds of tables... there must be a better way.
Is it possible to do this?
I think the easiest way is using a correlated subquery. Assuming race_id is in numerical order:
select r.*,
(select r2.race_id
from runners_table r2
where r2.race_id < r.race_id and r2.runner_id = r.runner_id
order by r2.race_id desc
limit 1
) as prev_race_id
from runners_table r;
If some other column determines the previous record, then the where and order by would change.
You can create 3 tables, one for race, one for runner and one for the relationship between them. Since you stored the date of every race you can get the previus one easily.
CREATE TABLE Race(
race_id INT,
data VARCHAR(100)
);
CREATE TABLE Runner(
runner_id INT,
data VARCHAR(100)
);
CREATE TABLE Race_Runner(
race_id INT,
runner_id INT,
fecha DATETIME
);
If you want to know the previous race date for a specific runner, try this query:
select race_id
from Race_Runner
where fecha <> (select max(fecha) from Race_Runner)
and runner_id = ? -- the runner you want
order by fecha desc
limit 1
The following query returns the value of each runner with the previous race date:
select runner_id,
LAG(fecha) over (partition by runner_id order by fecha desc)
as previous_date_race
from Race_Runner