Copy part of some SQL Server rows into same database - sql

I have a SQL Server database where I want to copy parts of the rows, and the other part of the row needs to be filled with new data, and into the same table. For example:
Table name: budget
ItemID Item Date
1 A 4/22/2012
2 B 4/15/2012
3 C 4/24/2012
When I copy the table, I want to copy the item column, but not the date column into the same table. The date column will get today's date, but Item will copy rows. Like below.
table name: budget
ItemID Item Date
1 A 4/22/2012
2 B 4/15/2012
3 C 4/24/2012
4 A 5/6/2012
5 B 5/6/2012
6 C 5/6/2012
I have tried:
SELECT Item
INTO Budget
FROM Budget
But I get error
There is already an object named 'Budget' in the database.
Also, I'm not sure how after I copy the rows to fill the rest of the column with new date data.
Thank you.

You need to try insert instead. Generate a query that has the new rows, and try something like:
Insert into <table>
Select *
From <q>

You are trying to create a new table called "Budget" which already exists by "SELECT INTO" statement.
Is your ItemID auto increment? Try this.
SELECT * into Budget FROM Budget_Temp
INSERT INTO Budget(Item,Date)
SELECT Item,'5/6/2012' FROM Budget_Temp
DROP TABLE Budget_Temp

Related

SQL make each column a new table

I have a table with an [ID] column, a [t] column indicate period, and 22 other accounting ledger related columns:
ID
t
Revenue
Expense
...20 other columns ...
1
1
23,040
21,204
xxx, xxx
1
2
21,302
19,302
xxx, xxx
1
3
51,302
29,302
xxx, xxx
1
4
61,302
49,302
xxx, xxx
2
1
23,040
21,204
xxx, xxx
2
2
21,302
19,302
xxx, xxx
2
3
51,302
29,302
xxx, xxx
2
4
61,302
49,302
xxx, xxx
I'd like to create new tables based on the columns here, like a table for Revenue, and a table for Expense, and pivot [t] as the columns in the new table.
Right now I'm writing 22 queries of repeated code, SELECT each column out, pivot, and CREATE my new table. And I have to manually and run them individually. I wonder if there's a smarter way to write one query and create 22 tables, each taking values from their respective column from the original table.
Thanks
Based on your DB you can search for the definition of your table on the system tables (eg: with Oracle you can use SYS.DBA_TABLES) and generate a CREATE TABLE statement based on that.
Then you can query your table and insert the values in the new table.

How can I use an input from another table in my query?

I'm creating a new table using PostgreSQL, but I need to get a parameter from another table as an input.
This is the table I have (I called table_1):
id column_1
1 100
2 100
3 100
4 100
5 100
I want to create a new table, but only using ids that are higher than the highest id from the table above (table_1). Something like this:
insert into table_new
select id, column_1 from table_old
where id > (max(id) from table_1)
How can I do this? I tried searching, but I got to several posts like https://community.powerbi.com/t5/Desktop/M-Query-Create-a-table-using-input-from-another-table/td-p/209923, Take one table as input and output using another table BigQuery and sql query needs input from another table, which are not exactly what I need.
Just use where id > (select max(id) from table_1).

SQL/T-SQL Substring LEFT or Right doesn't appear to resolve

I have a two table where I have some values in a column UniqueKeys such as:
Table 1
2016_2016-2 S2_001840_30_01
2017_2017-2 D4_002213_3_01
The problem is that I am trying to match these with table 2 Unique values where the values are written in a different order such as :
Table 2:
001840_2016-2_S2_30_D_179_364128_400985
002213_2017-2_D4_3_E_752_376901_422828
Table 1 is from a different source system and table 2 is from different one. What I am trying to achieve is create a new table TABLE 3 where when the unique values match between table 1 and table 2 then insert the data from certain columns of table 1 and 2 into table 3 or else ignore the rest.
The way the Unique values should be is the following:
Year and Period: 2016-2
Cycle : S2
Unit: 001840
Group: 30
Giving the end result in Table 3 as:
001840_2016-2_S2_30
002213_2017-2_D4_3
You need to split both input values by "_" and then recombine the parts in the way they lead to the same format. Then you can join the tables.
Use two functions, the first one for values from type table 1, the second for values from table 2.
Effekt:
SELECT ...
FROM table1
JOIN table2 ON splitfunction1(table1.Key1) = splitfunction2(table2.Key2);

union table, change serial primary key, postgresql

Postgresql:
I have two tables 'abc' and 'xyz' in postgresql. Both tables have same 'id' columns which type is 'serial primary key;'.
abc table id column values are 1,2,3 and also xyz id column containing same values 1,2,3,4
I want to union both tables with 'union all' constraint. But I want to change 'xyz' id column values to next value of 'abc' id column last value as 1,2,3,4,5,6,7
select id from abc
union all
select id from xyz
|id|
1
2
3
1
2
3
4
my wanted resuls as
|id|
1
2
3
4
5
6
7
BETTER - Thanks to #CaiusJard
This should do it for you
select id FROM abc
UNION ALL select x.id + a.maxid FROM xyz x,
(SELECT MAX(id) as maxid from abc) a
ORDER BY id
For anyone who's doing something like this:
I had a similar problem to this, I had table A and table B which had two different serials. My solution was to create a new table C which was identical to table B except it had an "oldid" column, and the id column was set to use the same sequence as table A. I then inserted all the data from table B into table C (putting the id in the oldid field). Once I fixed the refernces to point to from the oldid to the (new)id I was able to drop the oldid column.
In my case I needed to fix the old relations, and needed it to remain unique in the future (but I don't care that the ids from table A HAVE to all be before those from table C). Depending on what your trying to accomplish, this approach may be useful.
If anyone is going to use this approach, strictly speaking, there should be a trigger to prevent someone from manually setting an id in one table to match another. You should also alter the sequence to be owned by NONE so it's not dropped with table A, if table A is ever dropped.

Read Rows But search first

I want to make an import system that will look into one Datasource and copy new records into another DataSource.
Monthly I want to copy some tables data from one datasource to another datasource
SourceTableName : srcTable
DestinationTableName : destTable
Suppose first month in source table I have:
Id Name 1 john
3 Rahul 5 Andrew
All three rows Will be copy into desTable
Suppose Second Month in Source Table I have
Id Name 1 John
3 Rahul 5 Andrew
6 Vikas 7 Sonam
8 Divya
Firstly Sql Should get the last Row of desTable
and match that row into srcTable
and extract all new records from scrTable and copied into desTable
.....
Please let me know how I can write query for fulfill above purpose. If there is shorter approach, that would be helpful too.
Since you only care about adding new records, and don't need to handle updates or deletes... You can simply add the record from the source table if it doesn't exist in the destination table:
INSERT INTO destTable (ID, Name)
SELECT s.ID, s.Name
FROM
srcTable s
LEFT OUTER JOIN destTable d ON d.ID = s.ID
WHERE
d.ID IS NULL
You can write a stored procedure for do this action and execute that every time you want.
for this action you can from bellow query:
(Part 1 for insert new data, Part 2 for update change data)
Insert Into DestinationTable(ID, Name)
Select ID, Name
From SoiurceTable
Where Not Exists
(Select *
From TDestinationTablest
Where DestinationTable.ID = SoiurceTable.ID)
Go
Update DestinationTable
Set DestinationTable.Name = SoiurceTable.Name
From DestinationTable, SoiurceTable
Where DestinationTable.ID = SoiurceTable.ID
I hope it's helpful.