Get parent name from the children id with level in Oracle - oracle

I would like to retrieve the Name of the Parent from the Cildren Id in a query thanks to the ORACLE feature "CONNECT BY"
SELECT contact_id, contact_name,parent_id, LEVEL
FROM contacts
CONNECT BY PRIOR contact_id = parent_id;
But I would like also retrieve the name of the parent, and I try many times, but I cannot obtain that I want. Could you please help me with that ?

You can use PRIOR with name column as well.
SELECT contact_id, contact_name,parent_id, LEVEL, prior contact_name
FROM contacts
CONNECT BY PRIOR contact_id = parent_id;

Related

data extraction from oracle source

I am trying to extract data from oracle source using pyspark.
I am using this code.
from pyspark.sql import SparkSession
spark = SparkSession.builder.appName('Spark_Job').getOrCreate()
driver = 'oracle.jdbc.driver.OracleDriver'
url = 'jdbc:oracle:thin:#XXXXXXXXXXXXXXXXXX/XXX'
user = 'XXXXX'
password = 'XXXXXXXX'
query = 'SELECT col_1 from schema_1.view_1'
df = spark.read.format('jdbc').option('driver', driver).option('url', url).option('dbtable', query)\
.option('user', user).option('password', password).load()
df.show(10)
I am getting this error :
java.sql.SQLSyntaxErrorException: ORA-00903: invalid table name
ORA-00903 means that you tried to use table name, but - that table doesn't exist.
There's only one select statement here:
SELECT col_1 from schema_1.view_1
-------- ------
owner table (or view) name
("user")
Info you posted suggests that you're connected as
user = 'XXXXX'
If view_1 belongs to currently connected user, you don't have to specify owner name so query would then be just select col_1 from view_1.
If view_1 belongs to currently connected user, then view_1 must be correctly spelled. Oracle doesn't care about letter case unless you decided to enclose its (table's, view's) name into double quotes and used lower or mixed case - then you have to do it every time you reference that table, so query might be e.g. select col_1 from "View_1".
If view_1 belongs to user who is different from currently logged user (that's what information you posted suggests), then yes - preceding table's name with its owner's name is the way to do it. However, schema_1 (the owner) has to grant at least select privilege on view_1 to XXXXX - otherwise, it won't work. (Double quotes/letter case issue still stands.)
I can't tell which situation of these you have, but now you have something to check and act appropriately.

How to find the sum of each group which is grouped by another group?

I am actually trying to make a matrix table using Oracle Analytics tool and PL/SQL.
Let's say i have a query which has in select statement variables Employee, Description, orderid ,amount and is grouped by Employee, Description. Orderid and amount belong to the same group. From this query i want extract the sum of the amount of each description from all employees. Do you have any idea how i can do this?
Thank you.
Edit:
Let's say we have the following query:
Select Employee, Description, orderid ,amount
From Employees
Group by Employee,Description
I want to extract the sum of amount from each Description group but from all Employees.A way to do this could be like this:
Select Description,sum(amount)
From Employees
Group by Description
But the actual query is much more complex and if i choose to make another query for finding the sum of each description i have to link it somehow to the first query to be able to show the results at the report.
Do you have any idea of a way to do this through oracle analytics publisher?
Thank you.
Select coalesce(Employee,'ALL_EMPOYEES'), coalesce(Description,'ALL_DESCRIPTION'), orderid ,amount
From Employees
Group by ROLLUP (Employee,Description)
Select coalesce(Employee,'ALL_EMPOYEES'), coalesce(Description,'ALL_DESCRIPTION'), orderid ,amount
From Employees
Group by CUBE (Employee,Description)

How to query for Inactive Employees using BI Publisher in Oracle Fusion?

I'm new to BI Publisher and I'm using it through Oracle Fusion Applications.
I am trying to make a report relating to the Inactive Employees in an organization. However I am unable to figure out how to query for an inactive or terminated employee.
I initially used this query:
SELECT PERSON_ID, PERSON_NUMBER, EFFECTIVE_START_DATE, EFFECTIVE_END_DATE
FROM PER_ALL_PEOPLE_F
WHERE TRUNC(SYSDATE) NOT BETWEEN TRUNC(EFFECTIVE_START_DATE) AND TRUNC(EFFECTIVE_END_DATE)
My other considerations were attributes from the PER_ALL_ASSIGNMENTS_M table including PRIMARY_WORK_RELATION_FLAG, PRIMARY_ASSIGNMENT_FLAG and ASSIGNMENT_TYPE considering that the employee's assignment details would help somehow. However I was unsuccessful.
I wanted to know if there was any other proper way to query for inactive employees. Is there any particular attribute in any table which would tell me for certain that an employee is active or terminated? When an employee is terminated in Oracle Fusion, which all table attributes get affected?
Thank you for your help.
The easiest way to do this is simply :
SELECT * FROM YourTable t
WHERE TRUNC(t.END_DATE) <= trunc(sysdate)
Some times there is also an indication column like IS_ACTIVE or something. You can also consider adding it, and simply updating it to 1 for all the records returned from the above query.
Other then that, we can't really help you. We don't know your table structures, we don't know what data you store in them and which column indicates what .
I have found what i was looking for. ASSIGNMENT_STATUS_TYPE='INACTIVE' was what I needed (As mentioned in the question, this solution is without considering 'EFFECTIVE_END_DATE') Getting the 'latest' assignment status of an employee was what I needed to find. The following query works if the employee has only one Assignment assigned.
SELECT PAPF.PERSON_ID, PAPF.PERSON_NUMBER
FROM PER_ALL_PEOPLE_F PAPF, PER_ALL_ASSIGNMENTS_M PAAM
WHERE 1=1
AND TRUNC(PAAM.EFFECTIVE_START_DATE) = (SELECT MAX(TRUNC(PAAM_INNER.EFFECTIVE_START_DATE))
FROM PER_ALL_ASSIGNMENTS_M PAAM_INNER
WHERE PAAM_INNER.PERSON_ID=PAAM.PERSON_ID
GROUP BY PAAM_INNER.PERSON_ID)
AND PAPF.PERSON_ID=PAAM.PERSON_ID
AND PAAM.PRIMARY_FLAG='Y'
AND PAAM.ASSIGNMENT_STATUS_TYPE='INACTIVE'
AND TRUNC(SYSDATE) BETWEEN PAAM.EFFECTIVE_START_DATE AND PAAM.EFFECTIVE_END_DATE
AND TRUNC(SYSDATE) BETWEEN PAPF.EFFECTIVE_START_DATE AND PAPF.EFFECTIVE_END_DATE
ORDER BY 1 ASC
I had help from the Oracle Support Community to get to an answer.
Link: https://community.oracle.com/message/14000136#14000136
However in a case where an employee was given an assignment say starting from year 2000 and ending at 20015, then another assignment starting from 2016 till present, the above query will return one record of the said employee as 'Inactive' if the Max Effective_start_date condition is not checked. (Since one became Inactive on 2015), even though her current Assignment status is 'Active' and she is currently not terminated.
In such a case, it is wise to retrieve the record with the greatest 'EFFECTIVE_START_DATE' from the PER_ALL_ASSIGNMENTS_M table, ie, checking if EFFECTIVE_START_DATE = MAX(EFFECTIVE_START_DATE)

How to arrange data in SQL?

I am using Oracle SQL Plus.
Please refer to screenshot for reference.
After deletion of data of column sno=4, I want the output of sno to be displayed in serial order ie 1,2,3,4 instead of 1,2,3,5.
Please suggest the SQL query to achieve same.
try:
select ROWNUM as SNO, NAME,DOJ from company order by SNO
If you want the numbering to be persistent, you will need to update the SNO for all the remaining records in the table.
What you have done, by deleting the row is say that "row number 4 no longer exists". If you want to shuffle the remaining rows up, you will need to perform the following:
DELETE FROM company WHERE name='Flipkart';
UPDATE company SET sno=sno-1 WHERE sno>=4;
An alternative, if you don't want to alter the contents of the table itself.. But you want it to display correctly (specifically for this example)
SELECT
CASE
WHEN sno <= 4 THEN sno
ELSE sno-1
END AS sno,
name,
doj
FROM company
ORDER BY sno;

Oracle Connect By query resultset randomly not in hierarchy

I have prepared few queries using startwith and connectby for fetching all items of a table with parent - childs relationship.
Till now, these queries were working perfectly fine. But now, i noticed the hierarchy returned was not the same. hierarchy is returned in totally random way, although the data is same.
Can anyone suggest why this is happening..
Below is the sample query:
SELECT id,loc.title Title FROM
(SELECT level level,id id,parent_id Parent_Id,sort_order FROM table1
START WITH parent_id=0
CONNECT BY prior id = parent_id ORDER SIBLINGS BY sort_order)
INNER JOIN table2 loc ON id = loc.id WHERE loc.locale=?
the ORDER SIBLINGS BY will do what you want if you don't wrap the query as an inline view.
once you wrap it, then the ordering is no longer specified
i would suggest doing the JOIN directly in the inner query.. then your order by should work.

Resources