Conditional null not null in a select statement column - SQL Server - sql

Please take a look at this and let me know the possible solution?
Data to be shown from the table:
select a,b,d,e from table xyz.
when c is null show d with value
or
when c is not null show e with value
Data required:
Data looks like this
a b c d e
1 2 null 2
1 2 not null 2
From the above data, if c is null, display d = b else e = b.
How to write a proper SQL query for the above conditions, as I tried case it is not working.
Thanks in advance.

SELECT CASE WHEN c IS NULL THEN d ELSE e END
The bit about display d = b else e = b leads me to believe you may also be trying to compare NULL and NOT NULL values.
It's important to understand that SQL NULL means "Unknown" and therefore a comparison cannot take place between a known value and an unknown value.
In this case I suggest the use of Coalesce to change the value when it is NULL to something comparable that will not affect your logic.
Coalesce(d, 0) = b

SELECT CASE WHEN c IS NULL THEN d ELSE e END

Related

Join two tables on multiple conditions Using Oracle SQL

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')
);

Teradata : Update table values with join and if clause

I am working on a Teradata system in which I have 2 tables, Apple as A & Ball as B. Apple has 2 columns primId(integer) and updateValue(INteger). B only has primId(integer). What I am trying to do is, when A.primId=B.primId, then set updateValue=1 else to 0.
Table Apple:
Columns : Primary-key primId(Integer)
Other Columns : updateValue-(Integer)
Table Ball :
Columns : Primary-key primId(INteger)
My query so far :
update apple from apple a, ball b set updatevalue=1 where a.primId=b.primId;
I am getting a invalid query error, and I have no else clause yet. Any help would be nice.
Teradata doesn't support Outer Joins in an Update, but you could do it in FROM:
UPDATE apple
FROM
(
SELECT a.primId, CASE WHEN b.primId IS NULL THEN 0 ELSE 1 END AS x
FROM apple AS a
LEFT JOIN ball AS b
ON a.primId = b.primId
) AS src
SET upDateValue = src.x
WHERE apple.primId = src.primId
Probably more efficient:
UPDATE apple
SET upDateValue
= COALESCE((SELECT 1 FROM ball WHERE ball.primId = apple.primId),0);
But IMHO the best performing solution will be two Updates within a Multi Statement Request (MSR):
UPDATE apple
SET upDateValue = 1
WHERE EXISTS
( SELECT 1 FROM ball b WHERE apple.primId = b.primId)
;UPDATE apple
SET upDateValue = 0
WHERE updatevalue <> 1;
Please avoid implicit join syntax(comma separated) and use the appropriate syntax of joins:
UPDATE apple a
LEFT JOIN ball b
ON(a.primId=b.primId)
SET updatevalue= CASE WHEN b.primId is NULL THEN 0 ELSE 1 END ;
This query is a rather direct translation of your question:
update apple
set updatevalue = (case when exists (select 1 from ball b where apple.primId = b.primId)
then 1 else 0
end);
Can you please try this :
UPDATE a
FROM apple a
LEFT JOIN ball b ON a.id = b.id
SET a.updatevalue = CASE WHEN b.id > 0 THEN 1 ELSE 0 END
you are almost there. You just need to change alias.
update a
from apple a
left outer join ball b
on a.primId=b.primId
set updatevalue= (case when b.primId is not null then 1 else 0 end) ;
Try this.

SQL Server Return Null if exists

In SQL Server 2008 I am looking to create a query that will return a NULL in an aggregate if one exists, otherwise I'm looking for the maximum. This is a simplified example...
I have the following data:
CO Loc Term_Dt
1 A 7/15/2013
1 B
1 C 10/30/2000
2 A 8/10/2008
2 B 6/1/2015
2 C 4/30/2010
The result I'm looking for is:
CO Term_Dt
1 NULL
2 6/1/2015
because technically the Company is still open if at least one location has not yet been terminated.
Thanks
Just use aggregation and a case statement:
select co,
(case when count(term_dt) = count(*) then max(term_dt)
end) as term_dt
from table t
group by co;
count(<column>) counts the number of non-NULL values. If this doesn't match all the rows, then at least one is NULL. No else is needed for the case, because the default is NULL.
Generate a sub set of data with companies having null term dates and left join your super set to it. Any records in 2nd table which are not null you want to display as null so use a case statement.
This works because our outer table (A) returns
CO TERM_DT
1 7/15/2013
2 6/1/2015
But then our LEFT join on our inline view also adds B.Co...
CO TERM_DT B.CO
1 7/15/2013 1
2 6/1/2015 NULL
So you can see by saying we want to display NULL when B.CO is not null instead of the max(TERM_DT) will yield the desired results. This is accomplished using a case statement.
SELECT A.Co,
Case when B.CO is not null then Max(A.Term_DT) else NULL end as Term_DT
FROM tableName A
LEFT JOIN (SELECT Distinct CO from tableName where Term_dt is null) B
on A.Co = B.CO
GROUP BY CO

sql query with comparison but without removing without subquerying

my question is, is it possible to select certain rows in a table according to a comparison rule without removing anything from the result. To clarify what i want to to imagine following example.
i have a table with two values,
A | B | C
1 0 hey
1 1 there
2 1 this
3 0 is
3 1 a
4 0 test
now i want to select the rows that have a 0 in the B column, and an a in the C column without removing the results that don't have a 0 in column B but the same value in column A.
For that i could do a
select C from T where A in (select A from T where B = 0);
but isn't it possible to select all C values where column B contains a 0 and that match column A with those?
I'd gladly stand by if more information is needed since it is a quite fuzzy question, but SQL can be confusing sometimes.
Tough to tell without your example result set; but maybe something like this:
SELECT A, B, C
FROM myTable
WHERE (B = 0 AND C LIKE '%A%')
OR (B <> 0 AND B = A)
I think you just want an or condition:
select C
from MyTable
where b = 0 or A in (select A from T where B = 0)
Is this the version you want:
select C
from MyTable
where C = 'a' or A in (select A from T where B = 0)

Updating a COLUMN Based on a Field Value

All, I have four columns (A INT, B INT, C INT and D VARCHAR(1)) in a table TableName. I want to move the values from column C to either A or B based upon the value in D. So if D = 'A' I want to move the value in C to A. How can I achieve this?
DECLARE #Column COLUMN;
UPDATE TableName
SET (#Column =
(CASE
WHEN D = 'A' THEN A
WHEN D = 'B' THEN B
END)) = C;
Note. I understand the above is madness, but I am trying to express the problem as clearly as possible. I have also exhausted my search for answers. I am not new to SQL but this one has me stumped. Any help is as always, most appreciated.
Here,
UPDATE TableName
SET A = (CASE WHEN D = 'A' THEN C ELSE A END),
B = (CASE WHEN D = 'B' THEN C ELSE B END)
in this case, only 1 column will be changed since D has only one value at a time.
SQLFiddle Demo