Using Case Statement when there is no entries in table - left-join

I want to fulfill below condition for a field in the left outer join
If the field1 = Open and field2 ne 3 then I need 'OPEN'
else 'CLOSE'
and there is no entry in the table then i need 'NEUTRAL'
this is the coding written but it returns either OPEN and CLOSE but not neutral if table3 doesnt have entry based on join condition which is given below.
Kindly help.
SELECT coalesce ( (
case
when tab3.field1 = 'Open' and tab3.field2 <> '3' then 'OPEN'
else 'CLOSE'
end
),'NEUTRAL') as STATUS
FROM table1 AS tab1
LEFT OUTER JOIN table3 AS tab3
on tab1.material = tab3.material

You wanting something like this:
case
when tab3.material is null then 'NEUTRAL'
when tab3.field1 = 'Open' and tab3.field2 <> '3' then 'OPEN'
else 'CLOSE'
end as Status

Related

Need help rewriting a T-SQL query for results to get displayed in a stacked bar

I have come up with the following query which gives rise to the below result.
SELECT TOP (100) PERCENT s.UserfName AS ASSIGNEE, e.status, COUNT(*) AS [TOTAL COUNT], CASE WHEN e.status = "Open" THEN 'OPEN' END AS
FROM dbo.t_helpdesktickets AS e INNER JOIN
dbo.t_assetusers AS s ON e.currentlyat = s.UserID
WHERE (e.status IN ('open', 'closed'))
GROUP BY s.UserfName, e.status
How will I rewrite my query to return results as below
Use conditional aggregation:
SELECT
s.UserfName AS ASSIGNEE,
COUNT(CASE WHEN e.status = 'open' THEN 1 END) AS "OPEN",
COUNT(CASE WHEN e.status = 'closed' THEN 1 END) AS "CLOSED"
FROM dbo.t_assetusers AS s
LEFT JOIN dbo.t_helpdesktickets AS e
ON e.currentlyat = s.UserID
GROUP BY
s.UserfName;
Note that I switched to using a left join here, in case certain users might not have any records corresponding to either of the two statuses. Using this approach, the open/closed counts would then show up as zero.

IBM DB2 Case in Where clause?

I have a SQL Statement where I have to check on conditions on rows since it has duplicates.
Condition1 is to watch if the value from a column in tb1 is between two values in a two columns of tb2 but only on rows which has a 'Y' inside.
I have something like this:
SELECT DISTINCT
tb1.columnA, tb1.columnB, tb2.columnA, tb2.columnB,
CASE
WHEN tb1.col1 = 'Y'
THEN CONCAT(tb1.col2,tb2.col2)
ELSE 'no changes'
END as conditionColumn
FROM
tb1 left outer join tb2 on tb1.columnX tb2.columnX
WHERE
CASE
WHEN tb1.col1 = 'Y'
THEN Condition1
ELSE Condition2
END
From what I saw before, a case statement is not allowed in the where clause?
How can I handle this then?
EDIT
When I have a 'Y' in tb1.col1
the where clause should output me an ID specified in tb1.columnA and only the rows which values from tb1.ColumnB is between tb2.columnA and tb2.columnB. If it doesn't have a Y inside then it should simply just give me the ID
so I used this, but it gives me an syntax error:
SELECT DISTINCT
tb1.columnA, tb1.columnB, tb2.columnA, tb2.columnB,
CASE
WHEN tb1.col1 = 'Y'
THEN CONCAT(tb1.col2,tb2.col2)
ELSE 'no changes'
END as conditionColumn
FROM
tb1 left outer join tb2 on tb1.columnX tb2.columnX
WHERE
CASE
WHEN tb1.col1 = 'Y'
THEN tb1.columnA = 'MyID'
AND tb1.columnB BETWEEN tb2.columnA and tb2.columnB
ELSE tb1.columnA = 'MyID'
END
If you want to do this
WHERE
CASE
WHEN tb1.col1 = 'Y'
THEN Condition1
ELSE Condition2
END
then, likely what you are wanting is this
WHERE
( tb1.col1 = 'Y' AND Condition1 )
OR ( tb1.col1 <> 'Y' AND Condition2 )
You seem to be confused about the purpose of a WHERE clause...
All a WHERE clause does is decide rather or not a given row is returned. It can't change the data being returned.
To change the data, you need a CASE in the columns selected...
something like so...
SELECT DISTINCT
CASE
WHEN tb1.col1 = 'Y'
AND tb1.columnB BETWEEN tb2.columnA and tb2.columnB
then 'MyID'
ELSE tb1.columnA
END as newColumnA,
tb1.columnB, tb2.columnA, tb2.columnB,
CASE
WHEN tb1.col1 = 'Y'
THEN CONCAT(tb1.col2,tb2.col2)
ELSE 'no changes'
END as conditionColumn
FROM
tb1 left outer join tb2 on tb1.columnX tb2.columnX

