how to compare one value against 2 values in Oracle - oracle

I want to compare a a value against 2 values without using OR or DECODE. The value I want to compare with two values is the one which I am getting as a return code of a function. If I use OR or DECODE then I have to call function twice and it gives performance hit. Currently I am coding as below
select *
from table1 t1, table2 t1
where t1.empid = t2.empid
and myfunction(t2.balance) = t1.total OR myfunction(t2.balance) = -1
Please suggest if there is a way to call function once and compare with 2 values.

To shorten your code you could use IN operator which acts like OR.
select *
from table1 t1
join table2 t1 on
t1.empid = t2.empid
and myfunction(t2.balance) in (t1.total, -1)
I've also replaced old-fashioned join syntax in where clause for JOIN keyword and you're advised to be using that in your future SQL journeys.
Good thing to know would be that even though you call the function twice, most modern databases would actually call it only once, so I wouldn't be that much concerned about it.

Related

Consecutive JOIN and aliases: order of execution

I am trying to use FULLTEXT search as a preliminary filter before fetching data from another table. Consecutive JOINs follow to further refine the query and to mix-and-match rows (in reality there are up to 6 JOINs of the main table).
The first "filter" returns the IDs of the rows that are useful, so after joining I have a subset to continue with. My issue is performance, however, and my lack of understanding of how the SQL query is executed in SQLite.
SELECT *
FROM mytbl AS t1
JOIN
(SELECT someid
FROM myftstbl
WHERE
myftstbl MATCH 'MATCHME') AS prior
ON
t1.someid = prior.someid
AND t1.othercol = 'somevalue'
JOIN mytbl AS t2
ON
t2.someid = prior.someid
/* Or is this faster? t2.someid = t1.someid */
My thought process for the query above is that first, we retrieve the matched IDs from the myftstbl table and use those to JOIN on the main table t1 to get a sub-selection. Then we again JOIN a duplicate of the main table as t2. The part that I am unsure of is which approach would be faster: using the IDs from the matches, or from t2?
In other words: when I refer to t1.someid inside the second JOIN, does that contain only the someids after the first JOIN (so only those at the intersection of prior and those for which t1.othercol = 'somevalue) OR does it contain all the original someids of the whole original table?
You can assume that all columns are indexed. In fact, when I use one or the other approach, I find with EXPLAIN QUERY PLAN that different indices are being used for each query. So there must be a difference between the two.
The query should be simplified to
SELECT *
FROM mytbl AS t1
JOIN myftstbl USING (someid) -- or ON t1.someid = myftstbl.someid
JOIN mytbl AS t2 USING (someid) -- or ON t1.someid = t2.someid
WHERE myftstbl.{???} MATCH 'MATCHME' -- replace {???} with correct column name
AND t1.othercol = 'somevalue'
PS. The query logic is not clear for me, so it is saved as-is.

How to apply a filter based on the current row

I'm building a calculated column that needs to sum all the rows in the same table that share a few common properties and have a greater date value.
I know I need to use calculate to break the filter context, but I'm not sure how to reference the row being calculated vs the table of the same name inside the calculate function. In Sql, this would be done as a self join with two different aliases for the same table, what is the DAX equivalent?
SQL pseudo code:
select
t1.Name
,sum(t2.a)
from table t1
inner join table t2 on t1.b = t2.b
and t1.c < t2.c
group by t1.name
DAX (how do I correctly reference outer row vs inner table?):
calculate(sum(table[a]),
filter(all(table), table[b] = table[b])
)
As suggested by #RADO, the earlier function does this (but you might not guess that from the name!).
Using my previous example, it looks like this:
calculate(sum(table[a]),
filter(all(table), table[b] = earlier(table[b]))
)

ListAgg() ORA-01002: fetch out of sequence

I'm running a query where I don't want to include the GL column in the GROUP BY clause because I don't want to show a different row for each variation of the GL field. But I need to evaluate the field in subsequent processing, comparing it to a list of dynamically generated GL numbers. The best way I can think to do this is with a function and I'm trying ListAgg(). First off, is there a better function to use to compare the GL from each row to a list? Also, I am able to get the results below using SQL Developer, but not when I try to output it on a web page where Oracle returns a ORA-01002 error. We are using 12c in both cases. Below is the query and the output. Without ListAgg(), I have to include the GL column in the SELECT and GROUP BY and it undesirably outputs a separate row for each GL.
SELECT h.h_spa_id as spa_id,
h.submit_dt,
h.oa_ap_date,
ListAgg(gl,',') within group (order by gl) "mygl"
WHERE h.next_apprv= 'approverID'
and h.table1_id = d.table2_id
and h.table1_id = table3_id
group by h.h_spa_id,h.submit_dt,h.oa_ap_date
order by h.h_spa_id
H_SPA_ID SUBMIT_DT OA_AP_DATE MYGL
1627005 1/25/2008 10:11:53 AM 1/25/2008 11:15:56 AM 52287,52287,52287,52287,52287,52287,52287,52287,52287,52287,52385,52385,52385,52385,52385,52385,52385,52385,52385,52385,52385,52385,52385,52385,52385
You can use CAST and COLLECT to group it into a collection:
SELECT h.h_spa_id as spa_id,
h.submit_dt,
h.oa_ap_date,
CAST( COLLECT( DISTINCT gl ) AS SYS.ODCINUMBERLIST ) AS mygl
FROM h
INNER JOIN
d
ON ( h.table1_id = d.table2_id )
WHERE h.next_apprv = 'approverID'
AND h.table1_id = table3_id
GROUP BY h.h_spa_id,
h.submit_dt,
h.oa_ap_date
ORDER BY h.h_spa_id
Then when you use it for subsequent processing the values will be in an array.

Oracle: MIN() Statement causes empty row returns

I'm having a small issue with sorting the data returned from a query, with the aim of getting the oldest updated value in dataset so that I can update only that record. Here's what I'm doing:
WHERE ROWNUM = 1 AND TABLE1.ID != V_IGNOREID
AND TABLE1.LASTREADTIME = (SELECT MIN(TABLE1.LASTREADTIME) FROM TABLE1)
ORDER BY TABLE1.LASTREADTIME DESC;
It makes no difference as to whether the ORDER BY statement is included or not. If I only use the ROWNUM and equality checks, I get data, but it alternates between only two rows, which is why I'm trying to use the LASTREADTIME data (so that I can modify more than these two rows). Anybody have any thoughts on this, or any suggestions as to how I can use the MIN function effectively?
Cheers
select * from (
-- your original select without rownum and with order by
)
WHERE ROWNUM = 1
EDIT some explanation
I think the order by clause is applied on the resultset after the where clause. So if the rownum = 1 is in the same select statement with the order by, then it will be applied first and the order by will order only 1 row, which will be the first row of the unordered resultset.

two oracle queries(one with AND , the other with OR) will return same result?

Is there any condition under which the two queries will yield different results?
select * from a,b,c where a.id = b.id(+) and a.id=c.id(+);
select * from a,b,c where a.id = b.id(+) or a.id=c.id(+);
I think in both cases, it will return the row if the id is in table a.
The second select fails with ORA-01719, Outer join operator (+) not allowed in operand of OR or IN.
Yet another reason to use ANSI JOIN syntax. You couldn't even conceive of this question if you were doing so.

Resources