SQL make each column a new table - sql

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.

Related

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.

Transpose table in SQL

I have a table set up in the following manner.
CustomerNumber June15_Order June15_Billing July15_Order July15_Billing August15_Order August15_Billing
12345 5 55 3 45
5431 6 66 5 67
I would prefer it to be:
CustomerNumber Date Order Billing
12345 01/06/2015 5 55
12345 01/07/2015 3 45
5431 01/06/2015 6 66
5431 01/07/2015 5 67
Any thoughts as to how I would accurately transpose this table?
If you're just trying to get the old data into the new, you'll basically need to use brute force:
INSERT INTO NewTable
(CustomerNumber, [Date], [Order], Billing)
(
SELECT CustomerNumber, '06/15/2015', June15_Order, June15_Billing
FROM OldTable
UNION
SELECT CustomerNumber, '07/15/2015', July15_Order, July15_Billing
FROM OldTable
UNION
SELECT CustomerNumber, '08/15/2015', August15_Order, August15_Billing
FROM OldTable
)
Presuming there are columns for any month and any years, this gets ugly really fast. If the columns are set and hard-coded, use #John Pasquet's solution (+1). If you need the ability to work with any set of columns of the form MMMMDD_Type, here's an outline.
First pass:
Write a SELECT... UNPIVOT... query to transform the table
Map the resulting "label" column to a Date datatype and a "Type" (Order, Billing)
However, mapping result set column names of "July15" to "Jul 1, 2015" (or 01/07/2015) is hard, if not crazy hard. This leads to a second pass:
Build a "lookup" list of columns from sys.tables and sys.colmns
Pick out those that are to be unpivoted
Figure out the dates and types for each of them
Build the SELECT... UNPIVOT... in dynamic SQL, dumping the results to a temp table
Join this temp table to the lookup list by original column name, which (via the join) gets you the prepared date and type values
Seriously, this could get ridiculously complex. The smart money is on rebuild the tables with columns for date and type.
First create the a new table with the desired structure, after that you will need to create a stored procedure for that task, which will iterate over all rows.
On the columns you know old_col to new_col just take the value and save in a variable, for the others you will need to create condition for each month like a "contains june" and save in two variables date and value, after that each time you found a new month with value > 0 perform a insert on the new table with all the variables.

Insert records into records into table from 2 difference source in SQL Server

Just wondering will it be possible to insert records into a table from 2 difference sources in SQL?
Example:
Table 1
Number
1
2
Table 2
Name
Alex
Amy
I want to insert records into table 3 from table 1 and table 2 and the result for table 3 should be:
Number Name
1 Alex
2 Alex
1 Amy
2 Amy
Any way that I can do it in SQL Server?
Try a CROSS JOIN and a SELECT ... INTO:
This join relates every-with-every row. The result will be filled into a new table on the fly:
SELECT Nrs.Nr
,Nms.Name
INTO dbo.TheNewTable
FROM dbo.NumberTable AS Nrs
CROSS JOIN dbo.NameTable AS Nms;
See the result:
SELECT * FROM dbo.TheNewTable;
connect for two connections .
use a script not only in SQL :javca for example.
use hashmap and hashet ...
isnert in temporary table(drop on commit for example).
copy in table 3.
-don't forget to close connections.

Copy part of some SQL Server rows into same database

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