I have two tables in SQL Server that I want to merge.
The first table is dbo.bac and has a column counter, the second table is dbo.data and also has a column counter.
I want to load the first table and the second table with all their columns that have the same counter value. Thanks for the help...
You need to use JOIN to join both tables.
SELECT *
FROM dbo.bac A
INNER JOIN dbo.data B ON B.counter = A.counter
this should do it, but you need to filter your records as needed.
Related
I am trying to use SQL to create a table that concatenates all dates from a specific range to all items in another table. See image for an example.
I have a solution where I can create a column of "null" values in both tables and join on that column but wondering if there is a more sophisticated approach to doing this.
Example image
I've tried the following:
Added a constant value to each table
Then I joined the 2 tables on that constant value so that each row matched each row of both tables.
This got the intended result but I'm wondering if there's a better way to do this where I don't have to add the constant values:
SELECT c.Date_,k.user_email
FROM `operations-div-qa.7_dbtCloud.calendar_table_hours_st` c
JOIN `operations-div-qa.7_dbtCloud.table_key` k
ON c.match = k.match
ORDER BY Date_,user_email asc
It's not a concatenation in the image given, Its a join
select t1.dates Date ,t2.name Person
from table t1,table t2;
Cross join should work for you:
It joins every row from both tables with each other. Use this when there is no relationship between the tables.
Did not test so syntax may be slightly off.
SELECT c.Date_,k.user_email
FROM `operations-div-qa.7_dbtCloud.calendar_table_hours_st` c
CROSS JOIN `operations-div-qa.7_dbtCloud.table_key` k
ORDER BY Date_,user_email asc
I am looking for the best way to combine two tables in a way that will remove duplicate records based on email with a priority of replacing any duplicates with the values in "Table 2", I have considered full outer join and UNION ALL but Union all will be too large as each table has several 1000 columns. I want to create this combination table as my full reference table and save as a view so I can reference it without always adding a union or something to that effect in my already complex statements. From my understanding, a full outer join will not necessarily remove duplicates. I want to:
a. Create table with ALL columns from both tables (fields that don't apply to records in one table will just have null values)
b. Remove duplicate records from this master table based on email field but only remove the table 1 records and keep the table 2 duplicates as they have the information that I want
c. A left-join will not work as both tables have unique records that I want to retain and I would like all 1000+ columns to be retained from each table
I don't know how feasible this even is but thank you so much for any answers!
If I understand your question correctly you want to join two large tables with thousands of columns that (hopefully) are the same between the two tables using the email column as the join condition and replacing duplicate records between the two tables with the records from Table 2.
I had to do something similar a few days ago so maybe you can modify my query for your purposes:
WITH only_in_table_1 AS(
SELECT *
FROM table_1 A
WHERE NOT EXISTS
(SELECT * FROM table_2 B WHERE B.email_field = A.email_field))
SELECT * FROM table_2
UNION ALL
SELECT * FROM only_in_table_1
If the columns/fields aren't the same between tables you can use a full outer join on only_in_table_1 and table_2
try using a FULL OUTER JOIN between the two tables and then a COALESCE function on each resultset column to determine from which table/column the resultset column is populated
I need to join multiple tables in the where clause of a update statement. Precisely there are two tables with master slave relationship. I need to update a row in the master table but need to check for the foreign key entry in its slave table.
Table A
TableId,Empid,EmpName,EmpAdd
Table B
TableId,Empid,DeptId,DeptName
When a row is inserted in Table A, Table B also has an insert. Say I need to update EmpAdd of TableA and this shall be based on the columns Empid,DeptId,DeptName from the two tables. Therefore I guess I need to join two tables.
what about checking for EXISTS instead of JOINs:
UPDATE tbl_master m
SET m.some_column = some_value
WHERE m.masteID = updatetable_id
AND EXISTS (SELECT * FROM tbl_slave s WHERE s.masterID = m.masterID)
Im trying to move data from a source table to a Master table. my source table has a category, but no ID. I need to take the category name from the source table, join it to the category table, and insert that ID into my target table.
Here is what I am working with so far (SQL Server):
Select C_CATID, C_CategoryDesc, b.Record_No (using this to differentiate records, as i keep getting duplicates) from [Category_Tree_Lookup]
inner join [dbo].[TBL_C_Mapped SpendData_12_2014OLD03312015] b
on C_categorydescription = b.category
where C_CategoryDesc = b.category
I keep receiving too many records that are duplicates, so i cannot insert into the table.
Any help would be greatly appreciated!
You can use distinct function like this
select distinct C_CATID, C_CategoryDesc, b.Record_No from tablename
I am new to SQL and want to compare two tables using inner join and then use the result to compare it with the third table and so on. Also the third table does not share the same column name as compared in the first two.
Below is the SQL statement to compare two tables.
SELECT WORKLOAD_ITEM_ID wid
FROM OUTPUT o
INNER JOIN workload_item wi ON O.WORKLOAD_ITEM_ID = wi.WORKLOAD_ITEM_ID
Now whatever is returned I want to use that and do an inner join with a third table which does not have a column WORKLOAD_ITEM_ID. So the question is how to save and compare the result?
Please Help!
You can follow this to hold records in temp table
INSERT INTO #WORKLOAD_ITEM_TEMP
SELECT WORKLOAD_ITEM_ID wid
FROM OUTPUT o
INNER JOIN workload_item wi ON O.WORKLOAD_ITEM_ID = wi.WORKLOAD_ITEM_ID
Then with your third table made a join with this #WORKLOAD_ITEM_TEMP table.
Now you have to decide on which column need to put inner join. Either add a common column between output result set and third table or use just cross join.
I think you need to reevaluate your Database structure and make efficient tables relationship.