Update SQL table with data from another table - sql-server-2008

I have 2 existing tables, first one has a timestamp and data column, the second one has only a data column. Now I want to copy the data column of the 2nd table into the first but starting from a certain date overwriting the data that is already there. I have tried the following but then the values in the data column aren't updated correct.
UPDATE DB_Gas
SET DB_Gas.L_F_GAS = TEST_Table.data1
FROM DB_Gas
JOIN TEST_Table
ON (DB_Gas.Timestamp > '2017-03-01')
Executing this changes the correct rows, but they all have the first value of the second table.
DB_GAS table:
Timestamp | L_F_Gas
2017-02-28 | Null
2017-03-01 | Null
2017-03-02 | 123
2017-03-03 | 456
2017-03-04 | 753
Test_Table:
data1
963
369
951
Result must be like this:
DB_Gas :
Timestamp | L_F_Gas
2017-02-28 | Null
2017-03-01 | 963
2017-03-02 | 369
2017-03-03 | 951
2017-03-04 | 753

Related

Multiple DateTime range MySql

I have a table
---------+-----------+------------------+--------------------+
| id | user_id | start_date_time | end_date_time |
+---------+---------+-------------------+--------------------+
| 1 | 11 |2019-11-17 20:10:00|2019-11-17 21:05:00 |
| 2 | 11 |2019-11-17 20:18:00|2019-11-17 20:35:00 |
| 3 | 11 |2019-11-17 20:32:00|2019-11-17 21:18:00 |
| 4 | 11 |2019-11-17 20:40:00|2019-11-17 20:50:00 |
| 5 | 11 |2019-11-17 20:45:00|2019-11-17 21:20:00 |
| | | | |
+---------+---------+-------------------+--------------------+
Scenario 1 - If i query for all greater than '2019-11-17 20:18:00' I need to get all records.
Scenario 2 - If i query for all possible dates greater or equals '2019-11-17 21:18:00' It should return record 3 and 5.
For any given time it should look for Start_date_time and End_date_time where given time should be considered as start time and it should look for appropriate end_date_time and output the result.
In a nut shell input time should be taken as starting range and it should look for End_date_time and give me all values between.
How can i accomplish this?
i tried the following ways on db-fiddle https://www.db-fiddle.com/f/bPk1CYioL6cVasStZKzQ4j/7
if i query all records from a given time eg(2019-11-17 20:18:00) the input should be taken as a start datetime of range and look for the most greatest end_date_time and give me the records between them. Example 2019-11-17 20:18:00 this input takes range between input as val1 of range 2019-11-17 20:18:00 to 2019-11-17 21:20:00 the highest end date and give me all records between. And if i query with input 2019-11-17 21:05:00 this should take start range val1 as 2019-11-17 21:05:00 and 2019-11-17 21:20:00 output 1,3,5 records.
The requirement (with the results that you expect) is as simple as that:
select *
from times
where ? <= end_date_time
Replace ? with the datetime that you want to query.
See the demo.

TRUNCATE-INSERT vs SELECT-UPDATE-INSERT

I have a table that I am using as a temporary table. A cron runs every hour to set a certain value for each row.
| id | item_id | value |
+====+=========+=======+
| 1 | 5 | 52 |
| 2 | 34 | 314 |
| 3 | 27 | 189 |
| 4 | 19 | 200 |
+====+=========+=======+
What I would like to know is if it is better to first TRUNCATE and then refill this table or that I could rather SELECT the existing row, UPDATE it or INSERT it if it doesn't exist.
Insert the record if it doesn't exist in your temporary table and if it has already in your temporary table but you need to update it's value then update the specific record by only target it.
It would be more wise, because it will be reduce the operation execution time.

Populate MySQL table with dummy data using times from another table

