Rank by
Hi all,
Can anybody help me........ i am using
"rank() over (partition by orh.orh_id order by ol.orl_id asc) as seq_num " this is a part of my inner query.... The whole query is using a left outer join.... Now the problem is that... when I am selecting ORH_ID and SEQ_NUM using a cursor and inserting into a table... in which ORH_ID and SEQ_NUM are PKs.... I noticed some rejections due to unique key constraint.... That means I am fetching some duplicate ORH_IDs and SEQ_NUM... How is it possible to have duplicates of ORH_ID and SEQ_NUM together... because for each occurence of same ORH_ID will have a different sequence number based on ORL_ID....
If anybody has any idea to solve this please reply... Thanks in advance...
Message was edited by:
user626688
Possible, when you have duplicate records of orh_id and orl_id combination.
See this.
SQL> with t as
2 (select 1 col1, 10 col2 from dual
3 union all
4 select 2, 20 from dual
5 union all
6 select 1, 30 from dual
7 union all
8 select 1, 10 from dual
9 )
10 select col1, col2 from t
11 order by 1, 2
12 /
COL1 COL2
1 10
1 10
1 30
2 20
SQL> with t as
2 (select 1 col1, 10 col2 from dual
3 union all
4 select 2, 20 from dual
5 union all
6 select 1, 30 from dual
7 union all
8 select 1, 10 from dual
9 )
10 select col1, rank() over (partition by col1 order by col2) rn
11 from t
12 /
COL1 RN
1 1
1 1
1 3
2 1
SQL>Cheers
Sarma.
Similar Messages
-
Problem with rank in OBIEE 11G
Hi,
I have a problem with the function rank in obiie, i have two columns the first is the id of employees, and the second is the rank of revenues of employees, when I filter one employee and the rank is always 1, someone can help me?
CumpsI do not think this is going to be possible using a traditional filter on the query.
In order for the rank function to correctly work, it has to be aware of the other column values in the query to assign a rank to each row. When you filter the query ( either on the ID or the Rank) all of the other column values will be FILTERED OUT of the query. Rank will only see 1 value in the column, so it gets the #1.
You have 2 options here:
Selection steps: use selection steps to "keep" only the rows you are interested in. Selection steps basically allow you to"hide" some of the rows your query has returned, but still calculate against them for aggregate functions. If you are focusing on a top or bottom % this solution will work well.
If you are hell-bent on using a filter, the only way you will be able to get the rank function to work is to calculate the rank in a 2nd query, then use an outer join to attach the rank column onto your filtered employee query. -
F110- Automatic Payment. 2nd ranking bank not selected
Dear Experts,
I need your help here.
During the F110 (check payment), those banks customized as 2nd,3rd,4th or subsequent bank ranking could not produce any payment advice and checking printing. Where has goes wrong?
If I change the 2nd ranking (Citibank) to 1st ranking, then Citibank will works well.
I wonder why the F110 only takes the 1st ranking bank.
Please help.
Will assign points for solutions.
Thanks a lot in advance.Hi Nitin,
You mean the available amount for each house bank?
I have alraedy maintained the amount for each house bank at a very high amount and the F110 payment is not exceeding the amount set.
Please help. -
Using lag and rank in the same query
Hi
I am trying to find out the difference in time between peoples memberships and also the order that these memberships are taken out in. So far I have added in a rank statement to work out the order the memberships were created in, but now want to look at the difference between the dates returned. The SQL I used is:
SELECT owner_party_id,
mem_number,
support_id,
mem_start_date,
RANK() OVER (PARTITION BY owner_party_id ORDER BY mem_start_date ASC) MEMBERSHIP_SEQUENCE
FROM membership_all
WHERE version_type = 'CUR'
AND owner_party_id IN ('65051', '65051', '65348', '65348', '65607', '65607', '65607')
to get:
"OWNER_PARTY_ID"|"MEM_NUMBER"|"SUPPORT_ID"|"MEM_START_DATE"|"MEMBERSHIP_SEQUENCE"
65051|318874751|8014747|01-MAR-10|1
65051|412311060|21502883|15-AUG-12|2
65348|308672459|3526913|01-MAY-10|1
65348|409951130|20950524|18-JUN-12|2
65607|315830192|7510133|17-MAY-10|1
65607|406448110|20024246|16-MAR-12|2
65607|409738130|20903556|14-JUN-12|3
Now I would like to calculate the difference between the start dates of each of the owner_party_id groups, so to get something like this:
OWNER_PARTY_ID|MEM_NUMBER |SUPPORT_ID|MEM_START_DATE |MEMBERSHIP_SEQUENCE|Diff
65051|318874751|8014747|01-Mar-10|1|
65051|412311060|21502883|15-Aug-12|2|898
65348|308672459|3526913|01-May-10|1
65348|409951130|20950524|18-Jun-12|2|779
65607|315830192|7510133|17-May-10|1
65607|406448110|20024246|16-Mar-12|2|669
65607|409738130|20903556|14-Jun-12|3|90
I think that I need to use the Lag function in, but I am not too sure if it can be linkited to look at the data within a grouping of owner party id, as it would make no sense to calculate the difference in dates for two different owner party ids.
Any advice much appreciated.
Thanks
Edited by: 992871 on 09-Mar-2013 23:34Couple notes:
1. You wrote you want to get order that these memberships are taken out in, however, both your and Etbin's queries calculate order within each owner_party_id and not across all members. If you want to get rank and difference in time regardless of member's owner_party_id remove PARTITION BY caluse.
2. You might want to use DENSE_RANK and not RANK depending how you want to display rank. If two people joined at the same time and were second in rank, analytic RANK will be:
RANK
1
2
2
4
5
.while DENSE_RANK:
DENSE_RANK
1
2
2
3
4
.SY. -
Need Help to Densify and Rank Data
Hi Pros!
I've been researching, but can still use some help here with a telecom application. Say I have the following records in table cost:
Acct,Code, Rate
1, 001, .1
1, 0012, .1
2, 001, .2
2, 0012, .2
2, 00123, .2
2, 001234, .2
And I want the following results:
Row, Code, Acct, Rate, Rank,
1, 011, 1, .1, 1
2, 011, 2, .2, 2
3, 0012, 1, .1, 1
4, 0012, 2, .2, 2
5, 00123, 1, .1, 1
6, 00123, 2, .2, 2
7, 001234, 1, .1, 1
8, 001234, 2, .2, 2
I use the following query for the ranking:
SELECT code, acct, rate, drank
FROM (SELECT code, acct_id, DENSE_RANK() OVER(PARTITION BY code ORDER BY rate ASC) drank
FROM costs)
Because the data is not uniform (or dense), I would only get the following results:
Row Code Acct Rate Rank
1, 011, 1, .1, 1
2, 011, 2, .2, 2
3, 0012, 1, .1, 1
4, 0012, 2, .2, 2
5, 00123, 2, .2, 1
6, 001234, 2, .2, 1
So what I'm looking for is to densify the data so that for code 00123 and 001234
rates in account 1 would be duplicated for the higher codes as follows:
Acct Code Rate
1, 001, .1
1, 0012, .1
1, 00123, .1
1, 001234, .1
So if accounts without rates for the more specific codes are replicated out to match accounts with the most specific codes, I would have correct ranking. This is for routing purposes, so if a shorter code exists within a longer code and has a cheaper rate, then I would want to have a match there. Cost table can have 10 m rows or more.
Any help would be greatly appreciated
Thanks all.You may use partitioned outer join.
Completion of data series by analytical function -
Need help with RANK() on NULL data
Hi All
I am using Oracle 10g and running a query with RANK(), but it is not returning a desired output. Pleas HELP!!
I have a STATUS table that shows the history of order status.. I have a requirement to display the order and the last status date (max). If there is any NULL date for an order then show NULL.
STATUS
ORD_NO | STAT | DT
1 | Open |
1 | Pending |
2 | Open |
2 | Pending |
3 | Open |1/1/2009
3 | Pending |1/6/2009
3 | Close |
4 | Open |3/2/2009
4 | Close |3/4/2009
Result should be (max date for each ORD_NO otherwise NULL):
ORD_NO |DT
1 |
2 |
3 |
4 |3/4/2009
CREATE TABLE Status (ORD_NO NUMBER, STAT VARCHAR2(10), DT DATE);
INSERT INTO Status VALUES(1, 'Open', NULL);
INSERT INTO Status VALUES(1, 'Pending', NULL);
INSERT INTO Status VALUES(2, 'Open', NULL);
INSERT INTO Status VALUES(2, 'Pending',NULL);
INSERT INTO Status VALUES(3, 'Open', '1 JAN 2009');
INSERT INTO Status VALUES(3,'Pending', '6 JAN 2009');
INSERT INTO Status VALUES(3, 'Close', NULL);
INSERT INTO Status VALUES(4, 'Open', '2 MAR 2009');
INSERT INTO Status VALUES(4, 'Close', '4 MAR 2009');
COMMIT;
I tried using RANK function to rank all the orders by date. So used ORDER BY cluse on date in descending order thinking that the null dates would be on top and will be grouped together by each ORD_NO.
SELECT ORD_NO, DT, RANK() OVER (PARTITION BY ORD_NO ORDER BY DT DESC)
FROM Status;
...but the result was something..
ORD_NO |DT |RANKING
*1 | | 1*
*1 | | 1*
*2 | | 1*
*2 | | 1*3 | | 1
3 |1/6/2009 | 2
3 |1/1/2009 | 3
4 |3/4/2009 | 1
4 |3/2/2009 | 2
I am not sure why didn't the first two ORD_NOs didn't group together and why ranking of 1 was assigned to them. I was assuming something like:
ORD_NO |DT |RANKING
*1 | | 1*
*1 | | 2*
*2 | | 1*
*2 | | 1*
3 | | 1
3 |1/6/2009 | 2
3 |1/1/2009 | 3
4 |3/4/2009 | 1
4 |3/2/2009 | 2
Please guide me if I am missing something here?
Regards
SriHi,
If i well understood, you don't need rank
SELECT ord_no, MAX (dt)KEEP (DENSE_RANK LAST ORDER BY dt) dt
FROM status
GROUP BY ord_no
SQL> select * from status;
ORD_NO STAT DT
1 Open
1 Pending
2 Open
2 Pending
3 Open 2009-01-01
3 Pending 2009-01-06
3 Close
4 Open 2009-03-02
4 Close 2009-03-04
9 ligne(s) sélectionnée(s).
SQL> SELECT ord_no, MAX (dt)KEEP (DENSE_RANK LAST ORDER BY dt) dt
2 FROM status
3 GROUP BY ord_no;
ORD_NO DT
1
2
3
4 2009-03-04
SQL> -
How can i use multiple values in RANK
HI All,
we hav eto display RANK by mutilpe values .
i mean we have sales amount , qty, name we have to dispaly top 20 rank with amount if amount is equal after 6 th rank then second pirority goes to qunatity for remaiing ranks if after 15th rank if quantity also equal then it should rank based on name for remaing ranks is it possible in Obiee 11g?
If we are able to top 20 ranks by amount only then we are no need to worry about remaing values.
like RANK ( Amount , Qty , Name ) ....
Thanks in advance for your time.If yours is OBIEE 11g play with Selection Steps you will get that fixed easily the following blog will give you an idea
Rittman Mead Consulting » Blog Archive » Oracle BI EE 11g – Filters & Selection…
Thanks
NK -
Using ranking and sum to get top 5 products' share of total market
Hi everybody,
I have been playing around with ranking for a while now and I am stuck..
I have a crosstab where I want to show the top 5 products in a specific month. So I have month in my column and products in my rows. I applied a ranking based on revenue only showing the top 5 products. This works like a charm.
Now I would like to sum up the top 5 products' revenue and compare it with the revenue of all products in the market. I have managed to sum the top 5 products' revenue by using the "sum" function in the block, but I have not been able to compare this measure with a total market revenue measure. It seems like because I made the ranking on the top 5 products I cannot create a measure which includes more than these top 5 products and put it in that block.
Is this not something Webi can easily handle??
Thanks in advance!
Best regards,
MortenHi Jaime,
Thank for the suggestion. I forgot to mention that the month column is only showing one month. There is a filter on the month dimension, so that only one month is shown.
I tried using the In Report context, but it gave me wrong results... It seems like it included all months.. I also tried using the noFilter function, but it also gave me wrong results.
Best regards,
Morten -
Top 'n' using rank () function
Hi...
I'm using BO XI 3.0. In my report I need a user to enter TOP 'n' (n=5,10,20,50,100).
For example:
Caller No No of Calls
23467891 10
67698089 35
22377868 17
76378467 10
76374678 20
89978978 6
87786777 11
I already create dimension object for Caller No and measure object for No of Calls in universe.
So I use a rank filter in edit query but it doesn't work.
I also create another measure object in universe name =ranking
rank(No_of_Calls) over(partition by (Caller No) order by (No_of_Calls)).. but it's not work...
So can anybody help me....thanksHi,
Following information might be helpful in resolving the issue.
General Syntax of Ranking :
Rank( measure; (dimensions); [Top|Bottom])
Here is the work flow with an example :- (use efashion universe )
1) Create a webi report selecting objects quarter, year, sales revenue and create a cross tab
2) While creating a cross tab, use the Object Year in columns and object Quarter for rows.
3)Define a variable Var1 = Rank ([Sales Revenue] ; [Quarter]; [Year]) ForEach ([Year])
4) Insert a blank column next to year in the cross tab.
5) Drag and drop the variable Var1 in the new blank column inserted next to Object Year..
You will now find that you have ranking for the years 2001, 2002, 2003 being reflected
independently.
Regards,
Sarbhjeet Kaur -
Ranking is not working properly on measure variables
Dear All,
I am facing a strange issue in my report,i have created a cross tab report with vendor,month,qunatity i have applied rank(Top5) on this table this working fine.
I need to show quantity share% also in the report so to achieve this i have created two variable in webi:
Total Qty for share:
=Sum([Quantity] For All([Vendor Name])
%Qty share:
=[Quantity]*100/[Total Qty for share]
I have used %qty share in the table,now the table contains vendorname,month,%Qty share,i have applied rank(For Top5) in this table but after apply rank the values in the table is not showing properly .
For example before ranking i have 6 for the month of jan but after applying ranking it is becoming 14 in the table.
Why it is happening is there any problem in the variables.
I have tried rank in the both the ways ,the option in webi and the formula by using Rank function:
Rank([quantity[;Vendorname]) both is giving the wrong values.
Please suggest me it is very urgent where i need to deliver the report.
PFA screen shot for the values how it is showing.
BO Version:
BI 4.1 sp5
Regards,
MaheshHi Mahesh,
For
%Qty share:
=[Quantity]*100/[Total Qty for share]
use following formual
=([Quantity]/[Total Qty for share])*100
Thanks,
Swapnil -
Hello guys,
I'm using DS 1.3 OVER BI 4.1 SP3.
My model was universe-based until now.
Inside query specifications on universe, I was able to set TOP 10.
Now, I've advanced into OLAP over HANA, and I can't find a solution to TOP 10 (not even a dynamic ranking) on my Datasource.
Any simple solution for setting a constant ranking on a datasource which is HANA-OLAP-connection?
(And if we're at it, is there a solution for a dynamic ranking also? (plan for the future:))
Thank you,
Or.Hi Tammy,
Thanks for the reference, but It's not the right solution for me because I'm using graphical Calculation View.
From your link I somehow got into this link:
SAP HANA: Implementing RANK using Graphical calculation View
If I had "RANK" column it'll be simple actually as a Variable inside HANA,
but I have to say that this guide for creating a "rank" column is way-too complicated to replace the good-old TOP N inside SQL or other tools.
Do you know of any simpler way inside HANA, maybe in newer versions, of creating a RANK?
Thank you,
Or -
Sql query slowness due to rank and columns with null values:
Sql query slowness due to rank and columns with null values:
I have the following table in database with around 10 millions records:
Declaration:
create table PropertyOwners (
[Key] int not null primary key,
PropertyKey int not null,
BoughtDate DateTime,
OwnerKey int null,
GroupKey int null
go
[Key] is primary key and combination of PropertyKey, BoughtDate, OwnerKey and GroupKey is unique.
With the following index:
CREATE NONCLUSTERED INDEX [IX_PropertyOwners] ON [dbo].[PropertyOwners]
[PropertyKey] ASC,
[BoughtDate] DESC,
[OwnerKey] DESC,
[GroupKey] DESC
go
Description of the case:
For single BoughtDate one property can belong to multiple owners or single group, for single record there can either be OwnerKey or GroupKey but not both so one of them will be null for each record. I am trying to retrieve the data from the table using
following query for the OwnerKey. If there are same property rows for owners and group at the same time than the rows having OwnerKey with be preferred, that is why I am using "OwnerKey desc" in Rank function.
declare @ownerKey int = 40000
select PropertyKey, BoughtDate, OwnerKey, GroupKey
from (
select PropertyKey, BoughtDate, OwnerKey, GroupKey,
RANK() over (partition by PropertyKey order by BoughtDate desc, OwnerKey desc, GroupKey desc) as [Rank]
from PropertyOwners
) as result
where result.[Rank]=1 and result.[OwnerKey]=@ownerKey
It is taking 2-3 seconds to get the records which is too slow, similar time it is taking as I try to get the records using the GroupKey. But when I tried to get the records for the PropertyKey with the same query, it is executing in 10 milliseconds.
May be the slowness is due to as OwnerKey/GroupKey in the table can be null and sql server in unable to index it. I have also tried to use the Indexed view to pre ranked them but I can't use it in my query as Rank function is not supported in indexed
view.
Please note this table is updated once a day and using Sql Server 2008 R2. Any help will be greatly appreciated.create table #result (PropertyKey int not null, BoughtDate datetime, OwnerKey int null, GroupKey int null, [Rank] int not null)Create index idx ON #result(OwnerKey ,rnk)
insert into #result(PropertyKey, BoughtDate, OwnerKey, GroupKey, [Rank])
select PropertyKey, BoughtDate, OwnerKey, GroupKey,
RANK() over (partition by PropertyKey order by BoughtDate desc, OwnerKey desc, GroupKey desc) as [Rank]
from PropertyOwners
go
declare @ownerKey int = 1
select PropertyKey, BoughtDate, OwnerKey, GroupKey
from #result as result
where result.[Rank]=1
and result.[OwnerKey]=@ownerKey
go
Best Regards,Uri Dimant SQL Server MVP,
http://sqlblog.com/blogs/uri_dimant/
MS SQL optimization: MS SQL Development and Optimization
MS SQL Consulting:
Large scale of database and data cleansing
Remote DBA Services:
Improves MS SQL Database Performance
SQL Server Integration Services:
Business Intelligence -
Help needed in OBIEE Rank function
Hi All,
I have a request having Period, Amount,Branch code. In this i need to figure out the top N ranks (N is the user input which is populated using Presentation variable) based on the amount.
Then in the final output it should display as Top N branches with its previous period's data.
For eg: If we try to rank the amount for period=31-dec-2010. The final output should be displayed in pivot table as
*(Branchname in Pivottable section)*
* (Period to be displayed columnwise) 31-Mar-2010 30-Jun-2010 30-Sep-2010 31-Dec-2010*
Amount (Measure)
Can you guide me to achieve the above request?
Is there a way to calculate rank based on two columns?
Kindly help me.
Regards,
Bhuvan RCREATE OR REPLACE FUNCTION fucntion_name(latA IN NUMBER, longA IN NUMBER, latB IN NUMBER, longB IN NUMBER) RETURN NUMBER
IS
pi CONSTANT NUMBER:=3.14159;
theta NUMBER;
distX NUMBER;
distY NUMBER;
distZ NUMBER;
distP NUMBER;
BEGIN
theta :=longA - longB;
distX :=sin( latA * PI /180) * sin ( latB * PI /180) + cos ( latA * PI/180) * cos ( latB * PI/180) * cos ( theta * PI / 180);
distY :=acos(distX); --this is arc cosine
distZ :=distY * 180/PI; --PI refers to the mathematical PI
distP :=distZ * 60 * 1.1515; --this value should be returned. Of course the intermediate variable names don't matter.
RETURN distP;
END;Edited by: Ora on May 3, 2011 11:46 PM -
RAM once and for all (singl-/dual-rank)
Seriously, everyone says different things (seemingly)
What's best to do...replace my existing 8GB RAM with as many dual-rank 4GB modules as possible/affordable, or keep the existing and buy (the more expensive) single-rank 4GB chips?
dual rank chips are about half the price of single.
is there some reason not to just go the cheaper option?Ratbaggy wrote:
yeah cool, and thanks. interesting though...why would Apple use RAM that performs worse.
While technically faster, just like populating three identical DIMMs to enable triple channel didn't show any measurable performance difference, I would imagine if you really benchmarked single vs. double ranked DIMMs you would be hard pressed to find a measurable difference.
It comes to cost. Smaller capacity DIMMs are easier to manufacture in single ranked/fewer chip per module configurations. Higher density chips are more expensive, so that's why you see 4 and 8 GB DIMMs mainly as dual ranked DIMMs
It's not an evil conspiracy but simple economics. It's just too bad Apple isn't more up front about the whole rank issue, it would solve a lot of confusion. The only reason I even knew about it is I just had to read up on it for some server purchases. -
Since updating to Firefox 4.0.1 I have lost the site security ranking provided by Norton. can I incorporate this in the latest Firefox? Failing that, how can I return to my previous version?
http://community.norton.com/t5/Norton-Internet-Security-Norton/Norton-Toolbar-not-compatible-with-FF-4-0-1/td-p/442788
-
All podcast episodes have disappeared and I'm no longer ranking
I've successfully uploaded 6 podcast episodes over the past couple of weeks and had been ranking pretty well as of last night. Today I check and my podcast has disappeared from the ranks. When I search on my podcast, it is listed with artwork and description and ratings, but the episodes are all gone. I appreciate any suggestions or ideas on what happened and what I can do about it. Here is my link: https://itunes.apple.com/us/podcast/win-the-weight-game/id677470205
I would highly highly recommend if you are hosting with libsyn - use the libsyn feed - and not the one via powerpress. Publish the episodes to your libsyn account - and then connect your libsyn account to your wordpress site via the libsyn Wordpress feature.
http://support.libsyn.com/faqs/onpublish-wordpress/
There are so many tickets on here about issues with wordpress.
If you want your show to be in iTunes and be solid in iTunes - stick with the feed from libsyn and not from your wordpress / powerpress configuration. In the long run you will have much much fewer issues - and libsyn support is there to help you for any issues you might have.
Email me if you want to know how to switch to the libsyn feed.
rob (at) libsyn dot com
One of the best features of Libsyn - is offering you a feed that works all the time with iTunes.
Regards,
Rob W
libsyn
Maybe you are looking for
-
The problem is very simple. As soon as I downloaded Firefox 4.0 I lost my add-ons. The add-ons were the reason I have been using Firefox for several years. This seems to be ludicrous. The new version should be able to manage integration of existing a
-
Using my iPhone 5 (Verizon) in Europe...
Since the iPhone 5 on Verizon is unlocked, can I buy a pre-paid sim from one of the European carriers like Vodafone while vacationing there? I'm traveling to England, France and Italy the 2nd week of October, and would like to be able to use my phone
-
For work, I had to update my Microsoft Office (excel, word, outlook, etc.) to Office 2013. Once I did the update, my Acrobat XI Pro no longer pops up in the tab to convert excel to pdf. I figured out there was an update for Acrobat XI so I installe
-
Web service empty relationship problem
hi all, I have a pretty simple web service. Two entities, a citizen and a business. A citizen is able to register 0..n businesses. There is a WS which reads the citizen. When the business key list in citizen is empty the web service wont return the c
-
We have a three node 11.1.0.6 RAC. Each node has a different undo tablespace specified...these parms: INST_ID NAME VALUE DISPLAY_VALUE 1 db_flashback_retention_target 1440 1440 2 db_flashback_retention_target 1440