here is the query, i want to use case statement with count function in oracle.
Select case when count(*) > 0 then 'doSomething' else 'doSomething'
from student where student_name='faizan ahmed' and student_father='ahmed' and UPPER(student_dob)=UPPER('01-FEB-19');
please help me out, using plsql code.
ORA-00905: missing keyword
00905. 00000 - "missing keyword"
For this purpose, use exists instead:
Select (case when exists (select 1
from student
where student_name = 'faizan ahmed' and
student_father = 'ahmed' and
upper(student_dob) = upper('01-FEB-19');
then 'doSomething'
else 'doSomethingElse'
end)
from dual;
EXISTS is usually more efficient than a count, because it can stop at the first matching row instead of aggregating the whole table.
You're missing an END for CASE:
SELECT CASE WHEN COUNT (*) > 0 THEN
'doSomething'
ELSE 'doSomething'
END --> This
FROM student
WHERE student_name = 'faizan ahmed'
AND student_father = 'ahmed'
AND UPPER (student_dob) = date '2019-02-01' -- No! UPPER ('01-FEB-19');
It is easier to spot if you format code you write.
Apart from that, STUDENT_DOB seems to be a date. If so, then don't compare it to a string (because '01-feb-19' IS a string) but to a date (date '2019-02-01' - it is a date literal, consists of the date keyword and yyyy-mm-dd value).
Also, it is strange that you used UPPER with that "date" string, but all your names are lowercase. Hm?
Related
I have a code like that:
select
tbl.person
,COUNT(distinct tbl.project)
,if (tbl.stage like '%SIGNED%') then sum(tbl.value) else '0' end if as test
from
my_table tbl
group by
1
And it returns me that error message:
SQL Error [42601]: ERROR: syntax error at or near "then"
I didn't got it. As I saw on documentation, the if statement syntax appears to be used correctly
IF is to be used in procedures, not in queries. Use a case expression instead:
select
tbl.person
,COUNT(distinct tbl.project)
,sum(case when tbl.stage like '%SIGNED%' then tbl.value else 0 end) as test
from
my_table tbl
group by
1
Notes:
tbl.stage is not part of the group by, so it should most probably be enclosed within the aggregate expression, not outside of it
all values returned by a case expression need to have the same datatype. Since sum(tbl.value) is numeric, the else part of the case should return 0 (number), not '0' (string).
In Postgres, I would recommend using filter:
select tbl.person, COUNT(distinct tbl.project)
sum(tbl.value) filter (where tbl.stage like '%SIGNED%') as test
from my_table tbl
group by 1;
if is control flow logic. When working with queries, you want to learn how to think more as sets. So the idea is to filter the rows and add up the values after filtering.
replace
if (tbl.stage like '%SIGNED%') then sum(tbl.value) else '0' end if as test
with
sum(case when tbl.stage like '%SIGNED%' then tbl.value end) as test
I stumbled in a case where requires to mask data using keyword from other reference table, illustrated below:
1:
Table A contains thousands of keyword and Table B contains 15 millions ++ row for each day processing..
How can I replace data in table B using keyword in table A in new column?
I tried to use join but join can only match when the string exactly the same
Here is my code
select
sourcetype, hourx,minutex,t1.adn,hostname,t1.appsid,t1.taskid,
product_id,
location,
smsIncoming,
case
when smsIncoming regexp keyword = true then keyword
else 'undef' end smsIncoming_replaced
from(
select ... from ...
)t1
left join
(select adn,keyword,type,mapping_param,mapping_param_json,appsid,taskid,is_api,charlentgh,wordcount,max(datex)
from ( select adn,keyword,type,mapping_param,mapping_param_json,appsid,taskid,is_api,charlentgh,wordcount,datex ,last_update,
max(last_update) over (partition by keyword) as last_modified
from sqm_stg.reflex_service_map ) as sub
where last_update = last_modified
group by adn,keyword,type,mapping_param,mapping_param_json,appsid,taskid,is_api,charlentgh,wordcount)t2
on t1.adn=t2.adn and t1.appsid=t2.appsid and t1.taskid=t2.taskid
Need advice :)
Thanks
Use instr(string str, string substr) function: Returns the position of the first occurrence of substr in str. Returns null if either of the arguments are null and returns 0 if substr could not be found in str. Be aware that this is not zero based. The first character in str has index 1.
case
when instr(smsIncoming,keyword) >0 then keyword
else 'undef'
end smsIncoming_replaced
I have the a query which looks similar to:
SELECT
s.cola, s.colb, t.colc, t.cold, u.cole, u.colf, u.colg, u.colh, u.coli, u.colj, u.colk, u.coll
FROM table1 s
INNER JOIN table2 t
ON s.colb = t.colc
INNER JOIN table3 u
ON u.colm = t.cold
WHERE cast(s.cola as date) between date '2017-11-06' and date '2017-11-10'
ORDER BY 3
I need to add a new column, called col_new, which is to be filled by either u.colm or u.coln. This column will have values from u.colm if that column starts with a number. Otherwise it will have values from u.coln. It is known that either u.coln or u.colm starts with a number, for each entry in table u.
I tried the following query to test if entries starting with a number can be identified or not:
SELECT CASE WHEN ISNUMERIC(SUBSTRING(LTRIM(colm), 1, 1)) = 1
THEN 'yes'
ELSE 'no'
END AS col_new
FROM table_u
It returned the error: Syntax error: expected something between '(' and the 'substring' keyword.
Kindly suggest a solution.
Edit:
Exact Error:
[Teradata Database] [3706] Syntax error: expected something between '(' and the 'substring' keyword.
Instead of isnumeric(), just do a comparison:
SELECT (CASE WHEN LEFT(LTRIM(colm), 1) BETWEEN '0' AND '9' THEN 'yes'
ELSE 'no'
END) AS col_new
FROM table_u;
LEFT() is a convenient shorthand for the first "n" characters of a string.
I am using DB2.
I have a query that returns a specific date in the following format: yyyy-mm-dd
SELECT DATE FROM ABC.DTS
Then I have devised another query that is supposed to return results that match the date returned above
SELECT COUNT(*)
FROM ABC.ATY
WHERE ID between 1 and 1000000000
AND MONTH(PRS) = MONTH(from DTS)
AND YEAR(PRS) = YEAR(from DTS)
AND CKPYE = ' '
;
I am getting an error when I run the second query that says: ILLEGAL SYMBOL "MONTH". SOME SYMBOLS THAT MIGHT BE LEGAL ARE: XMLELEMENTXMLPI. SQLCODE=-104, SQLSTATE=42601, DRIVER=4.19.56.
How can I return the results that match the date that is returned from the first query?
Is there another route/strategy I need to instead of the one I am currently trying?
Many thanks in advance.
See if this works for you:
SELECT COUNT(*)
FROM ABC.ATY
JOIN ABC.DTS ON MONTH(PRS) = MONTH(Date) AND YEAR(PRS) = YEAR(DATE)
WHERE ID between 1 and 1000000000
AND CKPYE = ' '
;
I have a database table in sql server 2008. My query is breaking for a reason that is unknown to me:
select release_id, url_id,tt_projectid, release_title, tech_area, current_state, dateadd(ss,last_built,'12/31/1969 20:00:00') as recent_Date,
autobuild_count, manualbuild_count, bcm_build_count, config_count, force_count, transition_only_count,
auto_integ_count,last_auto_integ,dateadd(ss,integ_complete_date,'12/31/1969 20:00:00') as integ_date
from tablename
where (auto_integ_count > 0
and MONTH(last_auto_integ) = '1'
and YEAR(last_auto_integ) = '2013')
and (release_id > 36000)
order by release_id desc
The above query works fine, but when I change the last line of the where close from 'and' to 'or' I get this conversion error:
Conversion failed when converting date and/or time from character string.
I'm puzzled as to why changing
'and (release_id > 36000)'
to
'or (release_id > 36000)'
would cause such an error
The reason is because last_auto_integ is being stored as a string rather than as a date. These lines in the where clause are not being executed with the and -- by happenstance -- because none occur when release_id > 360000.
From what I can see, there are no other places in the query where you might be converting a string to a date format.
You can identify these values using:
select last_auto_integ
from tablename
where isdate(last_auto_integ) = 0 and last_auto_integ is not null
You can fix the problem in the query by using case:
where month(case when isdate(last_auto_integ) = 1 then last_auto_integ end) = 1 and
year(case when isdate(last_auto_integ) = 1 then last_auto_integ end) = 2013
Or, you can just use substring() to extract the month and year from whatever date format you are using.
Because when you change AND to OR you are getting a lot more rows returned, and one of these other expressions is failing:
dateadd(ss,integ_complete_date,'12/31/1969 20:00:00')
MONTH(last_auto_integ)
YEAR(last_auto_integ)
With only AND, it doesn't need to evaluate the other expressions for rows whose release_id is <= 36000, so it's not encountering the strings that are causing the problem on date conversion.