I want to create temporary table with some dataset to execute more complicated query to mysql DB.
I see two possible ways to do it.
create table and insert every row:
create temporary table dates (
date date not null
);
insert into dates values ('2010-01-01');
insert into dates values ('2010-02-01');
insert into dates values ('2010-03-01');
or create in-place table:
select *
from (
select '2010-01-01' as date
union select '2010-02-01' as date
union select '2010-03-01' as date
) as dates;
Is there other more concise way to do such table?
Some like to create permanent aux tables like sequence of numbers or dates and then just reference them in their queries. Other DB systems allow recursive CTEs which are more concise.
You could write a stored procedure that can loop and create your temporary table more concisely.
Although a bit silly of a suggestion, your provided SQL does not need the "AS Date" except on the first select of the derived table and you should use UNION ALL instead of UNION.
Related
Merging tables in RDS MYSQL not working. Can someone provide solution on this?
I have a requirement of Merging 10-12 table into a new table. The structure is same and all have almost more than 10M records. One table has 97M records.
All table have id as primary key.
The way we wanted is like:
Creating a new merged empty table with same structure.
Then insert one by one all table with query like
INSERT INTO "pipeline"."main_merged"
SELECT * FROM (
SELECT *
FROM "pipeline"."main_quarterly" q
WHERE q.language NOT LIKE ‘en%’
)
In Above query main_quarterly: can be main-2019Q1 , main-2019Q2 -------- main-2020Q4(9-10 tables).
Insert main table which has 97M record with insert ignore.
INSERT IGNORE INTO "pipeline"."main_merged"
SELECT * FROM (
SELECT *
FROM "pipeline"."main"
)
Then push all quaterly table with different where condition.
INSERT INTO "pipeline"."main_merged"
SELECT * FROM (
SELECT *
FROM "pipeline"."main_quarterly" q
WHERE q.language LIKE ‘en%’
)
In Above query main_quarterly: can be main-2019Q1 , main-2019Q2 -------- main-2020Q4(9-10 tables).
We tried pagination with stored procedure but MySQL workbench getting failed after 300-600 sec.
Tried where clause with id range.
Is there any solution to achive this.
I know using IN clause in Partition key is however I would like to write an UDF that from 2 dates returns an array/list of values that can be used for the IN clause.
Basically what I want to achieve is the following :
CREATE OR REPLACE FUNCTION date_range(start date, end date) called on null input returns set<date> language JAVA as
$$
// function implementation here that returns a list of dates between the 2 provided dates
$$
I would then run a CQL query like
SELECT * from my_table where t_id=3 AND t_date IN (date_range('2010-01-01', '2019-01-10');
Where the table structure would be
CREATE TABLE test ( t_id number, t_date date, t_value number) primary key ((t_number, t_date));
Any way I can achieve this?
You can't do this in Cassandra - UDFs are mostly applied to an individual columns. There is a JIRA for supporting UDFs in WHERE, but it's still open: https://issues.apache.org/jira/browse/CASSANDRA-8488
I would suggest to generate this list in application, and use it with the prepared queries - it could be more effective from performance point of view.
This may seem like a dumb question. I am wanting to set up an SQL db with records containing numbers. I would like to run an enquiry to select a group of records, then take the values in that group, do some basic arithmetic on the numbers and then save the results to a different table but still have them linked with a foreign key to the original record. Is that possible to do in SQL without taking the data to another application and then importing it back? If so, what is the basic function/procedure to complete this action?
I'm coming from an excel/macro/basic python background and want to investigate if it's worth the switch to SQL.
PS. I'm wanting to stay open source.
A tiny example using postgresql (9.6)
-- Create tables
CREATE TABLE initialValues(
id serial PRIMARY KEY,
value int
);
CREATE TABLE addOne(
id serial,
id_init_val int REFERENCES initialValues(id),
value int
);
-- Init values
INSERT INTO initialValues(value)
SELECT a.n
FROM generate_series(1, 100) as a(n);
-- Insert values in the second table by selecting the ones from the
-- First one .
WITH init_val as (SELECT i.id,i.value FROM initialValues i)
INSERT INTO addOne(id_init_val,value)
(SELECT id,value+1 FROM init_val);
In MySQL you can use CREATE TABLE ... SELECT (https://dev.mysql.com/doc/refman/8.0/en/create-table-select.html)
DROP TABLE Backup_LOAD_EMPLOYEE
SELECT * INTO dbo.Backup_LOAD_Employee FROM LOAD_Employee WHERE 1=1
TRUNCATE TABLE LOAD_Employee
I am bulk inserting employee data from external source . In my sp each time after import , I will truncate the load_employee table. Before truncate I would like to take a table backup,previous day data should truncate .
how to give auto increment table name ( in an SP)?
This doesn't answer your question directly (but you can use dynamic SQL), but a better solution is probably to put the backup date into a column, instead of creating one table per day. Then you can more easily query the archived data for multiple days, because it's all in one table. Something like this:
create table dbo.Backup_LOAD_Employee (
BackupDate date,
--- other columns
)
go
insert into dbo.Backup_LOAD_Employee (BackupDate, ...)
select cast(getdate() as date), ... -- other columns
from dbo.LOAD_Employee
truncate table dbo.LOAD_Employee
I want to bulk insert all rows from one table to another. I am confused on how to use Select with Insert. Is there a way that a new table is automatically created if it does not exist?
There are two ways to do this:
One is to INSERT INTO ... SELECT, which will insert the resultset of your query into an existing table with the same data structure as your query
INSERT INTO MyTable_Backup
SELECT * FROM MyTable
The other is to CREATE TABLE ... SELECT ..., which will create a new table based on the data structure of your query an insert the resultset.
CREATE TABLE MyTable_Backup
SELECT * FROM MyTable;
However one thing to note is that this will not match the indexes of the source table. If you need indexes, you need to add them manually.
trigger and/or select into are recommended here