Oracle: Generate WHERE clause from resultset ids? - sql

In Toad for Oracle 12:
I've selected rows from a table using a complex query.
I want to select those same rows in the system's application's WHERE clause.
However, the application doesn't support full SELECT statements, only WHERE clauses. And occasionally, it doesn't allow complex queries as subqueries in the WHERE clause, which is the case for my current query.
As an alternative, is there a way to get Toad to generate a WHERE clause from the resultset's IDs?
I would copy/paste the WHERE clause into the application. This is a common task, so it would be nice if there was something easy like a button in Toad that would do this.
Example:
Use the resultset...
ID VAL
1 A
2 B
3 C
...to generate a WHERE clause:
where id in (1,2,3)
Or if the IDs are text:
where id in ('1','2','3')

You can apply listagg function to your output and concatenate output IDs to list:
with a as (
<your_current_query>
)
select 'where id in ('
|| listagg(id, ',') within group(order by id)
|| ')' as where_clause
from a

You can use a subquery in the where clause:
where id in (select id
from . . . -- your complicated query here
)

Here is Maximo-specific version of #astentx's query:
with a as (
<<your query>>
)
select 'wonum in ('''
|| listagg(wonum, ''', ''') within group(order by wonum)
|| ''')' as where_clause
from a
And this query uses the syntax for Maximo's filtering fields:
with a as (
<<your query>>
)
select '='
|| listagg(attributename, ', =') within group(order by attributename)
|| '' as where_clause
from a
Related:
LISTAGG in Oracle to return distinct values
SQL Server: Generate WHERE clause from resultset ids?

Related

Oracle SQL query CONCAT, GROUP BY

I have this table
I wanted to sort by POS ASC and also aggregate the two columns X,Y so that my ID becomes Unique. So the result should be like this.
I tried for many hours and I could aggregate the column X,Y. But when I'm doing a GROUP BY ID and then WMCONCAT(X,Y) I couldn't sort it by the POS col....
Any help maybe..? Thanks.
Instead of using WM_CONCAT, try using the LISTAGG aggregate function: it allows you to specify an ordering of your choice:
SELECT ID,
LISTAGG('(' || X || ', ' || Y || ')', ', ') WITHIN GROUP (ORDER BY POS) AS XY
FROM tab
GROUP BY ID
Check the demo here.

oracle equivalent of hive collect_set function

What is the oracle equivalent of below hive query?
select appn_id,collect_set(CONCAT(upper(TRIM(dcsn_type_nm)),':',upper(TRIM(dcsn_outcm_nm))))
FROM <left join between few tables>
group by appn_id
EDIT:
Updated based on Gordon's answer -
select appn_id,listagg(upper(trim(dcsn_type_nm)) || ':' || upper(trim(dcsn_outcm_nm))) within group (order by null) set_type_outcm_nm
FROM <left join between few tables>
group by appn_id
I think the way this is being used, the equivalent is listagg():
select listagg(upper(trim(dcsn_type_nm)) || ':' || upper(trim(dcsn_outcm_nm))) with group (order by null)
listagg() is an aggregation function, so it combine data from multiple rows.

String formatting using LISTAGG in Oracle. Escaping single quote ` ' `

How can I format the output of listagg in Oracle to produce output(every field in the single quote) as 'student1', 'student2', 'student3'.
I have gone through documentation and other question on listagg but can't find much.
SQL Query to concatenate column values from multiple rows in Oracle
SELECT LISTAGG(student_name,',') WITHIN GROUP (ORDER BY student_name)
from students
Thanks
You could use:
SELECT LISTAGG('''' || student_name || '''',',')
WITHIN GROUP (ORDER BY student_name)
FROM students;
or using ENQUOTE_LITERAL function:
SELECT LISTAGG(DBMS_ASSERT.ENQUOTE_LITERAL(student_name),',')
WITHIN GROUP (ORDER BY student_name) AS r
FROM students;
DBFiddle Demo
This should do the job. You need to escape the ' in the query.
SELECT LISTAGG('''' || student_name || '''',', ') WITHIN GROUP (ORDER BY student_name)
FROM students

Group query rows result in one result

I need make a query that I get the result and put in one line separated per comma.
For example, I have this query:
SELECT
SIGLA
FROM
LANGUAGES
This query return the result below:
SIGLA
ESP
EN
BRA
I need to get this result in one single line that way:
SIGLA
ESP,EN,BRA
Can anyone help me?
Thank you!
SELECT LISTAGG(SIGLA, ', ') WITHIN GROUP (ORDER BY SIGLA) " As "S_List" FROM LANGUAGES
Should be the listagg sequence you are needing
try
SELECT LISTAGG( SIGLA, ',' ) within group (order by SIGLA) as NewSigla FROM LANGUAGES
If you want to get the values grouped together in the order that Oracle produces the rows then:
SELECT LISTAGG( SIGLA, ',' ) WITHIN GROUP ( ORDER BY ROWNUM ) AS SIGLA
FROM LANGUAGES;
If you want alphabetical ordering then replace ORDER BY ROWNUM with ORDER BY SIGLA (or, curiously, ORDER BY NULL).

ORA - 02287 sequence number not allowed here

I have a table name test which has three columns id, m_id and s_m_id
I am executing below query
select id,test.nextval listagg(m_id || ',' || s_m_id, ';') within group (order by m_id) as merge_ids
from test t group by id
than I am getting error ORA - 02287 sequence number not allowed here.
You're trying to do too many things in one go. Create a subquery for the grouping and add the sequence numbers later:
select id, test.nextval, merge_ids
from (
select id, listagg(m_id || ',' || s_m_id, ';') within group (order by m_id) as merge_ids
from test t
group by id
)