PostgreSQL - migrate a query with 'start with' and 'connect by' in oracle - oracle

I have the following query in oracle. I want to convert it to PostgreSQL form. Could someone help me out in this,
SELECT user_id, user_name, reports_to, position
FROM pr_operators
START WITH reports_to = 'dpercival'
CONNECT BY PRIOR user_id = reports_to;

A something like this should work for you (SQL Fiddle):
WITH RECURSIVE q AS (
SELECT po.user_id,po.user_name,po.reports_to,po.position
FROM pr_operators po
WHERE po.reports_to = 'dpercival'
UNION ALL
SELECT po.user_id,po.user_name,po.reports_to,po.position
FROM pr_operators po
JOIN q ON q.user_id=po.reports_to
)
SELECT * FROM q;
You can read more on recursive CTE's in the docs.
Note: your design looks strange -- reports_to contains string literals, yet it is being comapred with user_id which typicaly is of type integer.

Related

How to retrieve data from 3 tables using sub query oracle SQL

I want to retrieve users name and there responsibility_key where there end_date is null and i want to convert it to (sysdate+1) using nvl but i am only able to retrieve the responsibility_key not the name please help.
The error in the image says "column ambiguously defined". Take a close look. Your last END_DATE could refer to either the u alias or the table from the subquery. Change it to match the rest of your subquery (FIND_USER_GROUPS_DIRECT.END_DATE)
EDIT
Your query is
select u.USER_NAME, d.responsibility_key from FND_USER u,FND_RESPONSIBILITY_VL d
where responsibility_id in(
select responsibility_id from
FND_USER_RESP_GROUPS_DIRECT WHERE END_USER_RESP_GROUPS_DIRECT.END_DATE=nvl(END_DATE,sysdate+1)) and
u.END_DATE=nvl(END_DATE,SYSDATE + 1)
;
The query isn't formatted, which makes it hard to read.
Not all columns are qualified with table name (or aliases), as mentioned in the comments.
The query currently uses an implicit join.
The query is impossible to understand without seeing the table definitions (desc [table_name]).
For points 1 and 2, a properly formatted query will look something like
select u.user_name, d.responsibility_key
from
fnd_user u,
fnd_responsibility_vl d
where
d.responsibility_id in (
select urgd.responsibility_id
from
fnd_user_resp_groups_direct urgd
where
urgd.end_date = nvl(u.end_date, sysdate+1)
) and
u.end_date = nvl(urgd.end_date, sysdate + 1)
;
This makes it easier to read and in addition to this, you can see that without table definitions I guessed (see point 4) as to which tables the end_date column belongs in your query. If I had to guess, so does Oracle. That means you have an ambiguity problem. To fix it, take a close look at the end_date column as it appears in your original query and where you do not prefix it with anything, you need to prefix it with the appropriate alias (after you have aliased all your tables).
For point 3, you can write your query more clearly with an explicit join and by using aliases for all columns. As for the explicit join I have no idea what your tables look like but one possibility is something like
select u.user_name, d.responsibility_key
from fnd_user u
join fnd_responsibility_vl d
on u.id = d.user_id
where
d.responsibility_id in (
select responsibility_id
from fnd_user_resp_groups_direct urgd
where
urgd.end_date = nvl(u.end_date, sysdate+1)
) and
u.end_date = nvl(urgd.end_date, sysdate+1)
;
If you follow these points you will get to the root of the error.

Informatica Powercenter Add Select LISTAGG query

