I have similar table
How can i merge these two tables ? There are other columns, but these are the same.
How can i fill Table1 with values of Table2 or merge these tables ?
In Table2 is only 1 customer.
So the result table will have all customers with their values (Table1 will have Customer4 with Sales 50).
Thank you.
To update the table do
update t1
set t1.sales = t2.sales
from table1 t1
join table2 t2 on t1.customername = t2.customername
and as a select use
select t1.customername,
coalesce(t1.sales, t2.sales) as sales,
t1.date,
t1.variable1
from table1 t1
left join table2 t2 on t1.customername = t2.customername
Related
I have two tables containing a category and a date.
Table 1:
cat date
A 20160102
A 20160103
A 20160104
B 20170202
B 20170203
B 20170204
Table 2:
cat date
A 20160103
A 20160104
A 20160105
B 20170203
B 20170206
B 20170207
I now want to delete all rows from Table 1 where the dates are equal or later than the earliest date of Table 2 per category.
The earliest date of category A is 20160103. The earliest date of category B is 20170203. Hence, the entries ('A','20160103'), ('A','20160104'), and ('B','20170204') should be deleted from Table 1.
For testing, I try to create a SELECT statement that selects the values which I want to delete. Currently I came up with this:
SELECT
t1.id
,t1.holiday
,MIN(t2.holiday)
FROM
table1 t1
INNER JOIN table2 t2
ON t2.id = t1.id
GROUP BY
t1.id
,t1.holiday
The next logical step (for me) would be to add the following WHERE clause
SELECT
t1.id
,t1.holiday
,MIN(t2.holiday)
FROM
table1 t1
INNER JOIN table2 t2
ON t2.id = t1.id
WHERE
t1.holiday >= MIN(t2.holiday)
GROUP BY
t1.id
,t1.holiday
However, this yields the following error:
An aggregate may not appear in the WHERE clause unless it is in a subquery contained in a HAVING clause or a select list, and the column being aggregated is an outer reference.
How can I solve this problem?
This answers the first question (about deleting from table 1)
DELETE t1
FROM Table1 AS t1
INNER JOIN (
SELECT
cat,
min_date=MIN([date])
FROM
Table2
GROUP BY
cat
) AS t2 ON
t2.cat=t1.cat
WHERE
t1.[date]>=t2.min_date
I got two tables as below
table 1 :Which got multiple columns which refers to a value in the second table.
tbale2:Lookup table where it got a row for every possible value for the columns in the above table
What I want to do is lookup the values in columns ItemID,ORDIG,CatID,MediaID in table 1 from ValueID in table2 and return ValueName
so at the end my result should look like
ItemID OrgID CatID MediaID
i859049 Singapore Full 0001edf
You will need to join to the lookup table once for each value you need, but should likely need to use a LEFT instead of INNER join since the values may be null.
SELECT
T1.ItemId,
Items.ValueName AS ItemName,
T1.OrgID,
Orgs.ValueName AS OrgName,
T1.CatID,
Cats.ValueName AS CatName,
T1.MediaID,
Media.ValueName AS MediaName
FROM Table1 T1
LEFT OUTER JOIN Table2 Items
ON T1.ItemId = Items.ValueID
LEFT OUTER JOIN Table2 Orgs
ON T1.OrgId = Orgs.ValueID
LEFT OUTER JOIN Table2 Cats
ON T1.CatId = Cats.ValueID
LEFT OUTER JOIN Table2 Media
ON T1.MediaId = Media.ValueID
Any method to do this?
Table1
1
2
3
4
5
Table2
3 (with the condition)
4 (without the condition)
I want to:
Select all records from Table1 if it exists in Table 2, where...(condition)
Select all records from Table1 if it not exists in Table2
Combine both select results. Sort all results with their created date.
For example, the result should be:
Result
1
2
3
5
Hopefully this can help.
SELECT t1.* from table1 t1
JOIN table2 t2
ON t1.ID = t2.ID
UNION ALL
SELECT t1.* from table1 t1 where ID in
(
SELECT t2.ID from table1 t1 except Select t2.ID from table2 t2
)
ORDER BY t1.CreatedDate
You can achieve this by doing:
SELECT t1.id
FROM Table1 t1
LEFT JOIN Table2 t2 on t1.id = t2.id
WHERE condition OR t2.id IS NULL
ORDER BY t1.CreatedDate;
See fiddle (I assumed condition to be t2.id!=4, but it can be anything else depending on other data in your tables).
There could be multiple solution.
One way
we can get the result set using two different queries and at last combine both of the result-set using UNION
Another way,
First statement is saying that get all the result set from TABLE1 if it exists in TABLE2 as well with some criteria (condition in where clause)
means using INNER JOIN we can achieve this
Second statement is saying get all the result set from TABLE1 which are not present in TABLE2
means along with INNER JOIN ed query also include the TABLE1's data if not present in TABLE2
here we can take the help of LEFT OUTER JOIN (taking TABLE1 on the left side)
Assumption: (condition: t1.Id != 4)
Let's try to understand the query using both of the above mentioned ways
---- -- --Step1 Create table and insert records
---- create table1 with Id int identity columsn
--CREATE TABLE Table1 (Id INT IDENTITY(1,1), CreatedDate smalldatetime default(getdate()));
--go
---- insert 1st 5 integers into Table1
--INSERT INTO Table1 DEFAULT VALUES
--go 5
---- create Table2 with Id int column
--CREATE TABLE Table2 (Id INT , CreatedDate smalldatetime default(getdate()));
--go
---- insert records 3,5 into Table2
--INSERT INTO Table2(Id) VALUES (3), (4);
-- -- -- Solution: one way
; WITH cteMyFirstResult AS
(
-- 2.1. Select all records from Table1 if it exists in Table 2, where...(condition)
SELECT
Id, CreatedDate
FROM Table1 AS t1
WHERE t1.Id IN (SELECT Id FROM Table2 AS t2)
AND t1.Id != 4 -- assumption it can be any condition
),cteMySecondResult AS (
-- 2.2. Select all records from Table1 if it not exists in Table2
SELECT
Id, CreatedDate
FROM Table1 AS t1 WHERE t1.Id NOT IN (SELECT Id FROM Table2 AS t2)
)
-- 2.3. Combine both select results. Sort all results with their created date.
SELECT
Id, CreatedDate
FROM cteMyFirstResult
UNION
SELECT
Id, CreatedDate
FROM cteMySecondResult
ORDER BY CreatedDate;
-- -- Solution: Another way (with bug)
SELECT t1.Id, t1.CreatedDate
FROM Table1 AS t1
LEFT JOIN Table2 AS t2 on t1.id = t2.id
WHERE t1.Id != 4
Order by T1.CreatedDate;
-- in this query we are using the criteria after doing the join operation.
-- thus after filtering out the result set based on JOIN Condition this condition will get applied
-- and if there is any null record in the Table1 for column Id (used in join) will not come in the final result-set
-- to avoid this we can include NULL check along with our criteria
-- -- Solution: Another way
SELECT t1.Id, t1.CreatedDate
FROM Table1 AS t1
LEFT JOIN Table2 AS t2 on t1.id = t2.id
WHERE ( t1.Id != 4 ) OR t1.Id IS NULL -- include all your criteria within small-barcket)
Order by T1.CreatedDate;
Thanks for all responses.
I come out with the answer I want:
SELECT *
FROM Table1 t1
WHERE NOT EXISTS(SELECT 1 FROM Table2 t2
WHERE t1.ID = t2.ID
AND t2.CIF_KEY = #CifKey
AND t2.STATUS <> ''3'')
AND (condition in where clause)
I need to insert 3 columns from one table to another, using JOIN by 3 fields: name, surname and age
I need update column status, status1 and status2 in table_2 with values from table_1
IF
table_1.name = table_2.name
table_1.surname = table_2.surname
table_1.age= table_2.age
UPDATE t2
SET
t2.[status]=t1.[status]
,t2.[status1]=t1.[status1]
,t2.[status2]=t1.[status2]
FROM [table_1] t1
INNER JOIN [table_2] t2
ON (t1.name=t2.name AND t1.surname=t2.surname AND t1.age=t2.age)
As you mentioned in comments that these table from different databases, then please change only the two line like.
FROM [yourDataBase1Name].[dbo].[table_1] t1
INNER JOIN [yourDataBase2Name].[dbo].[table_2] t2
Just update the table with Join.
it will be something like this:
UPDATE t2
SET t2.status = t1.status,
t2.status1 = t1.status1,
t2.status2 = t1.status2
FROM t2 JOIN t1 on (t1.first_name = t2.first_name AND t1.last_name = t2.last_name AND t1.age = t2.age);
Look here for more information:SQL update query using joins
SQL Fiddle: http://sqlfiddle.com/#!3/b3951/1
Situation:
I have two tables
Table 1 always has records
Table 2 is the result of a select statement and may or may not have records
Desired Results:
If Table 2 has ANY records, I want only matching records from Table 1. Otherwise, I want all records from Table 1.
I realize I can do this:
DECLARE #count int
SELECT #count=COUNT(*) FROM Table2
IF #count>0
SELECT t1.* FROM Table1 t1 INNER JOIN Table2 t2 ON t1.id=t2.id
ELSE
SELECT * FROM Table1
However, I am trying to avoid IF statements if possible.
Is that even possible?
select t1.*
from Table1 t1
left join
Table2 t2
on t1.id = t2.id
where t2.id is not null -- Match found
or not exists -- Or Table2 is empty
(
select *
from Table2
)