in Education by
I am trying to make a a query against a table and am kind of stuck in constructing the WHERE clause. Table has a column "SUBCLASS" that has a value like "UN" or "U*". "U*" implies a matching of "U" and any other character(e.g. UB, UC, ...). Table looks like: ID ISC SUBCLASS --- ---- --------- 1 ABC UN 2 DEF UN 3 DEF U* Given a string UC12341001000012 or UN12341001000012, how would I construct the WHERE clause. I tried: SELECT * FROM MYTABLE WHERE x AND y AND (SUBCLASS='UC' OR SUBSTR(SUBCLASS, 1, 1) = SUBSTR('UC',1,1)) but it returns all rows. (I am using 'UC' here but actually it is a parameter passed to a stored procedure). So, given UC12341001000012, I should get the third record, given UN12341001000012 I should get first two records. JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
The (slightly) tricky bit is not including the wildcard U* row when there are exact matches. There are various approaches with subqueries and unions etc.; this one uses an inline view to flag and count the exact and wildcard matches, and then filters that inline view: select id, isc, subclass, exact, wild from ( select id, isc, subclass, case when subclass = substr(:str, 1, 2) then 'Y' end as exact, case when subclass = substr(:str, 1, 1) || '*' then 'Y' end as wild, count(case when subclass = substr(:str, 1, 2) then subclass end) over () as exact_cnt from mytable where subclass like substr(:str, 1, 1) || '%' -- optional ) where exact = 'Y' or (wild = 'Y' and exact_cnt = 0) I've used a :str bind variable in place of your shorter literal, partly because I think it's clearer, but also because with just UC it isn't obvious why I've used more substr() calls than you had; since with your full longer values you only want to look at the first two anyway. You can change it around a bit to not repeat the case expression (with another layer of inline view/CTE that you then count from), or change the inner filter to explicitly look for the same things the case expression is checking (or leave it out - depends on volume, indexes...), etc., but hopefully gives you the idea. With a CTE to provide your sample subclass data: var str varchar2(20); exec :str := 'UC12341001000012'; -- CTE for sample data with mytable (id, isc, subclass) as ( select 1, 'ABC', 'UN' from dual union all select 2, 'DEF', 'UN' from dual union all select 3, 'DEF', 'U*' from dual ) -- actual query select id, isc, subclass from ( select id, isc, subclass, case when subclass = substr(:str, 1, 2) then 'Y' end as exact, case when subclass = substr(:str, 1, 1) || '*' then 'Y' end as wild, count(case when subclass = substr(:str, 1, 2) then subclass end) over () as exact_cnt from mytable where subclass like substr(:str, 1, 1) || '%' -- optional ) where exact = 'Y' or (wild = 'Y' and exact_cnt = 0); ID ISC SU ---------- --- -- 3 DEF U* exec :str := 'UN12341001000012'; ID ISC SU ---------- --- -- 1 ABC UN 2 DEF UN in case of multiple rows being returned If you only want one of the exact-match rows, you could add a row_number() call in the inline view - with a suitable order by for however you want to split ties - and then add that to the outer filter: select id, isc, subclass from ( select id, isc, subclass, case when subclass = substr(:str, 1, 2) then 'Y' end as exact, case when subclass = substr(:str, 1, 1) || '*' then 'Y' end as wild, count(case when subclass = substr(:str, 1, 2) then subclass end) over () as exact_cnt, row_number() over (partition by subclass order by isc) as rn from mytable where subclass like substr(:str, 1, 1) || '%' -- optional ) where (exact = 'Y' or (wild = 'Y' and exact_cnt = 0)) and rn =1 ... or you can initially select all three rows but order them so that the wildcard one comes last, before applying a rownum filter: select id, isc, subclass from ( select id, isc, subclass from mytable where subclass = substr(:str, 1, 2) or subclass = substr(:str, 1, 1) || '*' order by case when subclass like '_*' then 2 else 1 end, isc -- or however you want to split ties ) where rownum = 1

Related questions

0 votes
    I have not used access before but I have to convert an access query into SQL so I can write a report in crystal ... like this in SQL? Select the correct answer from above options...
asked Feb 8, 2022 in Education by JackTerrance
0 votes
    I have not used access before but I have to convert an access query into SQL so I can write a report in crystal ... like this in SQL? Select the correct answer from above options...
asked Feb 5, 2022 in Education by JackTerrance
0 votes
    I am using oracle 10g. I have a database user TDM_DD which executes a procedure in which it creates ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 3, 2022 in Education by JackTerrance
0 votes
    I am getting empty row on following sql SELECT * FROM flyers WHERE fId='6' AND 'userId'='400' ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 8, 2022 in Education by JackTerrance
0 votes
    I'm new in LINQ so I hope this isn't a stupid question: I have a table with a lot of ... questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 2, 2022 in Education by JackTerrance
0 votes
    I'm new in LINQ so I hope this isn't a stupid question: I have a table with a lot of ... questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 2, 2022 in Education by JackTerrance
0 votes
    I'm new in LINQ so I hope this isn't a stupid question: I have a table with a lot of ... questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 2, 2022 in Education by JackTerrance
0 votes
    I'm new in LINQ so I hope this isn't a stupid question: I have a table with a lot of ... questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 2, 2022 in Education by JackTerrance
0 votes
    Is there any way to convert following SQL statement into LINQ? select ve.EntityID , fin1.FinanceStat as ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 23, 2022 in Education by JackTerrance
0 votes
    ANSI-standard SQL allows the use of special operators in conjunction with the WHERE clause. A special ... Schemes topic in chapter Concurrency Control of Database Management...
asked Oct 10, 2021 in Education by JackTerrance
0 votes
    I have a dilemma, I'm using Java and Oracle and trying to keep queries on PL/SQL side. Everything ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 22, 2022 in Education by JackTerrance
0 votes
    I have a dilemma, I'm using Java and Oracle and trying to keep queries on PL/SQL side. Everything ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 21, 2022 in Education by JackTerrance
0 votes
    I tried to create following function: CREATE OR REPLACE function CATEGORYTEST(n in number) return VARCHAR(200 ... for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 18, 2022 in Education by JackTerrance
0 votes
    I have function that receives department name and an aggregation operation (average, maximum, minimum) and applies ... for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 17, 2022 in Education by JackTerrance
0 votes
    I tried to create following function: CREATE OR REPLACE function CATEGORYTEST(n in number) return VARCHAR(200 ... for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 17, 2022 in Education by JackTerrance
...