I have learning informatica powercenter. I was able to do an INNER JOIN between two VIEWs. I added the INNER JOIN in the User Defined Join but still have to make correct changes in the Sql Query when click on Validate. Do I just need to only add to User Defined Join when I am joining VIEWS?
Here is my problem. I got another VIEWs which is one to many, so this will be my 3rd VIEW to add to the query. I got this running on TOAD. How do I add this to Infromatica? Do I just avoid the Sql Query section and add the entire query into User Defined Query? Do LISTAGG works in Informatica?
If not in the Query for LISTAGG (one-many relationship), is it better or is there a way to do this in the transformation like expression transformation?
Thanks.
This query is a SELECT, LISTAGG, CASE
SELECT PERSON_ID,
FIRST_NAME,
MIDDLE_NAME,
LAST_NAME,
LISTAGG(val,',') WITHIN GROUP (ORDER BY Table2.SKILL_SHORT_DESC )
FROM (SELECT DISTINCT Table1.PERSON_ID,
Table1.FIRST_NAME,
Table1.MIDDLE_NAME,
Table1.LAST_NAME,
(case
when Table2.SKILL_SHORT_DESC = '1' then '1:1'
when Table2.SKILL_SHORT_DESC = '2' then '2:2'
when Table2.SKILL_SHORT_DESC = '3' then '3:3'
when Table2.SKILL_SHORT_DESC = '4' then '4:4'
when Table2.SKILL_SHORT_DESC = '5' then '5:5'
when Table2.SKILL_SHORT_DESC = '6' then '6:6'
when Table2.SKILL_SHORT_DESC = '7' then '7:7'
when Table2.SKILL_SHORT_DESC = '8' then '8:8'
when Table2.SKILL_SHORT_DESC = '9' then '9:9'
else ''
end) as val
FROM Table1
LEFT JOIN Table2
ON Table2.PERSON_ID = Table1.PERSON_ID
)
GROUP BY PERSON_ID,FIRST_NAME,MIDDLE_NAME, LAST_NAME
Yes, LISTAGG should work. Assuming you are using oracle, it should work 10.x and higher version. Frankly, Informatica doesn't care what you write as SQL but its the database where SQL is being issued matters. You can write 'I am batman' and infa will return, 'Invalid syntax'.
Koushik
Please follow below steps,
Get any dummy source from the table
In source qualifier, remove all the ports and create the ports you required
In properties --> Sql query, add the query you have created. No need to provide any user defined joins
Below are the screen shots for better understanding.

cascading Input Control sql query return error: "ORA-01427: single-row subquery returns more than one row"

looking for solution on my sql query error.I'm trying to create second cascading Input Control in JaspersoftServer. The first Input Control works fine, however when I try to create a second cascade IC it returns with the error. I have 3 tables (user, client, user_client), many to many, so 1 linked table (user_client) between them.The 1st Input Control (client) - works well, end user will select the client, the client can have many users, so cascade is the key. Also, as the output, I would like to get not the user_id, but user's firstname and the lastname as one column field. And here is where i'm stuck. I'm pretty sure it is simple syntaxis error, but spent a good couple of hours to figure out what is wrong with it. Is anyone can have a look at it please and indicate where is the problem in my query ?! So far I've done:
select distinct
u.user_id,(
SELECT CONCAT(first_name, surname) AS user_name from tbl_user ),
c.client_id
FROM tbl_user u
left join tbl_user_client uc
on uc.user_id = u.user_id
left join tbl_client c
on c.client_id = uc.client_id
where c.client_id = uc.client_id
order by c.client_id
Thank you in advance.
P.S. JasperServer + Oracle 11g
You're doing an uncorrelated subquery to get the first/last name from the user table. There is no relationship between that subquery:
SELECT CONCAT(first_name, surname) AS user_name from tbl_user
... and the user ID in the main query, so the subquery will attempt to return every first/last name for all users, for every row your joins find.
You don't need to do a subquery at all as you already have the tbl_user information available:
select u.user_id,
CONCAT(u.first_name, u.surname) AS user_name
c.client_id
FROM tbl_user u
left join tbl_user_client uc
on uc.user_id = u.user_id
left join tbl_client c
on c.client_id = uc.client_id
where c.client_id = uc.client_id
order by c.client_id
If you want to put a space between the first and last name you'll either need nested concat() calls, since that function only takes two arguments:
select u.user_id,
CONCAT(u.first_name, CONCAT(' ', u.surname)) AS user_name
...
... or perhaps more readably use the concatenation operator instead:
select u.user_id,
u.first_name ||' '|| u.surname AS user_name
...
If the first control has selected a client and this query is supposed to find the users related to that client, you're joining the tables the wrong way round, aren't you? And you aren't filtering on the selected client - but no idea how that's actually implemented in Jasper. Maybe you do want the entire list and will filter it on the Jasper side.

