SQL Help
Please help ...
Data Sample:
A1 B1C1 D1
01 A 06/30/2008 1
01 B 06/10/2008 1
01 C 06/09/2008 1
01 D 06/09/2008 1
02 A 06/20/2008 1
02 C 06/20/2008 1
03 A 06/20/2008 1
04 A 06/10/2008 2
04 B 06/10/2008 1
04 C 06/09/2008 1
04 C 06/09/2008 1
Output Should be:
01 A 06/30/2008 1
02 A 06/20/2008 1
02 C 06/20/2008 1
03 A 06/20/2008 1
04 A 06/10/2008 2
Rules:
- Top D1 should be selected.
- If there are tie in D1 per A1, should be looking for C1 for the latest date
- if there are tie for the D1 and C1 per A1 this data should be selected
- if there are only one D1 which is the latest then inly 1 record will be selected.
Hi,
try this:
SQL> with t as ( -- "sample data"
2 select '01' a1, 'A' b1, to_date('06/30/2008','mm/dd/yyyy') c1, 1 d1 from dual union all
3 select '01', 'B', to_date('06/10/2008','mm/dd/yyyy'), 1 from dual union all
4 select '01', 'C' , to_date('06/09/2008','mm/dd/yyyy'), 1 from dual union all
5 select '01', 'D' , to_date('06/09/2008','mm/dd/yyyy'), 1 from dual union all
6 select '02', 'A' , to_date('06/20/2008','mm/dd/yyyy'), 1 from dual union all
7 select '02', 'C' , to_date('06/20/2008','mm/dd/yyyy'), 1 from dual union all
8 select '03', 'A' , to_date('06/20/2008','mm/dd/yyyy'), 1 from dual union all
9 select '04', 'A' , to_date('06/10/2008','mm/dd/yyyy'), 2 from dual union all
10 select '04', 'B' , to_date('06/10/2008','mm/dd/yyyy'), 1 from dual union all
11 select '04', 'C' , to_date('06/09/2008','mm/dd/yyyy'), 1 from dual union all
12 select '04', 'C' , to_date('06/09/2008','mm/dd/yyyy'), 1 from dual
13 ) -- "end sample data"
14 select a1, b1, c1, d1
15 from (select a1, b1, c1, d1,
16 max(c1) over(partition by a1) mxc1,
17 max(d1) over(partition by a1) mxd1,
18 min(d1) over(partition by a1) mind1
19 from t)
20 where 1 = case when mind1 = mxd1 then
21 case when c1 = mxc1 then 1 end
22 else case when d1 = mxd1 then 1 end
23 end
24 /
A1 B1 C1 D1
01 A 6/30/2008 1
02 A 6/20/2008 1
02 C 6/20/2008 1
03 A 6/20/2008 1
04 A 6/10/2008 2
Similar Messages
-
Hi again
I need some PIVOT sql help
In this query:
SELECT
SUM([RATES]) as RATES
,SUM([CONVERSION])as CONVERSION
FROM REPORTING
outputs
RATES CONVERSION
23 234
How would change this query to display a table like:
Name Amount
RATES 23
CONVERSION 234
Keep up the good work !nikos101 wrote:
> How would change this query to display a table like:
Do you *HAVE* to change the query... you could just display
it in the
desired format if that it the ultimate and only goal.
<table>
<tr>
<td>name</td>
<td>amount</td>
</tr>
<cfoutput query="sumQry">
<tr><td>Rates</td><td>#rates#</td></tr>
<tr><td>Conversion</td><td>#conversion#</td></tr>
</cfoutput>
</table> -
Hello,
I am having a query as a part of my procedure block that extracts the following xml and stores it into a variable that is of xmltype:
<FeatureRoot>
<Feature>
<FeatureName>qaz</FeatureName>
<FeatureAction>Add</FeatureName>
</Feature>
<Feature>
<FeatureName>wsx</FeatureName>
<FeatureAction>Remove</FeatureAction>
</Feature>
</FeatureRoot>
Now what i want to do is loop through this variable (which is of xmltype) and print the values of the tags <FeatureName> and <FeatureAction> for every Feature.
(ie)
qaz Add
wsx Remove
Can you please tell me how i can loop through to extract the values?
Many Thanks,
Kalyaniuser11912174 wrote:
Hello,
Thank you. The solutions may hold good on a normal context. What my problem is I want to run a loop through an anonymous block in pl/sql where I am already using a cursor where each record contains an xml value as above. So i need to be extracting for each run in a cursor. So i need to be extracting the <FeatureName> and <featureaction> values in each cursor run. Any help on this?
ThanksCan you not incorporate it as part of the cursor itself?
It can return the values as part of the data from the cursor rather than using PL/SQL extracts on the XML to do it.
SQL> ed
Wrote file afiedt.buf
1 with myxml as
2 (select xmltype('<FeatureRoot>
3 <Feature>
4 <FeatureName>qaz</FeatureName>
5 <FeatureAction>Add</FeatureAction>
6 </Feature>
7 <Feature>
8 <FeatureName>wsx</FeatureName>
9 <FeatureAction>Remove</FeatureAction>
10 </Feature>
11 </FeatureRoot>') myxml from dual union all
12 select xmltype('<FeatureRoot>
13 <Feature>
14 <FeatureName>aaa</FeatureName>
15 <FeatureAction>Add</FeatureAction>
16 </Feature>
17 <Feature>
18 <FeatureName>bbb</FeatureName>
19 <FeatureAction>Remove</FeatureAction>
20 </Feature>
21 </FeatureRoot>') from dual)
22 select extractvalue(VALUE(t),'/Feature/FeatureName') featurename
23 , extractvalue(VALUE(t),'/Feature/FeatureAction') featureaction
24 from myxml x
25* , table(xmlsequence(extract(x.myxml,'/FeatureRoot/Feature'))) t
SQL> /
FEATURENAME FEATUREACTION
qaz Add
wsx Remove
aaa Add
bbb Remove
SQL> -
I do not know if this is the right place for thius type of post. If it is not please advise where the right place is.
I need help generating a report, hopefully with SQL in 8.1.7
SQL Statement which produced the data below the query :
SELECT CHANGE.change_number, CHANGE.route_date as DATE_TO_CCB, nodetable.description AS Approver_required, (TRIM(BOTH ',' FROM CHANGE.PRODUCT_LINES)) AS PRODUCT_LINES /*, PROPERTYTABLE.VALUE as PRODUCT_LINES */
FROM CHANGE, signoff, workflow_process, nodetable /*, PROPERTYTABLE */
WHERE ( (CHANGE.ID = signoff.change_id)
AND (CHANGE.process_id = signoff.process_id)
AND ((nodetable.id = signoff.user_assigned) or (nodetable.id=signoff.user_signed))
AND (CHANGE.process_id = workflow_process.ID)
AND (CHANGE.ID = workflow_process.change_id)
AND (CHANGE.workflow_id = workflow_process.workflow_id)
AND (SIGNOFF.SIGNOFF_STATUS=0 )/* in (0, 2, 3)) */ /* 0=request needs attention, 2=request approved, 3=request rejected */
AND (SIGNOFF.REQUIRED=5 or SIGNOFF.REQUIRED=1) /* 1=Approver 5= Ad Hoc Approver */
AND (CHANGE.IN_REVIEW=1)
AND (CHANGE.RELEASE_DATE IS NULL)
AND (CHANGE.CLASS != '4928')
/* AND (PROPERTYTABLE.PROPERTYID IN (SELECT TRIM(BOTH ',' FROM CHANGE.PRODUCT_LINES) FROM CHANGE)) */
order by change.route_date desc
**** Results **********
CHANGE_NUMBER|DATE_TO_CCB|APPROVER_REQUIRED|PRODUCT_LINES
C02190|11/14/2008 3:34:02 PM|Anurag Upadhyay|270354,270362|
C02190|11/14/2008 3:34:02 PM|Dennis McGuire|270354,270362|
C02190|11/14/2008 3:34:02 PM|Hamid Khazaei|270354,270362|
C02190|11/14/2008 3:34:02 PM|Mandy York|270354,270362|
C02193|11/14/2008 3:05:18 PM|Hamid Khazaei|274279,266339,266340,266341|
C02193|11/14/2008 3:05:18 PM|Rob Brogle|274279,266339,266340,266341|
C02193|11/14/2008 3:05:18 PM|Xavier Otazo|274279,266339,266340,266341|
C02193|11/14/2008 3:05:18 PM|san|274279,266339,266340,266341|
C02194|11/14/2008 2:51:34 PM|Diana Young|271503|
C02194|11/14/2008 2:51:34 PM|Carl Krentz|271503|
C02194|11/14/2008 2:51:34 PM|Dennis Yen|271503|
C02194|11/14/2008 2:51:34 PM|Gordon Ries|271503|
C02194|11/14/2008 2:51:34 PM|Sunil Khatana|271503|
M00532|11/13/2008 1:34:42 PM|Dennis Yen|270356,270354,270360,274279,266339,266340,266341,276780,260784|
M00532|11/13/2008 1:34:42 PM|Jin Hong|270356,270354,270360,274279,266339,266340,266341,276780,260784|
M00532|11/13/2008 1:34:42 PM|Sunil Khatana|270356,270354,270360,274279,266339,266340,266341,276780,260784|
Each value in the numeric comma delimited string has a corresponding ID for the actual test string value in another table as shown below.
PROPERTYID|VALUE
260775|product 1
260776|Product 2
260777|Product x
260778|Product y
260779|Internal
260780|ORCA
260781|Tiger
260782|Orange product
260783|Restricted
260784|Product zz
266259|Product YYY
266260|Hercules
266261|Tangerine
*****Desired output****
CHANGE_NUMBER|DATE_TO_CCB|APPROVER_REQUIRED|PRODUCT_LINES
C02190|Nov/14/2008 03:34:02 PM|Anurag Upadhyay, Dennis McGuire, Hamid Khazaei, Mandy York|Product Y,Product 1
C02193|Nov/14/2008 03:05:18 PM|Hamid Khazaei, Rob Brogle, Xavier Otazo, san|Hercules,Apple,Product 3,Product zz
C02194|Nov/14/2008 02:51:34 PM|Diana Young, Carl Krentz, Dennis Yen, Gordon Ries, Sunil Khatana|Product 2
M00532|Nov/13/2008 01:34:42 PM|Dennis Yen, Jin Hong, Sunil Khatana|Product 1,Product 4,product yy,product YYY,ORCA,Tiger,Orange product,RestrictedHi,
Here's how you can do it in Oracle 8.1.
To get the individual sub-strings from product_lines, join your current result set to this "counter table"
( SELECT ROWNUM AS n
FROM all_objects
WHERE ROWNUM <= 10 -- upper bound on number of items
) cntrIf you don't know the worst case of how many items might be in product_lines, it's a little more complicated:
( SELECT ROWNUM AS n
FROM all_objects
WHERE ROWNUM <= 1 +
SELECT MAX ( LENGTH (product_lines)
- LENGTH (REPLACE (product_lines, ','))
FROM table_name
) cntrJoin this to the existing result set like this
WHERE ...
AND INSTR ( product_lines || ',' -- one extra comma added
, 1
, n
) > 0When you do the join, you will have
one copy of all the rows with one item in producgt_lines,
two copies of all the rows with two items in producgt_lines,
three copies of all the rows with three items in producgt_lines,
and so on.
When a row has been copied, each copy will have a different value of cntr.n.
To extract the n-th substring from product_lines:
SELECT ...
SUBSTR ( product_lines
, INSTR ( ',' || product_lines, ',', 1, n)
, ( INSTR (product_lines || ',', ',', 1, n)
- INSTR (',' || product_lines, ',', 1, n)
) AS product_lines_itemWhen you have derived this column, you can join to the table with the translations
WHERE TO_NUMBER (product_lines_item) = propertyidTo combine these rows into one row with a comma-delimited list, GROUP BY all the columns you want to select except the property_value ('produc 1', 'tangerine', etv.), and SELECT:
LTRIM ( MAX (CASE WHEN n = 1 THEN ',' || property_value END) ||
MAX (CASE WHEN n = 2 THEN ',' || property_value END) ||
MAX (CASE WHEN n = 10 THEN ',' || property_value END)
)I don't know a good way to re-combine the rows in Oracle 8 without assuming some limit on the number of items. I assumed there would never be more than 10 in the example above. You can say 20 or 100, I suppose, if you want to. If you guess too high, everything will still work: the query will just be slower.
This is a just one example of why packing several values into a single column is a bad idea. -
SQL help please. !!!!!!!!!!
hi
i need some help on the following problem. let's say i have a sql select that return the following rows
ME
EL
EO
FA
these are 4 rows of data returned by executing "select column1 from myTable where somecondition = true". however, i need to display these results as follow
MW,EL,EO,FA
which is just one row and they are concatenated as one result. is there any way to modify the sql to do it? your help is much appreciated. thankshi
i need some help on the following problem. let's say i
have a sql select that return the following rows
ME
EL
EO
FA
these are 4 rows of data returned by executing "select
column1 from myTable where somecondition = true".
however, i need to display these results as follow
MW,EL,EO,FA
which is just one row and they are concatenated as one
result. is there any way to modify the sql to do it?
your help is much appreciated. thanksit would be easier if you ahead know how many rows you going to get...
select
"row1" = select blah from table where blah = '1',
"row2" = select blah from table where blah = '2',
"row3" = select blah from table where blah = '3',
"row4" = select blah from table where blah = '4'
from table
make sure the sub select must return one record only... -
SQL help: Selecting tickets with no open tasks
Hi Gurus,
I'm new to SQL. I need your help in a script where I need to fetch the tickets with no open tasks.
Say, following is the Task table
Table: Task
| ticketid | taskid | taskstatus |
| 1 | 1 | O |
| 1 | 2 | O |
| 1 | 3 | O |
| 2 | 4 | C |
| 2 | 5 | C |
| 3 | 6 | C |
| 3 | 7 | O |
The query should return the ticketid(s) with all its taskstatus = 'C'.
Any help would be highly appreciated.
ThanksHi Surya,
Here's an Oracle style example (Sorry, I just can't do that ansi stuff)
SQL> with ticket as (select 1 ticketid, 'C' reason from dual union all
select 2 ticketid, 'O' reason from dual union all
select 3 ticketid, 'O' reason from dual union all
select 4 ticketid, 'C' reason from dual)
,taskstatus as (select 1 taskstatusid, 'O' taskstatus from dual union all
select 2 taskstatusid, 'C' taskstatus from dual union all
select 3 taskstatusid, 'P' taskstatus from dual union all
select 4 taskstatusid, 'F' taskstatus from dual union all
select 5 taskstatusid, 'O' taskstatus from dual union all
select 6 taskstatusid, 'C' taskstatus from dual union all
select 7 taskstatusid, 'P' taskstatus from dual union all
select 8 taskstatusid, 'F' taskstatus from dual)
,task as (select 1 ticketid, 1 taskid, 2 taskstatusid from dual union all
select 1 ticketid, 2 taskid, 3 taskstatusid from dual union all
select 1 ticketid, 3 taskid, 4 taskstatusid from dual union all
select 1 ticketid, 4 taskid, 4 taskstatusid from dual union all
select 2 ticketid, 5 taskid, 1 taskstatusid from dual union all
select 2 ticketid, 6 taskid, 1 taskstatusid from dual union all
select 2 ticketid, 7 taskid, 1 taskstatusid from dual union all
select 3 ticketid, 8 taskid, 2 taskstatusid from dual union all
select 3 ticketid, 9 taskid, 2 taskstatusid from dual union all
select 3 ticketid, 10 taskid, 6 taskstatusid from dual union all
select 4 ticketid, 11 taskid, 2 taskstatusid from dual union all
select 4 ticketid, 12 taskid, 6 taskstatusid from dual union all
select 4 ticketid, 13 taskid, 4 taskstatusid from dual union all
select 4 ticketid, 14 taskid, 8 taskstatusid from dual)
select a.ticketid, c.taskstatus
from task a, ticket b, taskstatus c
where a.ticketid = b.ticketid
and c.taskstatusid = a.taskstatusid
and (c.taskstatus = 'C' or c.taskstatus = 'F')
and b.reason = 'C'
and not exists (select null
from task a1, taskstatus c1
where a1.ticketid = a.ticketid
and c1.taskstatusid = a1.taskstatusid
and (c1.taskstatus = 'O' or c1.taskstatus = 'P'))
order by 1,2
TICKETID T
4 C
4 C
4 F
4 F
4 rows selected.Please note, how I made test data, that way is much more helpful than your tables
Regards
Peter -
SQL HELP , URGENT PLEASE
Hi,
I want some help in writing a SQL Query .Its besically a hierarchical query. Let me lay down the table structure first to explain my requirements better.
PORP_TABLE(NODE_LEVEL int, WBS_ID int, WBS_NUMBER varchar(60), LFT int,RGT int)
SELECT NODE_LEVEL, WBS_ID, LFT,RGT FROM PROPOSAL_WBS PW WHERE PROPOSAL_REV_ID = 7000
(SAMPLE DATA)
NODE WBS
LEVEL WBS_ID NUMBER LFT RGT
0 7055 ROOT 1 24
1 7056 1 2 5
1 7088 2 6 9
2 7057 1.1 3 4
2 7089 2.1 7 8
2 7091 3.1 11 14
2 7103 3.2 15 16
2 7105 4.1 19 20
1 7090 3 10 17
3 7092 3.1.1 12 13
1 7104 4 18 23
2 7106 4.2 21 22
ALLOCATION_DETAIL( WBS_ID int, COST_ID int, PERIOD Date, AMOUNT Float)
sample data
WBS_ID , COST_ID , PERIOD , AMOUNT
7057 100 01-jan-2005 5000
7057 100 01-feb-2005 2000
7057 100 01-mar-2005 1000
7057 100 01-apr-2005 6000
7057 100 01-may-2005 3000
7057 100 01-jun-2005 45000
7106 100 01-mar-2005 8000
7106 100 01-apr-2005 7000
7106 100 01-may-2005 9000
Now the PORP_TABLE has got the parents and childs. Only the leaf nodes in the hierarchy has the values stored in the ALLOCATION_DETAIL table. Now here is the scenario
In the example 7055 is the root WBS . The Leaf WBS are the one with max extension in the wbs number ( in this case it is 1.1, 2.1, 3.1.1, 3.2, 4.1 and 4.2)
Now the Starting period for each leaf node in the ALLOCATION_TABLE could be differrent . What that means is WBS 1.1 could start in Jan -2003 and WBS 3.1 Could be Jul-2005 . So the ending perios are also differrent for differrent WBS . Some can span 2 years some can 5 years.
So how to write a query so it retrieves the value for all the Wbs starting from the MIN ( PERIOD ) upto the MAX(PERIOD), and it should roll up also. Now there is No connect by Prior or any analytic functions available for this . THIS NEEDS TO BE DONE ONLY THROUGH TRADITIONAL SQL STATEMENT . And NO DB FUNCTIONS CAN BE USED .
Now if the WBS is a parent node then it should have the sum of all its child nodes for the COST category.
SO THE RESULT SET SHOULD BRING LIKE THIS
WBS_NUMBER, PERIOD_NUMER, COST_CATEGORY , AMOUNT
ROOT
1
1.1
2
2.1
3
3.1
3.1.1
3.2
4
4.1
4.2
......Hi,
<br>Read String Aggregation Techniques</br>
<br>HTH,</br>
<br>Nicolas.</br> -
SQL help. Identify changes on a field.
Greetings!
PS/SQL is not an option for me. I need help to use SQL, if possible for the following scenario.
Oracle 10G.
Table : JOB_DATA
EMPLID, DATE_EFF, DEPTID, JOBCODE
100, 11/1/2012, 34567, MNG
100, 10/1/2012, 34567, SUP
100, 9/1/2012, 28967, MNG
100, 8/15/2012, 28967, SUP
100,6/30/2012,15879, MNG
I need to get the following records only, that is ,whenever changes in Department ID.
100, 10/1/2012, 34567, SUP
100, 8/15/2012, 28967, SUP
100,6/30/2012,15879, MNG
Thanks in advance.Hi Rama,
Next time please post table structure and sample data.
Please read SQL and PL/SQL FAQ
Here another way to do it:
WITH job_data AS
SELECT 100 emplid, TO_DATE('11/1/2012', 'MM/DD/YYYY') date_eff, 34567 deptid, 'MNG' jobcode FROM DUAL UNION ALL
SELECT 100 emplid, TO_DATE('10/1/2012', 'MM/DD/YYYY') date_eff, 34567 deptid, 'SUP' jobcode FROM DUAL UNION ALL
SELECT 100 emplid, TO_DATE('9/1/2012' , 'MM/DD/YYYY') date_eff, 28967 deptid, 'MNG' jobcode FROM DUAL UNION ALL
SELECT 100 emplid, TO_DATE('8/15/2012', 'MM/DD/YYYY') date_eff, 28967 deptid, 'SUP' jobcode FROM DUAL UNION ALL
SELECT 100 emplid, TO_DATE('6/30/2012', 'MM/DD/YYYY') date_eff, 15879 deptid, 'MNG' jobcode FROM DUAL
SELECT emplid, date_eff, deptid, jobcode
FROM (SELECT j.*
, DENSE_RANK() OVER (PARTITION BY emplid, deptid ORDER BY date_eff) rn
FROM job_data j
WHERE rn = 1
ORDER BY date_eff DESC;
EMPLID DATE_EFF DEPTID JOBCODE
100 10/01/2012 34567 SUP
100 08/15/2012 28967 SUP
100 06/30/2012 15879 MNG PARTITION BY emplid, deptid means that any time this combination of value is changing the rank will start again from 1.
The rank is ordered by date_eff.
I assume that you don't have the same date_eff for more than one record. If you have same date_eff for more than one record you need to explain what will be the logic in this case.
Regards.
Al
Edited by: Alberto Faenza on Nov 15, 2012 8:50 PM
Clarifications added -
Hi all,
Can we seek for help in SQL?
I have an system analysis which calculate the concurrent usage of a computer system.
I have add all data which is stored in .CSV file and then insert into a table named as Activity.
Can we select the data from activity and generate an Excel file which report the calculation on the concurrent usage of a computer system?fionanycheng wrote:
Hi all,
Can we seek for help in SQL?
I have an system analysis which calculate the concurrent usage of a computer system.
I have add all data which is stored in .CSV file and then insert into a table named as Activity.
Can we select the data from activity and generate an Excel file which report the calculation on the concurrent usage of a computer system?
So, you have data in a .csv file
Then you load that data into a relational database.
Now you want to select that data and place it back into a file for Excel ......
Hmm.. Makes me wonder why you bothered with the database in the middle.
the database itself is more than capable of efficiently performing your calculations (assuming a reasonably rational table design).
Maybe you could provide a better description of the business problem to be solved, instead of a pre-conceived (and possibly ill-conceived) technical solution to that unspecified business problem. The we could provide better advice. -
Dear Experts,
If my below query returns any rows, I want this to trigger an alert, I'm writing a shell script to do this, need help how to implement the SQL part of it. I don't think IF EXISTS is directly supported in oracle, is there a PL/SQL alternative to check below query returns any rows and if so, I need to set an alert, with row data emailed out to group.
select a.col1 from table1 a
MINUS
select b.col1 from table2 b;ThanksIn a general case you should use the Stefanetti-Kyte method as suggested in {thread:id=2432829} where you could read:
If a row is duplicated in A , then MINUS will "de-dup" it silently
select *
from (select 1 from dual union all
select 1 from dual union all
select 1 from dual union all
select 2 from dual union all
select 3 from dual
) a
minus
(select 1 from dual union all
select 2 from dual union all
select 3 from dual
) bRegards
Etbin -
Grouping Rules in PL/SQL Help needed
Hi I have a requirement where I wan to group based on the below scenario
I have a Table_A
Table_A strcuture:
create table table_a
code number,
name varchar2 (100),
city varchar2 (100),
dept varchar2 (100));;
insert into table_a
values
1,'ABC','EA','A');
insert into table_a
values
1,'ABC','EA1','A');
insert into table_a
values
2,'BCD','EA2','A');
insert into table_a
values
2,'ABC','EA3','A'');
insert into table_a
values
3,'KBC','EA,'A');
insert into table_a
values
3,'ABC','EA,'A');
wan to group the above table data based on name,city,dept
I wan the data as
1 ABC EA1 A
BCD EA2 A
2 BCD EA2 A
ABC EA3 A
3 KBC EA A
BCD EA A
Kindly any help will be needful for meThis is more of a presentation layer problem I would not handle it in SQL. For example if the presentation layer is SQL Plus you can use BREAK command, like this.
SQL> select * from table_a;
CODE NAME CITY DEPT
1 ABC EA A
1 ABC EA1 A
2 BCD EA2 A
2 ABC EA3 A
3 KBC EA A
3 ABC EA A
6 rows selected.
SQL> break on code
SQL>
SQL> select * from table_a order by code;
CODE NAME CITY DEPT
1 ABC EA A
ABC EA1 A
2 BCD EA2 A
ABC EA3 A
3 KBC EA A
ABC EA A
6 rows selected.
But if you still wish to do it in SQL the right way is to use ROW_NUMBER like this.
SQL> clear breaks
breaks cleared
SQL>
SQL> select * from table_a;
CODE NAME CITY DEPT
1 ABC EA A
1 ABC EA1 A
2 BCD EA2 A
2 ABC EA3 A
3 KBC EA A
3 ABC EA A
6 rows selected.
SQL> select decode(rno, 1, code) code
2 , name
3 , city
4 , dept
5 from (
6 select row_number() over(partition by code order by name) rno
7 , t.*
8 from table_a t
9 )
10 /
CODE NAME CITY DEPT
1 ABC EA A
ABC EA1 A
2 ABC EA3 A
BCD EA2 A
3 ABC EA A
KBC EA A
6 rows selected. -
Oracle SQL HELP with convert GMT to EST and DST and Date offset
Hi, I have a query that does not seem to work trying to convert a date field that is in GMT to est and using extract(timezone_hour FROM TO_TIMESTAMP_TZ as an offsetr
HEre is my sql
dtl.start_dt_gmt + (extract(timezone_hour FROM TO_TIMESTAMP_TZ( dtl.start_dt_gmt,'DD-MON-YYYY HH24:MI:SS TZH:TZM'))/24 ) START_DT_Local
If the date (dtl.start_dt_gmt) is may 1 and gmt starts at 04:00 AM , the extract offset produces -4
However, if the date (dtl.start_dt_gmt) is Feb 1 which begins at 05:00 AM GMT, the date offset still gives 04. What am i doing wrong? Any help would be appreciated. Thanks.
SaulIf your data is not associated with timezone then you'll have to use something like
case when dt between A and B then dt-1/24 else dt end; <-- This will give you 1 hour back of EDT. So, as far as concern at database level, it is nothing to do at db level, because db is used by application, so you need to code in the app.
Oracle never actually changes a TIMEZONE column value when you set your system to be on daylight savings time. There are several built-in DST DATE conversion functions for changing to daylight savings time:
current_date
current_timestamp
localtimestamp
dbtimezone
sessiontimezone
extract
from_tz
to_timestamp
to_timestamp_tz
to_yminterval tz_offset
http://dba-oracle.com/t_oracle_daylight_saving_time_dst_date_conversion.htm
Regards
Girish Sharma -
Connect by - sql help : getting error ORA-01489: result of string concatena
here is an sql query and I am trying to cook a decode but since there are many columns invloved when I am trying to run this I am getting the following error:
ORA-01489: result of string concatenation is too long
Any kind of help is appreciated, I need to get this going otherwise I am dead :(
Regards
Rahul
SQL:
select sys_connect_by_path(c.decode_prep,'-') decode_prep
from (select 'DECODE(BIAPPS_11.'||substr(b.all_cols,instr(b.all_cols,',',1,a.rn)+1,instr(b.all_cols,',',1,a.rn+1)-instr(b.all_cols,',',1,a.rn)-1)||','||'RAHULKALRA.'||substr(b.all_cols,instr(b.all_cols,',',1,a.rn)+1,instr(b.all_cols,',',1,a.rn+1)-instr(b.all_cols,',',1,a.rn)-1)||',''1'',''0'')' decode_prep, rownum curr, rownum -1 prev
from (select rownum rn
from dual connect by rownum <=
(select (length('ROW_WID,COST_CENTER_NUM,COST_CENTER_NAME,COST_CENTER_DESC,CONTROL_AREA_NUM,CONTROL_AREA_NAME,CATEGORY_CODE,CATEGORY_NAME,CATEGORY_DESC,MANAGER_NAME,CURRENCY_CODE,CURRENCY_NAME,LANGUAGE_CODE,LANGUAGE_NAME,ST_ADDRESS1,ST_ADDRESS2,POST_OFFICE_BOX,CITY_NAME,STATE_CODE,STATE_NAME,REGION_CODE,REGION_NAME,COUNTRY_CODE,COUNTRY_NAME,POSTAL_CODE,PHONE_NUM,FAX_NUM,CSCN_HIER1_CODE,CSCN_HIER1_NAME,CSCN_HIER2_CODE,CSCN_HIER2_NAME,CSCN_HIER3_CODE,CSCN_HIER3_NAME,CSCN_HIER4_CODE,CSCN_HIER4_NAME,CSCN_HIER5_CODE,CSCN_HIER5_NAME,CSCN_HIER6_CODE,CSCN_HIER6_NAME,ACTIVE_FLG,CREATED_BY_WID,CHANGED_BY_WID,CREATED_ON_DT,CHANGED_ON_DT,AUX1_CHANGED_ON_DT,AUX2_CHANGED_ON_DT,AUX3_CHANGED_ON_DT,AUX4_CHANGED_ON_DT,SRC_EFF_FROM_DT,SRC_EFF_TO_DT,EFFECTIVE_FROM_DT,EFFECTIVE_TO_DT,DELETE_FLG,CURRENT_FLG,W_INSERT_DT,W_UPDATE_DT,DATASOURCE_NUM_ID,ETL_PROC_WID,INTEGRATION_ID,SET_ID,TENANT_ID,X_CUSTOM')
- length(replace('ROW_WID,COST_CENTER_NUM,COST_CENTER_NAME,COST_CENTER_DESC,CONTROL_AREA_NUM,CONTROL_AREA_NAME,CATEGORY_CODE,CATEGORY_NAME,CATEGORY_DESC,MANAGER_NAME,CURRENCY_CODE,CURRENCY_NAME,LANGUAGE_CODE,LANGUAGE_NAME,ST_ADDRESS1,ST_ADDRESS2,POST_OFFICE_BOX,CITY_NAME,STATE_CODE,STATE_NAME,REGION_CODE,REGION_NAME,COUNTRY_CODE,COUNTRY_NAME,POSTAL_CODE,PHONE_NUM,FAX_NUM,CSCN_HIER1_CODE,CSCN_HIER1_NAME,CSCN_HIER2_CODE,CSCN_HIER2_NAME,CSCN_HIER3_CODE,CSCN_HIER3_NAME,CSCN_HIER4_CODE,CSCN_HIER4_NAME,CSCN_HIER5_CODE,CSCN_HIER5_NAME,CSCN_HIER6_CODE,CSCN_HIER6_NAME,ACTIVE_FLG,CREATED_BY_WID,CHANGED_BY_WID,CREATED_ON_DT,CHANGED_ON_DT,AUX1_CHANGED_ON_DT,AUX2_CHANGED_ON_DT,AUX3_CHANGED_ON_DT,AUX4_CHANGED_ON_DT,SRC_EFF_FROM_DT,SRC_EFF_TO_DT,EFFECTIVE_FROM_DT,EFFECTIVE_TO_DT,DELETE_FLG,CURRENT_FLG,W_INSERT_DT,W_UPDATE_DT,DATASOURCE_NUM_ID,ETL_PROC_WID,INTEGRATION_ID,SET_ID,TENANT_ID,X_CUSTOM',',')))+1 total_cols
from dual)) a, (select ','||'ROW_WID,COST_CENTER_NUM,COST_CENTER_NAME,COST_CENTER_DESC,CONTROL_AREA_NUM,CONTROL_AREA_NAME,CATEGORY_CODE,CATEGORY_NAME,CATEGORY_DESC,MANAGER_NAME,CURRENCY_CODE,CURRENCY_NAME,LANGUAGE_CODE,LANGUAGE_NAME,ST_ADDRESS1,ST_ADDRESS2,POST_OFFICE_BOX,CITY_NAME,STATE_CODE,STATE_NAME,REGION_CODE,REGION_NAME,COUNTRY_CODE,COUNTRY_NAME,POSTAL_CODE,PHONE_NUM,FAX_NUM,CSCN_HIER1_CODE,CSCN_HIER1_NAME,CSCN_HIER2_CODE,CSCN_HIER2_NAME,CSCN_HIER3_CODE,CSCN_HIER3_NAME,CSCN_HIER4_CODE,CSCN_HIER4_NAME,CSCN_HIER5_CODE,CSCN_HIER5_NAME,CSCN_HIER6_CODE,CSCN_HIER6_NAME,ACTIVE_FLG,CREATED_BY_WID,CHANGED_BY_WID,CREATED_ON_DT,CHANGED_ON_DT,AUX1_CHANGED_ON_DT,AUX2_CHANGED_ON_DT,AUX3_CHANGED_ON_DT,AUX4_CHANGED_ON_DT,SRC_EFF_FROM_DT,SRC_EFF_TO_DT,EFFECTIVE_FROM_DT,EFFECTIVE_TO_DT,DELETE_FLG,CURRENT_FLG,W_INSERT_DT,W_UPDATE_DT,DATASOURCE_NUM_ID,ETL_PROC_WID,INTEGRATION_ID,SET_ID,TENANT_ID,X_CUSTOM'||',' all_cols from dual) b) c
start with curr = 1
connect by prior curr = prev
order by length(sys_connect_by_path(c.decode_prep,'-')) desc
same as above sql only difference is here I am pulling the first record from the result set which above query returns :
select ltrim(replace(decode_prep,'-','||'),'||') decode_prep
from (select sys_connect_by_path(c.decode_prep,'-') decode_prep
from (select 'DECODE(BIAPPS_11.'||substr(b.all_cols,instr(b.all_cols,',',1,a.rn)+1,instr(b.all_cols,',',1,a.rn+1)-instr(b.all_cols,',',1,a.rn)-1)||','||'RAHULKALRA.'||substr(b.all_cols,instr(b.all_cols,',',1,a.rn)+1,instr(b.all_cols,',',1,a.rn+1)-instr(b.all_cols,',',1,a.rn)-1)||',''1'',''0'')' decode_prep, rownum curr, rownum -1 prev
from (select rownum rn
from dual connect by rownum <=
(select (length('ROW_WID,COST_CENTER_NUM,COST_CENTER_NAME,COST_CENTER_DESC,CONTROL_AREA_NUM,CONTROL_AREA_NAME,CATEGORY_CODE,CATEGORY_NAME,CATEGORY_DESC,MANAGER_NAME,CURRENCY_CODE,CURRENCY_NAME,LANGUAGE_CODE,LANGUAGE_NAME,ST_ADDRESS1,ST_ADDRESS2,POST_OFFICE_BOX,CITY_NAME,STATE_CODE,STATE_NAME,REGION_CODE,REGION_NAME,COUNTRY_CODE,COUNTRY_NAME,POSTAL_CODE,PHONE_NUM,FAX_NUM,CSCN_HIER1_CODE,CSCN_HIER1_NAME,CSCN_HIER2_CODE,CSCN_HIER2_NAME,CSCN_HIER3_CODE,CSCN_HIER3_NAME,CSCN_HIER4_CODE,CSCN_HIER4_NAME,CSCN_HIER5_CODE,CSCN_HIER5_NAME,CSCN_HIER6_CODE,CSCN_HIER6_NAME,ACTIVE_FLG,CREATED_BY_WID,CHANGED_BY_WID,CREATED_ON_DT,CHANGED_ON_DT,AUX1_CHANGED_ON_DT,AUX2_CHANGED_ON_DT,AUX3_CHANGED_ON_DT,AUX4_CHANGED_ON_DT,SRC_EFF_FROM_DT,SRC_EFF_TO_DT,EFFECTIVE_FROM_DT,EFFECTIVE_TO_DT,DELETE_FLG,CURRENT_FLG,W_INSERT_DT,W_UPDATE_DT,DATASOURCE_NUM_ID,ETL_PROC_WID,INTEGRATION_ID,SET_ID,TENANT_ID,X_CUSTOM')
- length(replace('ROW_WID,COST_CENTER_NUM,COST_CENTER_NAME,COST_CENTER_DESC,CONTROL_AREA_NUM,CONTROL_AREA_NAME,CATEGORY_CODE,CATEGORY_NAME,CATEGORY_DESC,MANAGER_NAME,CURRENCY_CODE,CURRENCY_NAME,LANGUAGE_CODE,LANGUAGE_NAME,ST_ADDRESS1,ST_ADDRESS2,POST_OFFICE_BOX,CITY_NAME,STATE_CODE,STATE_NAME,REGION_CODE,REGION_NAME,COUNTRY_CODE,COUNTRY_NAME,POSTAL_CODE,PHONE_NUM,FAX_NUM,CSCN_HIER1_CODE,CSCN_HIER1_NAME,CSCN_HIER2_CODE,CSCN_HIER2_NAME,CSCN_HIER3_CODE,CSCN_HIER3_NAME,CSCN_HIER4_CODE,CSCN_HIER4_NAME,CSCN_HIER5_CODE,CSCN_HIER5_NAME,CSCN_HIER6_CODE,CSCN_HIER6_NAME,ACTIVE_FLG,CREATED_BY_WID,CHANGED_BY_WID,CREATED_ON_DT,CHANGED_ON_DT,AUX1_CHANGED_ON_DT,AUX2_CHANGED_ON_DT,AUX3_CHANGED_ON_DT,AUX4_CHANGED_ON_DT,SRC_EFF_FROM_DT,SRC_EFF_TO_DT,EFFECTIVE_FROM_DT,EFFECTIVE_TO_DT,DELETE_FLG,CURRENT_FLG,W_INSERT_DT,W_UPDATE_DT,DATASOURCE_NUM_ID,ETL_PROC_WID,INTEGRATION_ID,SET_ID,TENANT_ID,X_CUSTOM',',')))+1 total_cols
from dual)) a, (select ','||'ROW_WID,COST_CENTER_NUM,COST_CENTER_NAME,COST_CENTER_DESC,CONTROL_AREA_NUM,CONTROL_AREA_NAME,CATEGORY_CODE,CATEGORY_NAME,CATEGORY_DESC,MANAGER_NAME,CURRENCY_CODE,CURRENCY_NAME,LANGUAGE_CODE,LANGUAGE_NAME,ST_ADDRESS1,ST_ADDRESS2,POST_OFFICE_BOX,CITY_NAME,STATE_CODE,STATE_NAME,REGION_CODE,REGION_NAME,COUNTRY_CODE,COUNTRY_NAME,POSTAL_CODE,PHONE_NUM,FAX_NUM,CSCN_HIER1_CODE,CSCN_HIER1_NAME,CSCN_HIER2_CODE,CSCN_HIER2_NAME,CSCN_HIER3_CODE,CSCN_HIER3_NAME,CSCN_HIER4_CODE,CSCN_HIER4_NAME,CSCN_HIER5_CODE,CSCN_HIER5_NAME,CSCN_HIER6_CODE,CSCN_HIER6_NAME,ACTIVE_FLG,CREATED_BY_WID,CHANGED_BY_WID,CREATED_ON_DT,CHANGED_ON_DT,AUX1_CHANGED_ON_DT,AUX2_CHANGED_ON_DT,AUX3_CHANGED_ON_DT,AUX4_CHANGED_ON_DT,SRC_EFF_FROM_DT,SRC_EFF_TO_DT,EFFECTIVE_FROM_DT,EFFECTIVE_TO_DT,DELETE_FLG,CURRENT_FLG,W_INSERT_DT,W_UPDATE_DT,DATASOURCE_NUM_ID,ETL_PROC_WID,INTEGRATION_ID,SET_ID,TENANT_ID,X_CUSTOM'||',' all_cols from dual) b) c
start with curr = 1
connect by prior curr = prev
order by length(sys_connect_by_path(c.decode_prep,'-')) desc)
where rownum = 1
Edited by: Mac_Freak_Rahul on Nov 28, 2012 1:31 AM : in the first sql ')'
removed after desc in the last line so now this query will run and throw an error.Clearly your error is because the string concatenation you are doing with sys_connect_by_path is exceeding the 4000 bytes permitted by SQL.
In that case you need to concatenate your data into a CLOB datatype, for which you'll need a CLOB aggregation function...
create or replace type clobagg_type as object
text clob,
static function ODCIAggregateInitialize(sctx in out clobagg_type) return number,
member function ODCIAggregateIterate(self in out clobagg_type, value in clob) return number,
member function ODCIAggregateTerminate(self in clobagg_type, returnvalue out clob, flags in number) return number,
member function ODCIAggregateMerge(self in out clobagg_type, ctx2 in clobagg_type) return number
create or replace type body clobagg_type is
static function ODCIAggregateInitialize(sctx in out clobagg_type) return number is
begin
sctx := clobagg_type(null) ;
return ODCIConst.Success ;
end;
member function ODCIAggregateIterate(self in out clobagg_type, value in clob) return number is
begin
self.text := self.text || value ;
return ODCIConst.Success;
end;
member function ODCIAggregateTerminate(self in clobagg_type, returnvalue out clob, flags in number) return number is
begin
returnValue := self.text;
return ODCIConst.Success;
end;
member function ODCIAggregateMerge(self in out clobagg_type, ctx2 in clobagg_type) return number is
begin
self.text := self.text || ctx2.text;
return ODCIConst.Success;
end;
end;
create or replace function clobagg(input clob) return clob
deterministic
parallel_enable
aggregate using clobagg_type;
SQL> select trim(',' from clobagg(ename||',')) as enames from emp;
ENAMES
SMITH,ALLEN,WARD,JONES,MARTIN,BLAKE,CLARK,SCOTT,KING,TURNER,ADAMS,JAMES,FORD,MILLER
SQL> ed
Wrote file afiedt.buf
1 with t as
2 (select 'PFL' c1, 0 c2,110 c3 from dual union all
3 select 'LHL', 0 ,111 from dual union all
4 select 'PHL', 1, 111 from dual union all
5 select 'CHL', 2, 111 from dual union all
6 select 'DHL', 0, 112 from dual union all
7 select 'VHL', 1, 112 from dual union all
8 select 'CPHL', 0, 114 from dual union all
9 select 'WDCL', 1, 114 from dual union all
10 select 'AHL' ,2 ,114 from dual union all
11 select 'NFDL', 3, 114 from dual)
12 --
13 -- end of test data
14 --
15 select trim(clobagg(c1||' ')) as c1, c3
16 from (select * from t order by c3, c2)
17 group by c3
18* order by c3
SQL> /
C1 C3
PFL 110
LHL CHL PHL 111
DHL VHL 112
CPHL AHL NFDL WDCL 114 -
Hi All,
I have a PL/SQL stored procedure with close to 20+ arguments, many of which are optional in between. Hence
the procedure call involve many of the arguments which are null. for e.g
procedureName(arg1,arg2....arg20)
Call procedureName(1, 2, null, null, 3, 4, null...)
Instead of the above approach, I am planning to send two Varray as argument which specify key and value respectively. This would help me to only send argument which are not null and also would help me to dynamically add additional 'where clause' with out changing the PL/SQL procedure.
I am wondering about the usage of Varray from a performance perspective and also as a industry practice.
Any assistance will be of great help.
RameshBy key and value, I am assuming that you mean parameter name and value for that paramter. If that is correct, then, depending on where you are calling this from and how you are calling it, I would be tempted to go the named parameter route as Karthik suggested.
If you want to go with the array approach, I would do it with a single array of records (created in PL/SQL) or objects (created in SQL). The main advantage of using an array of objects is that, because they are database objects, they can usually be used by calling code in other languages, nt just PL/SQL.
Something like:
SQL> CREATE TYPE param_tp AS OBJECT (
2 param_name VARCHAR2(30),
3 param_value VARCHAR2(50));
4 /
Type created.
SQL> CREATE TYPE param_tbl AS TABLE OF param_tp;
2 /
Type created.If you go this route, you will need to check carefully in your procedure to make sure that the values passed as parameters can actually be converted to the correct type to match the original parameter types. If you will be passing dates, you will also need to ensure that all callers are aware of the specific date string format you will be expecting so that you will not have conversion issues.
John -
SQL help, how to group by column A and then get top frequency of column B?
Assuming the following table, operation represents each kind of transaction operation
table operation employee other column c
'<' id1
'<' id1
'<' id2
'*' id3
'/' id1
question, for each operation, which employee do the most? which employee do the 2nd most?
I thought I need to group by operation first, and then get the frequency of employee, and get the top 1 or top 2 frequency highest.
But I am stuct, and do not know how to write the sql.998408 wrote:
Assuming the following table, operation represents each kind of transaction operation
table operation employee other column c
'<' id1
'<' id1
'<' id2
'*' id3
'/' id1
question, for each operation, which employee do the most? which employee do the 2nd most?
I thought I need to group by operation first, and then get the frequency of employee, and get the top 1 or top 2 frequency highest.
But I am stuct, and do not know how to write the sql.Welcome to OTN. Ops! posted in wrong forum. Just post in {forum:id=75}. Before posting there close this thread marking as answered.
If someone's response is helpful or correct, please mark it accordingly.*
Maybe you are looking for
-
I have purchased music from iTunes but no Purchased Music playlist. How do I create one? Thx, Vermont Compaq Presario Windows XP
-
Error 1324 whenever new installs or updates are run.
We recently upgraded from XP to Windows 7 Pro 64-bit and every time a new program is installed or an update is run for an existing program we receive a 1324 error My Documents have an invalid path. We do have users My Documents re-directed to two di
-
Hi all I'm getting these type of errors when accessing content on my web server Dows anyone know why this could happen??? [23/Nov/2004:21:17:35] warning ( 2167): for host 192.168.107.20 trying to GET /RCU/css/ing_exchange.css, finish-response re
-
How to get repeat/shuffle blue arrow to display in the 12.1.1.4 version
I am able to right click at the top where the timer is and see the shuffle songs and repeat but nothing happens and it does not show the blue arrow so that I can click on to repeat song...It just went away and I can't get it back since I downloaded t
-
Need help with ADF Runtime 11.1.1.6 installation into WLS 10.3.6
Hello all! I have JDeveloper 11.1.1.6 and a standalone WLS 10.3.6 (in seperate Middleware homes as advised). I need to install the ADF Runtime libraries into the WLS but I don't know how. I downloaded them from OTN, the zip file is called ofm_appdev_