missing expression sql - sql

SELECT
create_date
,resolved_date
,item
,site
,status
,contact_time
,impact_label
FROM
mytable
WHERE
create_date BETWEEN to_date('2013/03/01','YYYY/MM/DD')
AND to_date('2015/08/06','YYYY/MM/DD')
and CASE item
WHEN in ('A','B') then '1'
WHEN in ('C') then '2'
WHEN in ('D') then '3'
ELSE null
END
GROUP BY
create_date
,resolved_date
,item
,site
,status
,contact_time
,impact_label
It says I have problem at the first when in, could someone please help?

If you want to use IN inside CASE you need different syntax :
...
case
when item in ('A','B') then '1'
when item in ('C') then '2'
when item in ('D') then '3'
--else null -- no need , ELSE NULL is default behaviour of CASE
end ...
Or change CASE to correct simple search syntax:
CASE item
WHEN 'A' THEN '1'
WHEN 'B' THEN '1'
WHEN 'C' THEN '2'
WHEN 'D' THEN '3'
ELSE NULL
END

CASE is an expression that returns a value. In your case, it is returning a string value.
The WHERE clause consists of boolean expressions. A string is not a boolean expression. I'm not sure what you intend, perhaps:
(CASE WHEN item in ('A','B') then '1'
WHEN item in ('C') then '2'
WHEN item in ('D') then '3'
ELSE null
END) IS NOT NULL
(Note the changes both to the CASE and the addition of IS NOT NULL.)
However, that is a silly expression, because it is equivalent to:
item in ('A', 'B', 'C', 'D')
Perhaps you want to add the CASE to the SELECT, giving it a proper column alias.

Related

How can I use CASE clause twice in SQL Query

