Query in analytic function
Hi,
I am using below query
select * from
SELECT FLAG,S_DATE,ROW_NUMBER() OVER (PARTITION BY
flag order by S_DATE,FLAG ) as d
FROM table_name
ORDER BY S_DATE
which gives below output
Flag | S_DATE | D
Y | 2/27/2012 5:33 | 1
Y | 2/27/2012 5:34 | 2
Y | 2/27/2012 5:34 | 3
N | 2/27/2012 5:34 | 1
N | 2/27/2012 5:34 | 2
N | 2/27/2012 5:34 | 3
N | 2/27/2012 5:35 | 4
N | 2/27/2012 5:35 | 5
Y | 2/27/2012 5:36 | 4
Y | 2/27/2012 5:36 | 5
Y | 2/27/2012 5:36 | 6
But i want the output to be in below order there is change in last 3 rows
Flag | S_DATE | D
Y | 2/27/2012 5:33 | 1
Y | 2/27/2012 5:34 | 2
Y | 2/27/2012 5:34 | 3
N | 2/27/2012 5:34 | 1
N | 2/27/2012 5:34 | 2
N | 2/27/2012 5:34 | 3
N | 2/27/2012 5:35 | 4
N | 2/27/2012 5:35 | 5
Y | 2/27/2012 5:36 | 1
Y | 2/27/2012 5:36 | 2
Y | 2/27/2012 5:36 | 3
ihave used the analytic function.
Edited by: user8858890 on Feb 27, 2012 2:00 AM
Hi,
user8858890 wrote:
... But i want the output to be in below order there is change in last 3 rows
Flag | S_DATE | D
Y | 2/27/2012 5:33 | 1
Y | 2/27/2012 5:34 | 2
Y | 2/27/2012 5:34 | 3
N | 2/27/2012 5:34 | 1
N | 2/27/2012 5:34 | 2
N | 2/27/2012 5:34 | 3
N | 2/27/2012 5:35 | 4
N | 2/27/2012 5:35 | 5
Y | 2/27/2012 5:36 | 1
Y | 2/27/2012 5:36 | 2
Y | 2/27/2012 5:36 | 3
Why do you want the last 3 rows (which have flag = 'Y') to be numbered 1, 2, 3, when the first 3 rows (which also have flag = 'Y') already have numbers 1, 2 and 3? Do you want a separate #1 whenevever there is a group of consecutive rows (when ordered by s_date) that have the same flag? If so, then you have to identify the groups, like this:
WITH got_grp_id AS
SELECT flag
, s_date
, ROWID AS r_id
, ROW_NUMBER () OVER ( ORDER BY s_date
, ROWID
- ROW_NUMBER () OVER ( PARTITION BY flag
ORDER BY s_date
, ROWID
) AS grp_id
FROM table_name
SELECT flag
, s_date
, ROW_NUMBER () OVER ( PARTITION BY flag
, grp_id
ORDER BY s_date
, r_id
) AS d
FROM got_grp_id
ORDER BY s_date
, grp_id
, d
;This assumes that each row can be uniquely idendified, so that the order is unambiguous. In your sample data, there are completely identical rows, so I used ROWID to uniquely identify the rows. Using ROWID assumes that table_name is a real table, not just a result set.
I hope this answers your question.
If not, post a little sample data (CREATE TABLE and INSERT statements, relevant columns only) for all the tables involved, and the results you want from that data.
Explain, using specific examples, how you get those results from that data.
Always say what version of Oracle you're using.
Similar Messages
-
SQL Query With analytical function
Hi
Below is the scenario which i am looking for in sql query using analytical functions
I/p
Col1
50
0
-150
-200
300
-100
-300
500
-100
O/p
Col1 col2
50 0
0 0
-150 -100
-200 -200
300 0
-100 0
-300 -100
500 400
-100 0Any help really appreciated
Thanks in advance
Edited by: unique on Aug 10, 2010 4:53 AM
Edited by: unique on Aug 10, 2010 4:55 AM
Edited by: unique on Aug 10, 2010 4:55 AMOh,In this case,There is OLAP solution ;-)
OLAP samples of my homepage http://www.geocities.jp/oraclesqlpuzzle/oracle-sql1-olap.html
with work(SK,Val) as(
select 1, 50 from dual union
select 2, 0 from dual union
select 3,-150 from dual union
select 4,-200 from dual union
select 5, 300 from dual union
select 6,-100 from dual union
select 7,-300 from dual union
select 8, 500 from dual union
select 9,-100 from dual)
select SK,Val,GID,
case when Val > 0
then greatest(0,sum(Val) over(partition by GID))
else Least(0,Val+greatest(0,sum(Val) over(partition by GID
order by SK rows between unbounded preceding
and 1 preceding)))
end as COL3
from (select SK,Val,
sum(greatest(0,sign(Val))) over(order by SK) as GID
from work)
order by SK;
SK VAL GID COL3
1 50 1 0
2 0 1 0
3 -150 1 -100
4 -200 1 -200
5 300 2 0
6 -100 2 0
7 -300 2 -100
8 500 3 400
9 -100 3 0 -
How can rewrite the Query using Analytical functions ?
Hi,
I have the SQL script as shown below ,
SELECT cd.cardid, cd.cardno,TT.TRANSACTIONTYPECODE,TT.TRANSACTIONTYPEDESC DESCRIPTION,
SUM (NVL (CASE tt.transactiontypecode
WHEN 'LOAD_ACH'
THEN th.transactionamount
END, 0)
) AS load_ach,
SUM
(NVL (CASE tt.transactiontypecode
WHEN 'FUND_TRANSFER_RECEIVED'
THEN th.transactionamount
END,
0
) AS Transfersin,
( SUM (NVL (CASE tt.transactiontypecode
WHEN 'FTRNS'
THEN th.transactionamount
END,
0
) +
SUM (NVL (CASE tt.transactiontypecode
WHEN 'SEND_MONEY'
THEN th.transactionamount
END, 0)
)) AS Transferout,
SUM (NVL (CASE tt.transactiontypecode
WHEN 'WITHDRAWAL_ACH'
THEN th.transactionamount
END, 0)
) AS withdrawal_ach,
SUM (NVL (CASE tt.transactiontypecode
WHEN 'WITHDRAWAL_CHECK'
THEN th.transactionamount
END, 0)
) AS withdrawal_check,
( SUM (NVL (CASE tt.transactiontypecode
WHEN 'WITHDRAWAL_CHECK_FEE'
THEN th.transactionamount
END,
0
) +
SUM (NVL (CASE tt.transactiontypecode
WHEN 'REJECTED_ACH_LOAD_FEE'
THEN th.transactionamount
END,
0
) +
SUM (NVL (CASE tt.transactiontypecode
WHEN 'WITHDRAWAL_ACH_REV'
THEN th.transactionamount
END,
0
) +
SUM (NVL (CASE tt.transactiontypecode
WHEN 'WITHDRAWAL_CHECK_REV'
THEN th.transactionamount
END,
0
) +
SUM
(NVL (CASE tt.transactiontypecode
WHEN 'WITHDRAWAL_CHECK_FEE_REV'
THEN th.transactionamount
END,
0
) +
SUM
(NVL (CASE tt.transactiontypecode
WHEN 'REJECTED_ACH_LOAD_FEE_REV'
THEN th.transactionamount
END,
0
) +
SUM (NVL (CASE tt.transactiontypecode
WHEN 'OVERDRAFT_FEE_REV'
THEN th.transactionamount
END, 0)
) +
SUM (NVL (CASE tt.transactiontypecode
WHEN 'STOP_CHECK_FEE_REV'
THEN th.transactionamount
END,
0
) +
SUM (NVL (CASE tt.transactiontypecode
WHEN 'LOAD_ACH_REV'
THEN th.transactionamount
END, 0)
) +
SUM (NVL (CASE tt.transactiontypecode
WHEN 'OVERDRAFT_FEE'
THEN th.transactionamount
END, 0)
) +
SUM (NVL (CASE tt.transactiontypecode
WHEN 'STOP_CHECK_FEE'
THEN th.transactionamount
END, 0)
)) AS Fee,
th.transactiondatetime
FROM carddetail cd,
transactionhistory th,
transactiontype tt,
(SELECT rmx_a.cardid, rmx_a.endingbalance prev_balance, rmx_a.NUMBEROFDAYS
FROM rmxactbalreport rmx_a,
(SELECT cardid, MAX (reportdate) reportdate
FROM rmxactbalreport
GROUP BY cardid) rmx_b
WHERE rmx_a.cardid = rmx_b.cardid AND rmx_a.reportdate = rmx_b.reportdate) a
WHERE th.transactiontypeid = tt.transactiontypeid
AND cd.cardid = th.cardid
AND cd.cardtype = 'P'
AND cd.cardid = a.cardid (+)
AND CD.CARDNO = '7116734387812758335'
--AND TT.TRANSACTIONTYPECODE = 'FUND_TRANSFER_RECEIVED'
GROUP BY cd.cardid, cd.cardno, numberofdays,th.transactiondatetime,tt.transactiontypecode,TT.TRANSACTIONTYPEDESC
Ouput of the above query is :
CARDID CARDNO TRANSACTIONTYPECODE DESCRIPTION LOAD_ACH TRANSFERSIN TRANSFEROUT WITHDRAWAL_ACH WITHDRAWAL_CHECK FEE TRANSACTIONDATETIME
6005 7116734387812758335 FUND_TRANSFER_RECEIVED Fund Transfer Received 0 3.75 0 0 0 0 21/09/2007 11:15:38 AM
6005 7116734387812758335 FUND_TRANSFER_RECEIVED Fund Transfer Received 0 272 0 0 0 0 05/10/2007 9:12:37 AM
6005 7116734387812758335 WITHDRAWAL_ACH Withdraw Funds via ACH 0 0 0 300 0 0 24/10/2007 3:43:54 PM
6005 7116734387812758335 SEND_MONEY Fund Transfer Sent 0 0 1 0 0 0 19/09/2007 1:17:48 PM
6005 7116734387812758335 FUND_TRANSFER_RECEIVED Fund Transfer Received 0 1 0 0 0 0 18/09/2007 7:25:23 PM
6005 7116734387812758335 LOAD_ACH Prepaid Deposit via ACH 300 0 0 0 0 0 02/10/2007 3:00:00 AM
I want the output like for Load_ACH there should be one record etc.,
Can any one help me , how can i rewrite the above query using analytical functions .,
SekharNot sure of your requirements but this mayhelp reduce your code;
<untested>
SUM (
CASE
WHEN tt.transactiontypecode IN
('WITHDRAWAL_CHECK_FEE', 'REJECTED_ACH_LOAD_FEE', 'WITHDRAWAL_ACH_REV', 'WITHDRAWAL_CHECK_REV',
'WITHDRAWAL_CHECK_FEE_REV', 'REJECTED_ACH_LOAD_FEE_REV', 'OVERDRAFT_FEE_REV','STOP_CHECK_FEE_REV',
'LOAD_ACH_REV', 'OVERDRAFT_FEE', 'STOP_CHECK_FEE')
THEN th.transactionamount
ELSE 0) feeAlso, you might want to edit your post and use [pre] and [/pre] tags around your code for formatting. -
Self-join query to Analytical function query
Hi All,
I have converted a self-join query to Analytical function query due to the performance reasons.
Query which is using Analytical function is giving the correct count as I compared it with query using Self-Join. Can you please tell what is wrong in the query.
==========================
Query using Self-Join
select count(1)
From (select t1.*, max(t1.dw_creation_dt) over (partition by t1.empl_id) pers_max_date
from ohr_pers_curr t1 ) pers
, (select t2.*, max(t2.dw_creation_dt) over (partition by t2.empl_id, t2.empl_rcd) job_max_date
from OHR_JOB_CURR t2) job
where pers.empl_id = job.empl_id
and pers.dw_creation_dt=pers.pers_max_date
and job.dw_creation_dt=job.job_max_date
and job.dummy_row_flag = 'N'
and pers.dw_creation_rsn_cd in ('N', 'U')
and job.dw_creation_rsn_cd in ('N', 'U')
================================================
Query Using Analytical function
select count(1)
From (select t1.*, max(t1.dw_creation_dt) over (partition by t1.empl_id) pers_max_date
from ohr_pers_curr t1 ) pers
, (select t2.*, max(t2.dw_creation_dt) over (partition by t2.empl_id, t2.empl_rcd) job_max_date
from OHR_JOB_CURR t2) job
where pers.empl_id = job.empl_id
and pers.dw_creation_dt=pers.pers_max_date
and job.dw_creation_dt=job.job_max_date
and job.dummy_row_flag = 'N'
and pers.dw_creation_rsn_cd in ('N', 'U')
and job.dw_creation_rsn_cd in ('N', 'U')
==================================Hi David,
The base is same but the problem is different.
As far as implementation concern these queries looks same, but when I see there counts, they do not match. I do not have any reason why this happening.
Regards
Gaurav -
Speed up query with analytic function
Hi
how can I speed up the query below ?
All time is in analytic function (WINDOW SORT)
Thanks for your help
11.2.0.1
Rows Row Source Operation
28987 HASH UNIQUE (cr=12677 pr=155778 pw=109730 time=25010 us cost=5502 size=3972960 card=14880)
1668196 WINDOW SORT (cr=12677 pr=155778 pw=109730 time=890411840 us cost=5502 size=3972960 card=14880)
1668196 HASH JOIN RIGHT OUTER (cr=12677 pr=0 pw=0 time=1069165 us cost=3787 size=3972960 card=14880)
30706 TABLE ACCESS FULL FLO_FML_EVENT (cr=270 pr=0 pw=0 time=7420 us cost=56 size=814158 card=30154)
194733 HASH JOIN RIGHT OUTER (cr=12407 pr=0 pw=0 time=571145 us cost=3730 size=3571200 card=14880)
613 VIEW (cr=342 pr=0 pw=0 time=489 us cost=71 size=23840 card=745)
613 HASH UNIQUE (cr=342 pr=0 pw=0 time=244 us cost=71 size=20115 card=745)
745 WINDOW SORT (cr=342 pr=0 pw=0 time=1736 us cost=71 size=20115 card=745)
745 MAT_VIEW ACCESS FULL MVECRF_CUR_QUERY (cr=342 pr=0 pw=0 time=1736 us cost=69 size=20115 card=745)
194733 HASH JOIN (cr=12065 pr=0 pw=0 time=431813 us cost=3658 size=3095040 card=14880)
43 MAT_VIEW ACCESS FULL MVECRF_VISIT_REVS (cr=3 pr=0 pw=0 time=0 us cost=2 size=946 card=43)
194733 HASH JOIN OUTER (cr=12062 pr=0 pw=0 time=292098 us cost=3656 size=2767680 card=14880)
194733 HASH JOIN OUTER (cr=10553 pr=0 pw=0 time=234394 us cost=2962 size=2574240 card=14880)
194733 HASH JOIN (cr=9999 pr=0 pw=0 time=379996 us cost=2570 size=2380800 card=14880)
30076 MAT_VIEW ACCESS FULL MVECRF_ACTIVATED_FORMS (cr=1817 pr=0 pw=0 time=28411 us cost=361 size=2000285 card=29855)
194733 HASH JOIN (cr=8182 pr=0 pw=0 time=209061 us cost=1613 size=9026301 card=97057)
628 MAT_VIEW ACCESS FULL MVECRF_STUDYVERSION_FORMS (cr=19 pr=0 pw=0 time=250 us cost=6 size=18212 card=628)
194733 MAT_VIEW ACCESS FULL MVECRF_FORMITEMS (cr=8163 pr=0 pw=0 time=80733 us cost=1606 size=12462912 card=194733)
132342 MAT_VIEW ACCESS FULL MVECRF_ITEM_SDV (cr=554 pr=0 pw=0 time=23678 us cost=112 size=1720446 card=132342)
221034 MAT_VIEW ACCESS FULL MVECRF_ITEMDATA (cr=1509 pr=0 pw=0 time=46459 us cost=299 size=2873442 card=221034)
SELECT
DISTINCT
'CL238093011' AS ETUDE,
FI.STUDYID,
FI.STUDYVERSIONID,
FI.SITEID,
FI.SUBJECTID,
FI.VISITID,
VR.VISITREFNAME,
FI.SUBJECTVISITID,
FI.FORMID,
FI.FORMINDEX,
SVF.FORMREFNAME,
SVF.FORMMNEMONIC AS FMLNOM,
EVENT_ITEM.EVENT AS EVENUM,
EVENT_ITEM.EVENT_ROW AS LIGNUM,
NULL AS CODVISEVE,
MIN(DID.MINENTEREDDATE)
OVER (
PARTITION BY FI.SUBJECTID, FI.VISITID, FI.FORMID, FI.FORMINDEX
AS ATTDAT1ERSAI,
MIN(IFSDV.ITEMFIRSTSDV)
OVER (
PARTITION BY FI.SUBJECTID, FI.VISITID, FI.FORMID, FI.FORMINDEX
AS ATTDAT1ERSDV,
MAX(IFSDV.ITEMFIRSTSDV)
OVER (
PARTITION BY FI.SUBJECTID, FI.VISITID, FI.FORMID, FI.FORMINDEX
AS ATTDATDERSDV,
DECODE (AF.SDVCOMPLETESTATE,
0,
'N',
1,
'Y')
AS ATTINDSDVCOP,
AF.FMINSDVCOMPLETESTATE AS ATTDAT1ERSDVCOP,
DECODE (AF.SDVPARTIALSTATE,
0,
'N',
1,
'Y')
AS ATTINDSDVPTL,
EVENT_ITEM.EVENT_RELECT AS ATTINDRVUMEDCOP,
DECODE (QUERY.NBQSTFML, NULL, 'N', 'Y') AS ATTINDQST,
DECODE (AF.MISSINGITEMSSTATE,
0,
'N',
1,
'Y')
AS ATTINDITMABS,
DECODE (AF.FROZENSTATE,
0,
'N',
1,
'Y')
AS ATTINDETACON,
AF.FMINFROZENSTATE AS ATTDAT1ERCON,
AF.FMAXFROZENSTATE AS ATTDATDERCON,
DECODE (AF.DELETEDSTATE,
0,
'N',
1,
'Y')
AS ATTINDETASPR,
EVENT_ITEM.ROW_DELETED AS ATTINDLIGSPR
FROM CL238093011.MVECRF_FORMITEMS FI,
CL238093011.MVECRF_STUDYVERSION_FORMS SVF,
CL238093011.MVECRF_ACTIVATED_FORMS AF,
CL238093011.MVECRF_ITEM_SDV IFSDV,
CL238093011.MVECRF_VISIT_REVS VR,
CL238093011.MVECRF_ITEMDATA DID,
(SELECT DISTINCT
SUBJECTID,
VISITID,
FORMID,
FORMINDEX,
COUNT (
DISTINCT QUERYID
OVER (
PARTITION BY SUBJECTID, VISITID, FORMID, FORMINDEX
NBQSTFML
FROM CL238093011.MVECRF_CUR_QUERY
WHERE QUERYSTATE IN (0, 1, 2)) QUERY,
CL238093011.FLO_FML_EVENT EVENT_ITEM
WHERE (AF.VISITDELETED IS NULL OR AF.VISITDELETED = 0)
AND AF.FORMTYPE NOT IN (4, 5, 6, 7, 8, 103)
AND (AF.DELETEDDYNAMICFORMSTATE IS NULL
OR AF.DELETEDDYNAMICFORMSTATE = 0)
AND FI.SUBJECTVISITID = AF.SUBJECTVISITID
AND FI.FORMID = AF.FORMID
AND FI.FORMREV = AF.FORMREV
AND FI.FORMINDEX = AF.FORMINDEX
AND FI.VISITID = VR.VISITID
AND FI.VISITREV = VR.VISITREV
AND FI.CONTEXTID = IFSDV.CONTEXTID(+)
AND FI.CONTEXTID = DID.CONTEXTID(+)
AND FI.SUBJECTID = QUERY.SUBJECTID(+)
AND FI.VISITID = QUERY.VISITID(+)
AND FI.FORMID = QUERY.FORMID(+)
AND FI.FORMINDEX = QUERY.FORMINDEX(+)
AND FI.STUDYVERSIONID = SVF.STUDYVERSIONID
AND FI.FORMID = SVF.FORMID
AND FI.VISITID = SVF.VISITID
AND FI.SUBJECTID = EVENT_ITEM.SUBJECTID(+)
AND FI.VISITID = EVENT_ITEM.VISITID(+)
AND FI.FORMID = EVENT_ITEM.FORMID(+)
AND FI.FORMINDEX = EVENT_ITEM.FORMINDEX(+)user12045475 wrote:
Hi
how can I speed up the query below ?
All time is in analytic function (WINDOW SORT)
Thanks for your help
11.2.0.1
Rows Row Source Operation
28987 HASH UNIQUE (cr=12677 pr=155778 pw=109730 time=25010 us cost=5502 size=3972960 card=14880)
1668196 WINDOW SORT (cr=12677 pr=155778 pw=109730 time=890411840 us cost=5502 size=3972960 card=14880)
1668196 HASH JOIN RIGHT OUTER (cr=12677 pr=0 pw=0 time=1069165 us cost=3787 size=3972960 card=14880)
30706 TABLE ACCESS FULL FLO_FML_EVENT (cr=270 pr=0 pw=0 time=7420 us cost=56 size=814158 card=30154)
194733 HASH JOIN RIGHT OUTER (cr=12407 pr=0 pw=0 time=571145 us cost=3730 size=3571200 card=14880)
613 VIEW (cr=342 pr=0 pw=0 time=489 us cost=71 size=23840 card=745)
613 HASH UNIQUE (cr=342 pr=0 pw=0 time=244 us cost=71 size=20115 card=745)
745 WINDOW SORT (cr=342 pr=0 pw=0 time=1736 us cost=71 size=20115 card=745)
745 MAT_VIEW ACCESS FULL MVECRF_CUR_QUERY (cr=342 pr=0 pw=0 time=1736 us cost=69 size=20115 card=745)
194733 HASH JOIN (cr=12065 pr=0 pw=0 time=431813 us cost=3658 size=3095040 card=14880)
43 MAT_VIEW ACCESS FULL MVECRF_VISIT_REVS (cr=3 pr=0 pw=0 time=0 us cost=2 size=946 card=43)
194733 HASH JOIN OUTER (cr=12062 pr=0 pw=0 time=292098 us cost=3656 size=2767680 card=14880)
194733 HASH JOIN OUTER (cr=10553 pr=0 pw=0 time=234394 us cost=2962 size=2574240 card=14880)
194733 HASH JOIN (cr=9999 pr=0 pw=0 time=379996 us cost=2570 size=2380800 card=14880)
30076 MAT_VIEW ACCESS FULL MVECRF_ACTIVATED_FORMS (cr=1817 pr=0 pw=0 time=28411 us cost=361 size=2000285 card=29855)
194733 HASH JOIN (cr=8182 pr=0 pw=0 time=209061 us cost=1613 size=9026301 card=97057)
628 MAT_VIEW ACCESS FULL MVECRF_STUDYVERSION_FORMS (cr=19 pr=0 pw=0 time=250 us cost=6 size=18212 card=628)
194733 MAT_VIEW ACCESS FULL MVECRF_FORMITEMS (cr=8163 pr=0 pw=0 time=80733 us cost=1606 size=12462912 card=194733)
132342 MAT_VIEW ACCESS FULL MVECRF_ITEM_SDV (cr=554 pr=0 pw=0 time=23678 us cost=112 size=1720446 card=132342)
221034 MAT_VIEW ACCESS FULL MVECRF_ITEMDATA (cr=1509 pr=0 pw=0 time=46459 us cost=299 size=2873442 card=221034)
SELECT
DISTINCT
'CL238093011' AS ETUDE,
FI.STUDYID,
FI.STUDYVERSIONID,
FI.SITEID,
FI.SUBJECTID,
FI.VISITID,
VR.VISITREFNAME,
FI.SUBJECTVISITID,
FI.FORMID,
FI.FORMINDEX,
SVF.FORMREFNAME,
SVF.FORMMNEMONIC AS FMLNOM,
EVENT_ITEM.EVENT AS EVENUM,
EVENT_ITEM.EVENT_ROW AS LIGNUM,
NULL AS CODVISEVE,
MIN(DID.MINENTEREDDATE)
OVER (
PARTITION BY FI.SUBJECTID, FI.VISITID, FI.FORMID, FI.FORMINDEX
AS ATTDAT1ERSAI,
MIN(IFSDV.ITEMFIRSTSDV)
OVER (
PARTITION BY FI.SUBJECTID, FI.VISITID, FI.FORMID, FI.FORMINDEX
AS ATTDAT1ERSDV,
MAX(IFSDV.ITEMFIRSTSDV)
OVER (
PARTITION BY FI.SUBJECTID, FI.VISITID, FI.FORMID, FI.FORMINDEX
AS ATTDATDERSDV,
DECODE (AF.SDVCOMPLETESTATE,
0,
'N',
1,
'Y')
AS ATTINDSDVCOP,
AF.FMINSDVCOMPLETESTATE AS ATTDAT1ERSDVCOP,
DECODE (AF.SDVPARTIALSTATE,
0,
'N',
1,
'Y')
AS ATTINDSDVPTL,
EVENT_ITEM.EVENT_RELECT AS ATTINDRVUMEDCOP,
DECODE (QUERY.NBQSTFML, NULL, 'N', 'Y') AS ATTINDQST,
DECODE (AF.MISSINGITEMSSTATE,
0,
'N',
1,
'Y')
AS ATTINDITMABS,
DECODE (AF.FROZENSTATE,
0,
'N',
1,
'Y')
AS ATTINDETACON,
AF.FMINFROZENSTATE AS ATTDAT1ERCON,
AF.FMAXFROZENSTATE AS ATTDATDERCON,
DECODE (AF.DELETEDSTATE,
0,
'N',
1,
'Y')
AS ATTINDETASPR,
EVENT_ITEM.ROW_DELETED AS ATTINDLIGSPR
FROM CL238093011.MVECRF_FORMITEMS FI,
CL238093011.MVECRF_STUDYVERSION_FORMS SVF,
CL238093011.MVECRF_ACTIVATED_FORMS AF,
CL238093011.MVECRF_ITEM_SDV IFSDV,
CL238093011.MVECRF_VISIT_REVS VR,
CL238093011.MVECRF_ITEMDATA DID,
(SELECT DISTINCT
SUBJECTID,
VISITID,
FORMID,
FORMINDEX,
COUNT (
DISTINCT QUERYID
OVER (
PARTITION BY SUBJECTID, VISITID, FORMID, FORMINDEX
NBQSTFML
FROM CL238093011.MVECRF_CUR_QUERY
WHERE QUERYSTATE IN (0, 1, 2)) QUERY,
CL238093011.FLO_FML_EVENT EVENT_ITEM
WHERE (AF.VISITDELETED IS NULL OR AF.VISITDELETED = 0)
AND AF.FORMTYPE NOT IN (4, 5, 6, 7, 8, 103)
AND (AF.DELETEDDYNAMICFORMSTATE IS NULL
OR AF.DELETEDDYNAMICFORMSTATE = 0)
AND FI.SUBJECTVISITID = AF.SUBJECTVISITID
AND FI.FORMID = AF.FORMID
AND FI.FORMREV = AF.FORMREV
AND FI.FORMINDEX = AF.FORMINDEX
AND FI.VISITID = VR.VISITID
AND FI.VISITREV = VR.VISITREV
AND FI.CONTEXTID = IFSDV.CONTEXTID(+)
AND FI.CONTEXTID = DID.CONTEXTID(+)
AND FI.SUBJECTID = QUERY.SUBJECTID(+)
AND FI.VISITID = QUERY.VISITID(+)
AND FI.FORMID = QUERY.FORMID(+)
AND FI.FORMINDEX = QUERY.FORMINDEX(+)
AND FI.STUDYVERSIONID = SVF.STUDYVERSIONID
AND FI.FORMID = SVF.FORMID
AND FI.VISITID = SVF.VISITID
AND FI.SUBJECTID = EVENT_ITEM.SUBJECTID(+)
AND FI.VISITID = EVENT_ITEM.VISITID(+)
AND FI.FORMID = EVENT_ITEM.FORMID(+)
AND FI.FORMINDEX = EVENT_ITEM.FORMINDEX(+)
Do you have the license for parallel query (may/may not help)? PQO can help with sorts ... -
Query using Analytical Function
/*Hi there, i have the following table:*/
create table test (charcode varchar2(10),numberv number,father varchar2(10));
insert into test values('1',null,null); // Level 1
insert into test values('1.1',null,'1'); // Level 2
insert into test values('1.1.1',10,'1.1'); // Level 3
insert into test values('1.1.2',50,'1.1'); // Level 3
insert into test values('1.2',100,'1'); // Level 2
I want the following result
charcode , numberv
1 160
1.1 60
1.1.1 10
1.1.2 50
1.2 100
The real table has more than 1,000,000 and i have more then 8 levels, without analytical sum, it spend more than 3 hours to process, and i know that with analytical way it should spend less than 15 minutes.
I have a ideia on how to start: /*
select charcode,sum(numberv) over(partition father order by charcode desc)
order by charcode desc;
/* but this result it´s far way from what i expect ( the charcode 1 wasn´t populate and other values appears different values than i want
Thanks
Hélio*/here is your query. it will work for charcode between 1 and 9999.9999.9999.9999
of course you could extend it
this use regexp, but you could also use substr and instr.
select charcode,
numberv,
decode(length(charcode)-length(replace(charcode,'.')),
0,
sum(NUMBERV) over (partition by
substr(
replace(regexp_replace(
regexp_replace(
regexp_replace('.'||replace(charcode,'.','..')||'.',
'\.([0-9]{1})\.' , '.000\1.'),
'\.([0-9]{2})\.' , '.00\1.'),
'\.([0-9]{3})\.' , '.0\1.'),'.')
,1, 4)
1,
sum(NUMBERV) over (partition by
substr(
replace(regexp_replace(
regexp_replace(
regexp_replace('.'||replace(charcode,'.','..')||'.',
'\.([0-9]{1})\.' , '.000\1.'),
'\.([0-9]{2})\.' , '.00\1.'),
'\.([0-9]{3})\.' , '.0\1.'),'.')
,5, 4)
2,
sum(NUMBERV) over (partition by
substr(
replace(regexp_replace(
regexp_replace(
regexp_replace('.'||replace(charcode,'.','..')||'.',
'\.([0-9]{1})\.' , '.000\1.'),
'\.([0-9]{2})\.' , '.00\1.'),
'\.([0-9]{3})\.' , '.0\1.'),'.')
,9, 4)
3,
sum(NUMBERV) over (partition by
substr(
replace(regexp_replace(
regexp_replace(
regexp_replace('.'||replace(charcode,'.','..')||'.',
'\.([0-9]{1})\.' , '.000\1.'),
'\.([0-9]{2})\.' , '.00\1.'),
'\.([0-9]{3})\.' , '.0\1.'),'.')
,13, 4)
4,
sum(NUMBERV) over (partition by
substr(
replace(regexp_replace(
regexp_replace(
regexp_replace('.'||replace(charcode,'.','..')||'.',
'\.([0-9]{1})\.' , '.000\1.'),
'\.([0-9]{2})\.' , '.00\1.'),
'\.([0-9]{3})\.' , '.0\1.'),'.')
,17, 4)
) "sum"
from test
order by
replace(regexp_replace(
regexp_replace(
regexp_replace('.'||replace(charcode,'.','..')||'.',
'\.([0-9]{1})\.' , '.000\1.'),
'\.([0-9]{2})\.' , '.00\1.'),
'\.([0-9]{3})\.' , '.0\1.'),'.')
CHARCODE NUMBERV sum
1 160
1.1 60
1.1.1 10 10
1.1.2 50 50
1.2 100 100I hope you appreciate !
Regards
Laurent -
Hierarchy query and analytical functions
Hi I have 2 tables which are
ACCOUNT TBL
ACCOUNT_ID PAYING_ACCOUNT_ID PARENT_ACCOUNT_ID
4571111 4571111 4571111
4571112 4571112 4571111
4571113 4571113 4571111
3995313 3995313 3995313
3996786 3995313 3995313
4008375 3995313 3995313CUSTOMER_STATUS
CUSTOMER_ID CUSTOMER_STATUS
4571111 Active
4571112 Active
4571113 Active
3995313 Active
3996786 Deactive
4008375 Active I need to produce an output like below:
Level_Label ACCOUNT_ID PAYING_ACCOUNT_ID PARENT_ACCOUNT_ID MY_TOTAL_PR_A MY_TOTAL_NPR_A TOTAL_PR_A TOTAL_NPR_A MY_TOTAL_PR_D MY_TOTAL_NPR_D TOTAL_PR_D TOTAL_NPR_D
3995313 3995313 3995313 3995313 0 1 0 1 0 1 0 1
4008375 4008375 3995313 3995313 0 0 0 1 0 0 0 1
3996786 3996786 3995313 3995313 0 0 0 1 0 0 0 1
4571111 4571111 4571111 4571111 2 0 2 0 0 0 0 0
4571112 4571112 4571112 4571111 0 0 2 0 0 0 0 0
4571113 4571113 4571113 4571111 0 0 2 0 0 0 0 0This is the logic and rational to fill-up above fields.
MY_TOTAL_PR_A Sum of all child accounts of current account that are PR (PAYING_ACCOUNT_ID = ACCOUNT_ID) and in sates considered Active.
The current account is not included in the sum, only child accounts
MY_TOTAL_NPR_A Sum of all child accounts of current account that are NPR (PAYING_ACCOUNT_ID != ACCOUNT_ID) and in sates considered Active.
The current account is not included in the sum, only child accounts
TOTAL_PR_A Sum of all accounts of the structure that are PR and in sates considered Active.
The TOP account is not included in the sum, only TOP account childs
TOTAL_NPR_A Sum of all accounts of the structure that are NPR and in sates considered Active.
The TOP account is not included in the sum, only TOP account childs
MY_TOTAL_PR_D Sum of all child accounts of current account that are PR and in sates considered Deactive.
The current account is not included in the sum, only child accounts
MY_TOTAL_NPR_D Sum of all child accounts of current account that are NPR and in sates considered Deactive.
The current account is not included in the sum, only child accounts
TOTAL_PR_D Sum of all accounts of the structure that are PR and in sates considered Deactive.
The TOP account is not included in the sum, only TOP account childs
TOTAL_NPR_D Sum of all accounts of the structure that are NPR and in sates considered Deactive.
The TOP account is not included in the sum, only TOP account childsThis is my code, I managed to calculate the MY_TOTAL_XXX filed but failed to calculate TOTAL_XXX. Appreciate any information / comment. Thanks
WITH got_descendants AS
SELECT CONNECT_BY_ROOT a.account_id AS ancestor_id
, a.paying_account_id
, a.account_id
, a.parent_account_id
, LEVEL AS lvl
, c.customer_status
FROM account a inner join customer_status c
on a.account_id = c.customer_id
CONNECT BY NOCYCLE PRIOR a.account_id = a.parent_account_id
--AND account_id != parent_account_id
), DUMMY2 AS
select g.* from got_descendants g
), DUMMY AS
SELECT ancestor_id
, COUNT (CASE WHEN lvl > 1
AND account_id = paying_account_id
And ancestor_id != account_id
AND customer_status = 'A' THEN 1 END) AS my_total_pr_a
, COUNT (CASE WHEN ancestor_id = paying_account_id
AND customer_status = 'A' THEN 1 END) AS total_pr_a
, COUNT (CASE WHEN lvl > 1
AND account_id != paying_account_id
And ancestor_id != account_id
AND customer_status = 'A' THEN 1 END) AS my_total_npr_a
, COUNT (CASE WHEN ancestor_id != paying_account_id
AND customer_status = 'A'
And ancestor_id != parent_account_id THEN 1 END) AS total_npr_a
, COUNT (CASE WHEN lvl > 1
AND account_id = paying_account_id
And ancestor_id != account_id
AND customer_status = 'D' THEN 1 END) AS my_total_pr_d
, COUNT (CASE WHEN ancestor_id = paying_account_id
AND customer_status = 'D' THEN 1 END) AS total_pr_d
, COUNT (CASE WHEN lvl > 1
AND account_id != paying_account_id
And ancestor_id != account_id
AND customer_status = 'D' THEN 1 END) AS my_total_npr_d
, COUNT (CASE WHEN ancestor_id != paying_account_id
AND customer_status = 'D' THEN 1 END) AS total_npr_d
FROM DUMMY2
GROUP BY ancestor_id
SELECT lpad(' ', 2*level) || ACCOUNT.ACCOUNT_ID AS LEVEL_LABEL, LEVEL, CONNECT_BY_ISCYCLE "Cycle",
ACCOUNT.PAYING_ACCOUNT_ID, ACCOUNT.PARENT_ACCOUNT_ID, ACCOUNT.ACCOUNT_ID,
DUMMY.my_total_pr_a, DUMMY.total_pr_a, DUMMY.my_total_npr_a, DUMMY.total_npr_a,
DUMMY.my_total_pr_d, DUMMY.total_pr_d, DUMMY.my_total_npr_d, DUMMY.total_npr_d
from ACCOUNT INNER JOIN DUMMY ON ACCOUNT.account_id = DUMMY.ancestor_id
START WITH ACCOUNT.parent_account_id = ACCOUNT.account_id
CONNECT BY NOCYCLE PRIOR ACCOUNT.account_id = ACCOUNT.parent_account_idDDL
CREATE TABLE ACCOUNT
"CUSTOMER_ID" NUMBER(20,0) NOT NULL ENABLE,
"PAYING_ACCOUNT_ID" NUMBER(20,0),
"PARENT_ACCOUNT_ID" NUMBER(20,0),
"ACCOUNT_ID" NUMBER,
"COMPANY_ID" NUMBER
CREATE TABLE CUSTOMER_STATUS
"CUSTOMER_ID" NUMBER(10,0),
"CUSTOMER_STATUS" VARCHAR2(1 BYTE)
Insert into ACCOUNT (ACCOUNT_ID,PAYING_ACCOUNT_ID,PARENT_ACCOUNT_ID) values (4571111,4571111,4571111);
Insert into ACCOUNT (ACCOUNT_ID,PAYING_ACCOUNT_ID,PARENT_ACCOUNT_ID) values (4571112,4571112,4571111);
Insert into ACCOUNT (ACCOUNT_ID,PAYING_ACCOUNT_ID,PARENT_ACCOUNT_ID) values (4571113,4571113,4571111);
Insert into ACCOUNT (ACCOUNT_ID,PAYING_ACCOUNT_ID,PARENT_ACCOUNT_ID) values (3996786,3995313,3995313);
Insert into ACCOUNT (ACCOUNT_ID,PAYING_ACCOUNT_ID,PARENT_ACCOUNT_ID) values (4008375,3995313,3995313);
Insert into ACCOUNT (ACCOUNT_ID,PAYING_ACCOUNT_ID,PARENT_ACCOUNT_ID) values (3995313,3995313,3995313);
Insert into CUSTOMER_STATUS (CUSTOMER_ID,CUSTOMER_STATUS) values (3996786,'D');
Insert into CUSTOMER_STATUS (CUSTOMER_ID,CUSTOMER_STATUS) values (4008375,'A');
Insert into CUSTOMER_STATUS (CUSTOMER_ID,CUSTOMER_STATUS) values (3995313,'A');
Insert into CUSTOMER_STATUS (CUSTOMER_ID,CUSTOMER_STATUS) values (4571111,'A');
Insert into CUSTOMER_STATUS (CUSTOMER_ID,CUSTOMER_STATUS) values (4571112,'A');
Insert into CUSTOMER_STATUS (CUSTOMER_ID,CUSTOMER_STATUS) values (4571113,'A');Hi thanks for your information and explanation..
To answer your doubt, below explain the rational
The logic for TOTAL_XXX
for instance, I've picked only below data to explain the rational
Account_id Paying_account Parent_account status
3995313 3995313 3995313 Active
3996786 3995313 3995313 Deactivated
4008375 3995313 3995313 Active
Showing accounts relations, status, PR\NPR, and hierarchy we have
Account 3995313 (PR, Active)
- Account 3996786 (NPR, Deactive)
- Account 4008375 (NPR, Active)
TOTAL_PR_ACCOUNTS_A - Sum of all accounts of the structure that are PR and in sates considered Active.
The TOP account is not included in the sum, only TOP account childs
For account 3995313 the TOTAL_PR_ACCOUNTS_A is 0 (Based on the definiton, to calculate TOTAL_PR_ACCOUNTS_A, we need go throught the entire structure for that
account. In this case, we have 3995313, 3996786 and 4008375. Now we go the this three account one by one
first is 3995313, this account is a TOP account in the entire structure, so is not included in the sum.
second is 3996786, this account is not a TOP account therefore should be included in the sum, however this account is NPR and Deactive, so is disqualify.
third is 4008375, this account is not a TOP account and is active account therefore should be included in the sum, however this account is NPR so is disqualify.)
For account 3996786 the TOTAL_PR_ACCOUNTS_A is 0 ( the definiton is exactly same as the above)
For account 4008375 the TOTAL_PR_ACCOUNTS_A is 0 ( the definiton is exactly same as the above)
For account 3995313 the TOTAL_NPR_ACCOUNTS_A is 1 (Based on the definiton, to calculate TOTAL_NPR_ACCOUNTS_A, we need go throught the entire structure for that
account. In this case, we have 3995313, 3996786 and 4008375. Now we go the this three account one by
first is 3995313, this account is a TOP account in the entire structure, so is not included in the sum.
second is 3996786, this account is not a TOP account and is NPR therefore should be included in the sum, however this account is Deactive, so is disqualify.
third is 4008375, this account is not a TOP account and is active and NPR account therefore should be included in the sum, so is disqualify.)
For account 3996786 the TOTAL_PR_ACCOUNTS_A is 1 ( the definiton is exactly same as the above)
For account 4008375 the TOTAL_PR_ACCOUNTS_A is 1 ( the definiton is exactly same as the above)After execute the code as provided, seems the result was correct but I need more times to verify as I have a milions of records in the DB.
I tried to modify the code to have better performance , but failed to process, Appreciate any help :)
WITH got_descendants AS
SELECT CONNECT_BY_ROOT a.account_id AS ancestor_id
, CONNECT_BY_ROOT a.parent_account_id AS parent_account_id -- ***** NEW *****
, a.paying_account_id
, a.account_id
, c.customer_status
, LEVEL AS lvl
, CONNECT_BY_ISCYCLE AS cycle
, CASE
WHEN CONNECT_BY_ROOT a.account_id
= CONNECT_BY_ROOT a.parent_account_id
THEN ROWNUM
END AS r_num
FROM account a inner join customer_status c
on a.account_id = c.customer_id
CONNECT BY NOCYCLE PRIOR a.account_id = a.parent_account_id
AND a.account_id != a.parent_account_id
ORDER SIBLINGS BY a.account_id -- Optional
, got_o_num AS
SELECT got_descendants.*
, MIN (r_num) OVER (PARTITION BY account_id) AS o_num
, MAX (lvl) OVER (PARTITION BY account_id) AS max_lvl
FROM got_descendants
), dummy AS
SELECT ancestor_id
, COUNT ( CASE WHEN lvl > 1
AND account_id = paying_account_id
AND ancestor_id != account_id
AND customer_status = 'Active'
THEN 1
END
) AS my_total_pr_a
, COUNT (CASE WHEN lvl > 1
AND account_id != paying_account_id
And ancestor_id != account_id
AND customer_status = 'Active' THEN 1 END) AS my_total_npr_a
, COUNT (CASE WHEN lvl > 1
AND account_id = paying_account_id
And ancestor_id != account_id
AND customer_status = 'Deactive' THEN 1 END) AS my_total_pr_d
, COUNT (CASE WHEN lvl > 1
AND account_id != paying_account_id
And ancestor_id != account_id
AND customer_status = 'Deactive' THEN 1 END) AS my_total_npr_d
FROM got_o_num
GROUP BY ancestor_id
--select o_num from dummy
SELECT LPAD ( ' '
, 2 * (MIN (max_lvl) - 1)
) || ancestor_id AS level_label
, MIN (max_lvl) AS "Level"
, MIN (cycle) AS "Cycle"
, ancestor_id AS account_id -- ***** NEW *****
, MIN (paying_account_id) AS paying_account
, MIN (parent_account_id) AS parent_account_id -- ***** NEW *****
, dummy.my_total_pr_a
, CONNECT_BY_ROOT dummy.my_total_pr_a AS total_pr_a
, dummy.my_total_npr_a
, CONNECT_BY_ROOT dummy.my_total_npr_a AS total_npr_a
, dummy.my_total_pr_d
, CONNECT_BY_ROOT dummy.my_total_pr_d AS total_pr_d
, dummy.my_total_npr_d
, CONNECT_BY_ROOT dummy.my_total_npr_d AS total_npr_d
FROM dummy
GROUP BY ancestor_id
ORDER BY MIN (o_num) -
Hi,
Please find below table structure and insert scritps. Requesting for vluable help.
create table temp2 (col1 number,col2 varchar2(10),col3 number,col4 varchar2(20));
insert into temp2 values (1,'a',100,'vvv');
insert into temp2 values (2,'b',200,'www');
insert into temp2 values (3,'c',300,'xxx');
insert into temp2 values (4,'d',400,'yyy');
insert into temp2 values (5,'e',500,'zzz');
insert into temp2 values (6,'f',600,'aaa');
insert into temp2 values (7,'g',700,'bbb');
insert into temp2 values (8,'h',800,'ccc');
I am trying to get same output, what we get from below UNION query with ANALYTICAL Function.
select * from temp2 where col1 in (1,2,3,4,5)
union
select * from temp2 where col1 in (1,2,5,6)
union
select * from temp2 where col1 in (1,2,7,8);
I am seeking help by this dummy example to understand the concept, how can we use analytical functional over UNION or OUTER JOINS.
In my exact query, I am using same table three times adding UNION clause. here also we scan temp2 three times, so for bulky tables using 'union' would be hampering query's performance
It means i go with three time scans of same table that is not performance oriented. With the help of above required concept, i will try to remove UNIONs from my exact query.
Thanks!!Thanks for your time BluShadow and sorry as i think i couldn't make my query clear.
I try it again. Below there are three queries, you may see all three queries are using same tables. Difference in all three queries are just few conditions, which makes all three queries diff with each other.
I know, u cant run below query in your database, but i think it will convey my doubt to you. I have mentioned no. of rows with each clause and total i am getting 67 rows as my output. (Reason may be first n third query's result set are the subset of Second Query dataset)
So i want to take all common rows as well as additional rows, if present in any of the query. This is getting easliy done with UNION clause but want to have it in other way as here my same is getting scanned again n again.
SELECT
START_TX.FX_TRAN_ID START_FX_TRAN_ID
,END_TX.FX_TRAN_ID END_FX_TRAN_ID
,START_TX.ENTERED_DT_TS
,USER
,START_TX.TRADE_DT
,START_TX.DEAL_NUMBER
,START_TX.FX_DEAL_TYPE
,START_TX.ORIENTATION_BUYSELL
,START_TX.BASE_CCY
,START_TX.BASE_CCY_AMT
,START_TX.SECONDARY_CCY
,START_TX.SECONDARY_CCY_AMT
,START_TX.MATURITY_DT
,START_TX.TRADE_RT
,START_TX.FORWARD_PTS
,START_TX.CORPORATE_PIPS
,START_TX.DEAL_OWNER_INITIALS
,START_TX.CORPORATE_DEALER
,START_TX.PROFIT_CENTER_CD
,START_TX.COUNTERPARTY_NM
,START_TX.COUNTERPARTY_NUMBER
FROM
(SELECT * FROM FX_TRANSACTIONS WHERE GMT_CONV_ENTERED_DT_TS >= TO_DATE('20-Nov-2013 4:00:01 AM','DD-Mon-YYYY HH:MI:SS AM')) START_TX
INNER JOIN
(SELECT * FROM FX_TRANSACTIONS WHERE GMT_CONV_ENTERED_DT_TS <= TO_DATE('20-Nov-2013 4:59:59 PM','DD-Mon-YYYY HH:MI:SS AM')) END_TX
ON START_TX.COUNTERPARTY_NM = END_TX.COUNTERPARTY_NM AND
START_TX.COUNTERPARTY_NUMBER = END_TX.COUNTERPARTY_NUMBER AND
START_TX.FX_DEAL_TYPE = END_TX.FX_DEAL_TYPE AND
START_TX.BASE_CCY = END_TX.BASE_CCY AND
START_TX.SECONDARY_CCY = END_TX.SECONDARY_CCY AND
NVL(START_TX.CORPORATE_DEALER,'nullX')=NVL(END_TX.CORPORATE_DEALER,'nullX') AND
START_TX.ORIENTATION_BUYSELL='B' AND
END_TX.ORIENTATION_BUYSELL='S' AND
START_TX.FX_TRAN_ID = 1850718 AND
(START_TX.BASE_CCY_AMT = END_TX.BASE_CCY_AMT
OR
START_TX.SECONDARY_CCY_AMT = END_TX.SECONDARY_CCY_AMT) -- 10 Rows
UNION
SELECT
START_TX.FX_TRAN_ID START_FX_TRAN_ID
,END_TX.FX_TRAN_ID END_FX_TRAN_ID
,START_TX.ENTERED_DT_TS
,USER
,START_TX.TRADE_DT
,START_TX.DEAL_NUMBER
,START_TX.FX_DEAL_TYPE
,START_TX.ORIENTATION_BUYSELL
,START_TX.BASE_CCY
,START_TX.BASE_CCY_AMT
,START_TX.SECONDARY_CCY
,START_TX.SECONDARY_CCY_AMT
,START_TX.MATURITY_DT
,START_TX.TRADE_RT
,START_TX.FORWARD_PTS
,START_TX.CORPORATE_PIPS
,START_TX.DEAL_OWNER_INITIALS
,START_TX.CORPORATE_DEALER
,START_TX.PROFIT_CENTER_CD
,START_TX.COUNTERPARTY_NM
,START_TX.COUNTERPARTY_NUMBER
FROM
(SELECT * FROM FX_TRANSACTIONS WHERE GMT_CONV_ENTERED_DT_TS >= TO_DATE('20-Nov-2013 4:00:01 AM','DD-Mon-YYYY HH:MI:SS AM')) START_TX
INNER JOIN
(SELECT * FROM FX_TRANSACTIONS WHERE GMT_CONV_ENTERED_DT_TS <= TO_DATE('20-Nov-2013 4:59:59 PM','DD-Mon-YYYY HH:MI:SS AM')) END_TX
ON START_TX.COUNTERPARTY_NM = END_TX.COUNTERPARTY_NM AND
START_TX.COUNTERPARTY_NUMBER = END_TX.COUNTERPARTY_NUMBER AND
START_TX.FX_DEAL_TYPE = END_TX.FX_DEAL_TYPE AND
START_TX.BASE_CCY = END_TX.BASE_CCY AND
START_TX.SECONDARY_CCY = END_TX.SECONDARY_CCY AND
NVL(START_TX.CORPORATE_DEALER,'nullX')=NVL(END_TX.CORPORATE_DEALER,'nullX') AND
START_TX.FX_TRAN_ID = 1850718 AND
START_TX.ORIENTATION_BUYSELL='B' AND
END_TX.ORIENTATION_BUYSELL='S' -- 67 Rows
UNION
SELECT
START_TX.FX_TRAN_ID START_FX_TRAN_ID
,END_TX.FX_TRAN_ID END_FX_TRAN_ID
,START_TX.ENTERED_DT_TS
,USER
,START_TX.TRADE_DT
,START_TX.DEAL_NUMBER
,START_TX.FX_DEAL_TYPE
,START_TX.ORIENTATION_BUYSELL
,START_TX.BASE_CCY
,START_TX.BASE_CCY_AMT
,START_TX.SECONDARY_CCY
,START_TX.SECONDARY_CCY_AMT
,START_TX.MATURITY_DT
,START_TX.TRADE_RT
,START_TX.FORWARD_PTS
,START_TX.CORPORATE_PIPS
,START_TX.DEAL_OWNER_INITIALS
,START_TX.CORPORATE_DEALER
,START_TX.PROFIT_CENTER_CD
,START_TX.COUNTERPARTY_NM
,START_TX.COUNTERPARTY_NUMBER
FROM
(SELECT * FROM FX_TRANSACTIONS WHERE GMT_CONV_ENTERED_DT_TS >= TO_DATE('20-Nov-2013 4:00:01 AM','DD-Mon-YYYY HH:MI:SS AM')) START_TX
INNER JOIN
(SELECT * FROM FX_TRANSACTIONS WHERE GMT_CONV_ENTERED_DT_TS <= TO_DATE('20-Nov-2013 4:59:59 PM','DD-Mon-YYYY HH:MI:SS AM')) END_TX
ON START_TX.COUNTERPARTY_NM = END_TX.COUNTERPARTY_NM AND
START_TX.COUNTERPARTY_NUMBER = END_TX.COUNTERPARTY_NUMBER AND
START_TX.FX_DEAL_TYPE = END_TX.FX_DEAL_TYPE AND
START_TX.BASE_CCY = END_TX.BASE_CCY AND
START_TX.SECONDARY_CCY = END_TX.SECONDARY_CCY AND
NVL(START_TX.CORPORATE_DEALER,'nullX')=NVL(END_TX.CORPORATE_DEALER,'nullX') AND
START_TX.ORIENTATION_BUYSELL='B' AND
END_TX.ORIENTATION_BUYSELL='S' AND
START_TX.FX_TRAN_ID = 1850718 AND
END_TX.BASE_CCY_AMT BETWEEN (START_TX.BASE_CCY_AMT - (START_TX.BASE_CCY_AMT * :PERC_DEV/100)) AND (START_TX.BASE_CCY_AMT + (START_TX.BASE_CCY_AMT * :PERC_DEV/100))
OR
END_TX.SECONDARY_CCY_AMT BETWEEN (START_TX.SECONDARY_CCY_AMT - (START_TX.SECONDARY_CCY_AMT*:PERC_DEV/100) ) AND (START_TX.SECONDARY_CCY_AMT + (START_TX.SECONDARY_CCY_AMT*:PERC_DEV/100))
); --- 10 Rows -
Analytic function to count rows based on Special criteria
Hi
I have the following query with analytic function but wrong results on the last column COUNT.
Please help me to achive the required result.Need to change the way how I select the last column.
1)I am getting the output order by b.sequence_no column . This is a must.
2)COUNT Column :
I don't want the total count based on thor column hence there is no point in grouping by that column.
The actual requirement to achieve COUNT is:
2a -If in the next row, if either the THOR and LOC combination changes to a new value, then COUNT=1
(In other words, if it is different from the following row)
2b-If the values of THOR and LOC repeats in the following row, then the count should be the total of all those same value rows until the rows become different.
(In this case 2b-WHERE THE ROWS ARE SAME- also I only want to show these same rows only once. This is shown in the "MY REQUIRED OUTPUT) .
My present query:
select r.name REGION ,
p.name PT,
do.name DELOFF,
ro.name ROUTE,
decode(th.thorfare_name,'OSIUNKNOWN',NULL,th.thorfare_name)
THOR,
l.name LOC ,
b.sequence_no SEQ,
CASE WHEN th.thorfare_name = LAG (th.thorfare_name)
OVER (order by b.sequence_no)
or th.thorfare_name = LEAD (th.thorfare_name)
OVER (order by b.sequence_no)
THEN COUNT(b.sequence_no) OVER (partition by r.name,th.thorfare_name,l.name order BY b.sequence_no
ELSE 1
END COUNT
from t_regions r,t_post_towns p,t_delivery_offices do, t_routes ro, t_counties c,t_head_offices ho,
t_buildings b,t_thoroughfares th,t_localities l
where th.thorfare_id = b.thorfare_id
and nvl(b.invalid,'N')='N'
and b.route_id=ro.route_id(+)
and b.locality_id =l.locality_id(+)
and ro.delivery_office_id=do.delivery_office_id(+)
and do.post_town_id = p.post_town_id(+)
and p.ho_id=ho.ho_id(+)
and ho.county_id = c.county_id(+)
and c.region_id = r.region_id(+)
and r.name='NAAS'
and do.DELIVERY_OFFICE_id= &&DELIVERY_OFFICE_id
and ro.route_id=3405
group by r.name,p.name,do.name,ro.name,th.thorfare_name,l.name,b.sequence_no
ORDER BY ro.name,b.sequence_no;My incorrect output[PART OF DATA]:
>
REGION PT DELOFF ROUTE THOR LOC SEQ COUNT
NAAS NAAS MAYNOOTH MAYNOOTHR010 DUBLINRD CEL 1 1
NAAS NAAS MAYNOOTH MAYNOOTHR010 NEWTOWNRD CEL 2 1
NAAS NAAS MAYNOOTH MAYNOOTHR010 PRIMHILL CEL 4 1
NAAS NAAS MAYNOOTH MAYNOOTHR010 NEWTOWNRD CEL 5 1
NAAS NAAS MAYNOOTH MAYNOOTHR010 THEGROVE CEL 2 1
NAAS NAAS MAYNOOTH MAYNOOTHR010 NEWTOWNRD CEL 7 3
NAAS NAAS MAYNOOTH MAYNOOTHR010 NEWTOWNRD CEL 8 4
NAAS NAAS MAYNOOTH MAYNOOTHR010 NEWTOWNRD CEL 9 5
NAAS NAAS MAYNOOTH MAYNOOTHR010 NEWTOWNRD CEL 10 6
NAAS NAAS MAYNOOTH MAYNOOTHR010 NEWTOWNRD CEL 11 7
NAAS NAAS MAYNOOTH MAYNOOTHR010 NEWTOWNRD CEL 12 8
NAAS NAAS MAYNOOTH MAYNOOTHR010 DUBLINRD CEL 15 2
NAAS NAAS MAYNOOTH MAYNOOTHR010 DUBLINRD CEL 19 3
NAAS NAAS MAYNOOTH MAYNOOTHR010 DUBLINRD CEL 24 4
NAAS NAAS MAYNOOTH MAYNOOTHR010 DUBLINRD CEL 29 5
NAAS NAAS MAYNOOTH MAYNOOTHR010 DUBLINRD CEL 34 6
NAAS NAAS MAYNOOTH MAYNOOTHR010 DUBLINRD CEL 39 7
NAAS NAAS MAYNOOTH MAYNOOTHR010 DUBLINRD CEL 42 2
NAAS NAAS MAYNOOTH MAYNOOTHR010 PRIMHILL CEL 43 2
NAAS NAAS MAYNOOTH MAYNOOTHR010 PRIMHILL CEL 44 3
My required output[PART OF DATA]-Please compare with the above.:
>
REGION PT DELOFF ROUTE THOR LOC COUNT
NAAS NAAS MAYNOOTH MAYNOOTHR010 DUBLINRD CEL 1
NAAS NAAS MAYNOOTH MAYNOOTHR010 NEWTOWNRD CEL 1
NAAS NAAS MAYNOOTH MAYNOOTHR010 PRIMHILL CEL 1
NAAS NAAS MAYNOOTH MAYNOOTHR010 NEWTOWNRD CEL 1
NAAS NAAS MAYNOOTH MAYNOOTHR010 THEGROVE CEL 1
NAAS NAAS MAYNOOTH MAYNOOTHR010 NEWTOWNRD CEL 6
NAAS NAAS MAYNOOTH MAYNOOTHR010 DUBLINRD CEL 7
NAAS NAAS MAYNOOTH MAYNOOTHR010 PRIMHILL CEL 2
NOTE :Count as 1 is correctly coming.
But where there is same rows and I want to take the total count on them, I am not getting.
Pls pls help.
Thanks
Edited by: Krithi on 04-Nov-2010 05:28Nicosa wrote:
Hi,
Can you give us some sample data (create table + inserts orders) to play with ?
Considering your output, I'm not even sure you need analytic count.Yes sure.
I am describing the query again here with 3 tables now to make this understand better.
Given below are the create table statements and insert statements for these 3 tables.
These tables are - BULDINGSV,THORV and LOCV
CREATE TABLE BUILDINGSV
BUILDING_ID NUMBER(10) NOT NULL,
INVALID VARCHAR2(1 BYTE),
ROUTE_ID NUMBER(10),
LOCALITY_ID NUMBER(10),
SEQUENCE_NO NUMBER(4),
THORFARE_ID NUMBER(10) NOT NULL
CREATE TABLE THORV
THORFARE_ID NUMBER(10) NOT NULL,
THORFARE_NAME VARCHAR2(40 BYTE) NOT NULL
CREATE TABLE LOCV
LOCALITY_ID NUMBER(10) NOT NULL,
NAME VARCHAR2(40 BYTE) NOT NULL);
Insert into BUILDINGSV
(BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
Values
(59002372, 'N', 3405, 37382613, 5, 9002375);
Insert into BUILDINGSV
(BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
Values
(59002363, 'N', 3405, 37382613, 57, 9002364);
Insert into BUILDINGSV
(BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
Values
(59002362, 'N', 3405, 37382613, 56, 9002364);
Insert into BUILDINGSV
(BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
Values
(59002360, 'N', 3405, 37382613, 52, 9002364);
Insert into BUILDINGSV
(BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
Values
(59002358, 'N', 3405, 37382613, 1, 9002364);
Insert into BUILDINGSV
(BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
Values
(59002240, 'N', 3405, 37382613, 6, 9002284);
Insert into BUILDINGSV
(BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
Values
(59002229, 'N', 3405, 37382613, 66, 35291883);
Insert into BUILDINGSV
(BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
Values
(59002228, 'N', 3405, 37382613, 65, 35291872);
Insert into BUILDINGSV
(BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
Values
(59002226, 'N', 3405, 37382613, 62, 35291883);
Insert into BUILDINGSV
(BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
Values
(59002222, 'N', 3405, 37382613, 43, 35291883);
Insert into BUILDINGSV
(BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
Values
(59002217, 'N', 3405, 37382613, 125, 35291883);
Insert into BUILDINGSV
(BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
Values
(59002221, 'N', 3405, 37382613, 58, 9002364);
Insert into BUILDINGSV
(BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
Values
(59002214, 'N', 3405, 37382613, 128, 35291883);
Insert into BUILDINGSV
(BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
Values
(33363182, 'N', 3405, 37382613, 114, 35291883);
Insert into BUILDINGSV
(BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
Values
(33363185, 'N', 3405, 37382613, 115, 35291883);
Insert into BUILDINGSV
(BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
Values
(59002371, 'N', 3405, 37382613, 2, 9002375);
Insert into BUILDINGSV
(BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
Values
(27003329, 'N', 3405, 37382613, 415, 9002375);
Insert into BUILDINGSV
(BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
Values
(59002359, 'N', 3405, 37382613, 15, 9002364);
Insert into BUILDINGSV
(BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
Values
(59002224, 'N', 3405, 37382613, 61, 35291883);
Insert into BUILDINGSV
(BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
Values
(27003318, 'N', 3405, 37382613, 411, 9002375);
Insert into BUILDINGSV
(BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
Values
(27003326, 'N', 3405, 37382613, 412, 9002375);
Insert into BUILDINGSV
(BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
Values
(27003327, 'N', 3405, 37382613, 413, 9002375);
Insert into BUILDINGSV
(BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
Values
(27003328, 'N', 3405, 37382613, 414, 9002375);
Insert into BUILDINGSV
(BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
Values
(27003330, 'N', 3405, 37382613, 416, 9002375);
Insert into BUILDINGSV
(BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
Values
(27003331, 'N', 3405, 37382613, 417, 9002375);
Insert into BUILDINGSV
(BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
Values
(27003332, 'N', 3405, 37382613, 410, 9002375);
Insert into BUILDINGSV
(BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
Values
(27004795, 'N', 3405, 37382613, 514, 9002375);
Insert into BUILDINGSV
(BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
Values
(27004807, 'N', 3405, 37382613, 515, 9002375);
Insert into BUILDINGSV
(BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
Values
(59002227, 'N', 3405, 37382613, 64, 35291872);
Insert into BUILDINGSV
(BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
Values
(33230805, 'N', 3405, 37382613, 44, 35291883);
Insert into BUILDINGSV
(BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
Values
(33231027, 'N', 3405, 37382613, 7, 9002375);
Insert into BUILDINGSV
(BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
Values
(33231058, 'N', 3405, 37382613, 9, 9002375);
Insert into BUILDINGSV
(BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
Values
(33231078, 'N', 3405, 37382613, 10, 9002375);
Insert into BUILDINGSV
(BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
Values
(33231087, 'N', 3405, 37382613, 11, 9002375);
Insert into BUILDINGSV
(BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
Values
(33231093, 'N', 3405, 37382613, 12, 9002375);
Insert into BUILDINGSV
(BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
Values
(33229890, 'N', 3405, 37382613, 55, 9002364);
Insert into BUILDINGSV
(BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
Values
(80561996, 'N', 3405, 34224751, 544, 35417360);
Insert into BUILDINGSV
(BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
Values
(80561997, 'N', 3405, 34224751, 543, 35417360);
Insert into BUILDINGSV
(BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
Values
(80561998, 'N', 3405, 34224751, 555, 35417360);
Insert into BUILDINGSV
(BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
Values
(80562000, 'N', 3405, 34224751, 541, 35417360);
Insert into BUILDINGSV
(BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
Values
(80562001, 'N', 3405, 34224751, 538, 35417360);
Insert into BUILDINGSV
(BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
Values
(80562028, 'N', 3405, 35417256, 525, 0);
Insert into BUILDINGSV
(BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
Values
(80562031, 'N', 3405, 35417256, 518, 35417271);
Insert into BUILDINGSV
(BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
Values
(80562032, 'N', 3405, 35417256, 519, 35417271);
Insert into BUILDINGSV
(BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
Values
(80562033, 'N', 3405, 35417256, 523, 35417271);
Insert into BUILDINGSV
(BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
Values
(80561939, 'N', 3405, 34224751, 551, 35417360);
Insert into BUILDINGSV
(BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
Values
(80561940, 'N', 3405, 34224751, 552, 35417360);
Insert into BUILDINGSV
(BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
Values
(80561941, 'N', 3405, 34224751, 553, 35417360);
Insert into BUILDINGSV
(BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
Values
(80561942, 'N', 3405, 35417256, 536, 0);
Insert into BUILDINGSV
(BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
Values
(80561943, 'N', 3405, 35417256, 537, 0);
Insert into BUILDINGSV
(BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
Values
(80561970, 'N', 3405, 35417256, 522, 35417271);
Insert into BUILDINGSV
(BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
Values
(80561972, 'N', 3405, 35417256, 527, 35417271);
Insert into BUILDINGSV
(BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
Values
(80561974, 'N', 3405, 35417256, 530, 35417271);
Insert into BUILDINGSV
(BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
Values
(80561975, 'N', 3405, 35417256, 531, 35417271);
Insert into BUILDINGSV
(BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
Values
(80561980, 'N', 3405, 34224751, 575, 35417360);
Insert into BUILDINGSV
(BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
Values
(80561981, 'N', 3405, 34224751, 574, 35417360);
Insert into BUILDINGSV
(BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
Values
(80561983, 'N', 3405, 34224751, 571, 35417360);
Insert into BUILDINGSV
(BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
Values
(80561984, 'N', 3405, 34224751, 570, 35417360);
Insert into BUILDINGSV
(BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
Values
(80561985, 'N', 3405, 34224751, 568, 35417360);
Insert into BUILDINGSV
(BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
Values
(80561986, 'N', 3405, 34224751, 567, 35417360);
Insert into BUILDINGSV
(BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
Values
(80561987, 'N', 3405, 34224751, 566, 35417360);
Insert into BUILDINGSV
(BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
Values
(80561989, 'N', 3405, 34224751, 563, 35417360);
Insert into BUILDINGSV
(BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
Values
(80561990, 'N', 3405, 34224751, 562, 35417360);
Insert into BUILDINGSV
(BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
Values
(80561991, 'N', 3405, 34224751, 560, 35417360);
Insert into BUILDINGSV
(BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
Values
(80561992, 'N', 3405, 34224751, 559, 35417360);
Insert into BUILDINGSV
(BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
Values
(80561993, 'N', 3405, 34224751, 558, 35417360);
Insert into BUILDINGSV
(BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
Values
(80561994, 'N', 3405, 34224751, 548, 35417360);
Insert into BUILDINGSV
(BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
Values
(80561995, 'N', 3405, 34224751, 546, 35417360);
Insert into BUILDINGSV
(BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
Values
(80562160, 'N', 3405, 37382613, 139, 35291878);
Insert into BUILDINGSV
(BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
Values
(80562161, 'N', 3405, 37382613, 140, 35291878);
Insert into BUILDINGSV
(BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
Values
(80562162, 'N', 3405, 37382613, 141, 35291878);
Insert into BUILDINGSV
(BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
Values
(80562163, 'N', 3405, 37382613, 142, 35291878);
Insert into BUILDINGSV
(BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
Values
(80562164, 'N', 3405, 37382613, 143, 35291878);
Insert into BUILDINGSV
(BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
Values
(80562165, 'N', 3405, 37382613, 145, 35291878);
Insert into BUILDINGSV
(BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
Values
(80562166, 'N', 3405, 37382613, 100, 35291883);
Insert into BUILDINGSV
(BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
Values
(80562167, 'N', 3405, 37382613, 102, 35291883);
Insert into BUILDINGSV
(BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
Values
(80562171, 'N', 3405, 37382613, 107, 35291883);
Insert into BUILDINGSV
(BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
Values
(80562172, 'N', 3405, 37382613, 108, 35291883);
Insert into BUILDINGSV
(BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
Values
(80562174, 'N', 3405, 37382613, 110, 35291883);
Insert into BUILDINGSV
(BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
Values
(80562175, 'N', 3405, 37382613, 111, 35291883);
Insert into BUILDINGSV
(BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
Values
(80562176, 'N', 3405, 37382613, 112, 35291883);
Insert into BUILDINGSV
(BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
Values
(80562177, 'N', 3405, 37382613, 113, 35291883);
Insert into BUILDINGSV
(BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
Values
(80562182, 'N', 3405, 37382613, 123, 35291883);
Insert into BUILDINGSV
(BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
Values
(80562183, 'N', 3405, 37382613, 121, 35291883);
Insert into BUILDINGSV
(BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
Values
(80562184, 'N', 3405, 37382613, 120, 35291883);
Insert into BUILDINGSV
(BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
Values
(80562185, 'N', 3405, 37382613, 118, 35291883);
Insert into BUILDINGSV
(BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
Values
(80562186, 'N', 3405, 37382613, 117, 35291883);
Insert into BUILDINGSV
(BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
Values
(80562187, 'N', 3405, 37382613, 116, 35291883);
Insert into BUILDINGSV
(BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
Values
(80562189, 'N', 3405, 37382613, 95, 35291883);
Insert into BUILDINGSV
(BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
Values
(80562190, 'N', 3405, 37382613, 94, 35291883);
Insert into BUILDINGSV
(BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
Values
(80562213, 'N', 3405, 37382613, 89, 35291872);
Insert into BUILDINGSV
(BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
Values
(80562240, 'N', 3405, 35417256, 516, 35417271);
Insert into BUILDINGSV
(BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
Values
(35329559, 'N', 3405, 35329152, 443, 35329551);
Insert into BUILDINGSV
(BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
Values
(35329560, 'N', 3405, 35329152, 444, 35329551);
Insert into BUILDINGSV
(BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
Values
(35329562, 'N', 3405, 35329152, 446, 35329551);
Insert into BUILDINGSV
(BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
Values
(35329109, 'N', 3405, 35329152, 433, 35329181);
Insert into BUILDINGSV
(BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
Values
(35329169, 'N', 3405, 35329152, 434, 35329181);
Insert into BUILDINGSV
(BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
Values
(35329557, 'N', 3405, 35329152, 441, 35329551);
Insert into BUILDINGSV
(BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
Values
(35329558, 'N', 3405, 35329152, 442, 35329551);
Insert into BUILDINGSV
(BUILDING_ID, INVALID, ROUTE_ID, LOCALITY_ID, SEQUENCE_NO, THORFARE_ID)
Values
(35329191, 'N', 3405, 35329152, 436, 35329181);
COMMIT;
Insert into THORV
(THORFARE_ID, THORFARE_NAME)
Values
(0, 'OSIUNKNOWN');
Insert into THORV
(THORFARE_ID, THORFARE_NAME)
Values
(9002284, 'THE GROVE');
Insert into THORV
(THORFARE_ID, THORFARE_NAME)
Values
(9002364, 'DUBLIN ROAD');
Insert into THORV
(THORFARE_ID, THORFARE_NAME)
Values
(9002375, 'NEWTOWN ROAD');
Insert into THORV
(THORFARE_ID, THORFARE_NAME)
Values
(35291872, 'HAZELHATCH ROAD');
Insert into THORV
(THORFARE_ID, THORFARE_NAME)
Values
(35291878, 'SIMMONSTOWN PARK');
Insert into THORV
(THORFARE_ID, THORFARE_NAME)
Values
(35291883, 'PRIMROSE HILL');
Insert into THORV
(THORFARE_ID, THORFARE_NAME)
Values
(35329181, 'THE COPSE');
Insert into THORV
(THORFARE_ID, THORFARE_NAME)
Values
(35329213, 'THE COURT');
Insert into THORV
(THORFARE_ID, THORFARE_NAME)
Values
(35329529, 'THE CRESCENT');
Insert into THORV
(THORFARE_ID, THORFARE_NAME)
Values
(35329551, 'THE LAWNS');
Insert into THORV
(THORFARE_ID, THORFARE_NAME)
Values
(35329580, 'THE DRIVE');
Insert into THORV
(THORFARE_ID, THORFARE_NAME)
Values
(35417271, 'TEMPLEMILLS COTTAGES');
Insert into THORV
(THORFARE_ID, THORFARE_NAME)
Values
(35417360, 'CHELMSFORD');
Insert into THORV
(THORFARE_ID, THORFARE_NAME)
Values
(36500023, 'THE CLOSE');
Insert into THORV
(THORFARE_ID, THORFARE_NAME)
Values
(36500101, 'THE GREEN');
Insert into THORV
(THORFARE_ID, THORFARE_NAME)
Values
(37375569, 'THE DOWNS');
Insert into THORV
(THORFARE_ID, THORFARE_NAME)
Values
(37375595, 'THE PARK');
Insert into THORV
(THORFARE_ID, THORFARE_NAME)
Values
(37375754, 'THE AVENUE');
Insert into THORV
(THORFARE_ID, THORFARE_NAME)
Values
(37375781, 'THE VIEW');
Insert into THORV
(THORFARE_ID, THORFARE_NAME)
Values
(37376046, 'THE CRESCENT');
Insert into THORV
(THORFARE_ID, THORFARE_NAME)
Values
(37376048, 'THE GLADE');
COMMIT;
Insert into LOCV
(LOCALITY_ID, NAME)
Values
(34224751, 'SIMMONSTOWN');
Insert into LOCV
(LOCALITY_ID, NAME)
Values
(35417256, 'TEMPLEMILLS');
Insert into LOCV
(LOCALITY_ID, NAME)
Values
(35329152, 'TEMPLE MANOR');
Insert into LOCV
(LOCALITY_ID, NAME)
Values
(37382613, 'CELBRIDGE');
Insert into LOCV
(LOCALITY_ID, NAME)
Values
(37375570, 'SAINT WOLSTAN''S ABBEY');
COMMIT;
------------------------------------------------------------------------------Now the query with wrong result:
select decode(th.thorfare_name,'OSIUNKNOWN',NULL,th.thorfare_name)
THOR,
l.name LOC,
b.sequence_no SEQ,
CASE WHEN th.thorfare_name = LAG (th.thorfare_name)
OVER (order by b.sequence_no)
or th.thorfare_name = LEAD (th.thorfare_name)
OVER (order by b.sequence_no)
THEN COUNT(b.sequence_no) OVER (partition by th.thorfare_name,l.name order BY b.sequence_no
ELSE 1
END COUNT from BUILDINGSV b,THORV th,LOCV l
where th.thorfare_id = b.thorfare_id
and nvl(b.invalid,'N')='N'
and b.route_id=3405
and b.locality_id =l.locality_id(+)
order by b.sequence_no;The query result -WRONG (only first few lines)
THOR LOC SEQ COUNT
DUBLIN ROAD CELBRIDGE 1 1
NEWTOWN ROAD CELBRIDGE 2 1
NEWTOWN ROAD CELBRIDGE 5 2
THE GROVE CELBRIDGE 6 1
NEWTOWN ROAD CELBRIDGE 7 3
NEWTOWN ROAD CELBRIDGE 9 4
NEWTOWN ROAD CELBRIDGE 10 5
NEWTOWN ROAD CELBRIDGE 11 6
NEWTOWN ROAD CELBRIDGE 12 7
DUBLIN ROAD CELBRIDGE 15 1
PRIMROSE HILL CELBRIDGE 43 1
PRIMROSE HILL CELBRIDGE 44 2
DUBLIN ROAD CELBRIDGE 52 3
DUBLIN ROAD CELBRIDGE 55 4
DUBLIN ROAD CELBRIDGE 56 5
DUBLIN ROAD CELBRIDGE 57 6
DUBLIN ROAD CELBRIDGE 58 7
PRIMROSE HILL CELBRIDGE 61 3
PRIMROSE HILL CELBRIDGE 62 4
HAZELHATCH ROAD CELBRIDGE 64 1
HAZELHATCH ROAD CELBRIDGE 65 2The query result -EXPECTED (only first few lines)
THOR LOC COUNT
DUBLIN ROAD CELBRIDGE 1
NEWTOWN ROAD CELBRIDGE 2
THE GROVE CELBRIDGE 1
NEWTOWN ROAD CELBRIDGE 5
DUBLIN ROAD CELBRIDGE 1
PRIMROSE HILL CELBRIDGE 2
DUBLIN ROAD CELBRIDGE 5
PRIMROSE HILL CELBRIDGE 2
HAZELHATCH ROAD CELBRIDGE 2Please note, in the expected result, I only need 1 row but need to show the total count of rows until the names change.
So the issues are
1) the count column values are wrong in my query.
2)I dont want to repeat the same rows(Please see EXPECTED output and compare it against the original)
3)Want the output in exactly same way as in EXPECTED OUTPUT as I dont want to group by thor name(Eg. I dont want the count for all DUBLIN ROAD but I want to examine rows for the next one, if THOR/LOC combination is different in next row then COUNT=1 else COUNT=Count of no. of rows for that thor/loc combination until the combination change -So there are same value multiple rows which i need to show it in 1 row with the total count)
I am explaining below this in more detail!!
I only need 1 row per same THOR/LOC names coming multiple times but I need the count shown against that 1 row(i.e COUNT= how many rows with same thor/loc combination until THOR/LOC combo changes value).
Then repeat the process until all rows are finished..
If there is no multiple row with same THOR/LOC coming in the following row-i.e the following row is a different THOR/LOC combination, then the count for that row is 1.
Hope this is clear.
Is this doable?
Thanks in advance.
Edited by: Krithi on 04-Nov-2010 07:45
Edited by: Krithi on 04-Nov-2010 07:45
Edited by: Krithi on 04-Nov-2010 08:31 -
[b]Using Analytic functions...[/b]
Hi All,
I need help in writing a query using analytic functions.
Foll is my scenario. I have a table cust_points
CREATE TABLE cust_points
( cust_id varchar2(10),
pts_dt date,
reward_points number(3),
bal_points number(3)
insert into cust_points values ('ABC',01-MAY-2004',5, 15)
insert into cust_points values ('ABC',05-MAY-2004',3, 12)
insert into cust_points values ('ABC',09-MAY-2004',3, 9)
insert into cust_points values ('XYZ',02-MAY-2004',8, 4)
insert into cust_points values ('XYZ',03-MAY-2004',5, 1)
insert into cust_points values ('JKL',10-MAY-2004',5, 11)
I want a result set which shows for each customer, the sum of reward his/her points
but balance points as of the last date. So for the above I should have foll results
cust_id reward_pts bal_points
ABC 11 9
XYZ 13 1
JKL 5 11
I having tried using last_value(), for eg
Select cust_id, sum(reward_points), last_value(bal_points) over (partition by cust_id)...but run into grouping errors.
Can anyone help ?try this...
SELECT a.pkcol,
nvl(SUM(b.col1),0) col1,
nvl(SUM(b.col2),0) col2,
nvl(SUM(b.col3),0) col3
FROM table1 a, table2 b, table3 c
WHERE a.pkcol = b.plcol(+)
AND a.pkcol = c.pkcol
GROUP BY a.pkcol;
SQL> select a.deptno,
2 nvl((select sum(sal) from test_emp b where a.deptno = b.deptno),0) col1,
3 nvl((select sum(comm) from test_emp b where a.deptno = b.deptno),0) col2
4 from test_dept a;
DEPTNO COL1 COL2
10 12786 0
20 13237 738
30 11217 2415
40 0 0
99 0 0
SQL> select a.deptno,
2 nvl(sum(b.sal),0) col1,
3 nvl(sum(b.comm),0) col2
4 from test_dept a,test_emp b
5 where a.deptno = b.deptno
6 group by a.deptno;
DEPTNO COL1 COL2
30 11217 2415
20 13237 738
10 12786 0
SQL> select a.deptno,
2 nvl(sum(b.sal),0) col1,
3 nvl(sum(b.comm),0) col2
4 from test_dept a,test_emp b
5 where a.deptno = b.deptno(+)
6 group by a.deptno;
DEPTNO COL1 COL2
10 12786 0
20 13237 738
30 11217 2415
40 0 0
99 0 0
SQL> -
I have been trying to fix this since yesterday ad I am close, here is the question
CREATE TABLE P_X_STG
PID NUMBER,
EID VARCHAR2(10 BYTE)
CREATE TABLE TAB_C
EID VARCHAR2(10 BYTE),
X NUMBER
SET DEFINE OFF;
Insert into P_X_STG
(PID, EID)
Values
(1, 'e3');
Insert into P_X_STG
(PID, EID)
Values
(1, 'e1');
Insert into P_X_STG
(PID, EID)
Values
(1, 'e2');
Insert into P_X_STG
(PID, EID)
Values
(2, 'e3');
Insert into P_X_STG
(PID, EID)
Values
(2, 'e4');
Insert into P_X_STG
(PID, EID)
Values
(3, 'e5');
COMMIT;
SET DEFINE OFF;
Insert into TAB_C
(EID, X)
Values
('e1', 100);
Insert into TAB_C
(EID, X)
Values
('e3', 300);
Insert into TAB_C
(X)
Values
(400);
COMMIT;we match both the tables by eid
if the eid matches, get corresponding x information
if a pid has multiple different eids, for matching eid, get corresponding x information, but for non matching, simply put a NULL to x
for matching eids, print "ematch", for non matchig eids, print "pmatch"
in the below query, for non matching eids, we copy x information from matching ones, can someone help me substitute tha with Null
SELECT pid,
eid,
x,
ematch
FROM ( SELECT p.pid,
p.eid,
CASE
WHEN p.eid = c.eid THEN c.x
ELSE LAG (x) OVER (ORDER BY pid)
END
x,
CASE WHEN p.eid = c.eid THEN 'ematch' ELSE 'pmatch' END
ematch
FROM p_x_stg p, tab_c c
WHERE p.eid = c.eid(+)
ORDER BY pid, eid)
WHERE x IS NOT NULL
ORDER BY pid;
result is
1 e1 100 ematch
1 e2 100 pmatch
1 e3 300 ematch
2 e3 300 ematch
2 e4 300 pmatchI want below result
1 e1 100 ematch
1 e2 null pmatch
1 e3 300 ematch
2 e3 300 ematch
2 e4 null pmatchfor 1, e2 and 2, e4, instead of copying, just put null
Edited by: user650888 on Apr 6, 2012 12:29 PMHi,
user650888 wrote:
thanks, can this be combined into one query ?Do you mean without a sub-query?
Why? What's wrong with a sub-query?
If uisng a sub-query is the clearest and most efficient way to get the results you want, why wouldn't you want to use a sub-query?
Suppose there was a way, that involved some other technique. How would we know that you didn't object to that technique as well?
At any rate, I don't see how to do it without a sub-query. Analytic functions are computed after the WHERE clause is applied. If you want to use the results of an analytic function in a WHERE clause (as here, we want to use the results of the analytic COUNT function in a WHERE clause), then you have to compute the analytic function separately, in another (sub-) query. -
Need analytic function suggestion
Hi,
I need advice related to analytic ( I think ) function in Oracle 9.
create table testx ( id number, arr number, fore number, actual number, result_x number, is_first number);
insert into testx values ( 1, null, null, 12, null , 0 );
insert into testx values ( 2, null, null, 14 , null, 0 );
insert into testx values ( 3, 4, 5, 16, 16, 1 );
insert into testx values ( 4, 5, 5, 18, 16, 0 );
insert into testx values ( 5, 5, 5, 20, 16, 0 );
insert into testx values ( 6, 5, 5, 22, 16, 0 );
insert into testx values ( 7, 5, 5, 24, 16, 0 );
insert into testx values ( 8, 5, 5, 25, 16, 0 );
insert into testx values ( 9, 5, 8, 25, 13, 0 );
insert into testx values ( 10, 5, 8, 21, 10, 0 );
insert into testx values ( 11, 5, 8, 19, 7, 0 );
insert into testx values ( 12, 5, 8, 18, 4, 0 );
I need ONE level query ( no subqueries ) which will calculate value stored in RESULT_X column.
Rule for calculation is:
1. when arr and fore columns are available first time then result_x = actual ( row with id = 3)
2. in other case result_x = (previous value of result_x + arr - fore )
3. order of records is stored in id column
I have problem with calculating previous value of result_x since it should be available in next row calculation and dependents on other columns values.
Thanks for help,
Regards,
PiotrHi, Piotr,
This produces the results you requested:
SELECT testx.*
, SUM ( CASE
WHEN is_first = 1
THEN result_x
ELSE arr - fore
END
) OVER (ORDER BY id) AS computed_result_x
FROM testx
ORDER BY id
;This relies on the fact that there is only one row where is_first=1, and that all the earlier rows have NULL as arr or fore.
If that's not the case in your real data, then I don't think it's possible in SQL without sub-queries. Why can't you use a sub-query?
The problem is that rows up to the one with is_first=1 have to be treated differently from rows after that point, so ithe CASE expression might need to know if a given row is before or after the one with is_first=1. If you need an analytic function to determine that, then you need a sub-query, becuase analytic functions can not be nested.
You could use MODEL or a recursive WITH clause to get the results you want, but they require sub-querries. -
Hi,
I am new to oracle , i have a doubt : I have written a query using analytical functions to get the desired , but i wanted the same in a normal query .
INPUT TABLE :
NAME AMOUNT
A 100
A 200
A 300
B 200
B 300
C 300
Query : SELECT name,AMOUNT,ROW_NUMBER( ) OVER (PARTITION BY name order by name NULLS LAST) SRLNO
FROM data ORDER BY name, SRLNO;
output table:
NAME AMOUNT SRLNO
A 100 1
A 200 2
A 300 3
B 200 1
B 300 2
C 300 1
QUESTION: I wanted the same output table with normal SQL quey , but i don't want to use any partition statement here (nor any analytical functions) .
Please provide solution for this .i don't want to use any partition statement here (nor any analytical functions) .Why not? What you want to do is what analytics does. I get suspicious when anybody - but especially somebody who's new to Oracle - aks how to do something different from using the built-ins provided by Oracle.
I wanted the same output table with normal SQL queySo you're ruling out Pipelined functions and MODEL clauses? Or not?
Cheers, APC
blog : http://radiofreetooting.blogspot.com -
Analytical function count(*) with order by Rows unbounded preceding
Hi
I have query about analytical function count(*) with order by (col) ROWS unbounded preceding.
If i removed order by rows unbouned preceding then it behaves some other way.
Can anybody tell me what is the impact of order by ROWS unbounded preceding with count(*) analytical function?
Please help me and thanks in advance.Sweety,
CURRENT ROW is the default behaviour of the analytical function if no windowing clause is provided. So if you are giving ROWS UNBOUNDED PRECEDING, It basically means that you want to compute COUNT(*) from the beginning of the window to the current ROW. In other words, the use of ROWS UNBOUNDED PRECEDING is to implicitly indicate the end of the window is the current row
The beginning of the window of a result set will depend on how you have defined your partition by clause in the analytical function.
If you specify ROWS 2 preceding, then it will calculate COUNT(*) from 2 ROWS prior to the current row. It is a physical offset.
Regards,
Message was edited by:
henryswift -
Restrict Query Resultset which uses Analytic Function
Gents,
Problem Definition: Using Analytic Function, get Total sales for the Product P1
and Customer C1 [Total sales for the customer itself] in one line.
I want to restrict the ResultSet of the query to Product P1,
please look at the data below, queries and problems..
Data
Customer Product Qtr Sales
C1 P1 19991 100.00
C1 P1 19992 125.00
C1 P1 19993 175.00
C1 P1 19994 300.00
C1 P2 19991 100.00
C1 P2 19992 125.00
C1 P2 19993 175.00
C1 P2 19994 300.00
C2 P1 19991 100.00
C2 P1 19992 125.00
C2 P1 19993 175.00
C2 P1 19994 300.00
Problem, I want to display....
Customer Product ProdSales CustSales
C1 P1 700 1400
But Without using outer query, i.e. please look below for the query that
returns this reult with two select, I want this result in one query only..
Select * From ----*** want to avoid this... ***----
(Select Customer,Product,
Sum(Sales) ProdSales,
Sum(Sum(Sales)) Over(Partition By Customer) CustSales
From t1
Where customer='C1')
Where
Product='P1' ;
Also, I want to avoid Hard coding of P1 in the select clause....
I mean, I can do it in one shot/select, but look at the query below, it uses
P1 in the select clause, which is No No!! P1 is allowed only in Where or Having ..
Select Customer,Decode(Product, 'P1','P1','P1') Product,
Decode(Product,'P1',Sales,0) ProdSales,
Sum(Sum(Sales)) Over (Partition By Customer ) CustSales
From t1
Where customer='C1' ;
This will get me what I want, but as I said earlier, I want to avoid using P1 in the
Select clause..
Goal is to Avoid using
1-> Two Select/Outer Query/In Line Views
2-> Product 'P1' in the Select clause...
Thanks
-Dhaval RasaniaI don't understand goal number 1 of not using an inline view.
What is the harm?
Maybe you are looking for
-
How to track calendars with multiple devices apple and droid
I have a iPhone 5 and iPad but would like to find a way to communicate my calendar with others using apple and droid products. How would I do that?
-
"E_ADEPT_REQUEST_REPLAY" Error in Packaging a PDF file
Hi Jim, As per ContentServer_Technical_Reference.pdf (page no 9) I passed <filename>, <location> and <src> parameter in our packaging request xml. File is packaging successfully and service is also placing the encrypted file to new location listed in
-
Flash Builder 4 LDAP issue on IIS 7 with Coldfusion 8
I have a cfc that returns empty strings back into my project when I attempt an auto login through LDAP. The same files perform correctly on a different server with IIS 6. I set up a simple cfm on the IIS 7 server and received the appropriate data. I
-
I created a password verification function failure
I created a function in sys to check complexity of passwords, however if I apply this to any oracle accounts (profile), my users can't sign on to any apex application. I get: ORA-28003: password verification for the specified password failed ORA-2000
-
How to correct continuous printing?
Using P2035n Laserjet printer via wireless from Windows 7 HP Laptop, printing is endless for entire content of subject being printed. I've reinstalled drivers, reset printer to factory defaults, unplugged printer, turned off and then on wireless con