I have one table filled with timestamps, and upon creating another table I want to fill it with zero values for each of those timestamps. For instance:
Timestamps table
Timestamps |
-----------|
2014-07-01 |
2014-07-02 |
2014-07-03 |
2014-07-04 |
2014-07-05 |
2014-07-06 |
And I want to create a second table like this:
Table 2
Timestamps | Values
-----------|--------
2014-07-01 | 0
2014-07-02 | 0
2014-07-03 | 0
2014-07-04 | 0
2014-07-05 | 0
2014-07-06 | 0
Is there an easy way to do this?
create table new_table as
select t.*, 0 as values
from your_table t;
Simple as that.

MYSQL between doesn't include max date

To start with, I'm sorry for the format. It's my first time and I really don't know how to show tables here.
This is my syntax:
SELECT order.id, order.begin, order.end, report.id,
DATE_FORMAT( report.add_date, '%Y-%m-%d' ) AS report_add_date, sums.id, sums.qty
FROM order, report, sums
WHERE (report.add_date BETWEEN order.begin AND order.end)
AND (report.id = sums.id)
ORDER BY order.id ASC
It gives the following result:
order.id | order.begin | order.end | report.id | report.add_date | sums.id | sums.qty
255 | 2013-10-21 | 2013-10-22 | 390 | 2013-10-21 | 390 | 250
256 | 2013-10-22 | 2013-10-23 | 393 | 2013-10-22 | 393 | 385
The final result should look like this:
order.id | order.begin | order.end | report.id | report.add_date | sums.id | sums.qty
255 | 2013-10-21 | 2013-10-22 | 390 | 2013-10-21 | 390 | 250
255 | 2013-10-21 | 2013-10-22 | 393 | 2013-10-22 | 393 | 385
256 | 2013-10-22 | 2013-10-23 | 393 | 2013-10-22 | 393 | 385
Hopefully, you can see, that I am trying to get all sums.qty for all order.id where report.add_date is between order.begin and order.end date.
There are 3 tables: order, report and sums.
Order contains the range dates (begin-end). Report contains date (add_date). Sums contains qty and it's related to Report by id.
Order.id 255 should get all sums.qty for dates between 2013-10-21 and 2013-10-22.
Order.id 256 should get all sums.qty for dates between 2013-10-22 and 2013-10-23.
The first one doesn't get sums.qty for the date of 2013-10-22, because this one goes to the latter.
As far as I understand, it doesn't repeat rows, so it shows every row only ones. So it stops assigning report.id to the previous order.id when the following order.id begins with the date the previous one ends.
What am I doing wrong? Thank you in advance!
I'm guessing add_date is a DATETIME and you're not converting it to DATE in your BETWEEN criteria. A date has a 0 time, so though BETWEEN is inclusive it won't include the same date if there is a non-zero time portion attached to it.
I'd also suggest switching to explicit joins as implicit joins have been deprecated for ages.

Query to select newly added records only

As I am new to mysql, let me clear this doubt. how to write a query to find/select the latest added records only?
Example:
Consider a Table, which is daily added certain amount of records. Now the table contain 1000 records. And the total 1000 records are taken out for some performance. After sometimes table is added 100 records. Now I would like take the remain 100 only from the 1100 to do some operation. How to do it?
(For example only, I have given the numbers, But originally I don't know the last updated count and the newly added)
Here My table contain three columns Sno, time, data. where Sno is indexed as primary key.
Sample table:
| sno | time | data |
| 1 | 2012-02-27 12:44:07 | 100 |
| 2 | 2012-02-27 12:44:07 | 120 |
| 3 | 2012-02-27 12:44:07 | 140 |
| 4 | 2012-02-27 12:44:07 | 160 |
| 5 | 2012-02-27 12:44:07 | 180 |
| 6 | 2012-02-27 12:44:07 | 160 |
| 7 | 2012-02-28 13:00:35 | 100 |
| 8 | 2012-03-02 15:23:25 | 160 |
Add TIMESTAMP field with 'ON UPDATE CURRENT_TIMESTAMP' option, and you will be able to find last added or last edited records.
Automatic Initialization and Updating for TIMESTAMP.
Create table as below
Create table sample
(id int auto_increment primary key,
time timestamp DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
data nvarchar(100)
);
then query as
select * from sample order by time desc limit 1