How to optimize CASE statement in SELECT query which has same check condition for three different columns

I have below select statement to fetch the data having CASE statement.
SELECT PK_ID
,MGR_ID
,EMP_ID
,CASE
WHEN msts.MGR_ID is null AND msts.EMP_ID is not null THEN
(SELECT 'A' from dual)
ELSE
(SELECT 'B' from dual)
END FIRST_COL
,CASE
WHEN msts.MGR_ID is null AND msts.EMP_ID is not null THEN
(SELECT 'P' from dual)
ELSE
(SELECT 'Q' from dual)
END SECOND_COL
,CASE
WHEN msts.MGR_ID is null AND msts.EMP_ID is not null THEN
(SELECT 'X' from dual)
ELIE
(SELECT 'Y' from dual)
END THIRID_COL
from m_sel_tabs msts
here, As i know that we can check condition with mulitiple columns but the result will be only one for CASE statement.
so my question is here that as my check CASE statement is the same for all three columns (FIRST_COL, SECOND_COL, THIRID_COL), hence is there any other way to write this query in an optimized way.
Thanks in advance.
Maybe I'm wrong, but - when saying that you have "the same 3 CASE statements" - well, you don't. Those are 3 different CASE statements. An expression is the same, yes, but you're selecting 3 different columns, with different results (A, B, P, Q, X, Y - in your example).
You could create a function which would "hide" code you currently use and make the SELECT prettier, such as
select
pk_id, ...,
f_col(1, MGR_ID, EMP_ID) first_col,
f_col(2, MGR_ID, EMP_ID) second_col,
f_col(3, MGR_ID, EMP_ID) third_col
from m_sel_tabs
but - at the end - it would be just the same (or, possibly somewhat worse because of context switching) as you'd have to put all that code somewhere (into the function, right?).

CTE with CASE statement

I'm trying a query like this;
WITH CTE AS
(
SELECT
ID, NAME, DEPT,
CASE WHEN Fixed = 1 THEN 'Yes' ELSE '' END,
CASE WHEN NotFixed = 1 THEN 'Yes' ELSE '' END,
)
When I try to execute this query, I'm getting error like
No Column was specified for column 4 of CTE
What's wrong with the query?
Read the error message: You forgot to give your calculated columns a name. This goes for both column 4 and column 5. So change your query to something like this:
WITH CTE AS
(
SELECT
ID, NAME, DEPT,
CASE WHEN Fixed = 1 THEN 'Yes' ELSE '' END AS [Expr1],
CASE WHEN NotFixed = 1 THEN 'Yes' ELSE '' END AS [Expr2],
. . .
)
Feel free to change [Expr1] and [Expr2] to something more meaningful...

show bool if row exists or doesn't

I have the table [Contracts] with columns [id], [number]. And I also have some numbers in the string format: '12342', '23252', '1256532'. I want to get the output something like this.
1535325 | no
12342 | yes
23252 | yes
434574 | no
1256532 | yes
of course I can write this and get the rows i have, but how can I determine if the row doesn't exist and get the output above:
SELECT [Id]
,[Number]
FROM [Contracts]
where [Number] in
('12342', '23252', '1256532')
You can put values into temporary table or a table variable and do left join:
declare #d table (Number varchar(10))
insert into #d values ('12342'), ('23252'), ('1256532'), ('xxxx') -- last one is not in Contracts
SELECT c.[Id], c.[Number], case when d.Number is NULL then 'no' else 'yes' end [This Number from C is in D also]
FROM [Contracts] c
left join #d d on d.Number = c.Number
for "opposite" use right join
SELECT c.[Id], d.[Number], case when c.Number is NULL then 'no' else 'yes' end [This Number from D is in C also]
FROM [Contracts] c
right join #d d on d.Number = c.Number