select into a table with different column names - sql

In SQL, Select into ... copies rows into a different (backup) table. Is this possible if the backup table has different structure (or different column names)? If not, what is the best way to achieve this?
Here is what I want to do: TableA has columns a1,a2,a3. I want to copy some rows from this table to another table TableB which has column b1,b2,b3,b4. Content of a1 to go into b1, a2 to b2 etc.

The column names do not matter at all, as long as data types match (or can be cast in the assignment).
If the data types of the columns don't match, try casting the values accordingly. Just try with small dummy tables. Be sure to list the target columns explicitly to avoid confusion. Like this:
INSERT INTO TableB (b1, b2, b3)
SELECT a1, a2, a3
FROM TableA
WHERE <some condition>;
More details in the SQLite manual here.

Related

How to map (relate) two unrelated tables without adding any column in sql and powerbi

I have 2 tables named A1 and B1. And both the tables has commonly 3 columns but in different variable. Considering A1 as the master table I have to map B1 but the values in A1 (those particular ID's(3 columns)) will not be present in B1 and vice versa. And the ID's present in both table contains duplicates. I have to find the relationship (map) the two tables without adding any extra attribute in SQL and POWERBI
Here is the sample table:
Copy the ID from A1 table to a new table. (Use 'Add as new query' option)
Merge the ID from B1 table to the new column you just created.
Right click and remove duplicates from the new column. Now you have a master ID column without any duplicate values.
Map this column with ID in A1 and B1.

Insert with select, dependent on the values in the table inserting into EDITED

So I need to figure out how to insert into a table, from another table, with a where clause that requires me to access the table that I am inserting into. I tried an alias from the table I am inserting into, but I quickly found out that you cannot do that. Basically, what I want to check is that the values that I am inserting into the table match a particular field within the table that I am inserting into. Here is what I've tried:
INSERT INTO "USER"."TABLE1" AS A1
SELECT *
FROM "USER"."TABLE2" AS A2
WHERE A2."HIERARCHYLEVEL" = 2
AND A2."PARENT" = A1."INSTANCE"
Obviously, this was to no avail. I've tried a couple other queries, but they didn't me anywhere, either. Any help would be much appreciated.
EDIT:
I would like to add rows to this table, not add columns to the table. The two tables are of the exact same structure -- in fact, I extracted the data already in table1 from table2. What I have in table1 currently is a bunch of records who have NO PARENT, but an instance. What I want to add is all the records who have a parent in table2 that are equal to the instance in table 1.
Currently there is no way to join on a table when inserting. The solution with the subselect where you select from the table, is the correct.
Aliasing the table you want to change is only possible with UPDATE, UPSERT and MERGE. For these operations it makes sense, as you need to match a column and then decide if you need to update it or insert something instead. In your example the line from table1 that you match is not relevant, as you don't want to change it, so from the statement point of view it is not really relevant that the table you use in your subselect is the same that the one you insert into.
As alternative, I can suggest you following solution, which is equivalent with yours:
INSERT INTO "user"."table1"
SELECT
A1."ROOT",
A1."INSTANCE",
A1."PARENT",
A1."HIERARCHYLEVEL"
FROM "user"."table2" AS A1
WHERE A1."INSTANCE" in (select "PARENT" from "user"."table1")
AND A2."HIERARCHYLEVEL" = 2
This gave me the answer I was looking for, although I am sure there is an easier -- or more efficient -- way to do it.
INSERT INTO "user"."table1"
SELECT
A1."ROOT",
A1."INSTANCE",
A1."PARENT",
A1."HIERARCHYLEVEL"
FROM "user"."table2" AS A1,
"user"."table1" AS A2
WHERE A1."INSTANCE" = A2."PARENT"
AND A2."HIERARCHYLEVEL" = 2

Combining different tables in excel by looking up values

Okay, so let's say I have two tables in Excel. I want to create a third table that looks up data from the other two and combines them. I'll explain what I mean
Here is my first table:
It has a number, which is like a primary key, and a corresponding name for each entry.
I also have a second table:
This one just contains some random info, and there's numerous entries. The Client Name is not listed here, but it does have the key for the client that each entry corresponds to.
I want to create a third table that shows me all the Client Info data, but replaces the key with the actual client name. So basically, wherever it sees a key of "1", it'll look that up from the first table and find "Comet" instead.
The desired table would look like this:
What kind of formula would I need to pull data from one table based on a value? Note that all my tables are in different worksheets.
Assuming that the three tables are in Sheet1, Sheet2, Sheet3, this is the formula you need in Sheet3, cell A2
=vlookup(Sheet2!A2,Sheet1!$A2$b$6,2,false)
Copy down as many rows as there are rows in Sheet2
If you need the client info from Sheet2, use this in Sheet3, cell B2 and copy down
=sheet2!B2
As you put an "SQL" tag, I assume that you are also looking for an SQL based answer. So in addition to #teylyn suggestion, you can use the following SQL query if you are working with SQL databases:
SELECT table1.Client_Name, table2.Client_Info
FROM table1
RIGHT JOIN table2 ON table1.ID_Number = table2.ID_Number
Here is what this query does:
The RIGHT JOIN will return all rows from the right table (here table2), with the matching rows in the left table (here table1) according to the condition specified by the ON clause (here, if the ID Number is the same). Then, the SELECT clause will return a table with ONLY the columns specified after the keyword SELECT.

How would you total the values in one column and keep a distinct value in another?

I have a database table that has multiple codes in one column that correspond to certain values in another column. For example, a particular code in column A corresponds to a value in column B. There are thousands of duplicate entries in column A that correspond to different values in column B. I want to add up all of the values in column B that have the particular code in column A, while only keeping one copy of the code from column A. You may think of the columns as key-value pairs, where column A contains the key and column B contains the value.
Basically, I want to add all the values in column B where column A is a specific value, and I want to do for this all of the unique "keys" in column A. I'm sure that this is a simple task; however, I am pretty new to SQL. Any help would be greatly appreciated.
Here is the result I'm looking for.
This should work:
SELECT
A,
SUM(B) AS sum_b
FROM [yourTable]
GROUP BY A
SELECT COLUMNA, SUM(ISNULL(COLUMNB,0)) AS TOTAL
FROM
dbo.TableName
GROUP BY COLUMNA

Relational Mapping between two Dynamic Amounts of Entities

I have two amounts of dynamic entities. For example
B1, B2, B3, B4, B5,... I don't know how many B's i have but i will have a specific table in SQL for each B. On the other hand i have a dynamic amount of A's each represented as a single row in Table AEnum.
Now i want to have a Mapping. The mapping looks like that:
Each a can have each entity of B or not => 1/0 => boolean.
For example:
A1 => B1 (true), B2 (true), B3 (false), B4 (true)...
A2 => B1 (true), B2 (false), B3 (false), B4 (true)...
A3 => B1 (false), B2 (true), B3 (true), B4 (true)...
So for each B i made a column with B's name in the Table AHasB, just storing a tinyint. This is however not very dynamic. I want to make it as dynamic as possible.
Possible idea:
Table BEnum stores Bid and BName (the Tablename of B's instance), AEnum stays the same just being a number of rows storing stuff about each A. Then AHasB would look like:
A1 => "B1, B4, B12"
A2 => "B9, B10"
Just storing it as a single string which i parse on the logic side, not the persistence side. With that solution the following would be the case:
When adding a B: I only need to add the B table manually and update the BEnum table
When deleting a B: Delete the table, update the BEnum table accordingly and update AHasB to delete all B instances out of the strings that just have been deleted
When adding a A: Just insert a new row in AHasB
When deleting an A: Just remove that row
I think that would be the most dynamic approach. Or is there any better way to save this in a SQL database?
If anyone asks: B1, B2, B3,... are all names for quite different tables. For example: cars, hobbys, etc. A1, A2, A3, are persons and the relation can be read as "For person X i want to know which hobbies he has, what cars he drives, etc."
Someone any better ideas?
In general, you can represent a relation between any entity sets as a table containing a column for each entity set. For example:
persons (person PK, name, age, ...)
cars (car PK, make, model, ...)
car_owners (person PK/FK, car PK/FK)
In car_owners, including both columns in the PK ensures the pair is unique, meaning a many-to-many relation. Limiting the PK to the one or other means entities in that column are required to be unique, hence a one-to-many relation.