SQL query for the following reqd.
Hello Everyone,
Say I have a table department with the following structure:
DEPTID NAME PARENT_DEP
1020 Corp Client
2020 Corp Sales 1020
2030 Corp Sales1 1020
3010 Mkt and sales
4010 Corp G & A Acct 2020
5010 Finance and Mkt 3010
What we find here is that department 4010 has its parent as 2020 and department 2020 has parent as 1020. The department 1020 is the root department which doesnot have any parent so its parent_dep value is NULL.
I want to write a SQL statement, which will list all the records starting from the child record 4010 and tracing back to list its parent record and so on till the root record is reached. The result should look like this(neednot be in this order as shown below)
DEPTID NAME PARENT_DEP
1020 Corp Client
2020 Corp Sales 1020
4010 Corp G & A Acct 2020
Can this be done using a single query.. if so can anyone help me ..... I want this to be done in SQL not in PLSQL.
Thanks
null
select deptid, dname, p_dno from dept
start with deptid=4010
connect by deptid = prior p_dno
order by level desc
Hope this helps...
Similar Messages
-
Extracting the Logical sql query for the specified report in OBIEE 11g
Hi ,
I want to extract the logical SQL Query for the Particular report in OBIEE 11.1.1.5.
Any pointers related to this will be very helpful.
Thanks,
Sonalifor a try please add Logical sql view to ur report it will dispaly the Logical sql for that Report..
Hope it will helps you. -
How to write selection Query for the following requirment.
Hi All,
I am new to ABAP, I need a help ,
I need to select all plants(WERKS) from MARC at Plant/Material level,
then I need to take all sales organozation(VKORG) from T001w,
then I need the company code(BUKRS) from TVKO based on VKORG,
then I need the currency key(WAERS) from T001 based on BUKRS,
Can any one help me in writing selection Query for the same?
Thanks All,
Debrup.Hi,
Its easy for you if you learn SELECT with JOIN to complete your task. So SEARCH the forum with SELECT statement and you will get a lot of examples using which you can write your own.
If you struck up anywhere revert back.
Regards
Karthik D -
Need a Query for the following Output
I have a table name Table1. some of the data are like the following
EmpCode ProductCode Quantity
20006 IMPLSA 5
20006 LACJDT 10
20006 LIVLSU 15
20006 PEPPSU 11
20006 SAFMTA 12
20006 SUCFTA 17
21475 IMPLSA 2
21475 LACJDT 7
21475 LIVLSU 4
21475 PEPPSU 8
21475 SUCFTA 12
20409 IMPLSA 11
20409 LACJDT 13
20409 LIVLSU 9
20409 SAFMTA 7
20409 SUCFTA 5
21112 IMPLSA 2
21112 LIVLSU 18
21112 PEPPSU 20
21112 SAFMTA 22
21112 SUCFTA 15
Here no of EmpCode and ProductCode are not fixed.
And i need the output like this
ProductCode 20006 21475 20409 21112
IMPLSA 5 2 11
2
LACJDT 10 7 13 0
LIVLSU 15 4 9 18
PEPPSU 11 8 0 20
SAFMTA 12 0 7 22
SUCFTA 17 12 5 15
I know that it may be solved by dynamic pivot. but i never write any dynamic pivot query.
So Please Help me.DECLARE @EmpList varchar(max),@SQL varchar(max)
SELECT @EmpList = STUFF((SELECT DISTINCT ',[' + CAST(EmpCode AS varchar(15)) + ']'
FROM Table1
ORDER BY ',[' + CAST(EmpCode AS varchar(15)) + ']'
FOR XML PATH('')),1,1,'')
SET @SQL='SELECT ProductCode,' + @EmpList +
' FROM table1 t
PIVOT(SUM(Quantity) FOR EmpCode IN (' + @EmpList + '))p'
EXEC (@SQL)
Please Mark This As Answer if it solved your issue
Please Mark This As Helpful if it helps to solve your issue
Visakh
My MSDN Page
My Personal Blog
My Facebook Page -
How to write a query for the following issue
Hello,
I would like to write a query to display the result in the following format
Item
Categort1
Categort2
Categort3
Categort4
Categort5
Categort6
Min
Max
Avg
Min
Max
Avg
Min
Max
Avg
Min
Max
Avg
Min
Max
Avg
Min
Max
Avg
01
02
03
04
For every item for the category i need to find Min,Max and Avg from the Value column
Table structure is as follows
ID
Item Id
Item
Category
value
1
01
A
Categort1
1
2
01
A
Categort1
2
3
01
A
Categort1
3
4
02
B
Categort2
7
5
02
B
Categort2
8
6
03
C
Categort3
6
7
04
D
Categort4
12
8
04
D
Categort4
14SELECT ItemID,
MIN(CASE WHEN Category = 'Categort1' THEN value END) AS Min_category1,
MAX(CASE WHEN Category = 'Categort1' THEN value END) AS Max_category1,
AVG(CASE WHEN Category = 'Categort1' THEN value END) AS Avg_category1,
MIN(CASE WHEN Category = 'Categort2' THEN value END) AS Min_category2,
MAX(CASE WHEN Category = 'Categort2' THEN value END) AS Max_category2,
AVG(CASE WHEN Category = 'Categort2' THEN value END) AS Avg_category2,
MIN(CASE WHEN Category = 'Categort6' THEN value END) AS Min_category6,
MAX(CASE WHEN Category = 'Categort6' THEN value END) AS Max_category6,
AVG(CASE WHEN Category = 'Categort6' THEN value END) AS Avg_category6
FROM Table
GROUP BY ItemID
The format can be achieved using tools like SSRS
Please Mark This As Answer if it helps to solve the issue Visakh ---------------------------- http://visakhm.blogspot.com/ https://www.facebook.com/VmBlogs -
What is the sql query for the real time reports Resource Stats?
Does anyone know what the query is that the real time report tool uses for the Resource Stats page? Trying to develop a custom report that displays similar information that is updated regularly.
Hi,
009 wrote:
Hi Frank,
Just wanted your opinion on the above given SQLI'm not sure I understand it.
I added some more formatting to help me read it:
SELECT A
, CASE WHEN LAG(A,1) OVER (ORDER BY A) IS NULL
OR A=LAG(A,1) OVER (ORDER BY A)
THEN LAG(B,1) OVER (ORDER BY A)
END B_LAG
, B
FROM (
SELECT A, B
FROM
SELECT '1'A,'Apple' B FROM DUAL UNION ALL
SELECT '1'A,'cat'B FROM DUAL UNION ALL
SELECT '2'A,'bat'B FROM DUAL UNION ALL
SELECT '3'A,'rat'B FROM DUAL UNION ALL
SELECT '2'A,'yellow'B FROM DUAL UNION ALL
SELECT '1'A,'pin'B FROM DUAL
CONNECT BY PRIOR A=B
ORDER BY A
);What is the purpose of the CONNECT BY in what you have so far?
Is the idea that you will add another CONNECT BY query, using
CONNECT BY b_lag = PRIOR b?
Do you think that will be better than using ROW_NUMBER?
Will it work if (a, b) is not unique? -
Please do write sql query for the output in the format
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO MAXIMUM_SAL
7369 SMITH CLERK 7902 17-DEC-80 800 20 3000
7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300 30 3000
7521 WARD SALESMAN 7698 22-FEB-81 1250 500 30 3000
7566 JONES MANAGER 7839 02-APR-81 2975 20 3000
7654 MARTIN SALESMAN 7698 28-SEP-81 1250 1400 30 3000
maximun salary of the all the employees should be displayed for each row of a employee record.WITH L AS (
SELECT 100 EMPNO, 'RAJESH' ENAME, 1000 SAL FROM DUAL
UNION
SELECT 200 EMPNO, 'VIJAYA' ENAME, 4000 SAL FROM DUAL
UNION
SELECT 300 EMPNO, 'KUMAR' ENAME, 1200 SAL FROM DUAL
UNION
SELECT 400 EMPNO, 'HARI' ENAME, 100 SAL FROM DUAL
SELECT empno, ENAME, SAL, MAX(SAL) OVER() FROM L;
EMPNO ENAME SAL MAX(SAL)OVER()
100 RAJESH 1000 4000
200 VIJAYA 4000 4000
300 KUMAR 1200 4000
400 HARI 100 4000 -
Problem writing a sql query for a select list based on a static LOV
Hi,
I have the following table...
VALIDATIONS
ID Number (PK)
APP_ID Number
REQUESTED Date
APPROVED Date
VALID_TIL Date
DEPT_ID Number (FK)
I have a search form with the following field item variables...
P11_DEPT_ID (select list based on dynamic LOV from depts table)
P11_VALID (select list based on static Yes/No LOV)
A report on the columns of the Validations table is shown based on the values in the search form. So far, my sql query for the report is...
SELECT v.APP_ID,
v.REQUESTED,
v.APPROVED,
v.VALID_TIL,
d.DEPT
FROM DEPTS d, VALIDATIONS v
WHERE d.DEPT_ID = v.DEPT_ID(+)
AND (d.DEPT_ID = :P11_DEPT_ID OR :P11_DEPT_ID = -1)
This query works so far. My problem is that I don't know how to do a search based on the P11_VALID item - if 'yes' is selected, then the VALID_TIL date is still valid. If 'no' is selected then the VALID_TIL date has passed.
Can anyone help me to extend my query to include this situation?
Thanks.Hello !
Let's have a look at my example:create table test
id number
,valid_til date
insert into test values( 1, sysdate-3 );
insert into test values( 2, sysdate-2 );
insert into test values( 3, sysdate-1 );
insert into test values( 4, sysdate );
insert into test values( 5, sysdate+1 );
insert into test values( 6, sysdate+2 );
commit;
select * from test;
def til=yes
select *
from test
where decode(sign(trunc(valid_til)-trunc(sysdate)),1,1,0,1,-1)
=decode('&til','yes',1,-1);
def til=no
select *
from test
where decode(sign(trunc(valid_til)-trunc(sysdate)),1,1,0,1,-1)
=decode('&til','yes',1,-1);
drop table test; It's working fine, I've tested it.
The above changes to my first idea I did because of time portion of the DATE datatype in Oracle and therefore the wrong result for today.
For understandings:
1.) TRUNC removes the time part of DATE
2.) The difference of to date-values is the number of days between.
3.) SIGN is the mathematical function and gives -1,0 or +1 according to an negative, zero or positiv argument.
4.) DECODE is like an IF.
Inspect your LOV for the returning values. According to my example they shoul be 'yes' and 'no'. If your values are different, you may have to modify the DECODE.
Good luck,
Heinz -
Oracle SQL query for getting specific special characters from a table
Hi all,
This is my table
Table Name- Table1
S.no Name
1 aaaaaaaa
2 a1234sgjghb
3 a@3$%jkhkjn
4 abcd-dfghjik
5 bbvxzckvbzxcv&^%#
6 ashgweqfg/gfjwgefj////
7 sdsaf$([]:'
8 <-fdsjgbdfsg
9 dfgfdgfd"uodf
10 aaaa bbbbz#$
11 cccc dddd-/mnm
The output has to be
S.no Name
3 a@3$%jkhkjn
5 bbvxzckvbzxcv&^%#
7 sdsaf$([]:'
8 <-fdsjgbdfsg
10 aaaa bbbbz#$
It has to return "Name" column which is having special characters,whereas some special chars like -, / ," and space are acceptable.
The Oracle query has to print columns having special characters excluding -,/," and space
Can anyone help me to get a SQL query for the above.
Thanks in advance.You can achieve it in multiple ways. Here are few.
SQL> with t
2 as
3 (
4 select 1 id, 'aaaaaaaa' name from dual union all
5 select 2 id, 'a1234sgjghb' name from dual union all
6 select 3 id, 'a@3$%jkhkjn' name from dual union all
7 select 4 id, 'abcd-dfghjik' name from dual union all
8 select 5 id, 'bbvxzckvbzxcv&^%#' name from dual union all
9 select 6 id, 'ashgweqfg/gfjwgefj////' name from dual union all
10 select 7 id, 'sdsaf$([]:''' name from dual union all
11 select 8 id, '<-fdsjgbdfsg' name from dual union all
12 select 9 id, 'dfgfdgfd"uodf' name from dual union all
13 select 10 id, 'aaaa bbbbz#$' name from dual union all
14 select 11 id, 'cccc dddd-/mnm' name from dual
15 )
16 select *
17 from t
18 where regexp_like(translate(name,'a-/" ','a'), '[^[:alnum:]]');
ID NAME
3 a@3$%jkhkjn
5 bbvxzckvbzxcv&^%#
7 sdsaf$([]:'
8 <-fdsjgbdfsg
10 aaaa bbbbz#$
SQL> with t
2 as
3 (
4 select 1 id, 'aaaaaaaa' name from dual union all
5 select 2 id, 'a1234sgjghb' name from dual union all
6 select 3 id, 'a@3$%jkhkjn' name from dual union all
7 select 4 id, 'abcd-dfghjik' name from dual union all
8 select 5 id, 'bbvxzckvbzxcv&^%#' name from dual union all
9 select 6 id, 'ashgweqfg/gfjwgefj////' name from dual union all
10 select 7 id, 'sdsaf$([]:''' name from dual union all
11 select 8 id, '<-fdsjgbdfsg' name from dual union all
12 select 9 id, 'dfgfdgfd"uodf' name from dual union all
13 select 10 id, 'aaaa bbbbz#$' name from dual union all
14 select 11 id, 'cccc dddd-/mnm' name from dual
15 )
16 select *
17 from t
18 where translate
19 (
20 lower(translate(name,'a-/" ','a'))
21 , '.0123456789abcdefghijklmnopqrstuvwxyz'
22 , '.'
23 ) is not null;
ID NAME
3 a@3$%jkhkjn
5 bbvxzckvbzxcv&^%#
7 sdsaf$([]:'
8 <-fdsjgbdfsg
10 aaaa bbbbz#$
SQL> -
Hi, getting Could not save the document to the repository for the following reason: [repo_proxy 30] DocumentFacade::uploadBlob - Query execute has failed : Error occurred while attempting to reconnect to CMS : Not a valid logon token. (FWB 00003) (hr=#0x80042a70) (WIS 30567) amongst lots of other errors when scheduling.
I was logged in as administrator and attempting to schedule a webi document to my self using the email option.
thanks in advanceHi Trinath,
Could you please confirm if you could save a new report as well or not; or is it specific to scheduling.
If you are unable to save a report also then I think this is due to the path of the Input File Repository Server or its temporary directory are not pointing to the same path, and their locations are set to 2 different hard drives
BOXI3.1 Server must use the same hard drive (local or network share) for the Input File Repository Server and its temporary directory.
- Shahnawaz -
OIM sql Query for getting the status of the task which got failed
Hi Everyone,
We have a requirement like we need to get the status of a particular task(say Create User in OID resource - Completed\Rejected status) for the particular user.We are able to get the status of the resource provisioed to the user but not the status of the particular task getting trigerred for the user.can someone put some light on this.We need to get the SQL query for this.
Thanks in Advance.
Regards,
MKNHi
Use this sample query to get the task status, also check the cooments
SELECT USR.USR_LOGIN, OSI.SCH_KEY,SCH.SCH_STATUS,STA.STA_BUCKET FROM
OSI,SCH,STA,MIL,TOS,PKG,OIU,USR,OBJ,OST
WHERE OSI.MIL_KEY=MIL.MIL_KEY
AND SCH.SCH_KEY=OSI.SCH_KEY
AND STA.STA_STATUS=SCH.SCH_STATUS
AND TOS.PKG_KEY=PKG.PKG_KEY
AND MIL.TOS_KEY=TOS.TOS_KEY
AND OIU.USR_KEY=USR.USR_KEY
AND OIU.OST_KEY=OST.OST_KEY
AND OST.OBJ_KEY=OBJ.OBJ_KEY
AND OSI.ORC_KEY=OIU.ORC_KEY
AND OBJ.OBJ_NAME='AD User'
AND OST.OST_STATUS = 'Provisioning' -- filter accordinglly
AND STA.STA_BUCKET = 'Pending' -- filter accordinglly
AND PKG.PKG_NAME='AD User' -- filter accordinglly
AND MIL.MIL_NAME='System Validation' ---- filter accordinglly
Thanks,
Kuldeep -
Neea a sql query for inverting the table
Hi,
I am facing the problem in forming a SQL statement in oracle 10g
Table structure:
user field1 field2 field3 field4 field5 field6 .......
one 11 12 13 14 15 16
two 21 22 23 24 25 26
three 31 32 33 34 35 36
i want a SQL query where the out put is
field1 11 21 31
field2 12 22 32
field3 13 23 33
field4 14 24 34
Regards,
Balu CHcheck this Forums page
<http://forums.oracle.com/forums/search.jspa?threadID=&q=Rows+to+Columns&objID=f75&dateRange=last90days&userID=&numResults=15>
will help u.
Rgds
Sudar -
How to write sql query for below example.
Hi,
I have requirement.
There are number of rows and I need the result through query as follows.Please help me to proved sql query for below mentioned requirement.
example: table TEST.
COLA COLB COLC COLD
MANAGER 5 NULL null
SR.MANAGE 6 3 NULL
VP 5 5 4
I have to write the sql query if COLA IS MANAGER THEN CONSIDER MANGER RECORD,AND IF ANY COLUMN FILED IS NULL FOR MANGER THEN CONSIDER COLA IS SR.MANGER, AND IF ANY COLUMN FILED IS NULL FOR SR,MANGER THEN CONSIDER VP records.
I need output as below.
COLB COLC COLD
5(MANGER) 3(sr.manger) 5(vp)
Please provide the for above mentioned output.
Thanks<<if COLA IS MANAGER THEN CONSIDER MANGER RECORD>>
What does this sentence means? COLA does not cnatin a single record but the number of records with diffrent values.
Regards
Arun -
How to write sql query for below mentioned eaxmple.
Hi,
I have requirement.
There are number of rows and I need the result through query as follows.Please help me to proved sql query for below mentioned requirement.
example: table TEST.
COLA COLB COLC COLD COLE COLF MANAGER 5 NULL NULL 3 NULL
SR.MANAGER 6 3 NULL NULL NULL
VP 5 5 4 5 5
I have to write the sql query if COLA IS MANAGER THEN CONSIDER MANGER RECORD,AND IF ANY COLUMN FILED IS NULL FOR MANGER THEN CONSIDER COLA IS SR.MANGER, AND IF ANY COLUMN FILED IS NULL FOR SR,MANGER THEN CONSIDER VP records.
I need output as below.
COLB COLC COLD COLE COLF
5(manager) 3(sr.manger) 4(vp) 3(manger) 3(vp)
Please provide the for above mentioned output.
ThanksDuplicate thread. please view the answer posted in your first thread.
how to write sql query.
And, please don't post any duplicate thread.
Regards.
Satyaki De. -
Need SQl Query for Revenue Code and Revenue Amount in Receivables
Hi,
I need a SQL Query to develop data set for the following columns:
Revenue Code,Revenue Code Description,Invoice Amount,Revenue Amount,Original Invoice Number,Original Invoice Date.
Can i get from ra_cust_trx_line_gl_dist_all and ra_customer_trx_all and ra_customer_trx_lines_all by joining them.But for the columns whcih to take
i am in dilemma.
Kindly any help will be needful for meHi,
I need a SQL Query to develop data set for the following columns:
Revenue Code,Revenue Code Description,Invoice Amount,Revenue Amount,Original Invoice Number,Original Invoice Date.
Can i get from ra_cust_trx_line_gl_dist_all and ra_customer_trx_all and ra_customer_trx_lines_all by joining them.But for the columns whcih to take
i am in dilemma.
Kindly any help will be needful for me
Maybe you are looking for
-
How can I make a 3 fold leaflet in photoshop?
Hi I am very new to photoshop but would like to create a professional looking A4 3 fold leaflet (takeaway-leaflet-type). Any idea's how I can create such thing, or any other application I can use? Thanks in advance
-
Hi, I need to extend a SupplierSitesVO. I downloaded the VO from the server to my local machine and opened the VO in Tutorial.jpr I also brought all the dependent objects by referring to server.xml to the respective folder, but when I am trying to re
-
Creating Tcode for COPA(KE30) report
Hi , i need to create Tcode for KE30 report through SE93 i tried with option transaction with parameters here i need to give Ke30 report name for ex :GP4BW2S4QUBUNA68Y2YXMIW8V6X but program name last 3 letters will change in production system how to
-
Nokia Series 60 MIDP SDK Beta 0.1 with JBuilder6
Hi, I installed Nokia Series 60 MIDP SDK Beta 0.1 and configured the JDK of JBuilder6. When I run the midlet, The EPOC emulator was pop up, but then there was a runtime error: Emulator command: C:\Nokia_Series_60_MIDP_SDK_Beta_0_1\bin\Epoc32\release\
-
TS1996 my zero key does not work as it should after software update any help
my zero key does not work after a software update when i try to use it /pp0 then volume symbol appears on the screen just happened after the update