I am trying to apply two conditions in one SQL Query.
(select DISTINCT (
CASE WHEN (
ABC.GemUserID = '99' )
OR ABC.GemUserID != '99'
THEN 'Yes'
ELSE 'No'
END)) AS AllWell
This gives me output as "Yes" where as the CASE is true only for 1 file like below :
Current Result:
99 , Yes
99 , Yes
99 , Yes
Expected Result:
99 , No
99 , No
99 , Yes
I am using the below query but the SQL Query Intellisence is identifying it as wrong.
Wrong Query:
(select DISTINCT (
CASE WHEN ( ABC.GEMUserID = '99' THEN 'Yes' else 'No'
CASE WHEN ( ABC.GEMUserID != '99' THEN 'No' else 'Yes'
END)) AS AllWell
After fixing the above Wrong Query:
(select DISTINCT
(CASE WHEN ABC.GemUserID = '99' THEN 'Yes' else 'No' END),
(CASE WHEN ABC.GemUserID != '99' THEN 'No' else 'Yes' END))
AS AllWell
But I am getting error:
Msg 116, Level 16, State 1, Line 17 Only one expression can be
specified in the select list when the subquery is not introduced with
EXISTS.
How to fix this?
select distinct is -- itself -- part of SQL syntax. The distinct is not a function. It should not be followed by parentheses. So, if I understand your question:
select DISTINCT
( CASE WHEN ABC.GEMUserID = '99' THEN 'Yes' else 'No' END),
( CASE WHEN ABC.GEMUserID <> '99' THEN 'No' else 'Yes' END) as AllWell
Do you plan on giving the first column a name?
select DISTINCT
CASE WHEN ABC.GEMUserID = '99' THEN 'Yes'
ELSE 'No' -- This is automatically When ABC.GEMUserID <> '99'
END AS AllWell
According to the error, your query is a subquery (probably behind IN?) in a larger SQL command. Therefore, it is not possible for such subquery to return more than one column.
So your first query, you've said:
CASE WHEN userID = 99 OR userID != 99
In other words:
CASE WHEN 1=1
This is why it returns yes for everything (not sure what the difference between your current and expected result should be considering that the userID is 99 for all rows).
For your erroneous query, seems you're returning that select in the middle of another select (since you alias it at the end). Due to that, you cannot return more than one column in your nested select. You do not need the second CASE statement, simply change your query to:
(select DISTINCT
CASE WHEN ABC.GemUserID = '99' THEN 'Yes' Else 'No' End) AS AllWell
Assuming that you hold the missing pieces to the query such as the FROM.

SQL Statement Case When With Select * Syntax

I have a SQL Statement as below:
SELECT * ,(CASE WHEN DRV_ID IS NOT NULL THEN CASE WHEN (DRV_ID)<'100' thenconcat(rental_type_c,'*')ENDELSE '9' end) as testing99FROM dmtb_driver;
just wondering why it will prompt me error "From key world no found." If i change my statement in such way:
SELECT DRV_Name,DRV_ID ,(CASE WHEN DRV_ID IS NOT NULL THEN CASE WHEN (DRV_ID)<'100' thenconcat(rental_type_c,'*')ENDELSE '9' end) as testing99FROM dmtb_driver;
it working perfectly. Just curious isn't i need to type out all the field name 1 by 1 instead of using '*'?
You need to alias your table.
SELECT
d.*,
CASE WHEN DRV_ID IS NOT NULL THEN
CASE WHEN (DRV_ID)<'100' THEN
CONCAT(rental_type_c,'*')
END
ELSE
'9'
END as testing99
FROM
dmtb_driver d;
I think your logic could use some simplification too.
Ii think you missed space before from and concat, also I don't think so two case statement is required.
SELECT * ,(CASE WHEN DRV_ID IS NOT NULL AND (DRV_ID)<'100' then concat(rental_type_c,'*') ELSE '9' end) as testing99
FROM dmtb_driver;

PostgreSQL, SELECT CASE COALESCE

I need to 'name' categories: mycat is a text column with possible values '0' to '4'.
SELECT CASE mycat
WHEN '0' THEN 'ZERO'
WHEN '1' THEN 'ONE'
WHEN '2' THEN 'TWO'
WHEN '3' THEN 'THREE'
WHEN '4' THEN 'OTHER'
END AS my_category,
COALESCE(SUM(col1), 0),
COALESCE(SUM(col2), 0),
COALESCE(SUM(col3), 0)
FROM mytable
GROUP BY mycat
ORDER BY mycat;
That works OK, but I have some an error in my program which very rarely writes null (or '' as I can see in pgAdmin). In such cases I have to treat that '' the same as '0'.
But I can't get that!
I try like this:
SELECT CASE COALESCE(mycat, '0')
But this doesn't solve it at all.
How to get that '' will be summed and grouped together with '0' category?
PostgreSQL 9.3, Windows.
you need to use COALESCE in the group by and order by also similar to how you planned to change the case expression, but postgres is giving error , so another option is to wrap your statement in a subquery and do group by
SELECT my_category,
COALESCE(SUM(col1), 0),
COALESCE(SUM(col2), 0),
COALESCE(SUM(col3), 0)
FROM
(
SELECT CASE coalesce(mycat ,'0')
WHEN '0' THEN 'ZERO'
WHEN '1' THEN 'ONE'
WHEN '2' THEN 'TWO'
WHEN '3' THEN 'THREE'
WHEN '4' THEN 'OTHER'
WHEN '' THEN 'ZERO'
END AS my_category,
col1,
col2,
col3
FROM mytable
) T
GROUP BY my_category
ORDER BY my_category
You can have this without subquery. You could repeat the expression in the GROUP BY and ORDER BY clause. But it's much simpler to use the ordinal number of the output column instead:
SELECT CASE mycat
WHEN '1' THEN 'ONE'
WHEN '2' THEN 'TWO'
WHEN '3' THEN 'THREE'
WHEN '4' THEN 'OTHER'
ELSE 'ZERO' -- catches all other values
END AS my_category
, COALESCE(SUM(col1), 0) AS sum1
, COALESCE(SUM(col2), 0) AS sum2
, COALESCE(SUM(col3), 0) AS sum3
FROM mytable
GROUP BY 1
ORDER BY 1;
I chose the simplest and fastest code. The ELSE branch catches 0, '' and NULL - or any other value not yet filtered! But you say there are no others.
A couple of rants:
mycat is 'text' column with possible values '0' to '4'.
This is wrong in two ways.
Obviously, there are empty strings ('') and / or NULL values, too.
With that fixed, integer, smallint, of "char" with a CHECK cnstraint would be sensible choices for the data type. (Maybe even enum.) text, not so much.
To find out your actual range of values:
SELECT mycat, count(*) AS ct
FROM mytable
GROUP BY 1
ORDER BY 2 DESC;
If your client obfuscates NULL and empty values, test with mycat IS NULL. You need to know and understand the difference in many situations.
This orders by the resulting text in my_category like: ONE, OTHER, THREE, TWO, ZERO? I doubt you want that.

Case when using IN clause

I need a query to return a certain result for a certain column depending on what value the column that is being run the select statement against has.
if the column is one of the following : I, D, U then I want to return Y
if the column is one of the following : N, E, D then I want to return N
else : I want to return NULL
I wrote the following statement but it doesn't work.
SELECT HIERARCHY_TYPE,
NODE_ID,
NODE_TYPE,
NODE_NAME,
NODE_LEVEL,
PREFERRED_ALIAS,
PARENT_NODE_ID,
CASE ACTIVE_INDICATOR
WHEN ('I' or 'U' or 'Y') THEN 'Y'
WHEN ('D' or 'E' or 'N') THEN 'N'
ELSE NULL
END
FROM MV_HIERARCHY MV;
Is there a way to rewrite it without using multiple OR clauses for each possible value?
I'd use the IN operator:
SELECT HIERARCHY_TYPE,
NODE_ID,
NODE_TYPE,
NODE_NAME,
NODE_LEVEL,
PREFERRED_ALIAS,
PARENT_NODE_ID,
CASE
WHEN ACTIVE_INDICATOR IN ('I','U','Y') THEN 'Y'
WHEN ACTIVE_INDICATOR IN ('D','E','N') THEN 'N'
ELSE NULL
END AS ACTIVE_INDICATOR
FROM MV_HIERARCHY MV;
CASE
WHEN ACTIVE_INDICATOR IN ('I','U','Y') THEN 'Y'
WHEN ACTIVE_INDICATOR IN ('D', 'E', 'N') THEN 'N'
ELSE NULL -- useless, but for readbility
END as ACTIVE_INDICATOR
You've got to repeat ACTIVE_INDICATOR, cause I don't think (may be wrong) you can use the syntax
CASE <field>
WHEN IN()
but you can use
CASE
WHEN <field> IN()
decode(mod(nullif(instr('IDUEYN',active_indicator),0),2),0,'N',1,'Y')

SQL case with different fields to check from same table

I have the following problem:
I have a select statement that includes a case part. Up til there it is easy the problem is that the case includes a check against another field in the same table.
select h.id,
case h.value
when 'P' then 'test'
when '' then 'failed'
when 'D' then 'passed'
else null end
as info,
b.text,
case h.diag
when h.value = '' [or 'failed' not sure tried both and didn't work]
else h.diag end
as diag1, h.date from valuetab h, texttab b where h.id=b.id
I want to have h.diag only to show values when h.value is not failed.
I always get the mistake that the = should be concat.. but that doesn't make sense in my eyes.
Any ideas??
Thats for all your help.
You can also write a case statement with your expression in a different place i.e.
SELECT CASE WHEN X = 1 THEN 'Y' WHEN X = 2 THEN 'Z'
I think what you want to do is something more like this:
SELECT CASE WHEN h.value = '' THEN h.diag end
Use the other form of case statement, which doesn't specify a column you want to look at:
select case
when column1 = 2 then 'Foo'
when other_column = 'blah' then 'Bar'
end
from table
The problem with using case column1 when... is that it implicitly compares column1 to each when clause. You can't then include a comparison to some other column in it.
You are missing a THEN portion of the WHEN clause, and specifying a condition where you could specify a value:
case h.value
when '' THEN NULL
else h.diag end
Ok got it....
after the 2nd case the "h.diag" must be removed....
so it is
case
when h.value = '' then null
else h.diag end
as diag1,