I want to run a T-SQL script where I create a temp table that will be aggregated by a certain field of another table, call it table X. The remaining fields of this temp table will be populated by performing aggregate functions on the fields of table X. Then I would like to do a MERGE / WHEN MATCHED with my temp table on a different table (call it table Y) after I have populated the temp table.
How do I create this temp table and populate it with aggregate functions? (I have already coded the MERGE part of the problem).
to create a temp table you will can do the following:
create table #temp
(
id int,
col1 int
)
then you will write an INSERT INTO
INSERT INTO #temp
SELECT col1, sum(col2)
FROM yourTable
Once you have created your temp table you can use it in your store procedure.
What bluefeet posted, or:
with rsAggregated as
(
select id, sum(x)
from tableX
group by id
)
merge...
Or, not seeing your merge statement, just
merge tableY using
(
select id, sum(x)
from tableX
group by id
) rsAggregated
on rsAggregated.id = tableY.id
when matched
...
when not matched
...
You can do the whole thing with one SELECT statement
SELECT col1 AS ID, sum(col2) AS col1 INTO #temp FROM yourTable
Related
I'm going to do an regression analysis through my data chunks. For that I need to find out various values. For each data set I need to get N:count(X) sumX sumY sumX*X etc.
Separately I wrote queries for those operations like
SELECT COUNT(X) FROM table_name
SELECT SUM(X*X) FROM table_name
I need to create another table which a row contain count(X), sumX , sumX*X etc. How can I write that kind of query?
You can add multiple aggregates to the same query and use create table as:
create table yournewtable as
select count(x) cnt, sum(x*x) sumxx, sum(x) sumx
from table_name
SQL Fiddle Demo
This will return you a single row. If you need to break it apart, look into group by.
CREATE TABLE first and then use INSERT INTO
CREATE TABLE yourTableName
(
col1 int,
col2 int,
col3 int
);
INSERT INTO yourTableName (col1, col2, col3)
SELECT
(SELECT COUNT(X) FROM table_name),
(SELECT SUM(X) from table_name),
(SELECT SUM(X*X) from table_name)
Hi Here i came across a situation in which by mistakenly Without dropping the table i have run the batch file of the table which consists of some insert statements in detail
I have a table like alert_priority consists of records like
Id priority_name
--- --------------
1 P0
2 P1
3 P2
and now by mistakenly without dropping alert_priority i have executed script file of the table which consists of some insert statements and now after executing the script my records in the table are like
Id priority_name
--- --------------
1 P0
2 P1
3 P2
1 P0
2 P1
3 P2
Now i want to delete the records which are extra(records after Id 3) and i should have all the records which are present before i have executed the script file.
Although i have an option to drop the table and execute the script file once again, I wanted to know is there any way which we can do through sql query
I have no primay keys in the table
First , consider setting your ID fields as AI (auto increasment) and even PK (Primary Key).
In order to remove those duplicated rows , we will create a new table and will move all
those duplicated rows to it.
After that , drop that table.
CREATE TEMPORARY TABLE bad_temp AS SELECT DISTINCT * FROM alert_priority
you can copy all unique records into a new table, then delete the old table:
SELECT DISTINCT * INTO new_table FROM old_table
In SQL-Server it would be easy using ROW_NUMBER, but alas MySQL doesn't have a function like that :-(
Best way to solve it would be as follows:
Create a new table identical in structure to the first, but with no
data.
Use the query: INSERT INTO name_of_new_table SELECT DISTINCT * FROM name_of_old_table
Drop the old table
Rename the new table to whatever the old table was called.
CREATE TABLE new_tbl(id int AUTO_INCREMENT,priority_name);
INSERT INTO new_tbl
select priority_name from old_tbl group by priority_name;
To just delete the duplicate new rows and leave the old ones in place (on the basis that I assume there are already other tables whose rows refer to the original rows):-
DELETE FROM alert_priority
WHERE Id IN (SELECT MaxId
FROM (SELECT priority_name, MAX(Id) AS MaxId, COUNT(Id) AS CountId
FROM alert_priority
GROUP BY priority_name
HAVING CountId > 1))
Following query will give you all records that you want to keep:
SELECT min(id)
FROM alert_priority
GROUP BY priority_name
HAVING count(*) > 1
OR min(id) = max(id)
To remove all duplicates, run this query:
DELETE FROM alert_priority
WHERE id NOT IN (
SELECT min(id)
FROM alert_priority
GROUP BY priority_name
HAVING count(*) > 1
OR min(id) = max(id)
)
Hi Here i came across a situation in which by mistakenly Without dropping the table i have run the batch file of the table which consists of some insert statements in detail
I have a table like alert_priority consists of records like
Id priority_name
--- --------------
1 P0
2 P1
3 P2
and now by mistakenly without dropping alert_priority i have executed script file of the table which consists of some insert statements and now after executing the script my records in the table are like
Id priority_name
--- --------------
1 P0
2 P1
3 P2
1 P0
2 P1
3 P2
Now i want to delete the records which are extra(records after Id 3) and i should have all the records which are present before i have executed the script file.
Although i have an option to drop the table and execute the script file once again, I wanted to know is there any way which we can do through sql query
I have no primay keys in the table
First , consider setting your ID fields as AI (auto increasment) and even PK (Primary Key).
In order to remove those duplicated rows , we will create a new table and will move all
those duplicated rows to it.
After that , drop that table.
CREATE TEMPORARY TABLE bad_temp AS SELECT DISTINCT * FROM alert_priority
you can copy all unique records into a new table, then delete the old table:
SELECT DISTINCT * INTO new_table FROM old_table
In SQL-Server it would be easy using ROW_NUMBER, but alas MySQL doesn't have a function like that :-(
Best way to solve it would be as follows:
Create a new table identical in structure to the first, but with no
data.
Use the query: INSERT INTO name_of_new_table SELECT DISTINCT * FROM name_of_old_table
Drop the old table
Rename the new table to whatever the old table was called.
CREATE TABLE new_tbl(id int AUTO_INCREMENT,priority_name);
INSERT INTO new_tbl
select priority_name from old_tbl group by priority_name;
To just delete the duplicate new rows and leave the old ones in place (on the basis that I assume there are already other tables whose rows refer to the original rows):-
DELETE FROM alert_priority
WHERE Id IN (SELECT MaxId
FROM (SELECT priority_name, MAX(Id) AS MaxId, COUNT(Id) AS CountId
FROM alert_priority
GROUP BY priority_name
HAVING CountId > 1))
Following query will give you all records that you want to keep:
SELECT min(id)
FROM alert_priority
GROUP BY priority_name
HAVING count(*) > 1
OR min(id) = max(id)
To remove all duplicates, run this query:
DELETE FROM alert_priority
WHERE id NOT IN (
SELECT min(id)
FROM alert_priority
GROUP BY priority_name
HAVING count(*) > 1
OR min(id) = max(id)
)
I have a table that contains some duplicate redords. I want to make records unique. I created a new table (say, destination) and I specified a unique column in it. How can copy records from table1 (source) such that, if the record inserted in the destination table, it does not insert it again.
You can use the "select into" construct and select insert only distinct rows, like this:
insert into table_without_dupes (column0, column1) select distinct column0, column1 from table_with_dupes
If you have autoincrement or other columns that makes the rows distinct, you can just leave them out of the insert and select parts of the statement.
Edit:
If you want to detect duplicates by a single column, you can use group by:
insert into table_without_dupes (column0, column1) select column0, column1 from table_with_dupes group by column0
MySQL will allow you to refer non-aggregated columns in select, but remember that the documentation says "The server is free to choose any value from each group", if you want to select one specific row of the groups, you might find this example useful.
Generic approach
insert into destination(col1,col2)
select DISTINCT col1,col2 from source as s where not exists
(select * from destination as d where d.col1=s.col1)
Edited
insert into destination(col1,col2)
SELECT distinct col1,col2 from source
Edited (Assuming col3 is duplicated and you want only one copy of it.)
insert into destination(col1,col2,col3,....,colN)
SELECT col1,col2,col3,...,colN from source as s1 inner join
(
select col1,col2,max(col3) as col3
from source
group by col1,col2
) as s2 on t1.col1=t2.col1 and t1.col2=t2.col2 and t1.col3=t2.col3
insert into <dest_table_name>
select distinct * from <source_table_name>;
I have a MySql table which has about 100k rows. there is one field say id which contains numbers from 1-35. all these records fall in this range of id i.e. all these records have value of id column between 1-35.
Now i want to create another table which will have one row of each id. i.e the new table should have 35 rows only.
How to go about it ?
create table new_table (id int);
insert into new_table
select distinct id from big_table;
Edit:
You can create the new_table by outputting the big_table create script and changing the name.
SHOW CREATE TABLE big_table;
/* modify the name of the output and execute */
insert into new_table
select * from big_table group by id
You have a table with 100.000 rows, and you want a new table with 35 rows. What values do you want for the remaining columns?
If the answer is: doesn't matter, this works:
CREATE TABLE newTable
SELECT * FROM yourTable
GROUP BY ID;
If you only want the IDs,
CREATE TABLE newTable
SELECT DISTINCT ID FROM yourTable;
You can copy data from one table to another even difference database(Schema) as following
INSERT INTO [DestDatabase].[DestTablName]
SELECT [ColumnName] FROM [SourceDatabase].[SourceTablName];
So, you can use two way:
1:
INSERT INTO tbl_New
SELECT DISTINCT id from tbl_Original;
2:
INSERT INTO tbl_New
SELECT id from tbl_Original GROUP BY id;