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;
Related
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.
I have a query that where i need to put condition when case statement is true.I have tried like this but not geeting the correct value.
SELECT
name,count(CASE WHEN date_part('year',time_stamp) = 2016 THEN answ_count end) AS Year15
FROM
companies companies
where
(CASE when no_answer='f' then value_s IS not NULL or value_n IS not NULL end )
SELECT name,
count(CASE WHEN date_part('year',time_stamp) = 2016 THEN answ_count end) AS Year15
FROM
companies companies
where
CASE when no_answer='f' then value_s ELSE '1' end IS not NULL
OR CASE when no_answer='f' then value_n ELSE '1' end IS not NULL
CASE is an expression, you can only specify a value after the THEN part, not a condition like you did THEN value_s IS NOT NULL
You can't use case like that.
It's hard to figure out what you are trying to do, but test the result of the case, rather than putting the test inside the case, like this:
...
where CASE when no_answer='f' then value_s else value_n end
IS not NULL
Is there a shorter/better way to write this SQL statement?
Edit: the inner sql select statement is a placeholder, the actual statement is more complex.
SELECT
CASE WHEN
ISNULL((SELECT TOP 1 x FROM y), '') = ''
THEN 'No'
ELSE 'Yes'
END AS BooleanValue
It feels very kludgey because it compares the result of the select statement to null, then sets to an empty string if null; just to check if it is an empty string, and set it to what it actually needs to be: a 'yes' or 'no' string.
Here's one way to do it a bit cleaner.
SELECT
CASE WHEN
(SELECT TOP 1 x FROM y) IS NULL
THEN 'No'
ELSE 'Yes'
END AS BooleanValue
This removes one extra command and should have the same output. I just tested it in sql server 2012.
SELECT ISNULL((SELECT TOP 1 'Yes' FROM x), 'No') as Boolean
What is wrong in this Oracle query. I am trying a nested case expression, but cant find the reason why the query fails.
SELECT USER_ID,
ADVISER_FORENAME,
ADVISER_SURNAME,
AGENT_CODE,
'ABC#WIPRO.COM' AS EMAIL_ADDRESS,
SUBMISSION_DATE,
STATUS_CHANGED_DATE,
CASE
WHEN (
CASE
WHEN TO_CHAR(TRUNC(status_changed_date), 'DY') IN ('fri','sat','sun')
THEN ((TRUNC(STATUS_CHANGED_DATE) - (TRUNC(SUBMISSION_DATE)))>4)
ELSE ((TRUNC(STATUS_CHANGED_DATE) - (TRUNC(SUBMISSION_DATE)))>6)
END)
THEN 'B'
ELSE 'A'
END AS CATEGORY_CODE,
PLAN_REF,
PRODUCT_CODE,
CASE
WHEN ((TRUNC(STATUS_CHANGED_DATE) - TRUNC(SUBMISSION_DATE))>4)
THEN 'IPP'
ELSE 'ICP'
END AS TOUCHPOINT_NAME
FROM PIPELINE_PLAN
WHERE STATUS =26
AND PRODUCT_TYPE =1
AND (TRUNC(STATUS_CHANGED_DATE) - TRUNC(SUBMISSION_DATE))<=4;
When i try running, it gives below error:
ORA-00907: missing right parenthesis
00907. 00000 - "missing right parenthesis"
*Cause:
*Action:
Error at Line: 53 Column: 84
I checked for missing parenthesis but found none.
Please help.
Your nested CASEs are a bit too clever. In oracle, comparisons (yielding a 3-way boolean result TRUE/FALSE/UNKNOWN) and expressions (yield other kinds of values) are not interchangeable.
The result of a CASE expression is not a boolean, so it can't be used as the argument to a WHEN in an outer CASE, which requires a boolean.
You have a structure like this in your query:
CASE
WHEN (
CASE
WHEN x IN ('1','2','3')
THEN (y>4)
ELSE (y>6)
END)
THEN 'B'
ELSE 'A'
END
which needs to be rewritten as
CASE
WHEN y > (CASE WHEN x IN ('1','2','3') THEN 4 ELSE 6 END)
THEN 'B'
ELSE 'A'
END
or
CASE
WHEN (x IN ('1','2','3') AND y>4) OR (x NOT IN ('1','2','3') AND y>6)
THEN 'B'
ELSE 'A'
END
I don't see a problem with the parentheses, but you cannot use booleans as results in the then clause of the case. Perhaps this version will work:
SELECT USER_ID, ADVISER_FORENAME, ADVISER_SURNAME, AGENT_CODE,
'ABC#WIPRO.COM' AS EMAIL_ADDRESS, SUBMISSION_DATE, STATUS_CHANGED_DATE,
(CASE WHEN (TRUNC(STATUS_CHANGED_DATE) - TRUNC(SUBMISSION_DATE) > 4 and
TO_CHAR(TRUNC(status_changed_date), 'DY') IN ('fri','sat','sun')
) or
(TRUNC(STATUS_CHANGED_DATE) - TRUNC(SUBMISSION_DATE) > 6 and
TO_CHAR(TRUNC(status_changed_date), 'DY') not IN ('fri','sat','sun')
)
THEN 'B'
ELSE 'A'
END) AS CATEGORY_CODE,
PLAN_REF, PRODUCT_CODE,
(CASE WHEN TRUNC(STATUS_CHANGED_DATE) - TRUNC(SUBMISSION_DATE) > 4
THEN 'IPP'
ELSE 'ICP'
END) AS TOUCHPOINT_NAME
FROM PIPELINE_PLAN
WHERE STATUS = 26 AND PRODUCT_TYPE = 1 AND
TRUNC(STATUS_CHANGED_DATE) - TRUNC(SUBMISSION_DATE) <= 4;
Also, based on the where clause, the condition TRUNC(STATUS_CHANGED_DATE) - TRUNC(SUBMISSION_DATE) > 6 can never be true.
I'm almost certain you cannot do this within the context of the case statement, and I haven't been able to find any documentation about it, but is it possible to do the following:
SELECT CASE WHEN testValue > 2
THEN testValue(Without Repeating it) ELSE FailValue)
END
FROM Table
A better more thorough example:
Select CASE WHEN (Foo-stuff+bar) > 2
THEN Conditional statement without >2 Else "Fail"
END
FROM TABLE
I am looking for a way to create a select without repeating the conditional query.
EDIT: Due to a poor example on my part, and the lack of answers I was looking for:
testValue = (Table.A / Table.B) * Table.C Table.D
SELECT CASE WHEN testValue > 2
THEN testValue ELSE FailValue)
END
FROM Table
Like so
DECLARE #t INT=1
SELECT CASE
WHEN #t>0 THEN
CASE
WHEN #t=1 THEN 'one'
ELSE 'not one'
END
ELSE 'less than one'
END
EDIT:
After looking more at the question, I think the best option is to create a function that calculates the value. That way, if you end up having multiple places where the calculation needs done, you only have one point to maintain the logic.
The query can be written slightly simpler, like this:
DECLARE #T INT = 2
SELECT CASE
WHEN #T < 1 THEN 'less than one'
WHEN #T = 1 THEN 'one'
ELSE 'greater than one'
END T
I am looking for a way to create a select without repeating the conditional query.
I'm assuming that you don't want to repeat Foo-stuff+bar. You could put your calculation into a derived table:
SELECT CASE WHEN a.TestValue > 2 THEN a.TestValue ELSE 'Fail' END
FROM (SELECT (Foo-stuff+bar) AS TestValue FROM MyTable) AS a
A common table expression would work just as well:
WITH a AS (SELECT (Foo-stuff+bar) AS TestValue FROM MyTable)
SELECT CASE WHEN a.TestValue > 2 THEN a.TestValue ELSE 'Fail' END
FROM a
Also, each part of your switch should return the same datatype, so you may have to cast one or more cases.
We can use case statement Like this
select Name,EmailId,gender=case
when gender='M' then 'F'
when gender='F' then 'M'
end
from [dbo].[Employees]
WE can also it as follow.
select Name,EmailId,case gender
when 'M' then 'F'
when 'F' then 'M'
end
from [dbo].[Employees]