I need help in populating ins_dts and upd_dts logic.
Insert and Update date present in both tables so I need to know in coalesce if value is populating from a.col_cmmt_txt, a.col_dscr_txt, a.col_annt_txt, then use insert and update date from test1 table else if populated value from b.target_col_desc then populate insert and update from test2 table :
select
a.schema, a.table, a.column,
coalesce(a.col_cmmt_txt, a.col_dscr_txt, a.col_annt_txt, b.target_col_descr) as coldscr,
a.dw_ins_dts, a.dw_upd_dts
from
test1 a
left join
test2 b on a.schema = upper(b.schema)
and a.table = upper(b.table)
and a.column = upper(target_col)
To me, it looks like a case:
SELECT a.schema,
a.table_,
a.column_,
COALESCE (a.col_cmmt_txt,
a.col_dscr_txt,
a.col_annt_txt,
b.target_col_descr) AS coldscr,
--
CASE
WHEN a.col_cmmt_txt IS NOT NULL
OR a.col_dscr_txt IS NOT NULL
OR a.col_annt_txt IS NOT NULL
THEN
a.dw_ins_dts
ELSE
b.dw_ins_dts
END,
--
CASE
WHEN a.col_cmmt_txt IS NOT NULL
OR a.col_dscr_txt IS NOT NULL
OR a.col_annt_txt IS NOT NULL
THEN
a.dw_upd_dts
ELSE
b.dw_upd_dts
END
FROM test1 a
LEFT JOIN test2 b
ON a.schema = UPPER (b.schema)
AND a.table_ = UPPER (b.table_)
AND a.column_ = UPPER (target_col)
Related
I have a 2 Tables with below structures
Table 1-- Containing Values like this.
OTHER_CODE
CAPACITY_CODE
Result
A
1
A
5
A
9
A
(null)
B
2
B
6
B
2
Table_2- With Values Like
OTHER_CODE
CAPACITY_CODE
Result
A
1
A
A
5
B
A
(null)
C
A
ELSE
D
B
ALL
E
(null)
ALL
F
I need to Join Table_1 with Table_2 on basis of columns OTHERCODE and CAPACITYCODE and update values in Column Result of Table**1 **using a Merge statement.
I need to handle and match Values based on ELSE and ALL values too.
Check for Direct Match
Check if ALL or ELSE condition
The Final TABLE_1 must look like
OTHER_CODE
CAPACITY_CODE
Result
Explanation
A
1
A
Direct Join
A
5
B
Direct Join
A
9
D
Satsifying ELSE condition
A
(null)
C
Direct join with NVL handling
B
2
E
As Value for CapacityCode in TableB is ALL
B
6
E
As Value for CapacityCode in TableB is ALL
B
2
E
As Value for CapacityCode in TableB is ALL
I Tried Joining both the tables but the was unable to satisfy Else and ALL conditions. Hope if someone can help me on this.
There are Several **Result ** Columns like , Result 1 ,2 in both tables which needs to be updated using the same logic.
Thanks in Advance.
here is a fiddle to work on https://dbfiddle.uk/FMKdWzQT
I got the query working. by using a case statement and assigning a number so I could use max then I just remove the number.
SELECT a.other_code,
a.capacity_code,
(
SELECT SUBSTR(max(
CASE WHEN b.other_code = a.other_code AND a.capacity_code = b.capacity_code THEN concat('3',b.myresult)
WHEN b.other_code = a.other_code AND a.capacity_code is null and b.capacity_code is null THEN concat('2',b.myresult)
WHEN b.other_code = a.other_code AND b.capacity_code in ('ELSE', 'ALL') THEN concat('1',b.myresult)
else null end),2)
FROM table2 b ) as myresult
FROM table1 a
however I can not get the update to work. I tried a merge it is give me the unstable row error and I tried an update select but that is giving me single row subquery error so maybe someone else can take a look at the fiddle. here was my attempt at the update.
UPDATE table1
SET myresult = (
SELECT myresult
FROM (
SELECT a.other_code,
a.capacity_code,
(
SELECT SUBSTR(max(
CASE WHEN b.other_code = a.other_code AND a.capacity_code = b.capacity_code THEN concat('3',b.myresult)
WHEN b.other_code = a.other_code AND a.capacity_code is null and b.capacity_code is null THEN concat('2',b.myresult)
WHEN b.other_code = a.other_code AND b.capacity_code in ('ELSE', 'ALL') THEN concat('1',b.myresult)
else null end),2)
FROM table2 b ) as myresult
FROM table1 a
)t2
WHERE table1.other_code = t2.other_code and nvl(table1.capacity_code,'x') = nvl(t2.capacity_code,'x')
);
I have an SQL query where I am joining 2 tables, on another table - A and B
Table A is being joined on table E on ID = ID
Table B is being joined on table E on ID = SKU
Table A and Table B both have columns Price, Value
Sometimes there are null values in here
I have done Select ID, (A.Price + B.Price) AS TEST, (A.Value + B.Value)
AS TEST2 from E
But I am getting NULL values for all of the results for test and test2
Query is:
select PII, PII_Count, SK_Count, (PII_Count - SK_Count) as TEST from CIEER
left join ccq on CIEER.PII = ccq.PRIE
left join ccrq on CIEER.PII = ccrq.SKIE
order by PII_Count desc
If PII_count or SK_Count is null then it will return null ,to prevent null from calculation you should use nvl function like below:
(nvl(PII_count, 0) - nvl(SK_Count,0)) as TEST
Alternatively you can also use COALESCE function as well.
I have a select query returning two results and what I want is to save them in a table type of variable. This is how I am doing it:
declare #CompletedTotalValues table (CMedian int, CPerc int);
update #CompletedTotalValues set CMedian = t.CMed, CPercentile = t.CPerc
from(
Select CMed = dbo.median(case when cr.Priority = 1 then cr.Days else null end),
CPerc = dbo.Percentile90(case when cr.Priority = 1 then cr.Days else null end)
from A a inner join B b on b.Id = a.Id
where b.StatusId = 3
) t;
Here, when I run the subquery, I see CMed is 25 and CPerc is 43. However, when I execute Select * from #CompletedTotalValues, it is returning both column blank (no value it shows). Where is my mistake? Any advice would be appreciated
Why not just insert the data in the first place?
DECLARE #CompletedTotalValues TABLE (CMedian INT, CPerc INT);
INSERT #CompletedTotalValues (CMedian, CPerc)
SELECT CMed = dbo.median(CASE WHEN cr.Priority = 1 THEN cr.Days END),
CPerc = dbo.Percentile90(CASE WHENn cr.Priority = 1 THEN cr.Days END)
FROM A
INNER JOIN B ON b.Id = a.Id
WHERE b.StatusId = 3;
If I go by the query you have shared, you are trying to update without even inserting any data in the table variable at first place. Is it?
I need to compare email record (by email address) between two tables (Table A as Production data and B as old data) to find the difference and show the result in column such as "New", "Delete" and etc.
If exists in Table A, not in Table B, it should mark "New"
else if exists in Table B, not in Table A, it should mark "Delete"
if appears in both table, it should mark "Maintain"
I want the result like that
DisplayName LastName Diremail Result
==============================================
XXX XXX a#a.com New
ABC ABC 1#a.com Delete
DDD DDD 2#a.com Maintain
My code as follows:
SELECT b.DisplayName,
b.LastName,
b.diremail,
Result = CASE WHEN a.DirEmail IS NULL THEN 'New'
when b.DirEmail IS null then 'delete'
else 'Maintain'
END
FROM vHRIS_StaffDB b
LEFT JOIN HRIS_DL_Lists a
ON a.DirEmail = b.DirEmail
WHERE (
a.DirEmail IS NULL
OR a.DisplayName != b.DisplayName
)
but the data not correct as the code not return record which should "Delete"
(found in table b, not in table a)
pls advise. Thanks.
Sounds like you need full join instead of left join. Try this:
SELECT coalesce(b.DisplayName, a.DisplayName) DisplayName,
coalesce(b.LastName, a.LastName) LastName,
coalesce(b.diremail, a.diremail) diremail,
Result = CASE WHEN a.DirEmail IS NULL THEN 'New'
when b.DirEmail IS null then 'delete'
else 'Maintain'
END
FROM vHRIS_StaffDB b
FULL JOIN HRIS_DL_Lists a
ON a.DirEmail = b.DirEmail
WHERE (
a.DirEmail IS NULL
OR a.DisplayName != b.DisplayName
)
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]
)