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.
Related
So basically I have a users table which has a column named "completed_surveys" which holds total number of completed surveys.
I need to create a query which would take step size number and would group them by that range.
Example result which would suit my needs:
+---------+-------------------+
| range | completed_surveys |
+---------+-------------------+
| 0-14 | 4566 |
| 14-28 | 3412 |
| 28-42 | 5456 |
| 42-56 | 33 |
| 56-70 | 31 |
| 70-84 | 441 |
| 84-98 | 576 |
| 98-112 | 23 |
| 112-126 | 12 |
| 126-140 | 1 |
+---------+-------------------+
What I have so far:
select concat(what should i add here??) as `range`,
count(users.completed_surveys) as `completed_surveys` from users WHERE users.completed_surveys > 0 group by 1 order by users.completed_surveys;
I think this query is correct however in the concat function I don't really know how to increase the previous number by 14. Any ideas?
One idea is to first create a helper table with values 0..9 .
CREATE TABLE tmp ( i int );
INSERT INTO tmp VALUES (0) , (1) ... (9);
Then join the two tables:
SELECT concat(i,' - ',(i+1)*7) as `range`,
count(users.completed_surveys) as `completed_surveys` from users
INNER JOIN tmp ON (users.completed_surveys>tmp.i AND users.completed_surveys<=(i+1)*7)
WHERE users.completed_surveys > 0
GROUP BY tmp.i
ORDER BY tmp.i
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
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.
The Transform Statement:
TRANSFORM Sum(January2015.txnamount) AS SumOftxnamount
SELECT January2015.txndate2, January2015.cbsaccno
FROM January2015
GROUP BY January2015.txndate2, January2015.cbsaccno
PIVOT January2015.txntypes;
gives output as below:
+------------+---------------+-----+-----+
| txndate2 | cbsaccno | A1 | A2 |
+------------+---------------+-----+-----+
| 01/01/2015 | 0021010392747 | 50 | 300 |
| 01/01/2015 | 0021010392891 | 100 | 200 |
+------------+---------------+-----+-----+
I want a fifth field (calculated field A1-A2)
which have values -250 and -100 for the sample data above.
Anyway to do that?
Thanks in advance.
Save the query you built (for example, PivotQuery). Create a new query that calls PivotQuery with the following SQL:
SELECT PivotQuery.*, [a1]-[a2] AS CalcField
FROM PivotQuery;
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