DBA_HIST_ACTIVE_SESS_HISTORY get sql by user and object schema

Hi I am learning ASH and AWR tables but any ideas as to how i can get list of sql, objects and schema owner accessed by a give user in last 30 days ? Basically get all SQL text, and then search within this SQL to see if a given object (table, package, function, view etc ) is accessed for a given schema and by which user ? Any ideas suggestion on where and how to start ?
You could join the following views -
DBA_HIST_ACTIVE_SESS_HISTORY
DBA_USERS
DBA_HIST_SQLTEXT
To filter the history for last 30 days, use sample_time of DBA_HIST_ACTIVE_SESS_HISTORY view.
Something like -
SELECT
h.sample_time,
u.username,
h.program,
h.module,
s.sql_text
FROM
DBA_HIST_ACTIVE_SESS_HISTORY h,
DBA_USERS u,
DBA_HIST_SQLTEXT s
WHERE sample_time >= SYSDATE - 30
AND h.user_id=u.user_id
AND h.sql_id = s.sql_iD
ORDER BY h.sample_time
/
The very best and simplest way to fetch related data using below query.
SELECT H.SAMPLE_TIME,
U.USERNAME,
H.PROGRAM,
H.MODULE,
S.SQL_TEXT,
H.SQL_ID,
H.TOP_LEVEL_SQL_ID,
H.BLOCKING_SESSION_STATUS
FROM DBA_HIST_ACTIVE_SESS_HISTORY H, DBA_USERS U, DBA_HIST_SQLTEXT S
WHERE H.SAMPLE_TIME >= SYSDATE - 30
AND H.SQL_ID = S.SQL_ID
--AND H.PROGRAM IN ('Toad.exe', 'SQL Developer')
--AND U.USERNAME ='YOUR_USERNAME'
ORDER BY H.SAMPLE_TIME DESC
In the above code you can also fetch data based on your requirements as below.
1. Custom user data: Just modify YOUR_USERNAME with your real username.
2. Program: Program name can be anything like SQL Developer or JDBC Thin client to identify from which client the queries are getting triggered, but optional.
Hope it will help and answer to your question. Thanks :)

Error in creating query in openbravo through hql

This question might have answer ... But not for openbravo with postgresql database.
I have openbravo 3.0 framework. In my window i have two date fields namely fromdate and todate. The requirement is i have to write a hql where clause to filter the records on the basis of current date.The date field is of timestamp without timezone.
Means fromdate < currentdate
and todate > currentdate .
I went through this link and wrote the hql where clause as
e.id in(select s.id from Tablename as s where s.fromdate < current_Date and s.todate>current_date)
when i open this window i get this error as
Exception when creating query select e from Tablename as e
where ( e.id in(select s.Tablename_ID from Tablename as s where s.fromdate < (current_date) and s.todate < (current_date)
however if i remove the current date conditions as
e.id in(select s.id from Tablename as s).. It is working fine.
Is it because of current_Date function ? .I tried even with now function .. but i get the same error.
!!! Got The Error.
There is a problem in the query i wrote , in the where clause i am selecting the id's which is not correct,hence when i gave the below query it was running correctly.
(Tablename.fromdate < currrent_date and TableName.todate>current_date) There was no problem with the current_date function.
I thought may be it would help some one!!!
Tip: If you want to write hql query correctly in openbravo , pls install the Hql query tool module that is available freely for community edition of openbravo.
Happy Coding

Resources