compare 2 columns from 2 different tables and update a column - sql

I want to compare a column in table a and table b
If the a value from table a can be found in table b then I want to update another column in table a with a 'yes' and if it cannot be found I want to say 'no'. This is what I have so far:
UPDATE a
set
[CCA Match Org] = CASE WHEN b.[serial] = a.[CSI] THEN 'yes' ELSE 'no' END
My error at the moment says:
The column prefix 'b' does not match with a table name or alias name used in the query.

Assuming the join is on b.[serial] = a.[CSI]:
UPDATE a
SET [CCA Match Org] = CASE WHEN b.[serial] IS NOT NULL
THEN 'yes' ELSE 'no' END
FROM a LEFT OUTER JOIN b
ON b.[serial] = a.[CSI];

This is a quick example, not sure if it will work in your case because you didnt give us more info on the table structures.
UPDATE a SET col='Yes' WHERE a.id IN (SELECT a.id FROM a JOIN b ON a.CSI = b.serial)

Related

Adding new column (with inner join) and insert (update) value with case when (SQL Server 2012)

What is the best approach to add a column to an existing table with values
I've the following tables:
Table_A
ClientID, StatementID, CustBuy
Table_B
NewClientID
I want to add a new column YTD_Jan13
alter Table_A add YTD_Jan13 varchar(20)
select
(case
when CustBuy=0 and StatementID>='01.01.2013' then 'New YTD'
else 'Repead YTD'
end) as YTD_Jan13
from A inner join B
on A.ClientID = B.NewClientID
Basically I want to insert value in a new column (YTD_Jan13) with following conditions:
If
1) Table_A.ClientID = Table_B.NewClientID
2) in a Table_A (CustBuy=0 and StatementID>='01.01.2013') then 'New YTD'
else 'Repead YTD'
I'm confused, I'm asking for your help
Thanks in advance
BR, Habib
alter Table_A add YTD_Jan13 varchar(20)
Then stick your 'else' value in
update Table_A Set YTD_Jan13 = 'Repeat YTD'
then (this is called an update with join)
UPDATE a
SET a.YTD_Jan13 = 'New YTD'
FROM Table_A a
INNER JOIN B
ON A.ClientID = B.ClientID
Where CustBuy = 0 and StatementID >= '01.01.2013'
NB There is an assumption that there are no records for clients in A that are not in B. If there are you'll have to get a bit cleverer so YTD_Jan13 would be left null in those cases.

Return a default value when a match found

I would like to return all records (entryIDs) and where the language flag is selected, I want to return Yes instead of the actual value in attributevalue field. I've tried this but it returns the actual value populated rathern than a Yes where match found. I think I need where exists as this returns too many rows for each language associated with the entryIDs.
SELECT distinct x.entryID, ISNOTNULL(a.attributeValue, 'Yes')
from Entry as x
left outer join EntryAttribute as e on e.entryID = x.entryID
left outer join AttributeString as a on a.AttributeID = e.AttributeID
where a.AttributeDefinitionID = 44
Use a CASE statement. For example:
CASE WHEN attributeValue IS NOT NULL THEN 'YES' ELSE 'NO' END

Creating a computed column from expressions used to created 2 earlier computed columns

SOLVED MY PROBLEM... SEE SECOND PIECE OF CODE
This is my sql statement which compares 2 rows serial numbers from different tables. if there is a match then a yes is displayed, but if there is not a match then a no is displayed
This is done twice for 2 different reasons on different tables
SELECT table1.serial1, table1.serial2,
CASE WHEN table2.serial1 IS NULL THEN 'No' ELSE 'Yes' END AS [computedCol1],
CASE WHEN table3.serial2 IS NULL THEN 'No' ELSE 'Yes' END AS [computedCol2]
FROM table1
LEFT JOIN table2
ON table2.serial1 = table1.serial1
LEFT JOIN dbo.EPG
ON table3.serial2 = table1.serial2
what I want to do then is create another column which places yes in the row if wither the first or second column are yes otherwise it will display a no. I realise you cant compre computed columns so what i was looking to do was repeat the expressions and have an or statement with no luck. I'm not very experienced writing anyhting other then basic sql... This is mty attempt:
SELECT DISTINCT table1.serial1, table1.serial2,
CASE WHEN table2.serial1 IS NULL THEN 'No' ELSE 'Yes' END AS [computedCol1],
CASE WHEN table3.serial2 IS NULL THEN 'No' ELSE 'Yes' END AS [computedCol2],
CASE WHEN table3.serial2 IS NULL OR table2.serial1 IS NULL THEN 'No' ELSE 'Yes' END AS [computedCol2]
FROM table1
LEFT JOIN table2
ON table2.serial1 = table1.serial1
LEFT JOIN dbo.EPG
table3.serial2 = table1.serial2
table3.serial2 OR table2.serial1 IS NULL isn't the condition you're looking for. You need table3.serial2 IS NULL OR table2.serial1 IS NULL.
You're trying to return two different values with the same alias, [computedCol2].

