Row Number() over partition by Order by in OBIEE Physical SQL
Hi ,
I created a OBIEE report in Dev using Answers. When I move the same report to Live, the count of rows was more in Live environment when compared to Dev.
The objects count is same in both Dev and Production.
WheniI verified the log file, I found that the Physical SQL in test has
ROW_NUMBER ()
OVER
PARTITION BY c1, c4, c5 order by c1,c4,c5) whereas the physical SQL in Production has
ROW_NUMBER ()
OVER
PARTITION BY c1, c4, c5, c8,c9 order by c1,c4,c5,c8,c9)
How this Partiton is done in OBIEE. I have just copied the report from catalog and moved to production.There is no changes in report.
Whether there are any settings to control it? HOw OBIEE does this Row Number() Partiton ? How can get this similiar to Test?
Please help!
Thanks
Johnny
Hi Saichand,
The links were helpful. But i am not getting how it is working in test and not in live.
I found one difference while deploying . The column names of the object both in Test and Production had spaces.For E.g: Full Name
When this column Full Name is pulled to the repsository in test , it automatically put double quotes for the column names in the physical sql when it hits the database.
But, In production , when I pulled the column the report gave error as Invalid Identifier since OBIEE generated column name as Full Name without double quotes.
Then I changed the column in Phyiscal Layer repository by having double Quotes for all columns. Afte that report worked fine.
Whether this has caused any issue in Row Partition.
Is there any setting to have column name in Double Quotes ?
Thanks,
Johnny
Similar Messages
-
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 -
Discoverer Plus - Lag / Over / Partition By / Order By (Conditional Syntax)
Good morning everybody
I have created the following syntax in Oracle Discoverer Plus and it works amazingly. However, I have a single problem.
This syntax gives me the comparison of two values (for instance: Jan.2010 values & Feb.2010 values). But it does not give me the value of all of the new records (for instance: if there is no values in Jan.2010, then it will not give me the value of Feb.2010). Hence, my report excludes all of the new records values.
If I am not mistaken, the below formulla will not be changed, but it needs some conditional syntax:
1. Compare syntax: if there is two similar records; and
2. Give the values of all the new records without any change in thier values
*(Amount-LAG(Amount,1)OVER(PARTITION BY "Sequence Number"ORDER BY "Sequence Number ASC))*
Thank very much in advance.
Mr. Zuhair Fardan
Kingdom of Bahrain
Email: [email protected]Zuhair,
change,
(Amount-LAG(Amount,1)OVER(PARTITION BY "Sequence Number"ORDER BY "Sequence Number ASC)){code}
to
{code}
(Amount-LAG(Amount,1,0)OVER(PARTITION BY "Sequence Number" ORDER BY "Sequence Number ASC))Note the extra zero in LAG(Amount,1,0). That's the default return value if nothing is found.
As it is you are subtracting null from the amount and getting null.
Hope that is helpful.
- James -
Composite Studio - return row number column
Hi, I’m trying to include a row number column in my result set in Composite Studio (SQL Scratchpad), however I’m getting various error messages such as the below. Is there any way to add a row number?
An internal error has occurred.
Cause: analytical function ROW_NUMBER is not supported natively.
com.compositesw.cdms.webapi.WebapiException: An internal error has occurred.
Cause: analytical function ROW_NUMBER is not supported natively.
analytical function ROW_NUMBER is not supported natively.
at com.compositesw.cdms.webapi.PrefetchCursor$LoadPipeThread.run(PrefetchCursor.java:273)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:662)
Caused by: analytical function ROW_NUMBER is not supported natively.
at com.compositesw.server.qe.QueryResult.next(SourceFile:311)
at com.compositesw.cdms.webapi.PrefetchCursor$LoadPipeThread.run(PrefetchCursor.java:253)
... 3 more
Caused by: java.lang.IllegalArgumentException: analytical function ROW_NUMBER is not supported natively.
at com.compositesw.server.qe.physical.util.FunctionExecutor.initialize(SourceFile:232)
at com.compositesw.server.qe.physical.util.FunctionExecutor.next(SourceFile:379)
at com.compositesw.server.qe.physical.operator.FunctionNode.next(SourceFile:197)
at com.compositesw.server.qe.topology.SelectPTree.nextInternal(SourceFile:399)
at com.compositesw.server.qe.topology.SelectPTree.next(SourceFile:378)
at com.compositesw.server.qe.QueryResult.next(SourceFile:295)
... 4 more
at com.compositesw.cdms.webapi.WebapiException_SOAPSerializer.doDeserialize(WebapiException_SOAPSerializer.java:58)
at com.sun.xml.rpc.encoding.ObjectSerializerBase.deserialize(ObjectSerializerBase.java:209)
at com.sun.xml.rpc.encoding.ReferenceableSerializerImpl.deserialize(ReferenceableSerializerImpl.java:172)
at com.compositesw.cdms.webapi.service.WMetaDataSvc_dataset_fetch_Fault_SOAPSerializer.deserializeDetail(WMetaDataSvc_dataset_fetch_Fault_SOAPSerializer.java:54)
at com.sun.xml.rpc.encoding.SOAPFaultInfoSerializer.doDeserialize(SOAPFaultInfoSerializer.java:151)
at com.sun.xml.rpc.encoding.ObjectSerializerBase.deserialize(ObjectSerializerBase.java:209)
at com.sun.xml.rpc.encoding.ReferenceableSerializerImpl.deserialize(ReferenceableSerializerImpl.java:172)
at com.compositesw.cdms.webapi.service.WMetaDataSvc_Stub._readBodyFaultElement(WMetaDataSvc_Stub.java:23530)
at com.sun.xml.rpc.client.StreamingSender._send(StreamingSender.java:238)
at com.compositesw.cdms.webapi.service.WMetaDataSvc_Stub.dataset_fetch(WMetaDataSvc_Stub.java:14247)
at com.compositesw.cdms.webapi.WDataset$FetcherThread.run(WDataset.java:273)
Version Information: Studio 6.2.5.00.32 (hotfix), Build ID 131217-101258-0800-72550, Server 6.2.5.00.32 (hotfix)Hi Tom,
I think you intended your question for the other DV community here: https://supportforums.cisco.com/community/12298251/cisco-data-virtualization
Although our parser allows the syntax of these analytic functions such as ROW_NUMBER to be submitted to us from a client, our query engine is not capable of processing it locally.
It is up to the user to ensure the resulting query plan will push the sql containing that function down to the external db (ie; oracle) to be processed there instead. Creating the query from inside a view will provide the ability to see the resulting query plan.
Which customer (company) are you with? If you need further assistance, I recommend you log a support case with Cisco.
Thank you,
Brad -
How to implement qualify row_number over(Partition by col order by col) and char2hexint in informatica in a way that is supported by pdo?
Apart from sql overriding or using stored procedure ,is there any other solution?Can rank transformation help here? ....But, I guess rank transformation cannot be pushed down..
help please !Hi Saichand,
The links were helpful. But i am not getting how it is working in test and not in live.
I found one difference while deploying . The column names of the object both in Test and Production had spaces.For E.g: Full Name
When this column Full Name is pulled to the repsository in test , it automatically put double quotes for the column names in the physical sql when it hits the database.
But, In production , when I pulled the column the report gave error as Invalid Identifier since OBIEE generated column name as Full Name without double quotes.
Then I changed the column in Phyiscal Layer repository by having double Quotes for all columns. Afte that report worked fine.
Whether this has caused any issue in Row Partition.
Is there any setting to have column name in Double Quotes ?
Thanks,
Johnny -
Hi,
Below are my table structure
create table Test (ProductId int primary key identity(1,1), productnumber varchar(50),productname varchar(50),Quantity int)
insert into Test values('PN1001','Pepsi',20),
('PN1001','curd',2),
('PN1001','Noodles',10),
('PN1001','Shrimp',40),
('PN1002','Mango',10),
('PN1002','Pizza',5),
('PN1002','Soup',10),
('PN1002','Honey',30),
('PN1001','Pepsi',15),
('PN1001','Curd',50),
('PN1001','Noodles',13),
('PN1001','Shrimp',32),
('PN1002','Mango',11),
('PN1002','Pizza',14),
('PN1002','Soup',18),
('PN1002','Honey',20)
I need to take the last inserted set of records. unfortunately i don't have created date in my table and not sure why it hasn't created
when the guy build the table schema. i know this is poor table structure cannot do anything at the moment.
Me required output :
select 'PN1001' as productnumber,'Pepsi' as productname,15 as Quantity union all
select 'PN1001' as productnumber,'curd' as productname,50 as Quantity union all
select 'PN1001' as productnumber,'Noodles' as productname,13 as Quantity union all
select 'PN1001' as productnumber,'Shrimp' as productname,32 as Quantity union all
select 'PN1002' as productnumber,'Mango' as productname,11 as Quantity union all
select 'PN1002' as productnumber,'Pizza' as productname,14 as Quantity union all
select 'PN1002' as productnumber,'Soup' as productname,18 as Quantity union all
select 'PN1004' as productnumber,'Honey' as productname,20 as Quantity
ie. The logic i need to achieve is to get the last insert records based on productnumber.
This is my try
;with TotalRecords as (
select ROW_NUMBER() OVER
(PARTITION BY productnumber
Order BY productnumber DESC )
AS RowNumber,productnumber, productname,Quantity from Test)
select * from TotalRecords
please Note : on this sample i can specify Rownumber = 3 to get
latest records. but i should not do that because i don't need to specify the particular number as it can change any time. so please help me achieving my requiement
Any sample query please
loving dotnetThat you for trying to post DDL, but you failed. This is not a proper schema. Why do you think that IDENTITY is ever used in RDBMS?? This proprietary feature is a physical table property. It count the insertion attempts on one table, in one SQL Server
data base on one machine. This is as silly as identifying an automobile by the current parking space number in on garage in one building. NO! You use the VIN.
Your guy screwed you completely and you are dead.
He do not even know the differences between a row and record. He did not normalize. Why don't you people know that products have industry standard code? The current direction in retail is the GTIN; do the research your people skipped.
CREATE TABLE Product_Test
(product_gtin CHAR(15) NOT NULL
REFERENCES Products (product_gtin)
ON UPDATE CASCADE,
stock_date DATE DEFAULT CURRENT_TIMESTAMP NOT NULL,
product_qty INTEGER NOT NULL CHECK (product_qty > 0))
PRIMARY KEY (product_gtin, stock_date));
CREATE TABLE Products
(product_gtin CHAR(15) NOT NULL PRIMARY KEY,
product_name VARCHAR(50) NOT NULL);
See how DRI actions do the work for you?
>> I need to take the last inserted set of records [sic]. <<
Another huge misconception! A table is a set; a set has no ordering. This is explained the first day of any RDBMS class. The second day, he should have gotten The Information Principle; any relationship is modeled with scalar values in the columns of a table.
That is why we need a stock date.
>> Unfortunately I don't have creation date in my table and not sure why it hasn't created when the guy build the table schema. I know this is poor table structure cannot do anything at the moment. <<
The date ought to the stocking date for the products, not the creation date of the physical row in the table! See the difference? Your mindset is still not up to a logical data model.
Can you kill this idiot? We cannot help you with this! We have no way to be sure that the rows logical ordering is the same as some physical ordering.
--CELKO-- Books in Celko Series for Morgan-Kaufmann Publishing: Analytics and OLAP in SQL / Data and Databases: Concepts in Practice Data / Measurements and Standards in SQL SQL for Smarties / SQL Programming Style / SQL Puzzles and Answers / Thinking
in Sets / Trees and Hierarchies in SQL -
Hi list,
does any one know how I can get the row number the same as what I have in column rowno?
thanks
Arvin
REATE TABLE dbo.temptable
( y int NOT NULL,
e int not null,
c int not null,
rowno int not null)
/* insert values */
INSERT INTO dbo.temptable(y,e,c,rowno ) VALUES
(1,123,1,1),
(1,123,2,1),
(1,123,3,1),
(1,5435,1,2),
(1,5435,2,2),
(1,5435,3,2),
(1,83444,1,3),
(1,83444,1,3),
(2,111,1,1),
(2,111,2,1),
(2,32245,1,2),
(2,32245,3,2);Use Dense_Rank function.
Refer the below query,
select *,dense_rank() over (partition by y order by y,e) myrowno from dbo.temptable
Regards, RSingh -
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. -
I am having the data as like below(sample data)
Col1 Col2
A AA
A BB
A CC
B BA
B BB
C CA
D DA
D DB
E EA
E EB
I want to show the seq number as a third column as like below. (seq number is with respect to Col1)
Col1 Col2 num
A AA 1
A BB 1
A CC 1
B BA 2
B BB 2
C CA 3
D DA 4
D DB 4
E EA 5
E EB 5
Thanks in Advance,
BalaHi,
Try this
WITH T AS (
SELECT 'A' AS COL1, 'AA' AS COL2 FROM DUAL UNION ALL
SELECT 'A','BB' FROM DUAL UNION ALL
SELECT 'A','CC' FROM DUAL UNION ALL
SELECT 'B','BA' FROM DUAL UNION ALL
SELECT 'B','BB' FROM DUAL UNION ALL
SELECT 'C','CA' FROM DUAL UNION ALL
SELECT 'D','DA' FROM DUAL UNION ALL
SELECT 'D','DB' FROM DUAL UNION ALL
SELECT 'E','EA' FROM DUAL UNION ALL
SELECT 'E','EB' FROM DUAL)
SELECT COL1, COL2, RANK() OVER (partition by col1 order by col2) FROM Tor
WITH T AS (
SELECT 'A' AS COL1, 'AA' AS COL2 FROM DUAL UNION ALL
SELECT 'A','BB' FROM DUAL UNION ALL
SELECT 'A','CC' FROM DUAL UNION ALL
SELECT 'B','BA' FROM DUAL UNION ALL
SELECT 'B','BB' FROM DUAL UNION ALL
SELECT 'C','CA' FROM DUAL UNION ALL
SELECT 'D','DA' FROM DUAL UNION ALL
SELECT 'D','DB' FROM DUAL UNION ALL
SELECT 'E','EA' FROM DUAL UNION ALL
SELECT 'E','EB' FROM DUAL)
SELECT COL1, COL2, dense_RANK() OVER (order by col1) FROM T@peter - apologies for replying to your post
cheers
VT
Edited by: VT on Jan 27, 2011 7:47 PM -
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 -
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 -
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. -
ROW_NUMBER() OVER (PARTITION
hi, can anyone explain, especially this part
ROW_NUMBER() OVER (PARTITION BY A.USER_ID ORDER BY A.TNP_TRACK_ID DESC) RN
Thanks
newbie
SELECT A.TNP_TRACK_ID, A.USER_ID, NVL(A.DELETED,0) DELETED, A.CREATE_TIME, A.DELETE_TIME, B.PRODUCT_CODE, C.LOGIN_NAME,
ROW_NUMBER() OVER (PARTITION BY A.USER_ID ORDER BY A.TNP_TRACK_ID DESC) RN
FROM MISPNA.TNP_TRACKABLE_' || TO_CHAR(p_reporting_month, 'YYYYMM') || ' A
JOIN TMP_VRZ_LATEST_SOC B ON A.TNP_TRACK_ID = B.TRACK_ID
JOIN MISPNA.TN_LOGIN C ON A.USER_ID = C.USER_ID
WHERE A.DELETED<>1Hi,
949523 wrote:
thank you very much, the whole original script is
SELECT TNP_TRACK_ID, USER_ID, DELETED, CREATE_TIME, DELETE_TIME, PRODUCT_CODE, LOGIN_NAME
FROM (
SELECT A.TNP_TRACK_ID, A.USER_ID, NVL(A.DELETED,0) DELETED, A.CREATE_TIME, A.DELETE_TIME, B.PRODUCT_CODE, C.LOGIN_NAME,
ROW_NUMBER() OVER (PARTITION BY A.USER_ID ORDER BY A.TNP_TRACK_ID DESC) RN
FROM MISPNA.TNP_TRACKABLE_' || TO_CHAR(p_reporting_month, 'YYYYMM') || ' A
JOIN TMP_VRZ_LATEST_SOC B ON A.TNP_TRACK_ID = B.TRACK_ID
JOIN MISPNA.TN_LOGIN C ON A.USER_ID = C.USER_ID
WHERE A.DELETED <>1
WHERE RN != 1';I assume the single-quote near the end is just an editing typo.
>
so seems like with specified user_id, it will pick greatest tnp_track_id ? ( as it asks RN=1)That's what it would be doing if the last line read
WHERE rn = 1; for each user_id, that query (with = on the last line) would display all the SELECTed values from the row with the greatest tnp_track_id.
Since you're using != , however, then it's doing just the opposite; it's displaying all rows except the row with the greatest tnp_track_id.
The sub-query is necessary here. All analytic functions, including ROW_NUMBER, are evaluated after the WHERE clause has been applied. To use the results of an analytic function in a WHERE clause, you have to compute the function in a sub-query first.
Edited by: Frank Kulash on Jan 7, 2013 8:08 PM
Just noticed you're saying "WHERE RN != 1" ( not equals) -
Data Source - Bad Field Size (Row Number)
Hi,
I get following Exception "Data Source - Bad Field Size" when I run:
oForm_ActivityView.DataSources.DataTables.Item("mtx_C").ExecuteQuery("SELECT...
The Select statement run fine in SQL 2005.
I am using following SQL statement to get the row number:
ROW_NUMBER()OVER (ORDER BY OCLG.ClgCode) AS RowNumber,
If I remove the statement it works fine.
How do I get the row number in B1 to my Matrix?
Thank you,
RuneI fought with this in the past too and solution for me was:
original query
select ROW_NUMBER() OVER (ORDER BY ItemCode) AS RowNumber, * from oitm
new query
select t.* from (select ROW_NUMBER() OVER (ORDER BY ItemCode) AS RowNumber, * from oitm) t
or convert the rownumber to nvarchar instead of integer as
convert(nvarchar(10), ROW_NUMBER() OVER (ORDER BY ItemCode) ) as RowNumbver
check it and let me know -
Maximum number of partitions allowed per table.
Interesting findings with interval partitioning:
SQL> SELECT *
2 FROM v$version
3 /
BANNER
Oracle Database 11g Enterprise Edition Release 11.2.0.3.0 - 64bit Production
PL/SQL Release 11.2.0.3.0 - Production
CORE 11.2.0.3.0 Production
TNS for 64-bit Windows: Version 11.2.0.3.0 - Production
NLSRTL Version 11.2.0.3.0 - Production
SQL> DROP TABLE tbl PURGE
2 /
Table dropped.
SQL> CREATE TABLE tbl(
2 id number(6),
3 dt date
4 )
5 PARTITION BY RANGE(dt)
6 INTERVAL (INTERVAL '1' DAY)
7 (
8 PARTITION p1 VALUES LESS THAN (date '-857-12-31')
9 )
10 /
Table created.
SQL> select partition_name,
2 high_value
3 from user_tab_partitions
4 where table_name = 'TBL'
5 /
PARTITION_NAME HIGH_VALUE
P1 TO_DATE('-0857-12-31 00:00:00', 'SYYYY-MM-DD HH24:MI:SS', 'N
LS_CALENDAR=GREGORIAN')
SQL> INSERT
2 INTO tbl
3 VALUES(
4 1,
5 sysdate
6 )
7 /
1 row created.
SQL> DROP TABLE tbl PURGE
2 /
Table dropped.
SQL> CREATE TABLE tbl(
2 id number(6),
3 dt date
4 )
5 PARTITION BY RANGE(dt)
6 INTERVAL (INTERVAL '1' DAY)
7 (
8 PARTITION p1 VALUES LESS THAN (date '-858-01-01')
9 )
10 /
Table created.
SQL> select partition_name,
2 high_value
3 from user_tab_partitions
4 where table_name = 'TBL'
5 /
PARTITION_NAME HIGH_VALUE
P1 TO_DATE('-0858-01-01 00:00:00', 'SYYYY-MM-DD HH24:MI:SS', 'N
LS_CALENDAR=GREGORIAN')
SQL> INSERT
2 INTO tbl
3 VALUES(
4 1,
5 sysdate
6 )
7 /
INTO tbl
ERROR at line 2:
ORA-14300: partitioning key maps to a partition outside maximum permitted number of partitions
SQL> From Logical Database Limits:
Maximum number of partitions allowed per table or index: 1024K - 1
I always thought limit implies to number of actual, not potential partitions, however it looks like I was wrong, although it makes not much sense to limit potential and not actual partitions:
SQL> select trunc(sysdate) - date '-858-01-01',
2 1024 * 1024 - 1
3 from dual
4 /
TRUNC(SYSDATE)-DATE'-858-01-01' 1024*1024-1
1048661 1048575
SQL> select to_char(DATE'-858-01-01' + 1048575,'MM/DD/YYYY')
2 from dual
3 /
TO_CHAR(DA
11/17/2012
SQL> So tomorrow "magic" date should increase by one day. I'll test it. But more interesting if tomorrow I will be able to insert a row that forms a new partition into table TBL.
SY.rp0428 wrote:
The other argument is that Oracle has to be able to automatically create any partition required and it can only create 1024k - 1. So if you create yours with sysdate how could it create all of the others?Not sure I follow. What is the purpose of counting potential partitions? Partition part# iin sys.tabpart$ is not assigned based on potential partition position. If I issue a DDL to create new partition regardless of interval/non-interval partitioning Oracle has to check how many partitions table has so far or po and raise same/similar exception if partition I am asking to create is over the limit. And, in any case, knowing we can create all potential partitions at table create time doesn't mean I will not try to insert data outside the range. So there is absolutely no guarantee Oracle can automatically create any partition requested. Again, I don't understand why creating non-interval partitioned table with a single initial partition has partition count of 1:
SQL> DROP TABLE tbl1 PURGE
2 /
Table dropped.
SQL> CREATE TABLE tbl1(
2 id number(6),
3 dt date
4 )
5 PARTITION BY RANGE(dt)
6 (
7 PARTITION p1 VALUES LESS THAN (date '-857-12-31')
8 )
9 /
Table created.
SQL> SELECT partition_count
2 FROM user_part_tables
3 WHERE table_name = 'TBL1'
4 /
PARTITION_COUNT
1
SQL>And interval partitioned table with same single initial partition has partition count of 1048575:
SQL> CREATE TABLE tbl1(
2 id number(6),
3 dt date
4 )
5 PARTITION BY RANGE(dt)
6 INTERVAL (INTERVAL '1' DAY)
7 (
8 PARTITION p1 VALUES LESS THAN (date '-857-12-31')
9 )
10 /
Table created.
SQL> SELECT partition_count
2 FROM user_part_tables
3 WHERE table_name = 'TBL1'
4 /
PARTITION_COUNT
1048575
SQL> Would be interesting to find out what forces Oracle to go into potential partition mode for interval partitioning.
SY.
Maybe you are looking for
-
Type conflict with ASSIGN in assoc with CL_ABAP_TABLEDESCR
Hello Xperts, over the functionalities within CL_ABAP_TABLEDESCR I have added two fields to an ITAB. "thismanpo type /bic/oiblmansec1, gs_comp-name = 'THISMANPO'. gs_element ?= cl_abap_elemdescr=>describe_by_name( '/bic/oiblmansec1' ). gs
-
Trying to add a new node to OAS
I'm trying to add a new node to our OAS server, and it always fails. Any hints on what I might be doing wrong?
-
Installed LasrJet P1102w Registration and creation of eprint account I type the wrong email address
I have just completed installing my HP LasrJet P1102w and when the installation was complete it printed a Test Page. However, during the registration of the product I messed up on the Email address account to include eprint account. Now I cannot do a
-
Unwanted printing of advanced payment when printing a sales order or pick
It will be great to have an option of disabling the automatic printing of the advanced payment incoming payment when printing a sales order or invoice. The option is currently not available on the document printing options.
-
How to show colmn value in the legend in ssrs Chart?
How to display Legend values in my ssrs Report like below I want the values like below. SuccesCount-2000 FailCount-100 No status-150 Can U pls help me for this ThanX!