I have a Table:-
+-----+--------------+--------------+----------+--------------------+---------------+-----------------+
| id | CustomerName | VideoQuality | IsActive | BufferedTime | ElapsedTime | TotalBufferTime |
+-----+--------------+--------------+----------+--------------------+---------------+-----------------+
| 139 | HotStar | 180 | Yes | 10.367167126617211 | 30.000000000 | NULL |
| 140 | HotStar | 1300 | NULL | 5.43524230876729 | 34.000000000 | NULL |
| 141 | HotStar | 1300 | NULL | 5.671054515212042 | 38.000000000 | NULL |
| 142 | HotStar | 1300 | NULL | 5.045639532902047 | 41.000000000 | NULL |
| 143 | HotStar | 1300 | NULL | 5.455747718023355 | 44.000000000 | NULL |
| 144 | HotStar | 1300 | NULL | 5.691559924468107 | 49.000000000 | NULL |
i want to calculate the columns BufferTime and ElapsedTime and insert that output to TotalBufferTime column but i want to skip the first row of the BufferTime.
So the fisrt calculation will be 5.43 + 30.000 second calculation will be 5.67 + 34.00 and so on.
I also have a column IsActive which shows the first row of Buffer time.
I want to do something like this :-
update RequestInfo SET `TotalBufferTime` = BufferedTime + ElapsedTime;
only thing i want to skip only the first row of the column buffered time.
Assuming you a field id that determines row order in your table, you can use a correlated subquery so as to get BufferedTime of previous row like this:
SELECT t1.CustomerName, t1.VideoQuality, t1.IsActive, t1.BufferedTime,
t1.ElapsedTime,
(SELECT t2.BufferedTime
FROM mytable AS t2
WHERE t2.td > t1.id
ORDER BY id LIMIT 1) + t1.ElapsedTime AS TotalBufferTime
FROM mytable AS t1
WHERE IsActive IS NULL
Edit:
To UPDATE you can use the following query:
SET #et = 0;
SET #ElapsedTime = NULL;
UPDATE RequestInfo
SET TotalBufferTime = CASE
WHEN (#et := #ElapsedTime) < 0 THEN NULL
WHEN #ElapsedTime := ElapsedTime THEN BufferedTime + #et
END
ORDER BY id;
The trick here is to use a CASE expression where the first WHEN clause is always evaluated (because it is the first one) but is never true. This way #et variable is initialized with the value of #ElapsedTime, i.e. the value of the previous record.
Demo here
Related
I can't figure out how to check on database side if two ranges, that can handle null values (ex. range A: null - null range B: 3 - 10), overlaps.
In this case, those two ranges overlaps because in my code null - null it's equal to -∞ and +∞ so 3 - 10 is inside -∞ - +∞.
The problem is that i need to build a query that returns all the records from my table stock_rule that have a range that overlaps with the stock_rule record that i'm trying to create.
If the count is major than zero then i can't save the record.
I'm trying to achieve that using COALESCE function (MySQL 8.0) in this way:
COALESCE(rule.min_price, 0)<=COALESCE(:minPrice, rule.min_price,0) AND
COALESCE(rule.max_price, 0)<=COALESCE(:minPrice, rule.max_price, 0) AND
COALESCE(rule.min_price, 0)<=COALESCE(:maxPrice, rule.min_price,0) AND
COALESCE(rule.max_price, 0)<=COALESCE(:maxPrice, rule.max_price, 0) AND
COALESCE(:minPrice, 0)>=COALESCE(rule.min_price, :minPrice, 0) AND
COALESCE(:maxPrice,0)<=COALESCE(rule.min_price, :maxPrice, 0) AND
COALESCE(:minPrice,0)>=COALESCE(rule.max_price, :minPrice, 0) AND
COALESCE(:maxPrice, 0)<=COALESCE(rule.max_price, :maxPrice, 0)
I guess something like this would work...
DROP TABLE ranges;
CREATE TABLE ranges
(id seriAL PRIMARY KEY
,range_start INT NULL
,range_end INT NULL
);
INSERT INTO ranges VALUES
(1,NULL,NULL),
(2,3,10),
(3,12,NULL),
(4,NULL,20),
(5,10,11);
SELECT *
FROM ranges x
JOIN ranges y
ON y.id <> x.id
AND COALESCE(x.range_start,0) <= y.range_end
AND COALESCE(x.range_end,(SELECT MAX(range_end) FROM ranges)) >= y.range_start;
+----+-------------+-----------+----+-------------+-----------+
| id | range_start | range_end | id | range_start | range_end |
+----+-------------+-----------+----+-------------+-----------+
| 1 | NULL | NULL | 2 | 3 | 10 |
| 4 | NULL | 20 | 2 | 3 | 10 |
| 5 | 10 | 11 | 2 | 3 | 10 |
| 1 | NULL | NULL | 5 | 10 | 11 |
| 2 | 3 | 10 | 5 | 10 | 11 |
| 4 | NULL | 20 | 5 | 10 | 11 |
+----+-------------+-----------+----+-------------+-----------+
mysql>
+----+------------+------------+------------+----------+
| id | phone_no | join_date | city | blood_gp |
+----+------------+------------+------------+----------+
| 1 | 80077672xx | 1997-07-19 | Delhi | NULL |
| 2 | 80077642xx | 1998-07-19 | New Delhi | NULL |
| 3 | 80477642xx | 1999-07-19 | Mumbai | NULL |
| 4 | 80077654xx | 1997-05-31 | Kolkata | NULL |
+----+------------+------------+------------+----------+
I want to enter all the blood groups at once . Is there a way to do so ?
you can use single query with select and update
UPDATE table1 , (SELECT * FROM table2 where 1) src
SET table1.blood_gp = src.filed2 where 1 ;
if you want to insert multiple row data using single query then use this code
INSERT INTO yourtable (x,y,z) VALUES (a1,a2,a3), (b1,b2,b3);
or if you want to update one column value all filed then use this code
update yourtable set blood_gp = 'yourvalue' where 1;
if any problem then inform me
Just make an update query without where clause.
update table set blood_gp = 'value'
That's generalize query.
I have the following result set, that I'm trying to drill down
+----+---------+---------------+---------------------+----------------------+---------------+-----------+------------------+------------------+
| id | auth_id | trusts_number | buy_sell_actions_id | corporate_actions_id | fx_actions_id | submitted | created_at | updated_at |
+----+---------+---------------+---------------------+----------------------+---------------+-----------+------------------+------------------+
| 2 | 6 | N100723 | 2 | NULL | NULL | 0 | 08/05/2015 11:30 | 08/05/2015 15:32 |
| 5 | 6 | N100723 | NULL | NULL | 1 | 0 | 08/05/2015 15:10 | 08/05/2015 15:10 |
| 6 | 6 | N100723 | NULL | NULL | 2 | 1 | 08/05/2015 15:12 | 08/05/2015 15:41 |
+----+---------+---------------+---------------------+----------------------+---------------+-----------+------------------+------------------+
This result set is generated with the query
SELECT * FROM actions WHERE auth_id = 6 AND trusts_number = 'N100723'
I also want to get rid of any field with fx_actions is NULL, so I change the query to
SELECT * FROM actions WHERE auth_id = 6 AND trusts_number = 'N100723' AND fx_actions_id != NULL
However this returns an empty result set. I've never used "negative" query parameters in MySQL before, so I'm not sure if they should take on a different syntax or what?
Any help would be much appreciated.
Normal comparison operators don't work well with NULL. Both Something = NULL and Something != NULL will return 'unknown', which causes the row to be omitted in the result. Use the special operators IS NULL and IS NOT NULL instead:
SELECT * FROM actions
WHERE auth_id = 6
AND trusts_number = 'N100723'
AND fx_actions_id IS NOT NULL
Wikipedia on NULL and its background
Because null isn't a value, you should use IS NOT NULL
I am trying to populate an empty table(t) from another table(t2) based on a flag field being set. He is my attempt below and the table data.
UPDATE 2014PriceSheetIssues AS t
JOIN TransSalesAvebyType2013Combined AS t2
SET t.`Tran_Type`=t2.`Tran_Type` WHERE t.`rflag`='1';
When I run the script, I receive (0) zero records affected.??
+-----------+----------------+-------------------+-------+-------+
| Tran_Type | RetailAvePrice | WholesaleAvePrice | Rflag | Wflag |
+-----------+----------------+-------------------+-------+-------+
| 125C | 992 | 650 | 1 | NULL |
| 2004R | 1500 | NULL | 1 | NULL |
| 4EAT | 1480 | 1999 | 1 | 1 |
+-----------+----------------+-------------------+-------+-------+
I think you should just do the following
INSERT INTO 2014PriceSheetIssues
( `fldX`, `fldY` )
VALUES (
SELECT `fldX`, `fldY`
FROM TransSalesAvebyType2013Combined
WHERE 2014PriceSheetIssues.`rflag`='1'
)
The select query gets the values and the insert puts it in the (empty) other table.
Hi all i have a table with the time in each row
how do i get the average time for each row with a select
eg 22:56:39 should be the result
+---------------------+---------------------+
| Day_16 | Day_12 |
+---------------------+---------------------+
| NULL | NULL |
| NULL | NULL |
| NULL | NULL |
| 2011-01-16 23:52:34 | 2011-02-15 22:00:45 |
Ps there is a ID for each row also
SELECT SEC_TO_TIME(AVG(TIME_TO_SEC(day_16),TIME_TO_SEC(day_12))) FROM Table1;
SELECT FROM_UNIXTIME(UNIX_TIMESTAMP(Day_12) + ((UNIX_TIMESTAMP(Day_16) - UNIX_TIMESTAMP(Day_12)) / 2)) FROM tablename
Edit: Ulvund's solution is much cleaner