Compare tables and identify new or changed fields

New to SQL, would like to compare fields between a stg and src table. The identify any differences between the tables and assign transaction status of 'C' for change. Any new records will be set with 'A' for add.
STG_DM_CLIENT and SRC_DM_CLIENT
What is the best way to do it, would it be best to do a some form of union all. Unsure how to proceed, any assistance welcomed.
You can identify new records by using NOT IN or NOT EXISTS
update STG_DM_CLIENT SET TransactionStatus = 'A' WHERE ID IN
(select Id from STG_DM_CLIENT
where Id not in (select Id from SRC_DM_CLIENT))
Then, you can identify changed records by comparing fields:
update STG_DM_CLIENT SET TransactionStatus = 'C' WHERE ID IN
(select STG_DM_CLIENT.Id from STG_DM_CLIENT
join SRC_DM_CLIENT on SRC_DM_CLIENT.Id = STG_DM_CLIENT.Id
where (SRC_DM_CLIENT.Field1 != STG_DM_CLIENT.Field1
OR SRC_DM_CLIENT.Field2 != STG_DM_CLIENT.Field2 ...))
update
[STG]
set
TransactionStatus = CASE WHEN [SRC].id IS NULL THEN 'A' ELSE 'C' END
from
STG_DM_CLIENT AS [STG]
left join
SRC_DM_CLIENT AS [SRC]
ON [STG].id = [SRC].id -- Or whatever relates the records 1:1
WHERE
[SRC].id IS NULL
OR [STG].field1 <> [SRC].field1
OR [STG].field2 <> [SRC].field2
OR [STG].field3 <> [SRC].field3
...
OR [STG].fieldn <> [SRC].fieldn

UPDATE row when matching row exists in another table

I need to update a field on a table to be true only if a matching row exists in another table, for all the rows where the column is currently null in the main table.
This is a description of what I want to achieve:
UPDATE [LenqReloaded].[dbo].[Enquiry] A
SET [ResponseLetterSent] = 1
WHERE [ResponseLetterSent] IS NULL
AND EXISTS
(
SELECT * FROM [LenqReloaded].[dbo].[Attachment] B
WHERE A.[EnquiryID] = B.[EnquiryID]
)
This isn't syntactically correct.
I can't code it via an IF EXISTS... statement because I don't have the [EnquiryID] without reading the data from the table.
How should I format my UPDATE statement?
You weren't far off...
UPDATE A
SET A.[ResponseLetterSent] = 1
FROM [LenqReloaded].[dbo].[Enquiry] A
WHERE A.[ResponseLetterSent] IS NULL
AND EXISTS ( SELECT * FROM [LenqReloaded].[dbo].[Attachment] B WHERE A.[EnquiryID] = B.[EnquiryID] )
You need to use a join in your update:
UPDATE [LenqReloaded].[dbo].[Enquiry] SET [ResponseLetterSent] = 1
FROM [LenqReloaded].[dbo].[Enquiry] A
join [LenqReloaded].[dbo].[Attachment] B on A.[EnquiryID] = B.[EnquiryID]
WHERE A.[ResponseLetterSent] IS NULL
This seems counterintuitive, but you need to establish a table alias in a From clause but use that alias in the Update Clause...
Update E Set
ResponseLetterSent = 1
From LenqReloaded.dbo.Enquiry E
Where ResponseLetterSent Is Null
And Exists (Select * From LenqReloaded.dbo.Attachment
Where EnquiryID = E.EnquiryID)
The thing you are missing is the 'from' clause, which is a t-sql extension - it is the only way to assign an alias to the updated table
update [lenqreloaded].[dbo].[enquiry]
set [responselettersent] = 1
from [lenqreloaded].[dbo].[enquiry] a
where [responselettersent] is null
and exists (
select *
from [lenqreloaded].[dbo].[attachment] b
where a.[enquiryid] = b.[enquiryid]
)