SUM Over Partition by
Hi
I have an calculated field (age_group) which gives month age (0-2Y,2-4Y......)
I want to sum a measure (which has a sum aggragation rule in the repository) to show the sum of the measure over partition of age_group and the quarter (another dimension).
when i perform a calculated Item in the request SUM(measure) it gives wrong answers. i want actually to imitate the total option (which is calculated right) in a formula in order to use this formula column in the request
Any idea how to perform it?
Thanks
Hi
Thanks for your reply
I will try to explain exactly what i mean:
I have the following details
QTR Age_group Money
2006_Q3 0-2y 10000
2006_Q3 2-4Y 15000
2006_Q3 4-6Y 20000
2006_Q3 more_6 10000
Total 2006_Q3 55000
I want to calculate the total and to percent it as new column
meaning I want a new report that will look like this:
QTR Age_group Money Money_calc
2006_Q3 0-2y 10000 55000
2006_Q3 2-4Y 15000 55000
2006_Q3 4-6Y 20000 55000
2006_Q3 more_6 10000 55000
Thanks
Similar Messages
-
Analytical function SUM() OVER (PARTITION BY ) in Crosstab
I am trying to resolve this from a very long time. I have an amount column that has to be grouped on Year, but all the other columns grouped by month. I am trying to achieve this using analytic function SUM(Case when (Condition1 and Condition2) then Sum(Amount) else 0 end) OVER ( PARTITION BY Account, Year), Where Account, Sub Account are the left axis columns. Now, column displays the values correctly, but at different rows. This is confusing.............
For Ex: For Account 00001, there are 3 sub accounts 1000,2000,3000. For Sub account 3000, conditions 1 and 2 are satisfied, so it should display the Amount in the row corresponding to Sub account 3000, and 0 for remaining Sub Accounts. And the Total amount of all the sub accounts, which will be the same as amount for SubAccount 3000 should be displayed in the row corresponding to Account 00001.
But I get blank rows for 1000 and 3000 Sub accounts and Amount displayed in 2000 Sub account, and blank for Account 00001 also.
When I created the same workbook in Tabular form, the same amount is displayed for all the SubAccounts of a single Account.
When I used this CASE statement in TOAD, I figured that this is due to the Analytic function. When I use a group by clause as shown below instead of partition by, I get the results I need.
SELECT (Case when (Condition1 and Condition2) then Sum(Amount) else 0 end), Account, Sub Account FROM tables WHERE conditions GROUP BY Year, Account, Sub Account
But I cannot use groupby for whole SQL of the workbook as I need the other columns with page item 'MONTH' not 'Year'.
Could somebody please help me with this?Hi,
In your tabular form do you get the correct total display against all you subaccounts and account? If this correct then you can use case to ensure that the total is displayed only for the single account.
Once you have the correct totals working in a tabular form it is easier to re-produce what you want in a cross-tab.
Rod West -
PLSQL does not support 'Sum Over Partition'
I have something (and many obvious syntactical variations) like the following which works great in SQLPLUS but not in PLSQL:
select
table1.aField,
table2.bField,
sum (table1.type) over (partition by type)
from
table1, table2
where
aField = something;
PLSQL does not seem to like the "(" in from of the partition keyword.
I get the following PL/SQL error when I attempt to compile:
4/26 PLS-00103: Encountered the symbol "(" when expecting one of the following:
, fromWhat Oracle version are you running?
Is it Oracle8i?
Begining with Oracle9i, SQL parsers for SQL and PL/SQL were merged togather. Before 9i, many of the new features introduced in SQL were not recognized by the PL/SQL parser.
The following example works as expected on Oracle9i:
SQL> begin
2 for rec in (select deptno, ename, sum(sal) over (partition by deptno)total from scott.emp)
3 loop
4 dbms_output.put_line(rec.ename||' - '||rec.total) ;
5 end loop ;
6 end ;
7 /
CLARK - 8750
KING - 8750
MILLER - 8750
SMITH - 10875
ADAMS - 10875
FORD - 10875
SCOTT - 10875
JONES - 10875
ALLEN - 9400
BLAKE - 9400
MARTIN - 9400
JAMES - 9400
TURNER - 9400
WARD - 9400
PL/SQL procedure successfully completed.
SQL> disconnect
Disconnected from Oracle9i Enterprise Edition Release 9.2.0.3.0 - Production
With the Partitioning, OLAP and Oracle Data Mining options
JServer Release 9.2.0.3.0 - Production
SQL> -
SUM OVER PARTITION BY condition?
I have a piece of SQL similar to:
SELECT person,
amount,
type,
SUM(amount) OVER (PARTITION BY person) sum_amount_person
FROM table_a
What I would like to be able to do is use a conditional PARTITION BY clause, so rather than partition and summing for each person I would like to be able to sum for each person where type = 'ABC'
I would expect the syntax to be something like
SELECT person,
amount,
type,
SUM(amount) OVER (PARTITION BY person WHERE type = 'ABC') sum_amount_person
FROM table_a
Is this possible? Or am I missing a much simpler solution?
RichardThe proposed query does not compile on my Windows Oracle 9.2.0.5 or 10.1. This could be generated by the ambiguty introduced by DECODE in the evaluation of query (does it filter the selected rows, or the rows summarized for each selected row, or both?).
I propose two alternatives. The requirements are not specific enough to allow me to choose between them.
SQL> SELECT * FROM table_a ORDER BY 1, 3;
PERSON AMOUNT TYP
john 12 abc
john 8 abc
john 20 def
mike 15 abc
mike 30 ghi
steve 30 abc
6 rows selected.
SQL> SELECT person,
2 amount,
3 type,
4 SUM(decode(type, 'ABC',amount, to_number(NULL)) OVER (PARTITION BY person) sum_amount_person
5 FROM table_a;
SUM(decode(type, 'ABC',amount, to_number(NULL)) OVER (PARTITION BY person) sum_amount_person
ERROR at line 4:
ORA-30483: window functions are not allowed here
SQL> SELECT person,
2 amount,
3 type,
4 CASE type WHEN 'abc' THEN SUM(amount) OVER (PARTITION BY person) END sum_amount_person
5 FROM table_a;
PERSON AMOUNT TYP SUM_AMOUNT_PERSON
john 12 abc 40
john 20 def
john 8 abc 40
mike 15 abc 45
mike 30 ghi
steve 30 abc 30
6 rows selected.
SQL> SELECT person,
2 amount,
3 type,
4 CASE type WHEN 'abc' THEN SUM(amount) OVER (PARTITION BY person, type) END sum_amount_person
5 FROM table_a;
PERSON AMOUNT TYP SUM_AMOUNT_PERSON
john 12 abc 20
john 8 abc 20
john 20 def
mike 15 abc 15
mike 30 ghi
steve 30 abc 30
6 rows selected. -
Case Statement in Analytic Function SUM(n) OVER(PARTITION BY x)
Hi Guys,
I have the following SQL that doesn't seem to consider the When clause I am using in the case staement inside the analytic function(SUM). Could somebody let me know why? and suggest the solution?
Select SUM(Case When (A.Flag = 'B' and B.Status != 'C') Then (NVL(A.Amount_Cr, 0) - (NVL(A.Amount_Dr,0))) Else 0 End) OVER (PARTITION BY A.Period_Year) Annual_amount
, A.period_year
, B.status
, A.Flag
from A, B, C
where A.period_year = 2006
and C.Account = '301010'
--and B.STATUS != 'C'
--and A.Flag = 'B'
and A.Col_x = B.Col_x
and A.Col_y = C.Col_y
When I use this SQL, I get
Annual_Amount Period_Year Status Flag
5721017.5 --------- 2006 ---------- C -------- B
5721017.5 --------- 2006 ---------- O -------- B
5721017.5 --------- 2006 ---------- NULL ----- A
And when I put the conditions in the where clause, I get
Annual_Amount Period_Year Status Flag
5721017.5 ---------- 2006 ---------- O -------- BHere are some scripts,
create table testtable1 ( ColxID number(10), ColyID number(10) , Periodname varchar2(15), Flag varchar2(1), Periodyear number(15), debit number, credit number)
insert into testtable1 values(1, 1000, 'JAN-06', 'A', 2006, 7555523.71, 7647668)
insert into testtable1 values(2, 1001, 'FEB-06', 'B', 2006, 112710, 156047)
insert into testtable1 values(3, 1002, 'MAR-06', 'A', 2006, 200.57, 22376.43)
insert into testtable1 values(4, 1003, 'APR-06', 'B', 2006, 0, 53846)
insert into testtable1 values(5, 1004, 'MAY-06', 'A', 2006, 6349227.19, 6650278.03)
create table testtable2 ( ColxID number(10), Account number(10))
insert into testtable2 values(1, 300100)
insert into testtable2 values(2, 300200)
insert into testtable2 values(3, 300300)
insert into testtable2 values(4, 300400)
insert into testtable2 values(5, 300500)
create table apps.testtable3 ( ColyID number(10), Status varchar2(1))
insert into testtable3 values(1000, 'C')
insert into testtable3 values(1001, 'O')
insert into testtable3 values(1002, 'C')
My SQL:
select t1.periodyear
, SUM(Case When (t1.Flag = 'B' and t3.Status != 'C') Then (NVL(t1.credit, 0) - (NVL(t1.debit,0))) Else 0 End) OVER (PARTITION BY t1.PeriodYear)
Annual_amount
, t1.flag
, t3.status
, t2.account
from testtable1 t1, testtable2 t2, testtable3 t3
where t1.colxid = t2.colxid
and t1.colyid = t3.colyid(+)
--and t1.Flag = 'B' and t3.Status != 'C'
Result:
PeriodYear ----- AnnualAmount ----- Flag ----- Status ----- Account
2006 ------------------ 43337 --------------- A ----------- C ---------- 300100
2006 ------------------ 43337 --------------- B ----------- O ---------- 300200
2006 ------------------ 43337 --------------- A ----------- C ---------- 300300
2006 ------------------ 43337 --------------- B ------------ ----------- 300400
2006 ------------------ 43337 --------------- A ------------ ----------- 300500
With condition "t1.Flag = 'B' and t3.Status != 'C'" in where clause instead of in Case statement, Result is (which is desired)
PeriodYear ----- AnnualAmount ----- Flag ----- Status ----- Account
2006 ------------------ 43337 --------------- B ----------- O ---------- 300200 -
Issue with OBIEE ROW_NUMBER() OVER (PARTITION BY)
Hi All,
I am facing some issue with the ROW_NUMBER() OVER (PARTITION BY function in the query that is being generated. I am currently on version 11.1.1.6. I have 1 FACT and 1 Dimension table. Within the dimension I have create a level based hierarchy namely REGION -> GROUP - DIVISION etc. Now the problem is that the OBIEE automatically applies *"ROW_NUMBER() OVER (PARTITION BY T9.PRODUCT_TYPE_DESC, T130.DIVISION_DESC, T130.REGION_DESC ORDER BY T9.PRODUCT_TYPE_DESC ASC, T130.DIVISION_DESC ASC, T130.REGION_DESC ASC) "
to the query where in it is not required at it returns with 3 different row numbers. If i remove this line and the where clause I am able to get correct results however its not working whatever I do in the RPD. Please advise.
WITH
SAWITH0 AS (select D1.c1 as c1,
D1.c2 as c2,
D1.c3 as c3,
D1.c4 as c4,
D1.c5 as c5,
D1.c6 as c6
from
(select sum(T157.PL_GRAND_TOTAL) as c1,
sum(T157.TRANSACTION_AMT) as c2,
T130.DIVISION_DESC as c3,
T130.REGION_DESC as c4,
T130.GROUP_DESC as c5,
T9.PRODUCT_TYPE_DESC as c6,
ROW_NUMBER() OVER (PARTITION BY T9.PRODUCT_TYPE_DESC, T130.DIVISION_DESC, T130.REGION_DESC ORDER BY T9.PRODUCT_TYPE_DESC ASC, T130.DIVISION_DESC ASC, T130.REGION_DESC ASC) as c7
from
DIM_ALL_MODULES_REF T9,
DIM_MIS_TREE_REF T130,
DIM_DATE_SERIES T123,
FCT_ALL_MODULES_TRANS T157
where ( T9.SGK_MIS_ID = T130.SGK_MIS_ID and T9.SGK_MODULE_ID = T157.SGK_MODULE_ID and T123.SGK_TIME_ID = T157.SGK_TIME_ID and T123.TRANS_DATE between TO_DATE('2011-01-01 00:00:00' , 'YYYY-MM-DD HH24:MI:SS') and TO_DATE('2011-03-31 00:00:00' , 'YYYY-MM-DD HH24:MI:SS') )
group by T9.PRODUCT_TYPE_DESC, T130.DIVISION_DESC, T130.GROUP_DESC, T130.REGION_DESC
) D1
where ( D1.c7 = 1 ) ),
SACOMMON42934 AS (select T130.DIVISION_DESC as c2,
T130.REGION_DESC as c3,
T130.GROUP_DESC as c4,
T9.PRODUCT_TYPE_DESC as c5,
sum(T258.BEG_NMK_EQ_COST_AMT) as c6,
T123.TRANS_DATE as c7,
sum(T258.END_NMK_EQ_COST_AMT) as c8
from
DIM_ALL_MODULES_REF T9,
DIM_MIS_TREE_REF T130,
DIM_DATE_SERIES T123,
FCT_MODULES_BEG_END_BAL T258
where ( T123.SGK_TIME_ID = T258.SGK_TIME_ID and T9.SGK_MIS_ID = T130.SGK_MIS_ID and T9.SGK_MODULE_ID = T258.SGK_MODULE_ID and T258.PRODUCT_TYPE_ID = 2 and T123.TRANS_DATE between TO_DATE('2011-01-01 00:00:00' , 'YYYY-MM-DD HH24:MI:SS') and TO_DATE('2011-03-31 00:00:00' , 'YYYY-MM-DD HH24:MI:SS') )
group by T9.PRODUCT_TYPE_DESC, T123.TRANS_DATE, T130.DIVISION_DESC, T130.GROUP_DESC, T130.REGION_DESC),Hi Dhar,
Thanks for replying back. But the ROW_NUMBER thing is inserted with the "WITH SUPPORTED" clause only but that is something which are the default settings. However, I notice that ROW_NUMBER() OVER (PARTITION BY) lines are erratic and are not consistent. So sometimes the drill works properly when no ROW_NUMBER() OVER (PARTITION BY) is generated but other times data is wrong. I am not sure why this behaviour is erratic even though I select the same values.
This is surely a product problem as the condition where c7 = 1 filters the result and does not take into account all values.
Even when I do not use WITH_SUPPORTED clause the stitch does not happen properly. Please advice if this is ever going to be by Oracle or any work around for this?
Thanks -
How to use expression in over partition function
sum(a) over(partition by b order by c range between interval '1' day following and '7' day following)
the above query works fine
but i want to use like this
sum(a) over(partition by b order by c range between interval Trunc(Trunc(sysdate,'MONTH')+1,'MONTH') day following and '7' day following)
is it work ??Balaji.tk wrote:
is it work ??Have you tried it? If not, have a go. ;) -
Hi,
I'm trying to do something which I would guess is quite a common query, but after scratching my head and perusing the web I am still no closer to a solution.
I am running:
Oracle Database 10g Enterprise Edition Release 10.2.0.4.0 - 64bi
I'm looking to sum up a set of values, taking into account both a parent grouping and start and end dates.
For the parent grouping I am using:
+SUM([value]) over (Partition by [Parent] order by [Parent],[Child])+
And I was hoping to be able to extend this SUM to also handle the start and end dates, so the final output would contain a sum of the values for each different time period.
As an example, using the data below I'm trying to sum up the price of the components of a car over time:
row, product, component, rate, start date, end date
1, car, chassis, 180, 01/01/2000, 31/12/2009
2, car, chassis, 200, 01/01/2010, 01/01/2050
3, car, engine, 100, 01/01/2000, 01/01/2050
Notice there is a change of price for Component 'chassis', so the output I'm looking for is:
row, product, component, rate, start date, end date, sum
1, car, chassis, 180, 01/01/2000, 31/12/2009, 280
2, car, engine, 100, 01/01/2000, 31/12/2009, 280
3, car, chassis, 200, 01/01/2010, 01/01/2050, 300
4, car, engine, 100, 01/01/2010, 01/01/2050, 300
But in reality all I need is:
row, product, start date, end date, sum
1, car, 01/01/2000, 31/12/2009, 280
2, car, 01/01/2010, 01/01/2050, 300
Preferably the query would be in a view rather than a stored procedure, and it needs to be able to handle many 'products', 'components' and start/end dates.
All help most appreciated, and if any more info is required, please let me know.
Thanks,
JulianHi Frank,
Thanks for picking up this query, I'll try to explain my points in more detail:
+SUM([value]) over (Partition by [Parent] order by [Parent],[Child])+I don't see columns called value, parent or child in the sample data below.
Is value the same as rate? What are parent and child? In the example:
Product is the parent
Component is the child
Rate is the value
Whenever you have a problem, post CREATE TABLE and INSERT statments for your sample data.CREATE TABLE "REPOSITORY"."PRODUCT_RATES"
( "PRODUCT" VARCHAR2(255 BYTE),
"COMPONENT" VARCHAR2(255 BYTE),
"RATE" NUMBER(9,2),
"START_DATE" DATE,
"END_DATE" DATE
) PCTFREE 10 PCTUSED 40 INITRANS 1 MAXTRANS 255 NOCOMPRESS LOGGING
STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT)
TABLESPACE "SHOP_AREA" ;
insert into REPOSITORY.PRODUCT_RATES (PRODUCT, COMPONENT, RATE, START_DATE, END_DATE) values ('car', 'chassis', 180, to_date('01-01-2000','dd-mm-yyyy'), to_date('31-12-2009','dd-mm-yyyy'))
insert into REPOSITORY.PRODUCT_RATES (PRODUCT, COMPONENT, RATE, START_DATE, END_DATE) values ('car', 'chassis', 200, to_date('01-01-2010','dd-mm-yyyy'), to_date('01-01-2050','dd-mm-yyyy'))
insert into REPOSITORY.PRODUCT_RATES (PRODUCT, COMPONENT, RATE, START_DATE, END_DATE) values ('car', 'engine', 100, to_date('01-01-2000','dd-mm-yyyy'), to_date('01-01-2050','dd-mm-yyyy'))
Although the above short scenario highlights my issue, to expand on the example data set:
insert into REPOSITORY.PRODUCT_RATES (PRODUCT, COMPONENT, RATE, START_DATE, END_DATE) values ('family', 'wife', 500, to_date('01-01-2000','dd-mm-yyyy'), to_date('31-12-2001','dd-mm-yyyy'))
insert into REPOSITORY.PRODUCT_RATES (PRODUCT, COMPONENT, RATE, START_DATE, END_DATE) values ('family', 'wife', 999, to_date('01-01-2002','dd-mm-yyyy'), to_date('01-01-2050','dd-mm-yyyy'))
insert into REPOSITORY.PRODUCT_RATES (PRODUCT, COMPONENT, RATE, START_DATE, END_DATE) values ('family', 'baby', 250, to_date('01-01-2000','dd-mm-yyyy'), to_date('31-12-2004','dd-mm-yyyy'))
insert into REPOSITORY.PRODUCT_RATES (PRODUCT, COMPONENT, RATE, START_DATE, END_DATE) values ('family', 'baby', 500, to_date('01-01-2005','dd-mm-yyyy'), to_date('01-01-2050','dd-mm-yyyy'))
Notice there is a change of price for Component 'chassis', so the output I'm looking for is:
row, product, component, rate, start date, end date, sum
1, car, chassis, 180, 01/01/2000, 31/12/2009, 280
2, car, engine, 100, 01/01/2000, 31/12/2009, 280
3, car, chassis, 200, 01/01/2010, 01/01/2050, 300
4, car, engine, 100, 01/01/2010, 01/01/2050, 300Explain how you get 4 rows of output when the table contains only 3 rows. Are you saying that, because some row has end_date=31/12/2009, then any other row that includes that date has to be split into two, with one row ending on 31/12/2009 and the other one beginning on the next day?
Explain, step by step, how you get the values in the desired output, especially the last column.
But in reality all I need is:Sorry, I can;'t understand what you want.
Are you saying that the output above sould be acceptable, but the output below would be even better?
row, product, start date, end date, sum
1, car, 01/01/2000, 31/12/2009, 280
2, car, 01/01/2010, 01/01/2050, 300
Preferably the query would be in a view rather than a stored procedure, and it needs to be able to handle many 'products', 'components' and start/end dates.Include a couple of differtent products in your sample data and results.
I'm not sure what you want, but there's nothing in what you've said so far that makes me think a stored procedure would be needed.The only output I actually require is:
row, product, component, rate, start date, end date, sum
1, car, 01/01/2000, 31/12/2009, 280
2, car, 01/01/2010, 01/01/2050, 300and with the extended data set:
3, family, 750, 01/01/2000, 31/12/2001
4, family, 1249, 01/01/2002, 31/12/2004
5, family, 1499, 01/01/2005, 31/12/2050however, I was thinking that the data set would need to be somehow expanded to get to the above end result, hence why I included the 'middle step' of:
row, product, component, rate, start date, end date, sum
1, car, chassis, 180, 01/01/2000, 31/12/2009, 280
2, car, engine, 100, 01/01/2000, 31/12/2009, 280
3, car, chassis, 200, 01/01/2010, 01/01/2050, 300
4, car, engine, 100, 01/01/2010, 01/01/2050, 300however, this may be irrelevent.
By the way, there's no point in using the same expression in both the PARTITON BY and ORDER BY clauses of the same analytic function call. For example, if you "PARTITION BY parent", then, when "ORDER BY parent, child" is evaluated, rows will only be compared to other rows with the same parent, so they'll all tie for first place in "ORDER BY parent". OK, thanks.
So far I have got to:
select
sum(rate) over (partition by product) as sum,
a.*
from product_rates a
which results in:
SUM PRODUCT COMPONENT RATE START_DATE END_DATE
480 car engine 100 2000-01-01 00:00:00.0 2050-01-01 00:00:00.0
480 car chassis 200 2010-01-01 00:00:00.0 2050-01-01 00:00:00.0
480 car chassis 180 2000-01-01 00:00:00.0 2009-12-31 00:00:00.0
2249 family baby 250 2000-01-01 00:00:00.0 2004-12-31 00:00:00.0
2249 family wife 999 2002-01-01 00:00:00.0 2050-01-01 00:00:00.0
2249 family baby 500 2005-01-01 00:00:00.0 2050-01-01 00:00:00.0
2249 family wife 500 2000-01-01 00:00:00.0 2001-12-31 00:00:00.0
but this shows that all price variations for a component over time are being summed (e.g. car enging 100 + car chassis 200 + car chassis 180 = 480)
Hope that goes someway expaling my query better.
Also, quick query to improve my postings - how do i indent without making text ittallic, and how do you make code a different font?
Thanks again.
Julian -
Duplicate rows in over partition by
select * from zzz
id amt
1 10
1 20
2 5
2 6
3 10
select id, sum(amt) over (partition by id order by id) sum from zzz
1 30
1 30
2 11
2 11
3 10Why I'm getting duplicate rows??
Can't use distinct or group by as this is just a small demo and I'll be using my query as part of another complex join.partition by won't filter the results based on group. It shows the exact table rows. The use of this analytical function is you can use the group function without using the group by clause showing all records in the table and group funtion as well. So this is not showing duplicate data but showing the actual data.
If the data is to be filtered based on group, you can use explicitely group by clause.
Hope this helps -
Are there any MDX gurus who can help me?
I am trying to produce an MDX query that generates a ranked result set, within this I am trying to get two levels of ranking based on Net Sales, firstly the ranking within the overall set, and secondly a ranking partitioned by an attribute dimension (the equivalent of RANK () OVER (PARTITION BY ...) in SQL Server), with the final result set sorted alphabetically by the attribute name and secondly by Net Sales. So far I have got the sorting and the overall ranking to work but not the partitioned rank. Any solution will need to be fast as the base dimension has 100K members.
My current MDX looks like this:
WITH
SET [Divisions] AS '[AttributeContract].[Att_CC01].Children'
SET [ContractsByDiv] AS
'GENERATE(
ORDER(
[AttributeContract].[Att_CC01].Children,
AttributeContract.CurrentMember.[MEMBER_NAME],
BASC
CROSSJOIN(
AttributeContract.CurrentMember
ORDER(
NonEmptySubset(
UDA([Entity].[Entity Contract Reporting], "Contract")
[Net Sales],
BDESC
MEMBER [Account].[Overall Rank] AS 'Rank([ContractsByDiv].CurrentTuple,[ContractsByDiv],[Net Sales])'
MEMBER [Account].[Rank In Div] AS '1'
SELECT
[Net Sales]
,[Overall Rank]
,[Rank In Div]
} ON COLUMNS,
[ContractsByDiv]
} ON ROWS
FROM HCREPRT2.Analysis
WHERE
[Year].[FY13],
[Period].[BegBalance],
[ISBN Type].[Total ISBN Type],
[Lifecycle].[Front List],
[Scenario].[DPG_Budget],
[Market].[Total Market],
[Version].[Working],
[Sales Channel].[Total Sales Channel]
Any suggestions as to how to do this or whether it is possible?
Regards,
Gavin
Edited by: GavinH on 07-Mar-2012 02:57This was the solution I came up with:
The following query returns a result set with the the data ranked across the overall set and with a ranking partioned by division:
WITH
SET [Divisions] AS 'ORDER([AttributeContract].[Att_CC01].Children,AttributeContract.CurrentMember.[MEMBER_NAME],BASC)'
SET [EntitySet] AS 'ORDER(NonEmptySubset(UDA([Entity].[Entity Contract Reporting], "Contract")),[Net Sales],BDESC)'
SET [ContractsByDiv] AS
'GENERATE(
[Divisions],
CROSSJOIN(
AttributeContract.CurrentMember
NonEmptySubset([EntitySet])
-- Rank in whole data set
MEMBER [Account].[Overall Rank] AS 'Rank([ContractsByDiv].CurrentTuple,[ContractsByDiv],[Net Sales])'
-- Ranking in division
MEMBER [Account].[Rank In Div] AS
'Rank(
([AttributeContract].CurrentMember,[Entity].[Entity Contract Reporting].CurrentMember),
CROSSJOIN(
AttributeContract.CurrentMember
NonEmptySubset([EntitySet])
[Net Sales]
-- Rownumber
MEMBER [Account].[RowNumber] AS 'RANK([ContractsByDiv].CurrentTuple,[ContractsByDiv],1,ORDINALRANK)'
SELECT
[Net Sales]
,[Overall Rank]
,[Rank In Div]
,[RowNumber]
} ON COLUMNS,
[ContractsByDiv]
} ON ROWS
FROM HCREPRT2.Analysis
WHERE
[Year].[FY13],
[Period].[BegBalance],
[ISBN Type].[Total ISBN Type],
[Lifecycle].[Front List],
[Scenario].[DPG_Budget],
[Market].[Total Market],
[Version].[Working],
[Sales Channel].[Total Sales Channel]
The key was to use the cross join portion of the generate statement used to create the overall set as the set for the intra divisional ranking. -
Hi,
I am using OWB V9i. I need to implement an sql statement in a mapping.The statement is
CASE WHEN (tr_dt=lead(tr_dt) OVER(PARTITION BY ID ORDER BY ID,tr_dt))
THEN lead(ef_dt) OVER(PARTITION BY ID ORDER BY ID,tr_dt)
ELSE tr_dt
END
I am not able to put the case statement in an expression as it has OVER PARTITION BY which the expression operator is not accepting. Is there any way i can implement the same in the mapping without using a table function or any other procedure???
Regards
BharathOWB's support for analytics has always been pretty sparse. They did release a doc that showed a way to build some analytics using various workarounds here:
http://www.oracle.com/technology/sample_code/products/warehouse/files/analyticfunctions.pdf
However my approach has always been to tend towards simplicity. A convoluted "workaround"mapping is harder to tune, troubleshoot, or maintain, and I always worry about how OWB will handle such workarounds over subsequent release upgrades. So, for anything not clearly natively supported by OWB - I build a view with the analytic and use that as my source.
Mike -
Group by and over partition.
Hi to everybody....
i ahev a table like this:
id name salary
1 John 1000
2 Tom 1300
3 Alan 1400
4 Mark 1800
and i do a query like this:
select max(salary) from table
and i have 1800
What i would like to have, is a query that return me the id, the name and the max salary.
i can do
select * from
select max(salary) as max from table) AAA,
table
where AAA.max =table.salary.
But i would like to do it in only one query using the group by expression and over partition statement.
Thank's in advance to everybody!Flavio,
You can use...
sql> select max(sal) from emp;
MAX(SAL)
5000
sql> select empno, ename, max(sal) over () from emp;
EMPNO ENAME MAX(SAL)OVER()
7369 SMITH 5000
7499 ALLEN 5000
7521 WARD 5000
7566 JONES 5000
7654 MARTIN 5000
7698 BLAKE 5000
7782 CLARK 5000
7788 SCOTT 5000
7839 KING 5000
7844 TURNER 5000
7876 ADAMS 5000
EMPNO ENAME MAX(SAL)OVER()
7900 JAMES 5000
7902 FORD 5000
7934 MILLER 5000If you need to have the max per department , you just would need to add....
1* select empno, ename, max(sal) over (partition by deptno) from emp
sql> /
EMPNO ENAME MAX(SAL)OVER(PARTITIONBYDEPTNO)
7782 CLARK 5000
7839 KING 5000
7934 MILLER 5000
7566 JONES 3000
7902 FORD 3000
7876 ADAMS 3000
7369 SMITH 3000
7788 SCOTT 3000
7521 WARD 2850
7844 TURNER 2850
7499 ALLEN 2850
EMPNO ENAME MAX(SAL)OVER(PARTITIONBYDEPTNO)
7900 JAMES 2850
7698 BLAKE 2850
7654 MARTIN 2850HTH,
Rajesh. -
Issue with ROW_NUMBER() OVER (PARTITION)
Hi,
Please read the Thread completely, I have created a Report to see Yearly Turnover%, The report works fine but when I take Quarter and Months in it, it restricts the result to only 1 line(i.e., my first year's Turnover) and when I opened the Physical query, I saw the BI Server is using
ROW_NUMBER() OVER (PARTITION BY)
which is not at all necessary.
*I tried disabling WITH_CLAUSE_SUPPORTED and PERF_PREFER_MINIMAL_WITH_USAGE but didn't get any resolution, Could anybody help me out?
thanks in Advance,
AnandYou said 'it restricts the result to only 1 line' means the value is nor drill down to next level?
In that case Yearly Turnover% is set to Year level? -
Need help converting SQL "OVER (PARTITION BY )" to JPQL equivalent - JPA
Having trouble converting this query:
select
sdi,
val,
vldtn,
TO_CHAR(sdt, 'yyyy-mm-dd hh:mi:ss AM')
from
select
sdi,
val,
vldtn,
sdt,
max(sdt) over (partition by sdi) as MaxDate1
from
r_ins
) x
where x.sdt = x.MaxDate1
and sdi in (1234,2345,3456,4567,5678,6789,7890);
to JPQL equivalent using JPA
Able to convert simple queries but I do not know how to handle the "over (partition by sdi)" portion of the query.
Can anyone help
TIA
JerPaul Horth wrote:
Why have the power (and cost) of Oracle and then not use those powerful features because you are restricting yourself to a vanilla set of SQL because you are using some generic framework.You know how it is :
1 - Application developers create code & queries but only test them on tiny database with low volume and no concurrency at all.
2 - Application goes Live, Database grows (as expected) but stupid optimizer is not as fast as on test environment (that was mostly empty)
3 - Queries are now 200 times slower.
4 - Expensive DB expert comes and gathers statistics, creates indexes, rewrites queries, uses hint/outline/SQLprofile.
Conclusion : Database is evil and prevent application from working efficiently, the proof of all that being : nothing had to be done on application's side to make things work correctly. Database is declared guilty.
Which could translate to :
1 - Team buy a formula one with 800HP that can reach 200mph in less than 10 seconds.
2 - Give it a pilot that doesn't even want to understand what-the-heck is a gearbox/transmission. Pilot only drives in 1st gear.
3 - The formula one is now doing 0.003 miles per gallon, doing the hell of a noise, and is limited to 80mph +(any $10000 family wagon is faster in average)+
4 - Expensive expert comes and check everything in the formula one. Finally understand the problem and modify the gearbox to a sloppy automatic transmission
Conclusion : Formula 1 is evil and prevent pilot from being fast. The proof of that being : nothing had to be changed on pilot's side to make things work correctly. Formula 1 is declared guilty.
You cannot win race without understanding how the car/engine/transmission/physics/race rules work.
Too much levels of abstraction is bad. Treating the database as a black box is the most seen "Bad Idea" these days (to my point of view).
Warning: I am biased towards Oracle :-)And so am I.
(^_^) -
Over partition: how to use to return the max of two columns
For each unique id, I want to select the value of col2 in the record with the most recent date.
When the rows with the same IDs have the same dates, I want the max value from col2.
I want one row for each ID, but I'm getting two rows for ID 3333333.
with data as
select 1111111 as id, 'a' as col2, to_date('01-JAN-09','dd-mon-yyyy') as the_date from dual union all
select 2222222 as id, 'b' as col2, to_date('02-JAN-09','dd-mon-yyyy') as the_date from dual union all
select 2222222 as id, 'c' as col2, to_date('03-JAN-09','dd-mon-yyyy') as the_date from dual union all
select 2222222 as id, 'd' as col2, to_date('04-JAN-09','dd-mon-yyyy') as the_date from dual union all
select 3333333 as id, 'e' as col2, to_date('05-JAN-09','dd-mon-yyyy') as the_date from dual union all
select 3333333 as id, 'f' as col2, to_date('05-JAN-09','dd-mon-yyyy') as the_date from dual
select id, col2, the_date
from
select id, the_date, col2, max(the_date) over (partition by id) as max_the_date, max(col2) over (partition by col2) as max_col2
from data
where the_date = max_the_date and col2 = max_col2 order by id
Expecting this:
ID COL2 THE_DATE
1111111 a 1/1/0009
2222222 d 1/4/0009
3333333 f 1/5/0009
but I'm getting 2 rows for ID 3333333
Any suggestions?TRy this code without subquery
SELECT ID, MAX (the_date)KEEP (DENSE_RANK LAST ORDER BY the_date),
MAX (col2)KEEP (DENSE_RANK LAST ORDER BY the_date)
FROM DATA
GROUP BY ID
ORDER BY ID
SQL> WITH DATA AS
2 (SELECT 1111111 AS ID, 'a' AS col2,
3 TO_DATE ('01-01-2009', 'dd-mm-yyyy') AS the_date
4 FROM DUAL
5 UNION ALL
6 SELECT 2222222 AS ID, 'b' AS col2,
7 TO_DATE ('02-01-2009', 'dd-mm-yyyy') AS the_date
8 FROM DUAL
9 UNION ALL
10 SELECT 2222222 AS ID, 'c' AS col2,
11 TO_DATE ('03-01-2009', 'dd-mm-yyyy') AS the_date
12 FROM DUAL
13 UNION ALL
14 SELECT 2222222 AS ID, 'd' AS col2,
15 TO_DATE ('04-01-2009', 'dd-mm-yyyy') AS the_date
16 FROM DUAL
17 UNION ALL
18 SELECT 3333333 AS ID, 'e' AS col2,
19 TO_DATE ('05-01-2009', 'dd-mm-yyyy') AS the_date
20 FROM DUAL
21 UNION ALL
22 SELECT 3333333 AS ID, 'f' AS col2,
23 TO_DATE ('05-01-2009', 'dd-mm-yyyy') AS the_date
24 FROM DUAL)
25 SELECT ID, MAX (the_date)KEEP (DENSE_RANK LAST ORDER BY the_date ),
26 MAX (col2)KEEP (DENSE_RANK LAST ORDER BY the_date )
27 FROM DATA
28 group by id
29 ORDER BY ID;
ID MAX(THE_DA M
1111111 2009-01-01 a
2222222 2009-01-04 d
3333333 2009-01-05 f
SQL> Edited by: Salim Chelabi on 2009-03-05 11:49
Edited by: Salim Chelabi on 2009-03-05 11:50
Maybe you are looking for
-
How to contact Internet from my local network?
I'm able to get onto the Internet by piggybacking on a nearby open LAN.( I am not a new user, but I'm not a computer scientist.) This is a desperation act because I can no longer connect to the Internet via Earthlink, my ISP. I have checked every asp
-
Hello can any one guide on how to use Amazon instant video app via UAE App Store. It seems like it's only available on US store however since I am based in Dubai and would like to purchase or rent movies via Amazon therefore would appreciate any help
-
Sound Blaster Audigy w/ 1394 SB0
I have a Sound Blaster Audigy w/ 394 (SB0090) can I hook up the my front sound header?of my case, and if how?
-
I dragged music to my nano icon on the desktop, but it didn't put the files in the right folder. How can I move them?
-
Iphoto9 won't work properly in Snow Leopard
On my Macbook (2008) iphoto9 won't work properly in Snow Leopard. What do I do? Upgrade to iphoto 11 or is there another way? I was happy with iphoto9.