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
Similar Messages
-
From two given tables, how do you fetch the values from two columns using values from one column(get values from col.A if col.A is not null and get values from col.B if col.A is null)?
Hi,
Use NVL or COALESCE:
NVL (col_a, col_b)
Returns col_a if col_a is not NULL; otherwise, it returns col_b.
Col_a and col_b must have similar (if not identical) datatypes; for example, if col_a is a DATE, then col_b can be another DATE or it can be a TIMESTAMP, but it can't be a VARCHAR2.
For more about NVL and COALESCE, see the SQL Language manual: http://docs.oracle.com/cd/E11882_01/server.112/e26088/functions119.htm#sthref1310
I hope this answers your question.
If not, post a little sample data (CREATE TABLE and INSERT statements, relevant columns only) for all tables involved, and also post the results you want from that data.
Explain, using specific examples, how you get those results from that data.
Always say which version of Oracle you're using (e.g., 11.2.0.2.0).
See the forum FAQ: https://forums.oracle.com/message/9362002 -
I just bought photoshop elements 13 last night. Today I am watching tutorial videos to show me how to use it, but the videos are showing me things I don't have. Asking me to click on tabs i don't have. Why is this? Is there another way I can learn? Or is my download messed up and not giving me everything I need?
Got a link showing us which video tutorials you're watching and examples of what's missing in your software?
-
How to use shortDesc in the disabled inputtext
hi,all
i encounter a problem,our team must use disabled inputtext when the text is only used to output string.
the reson we don't use outputext is if there is noting in the outputtext,there is nothing displayed.
how to use shortDesc in the disabled inputtext?
here is the code:
<af:inputText value="#{bindings.shortdesc.inputValue}" disable="true"
id="ot2" columns="25" shortDesc="#{bindings.longdesc.inputValue}"
simple="true"/>
thanks allHi,
........... I use JDev 11g ..............
If you make your inputText disabled -----> shortDesc will not be displayed.
You can make a workaround to achieve this by using popup as :
1- add showPopupBehavior inside your inputText as :
<af:inputText label="label1" id="it1" disabled="true">
<af:showPopupBehavior triggerType="mouseOver"
align="endBefore" alignId="it1"
popupId="p1"/>
</af:inputText>2- add a popup which will display your shortDesc text
<af:popup id="p1">
<af:outputText value="Sameh POPUP" id="ot1"/>
</af:popup>I hope it is useful.
Sameh Nassar -
How to use labview with the handyboard
Hi,
how to use labview with the handyboard
Thx...I'm assuming you're talking about this, since you didn't provide a link for those of us who don't know what you're talking about.
As the other poster said, you didn't say how you want to use LabVIEW with it. If you want to write LabVIEW programs than run on the microprocessor, then you're out of luck. If you want LabVIEW to interact with it, then you've got a couple of options, SPI probably being the best, but it also has DI and AI that you could use to communicate with it - the DI's could be used as a parallel interface.
Message Edited by Matthew Kelton on 12-17-2007 02:21 PM -
1,How to use PS to judge the existence of a folder in the specified directory?(use javascript)
2, how to use PS in the specified directory to create to create the folder?(use javascript)
Thanks you!Thanks you!
-
I´ve made four Columns with text and an Image over the middle of two columns. How do I put a text under the Image that spans over the Columns?
Did you mean that?
or
Did you mean that?
Let me know. -
I downloaded the free trial for Adobe XI Pro but now I cant find it on my computer or figure out how to use it. The only thing I see that downloaded is Adobe Download Assistant. How can I access the program?
Assistanttolindsey the Adobe Download Assistant will download the installation files for Adobe Acrobat Professional XI. By default they are saved to your download folder. You can begin the installation process from there.
-
When i put my mouse over a tab it used to show the site now it dose not
when i put my mouse over a tab it used to show the web page now it dose not do that any ideas
== This happened ==
A few times a week
== im not sureTab Scope: https://addons.mozilla.org/en-US/firefox/addon/4882/
Image size can be set to 2 levels and viewed with +/- built-in to the add-on drop-down interface/image. -
How can I get extract the data between two cursors on an XY graph
How can I get extract the data between two cursors on an XY graph
Well, you say xy graph, so this might be a more complicated problem.
For a waveform graph it's trivial. Simply get the two cursor indices (property: cursor index) and apply them to array subset of the data. Is that all you need?
Here's how the above code would look like. using cursor.index instead of cursor.x elimnates the need to include scaling information.
For an xy graph, there could be multiple segments (e.g. imagine a spiral that passes the desired x range multiple times from both sides). This would neeed significantly more code for a general solution.
Message Edited by altenbach on 11-24-2009 07:53 AM
LabVIEW Champion . Do more with less code and in less time .
Attachments:
cursorsubset.png 17 KB -
How to get the difference between two columns in a column group
Hi All,
My first time here and really new to programming. I would like to get the difference between 2 columns that are inside
a column group.
Here is my sample table below: The Column Group is PeriodNumber and can only choose 2. like 1 and 2.. I would like to have a third row which will simply calculate the difference between the amounts in PeriodNumber 1 and 2.
PeriodNumber
Account 1 2
1) Cash 10,000 15,000
2) Receivables 12,000 11,500
3) Equipment 5,000 5,500
Total Assets 27,000 32,000Hi yabgestopa,
From your description, you want to get the difference between two columns in a column group. After testing it in my environment, we can use custom code to achieve your requirement. For more details, you can refer to the following steps:
Copy the custom code below and paste it to your report. (Right-click report>Report Properties>Code)
Dim Shared Num1 As Integer
Dim shared Num2 As Integer
Public Function GetAmount(Amount as Integer, Type as String)
If Type = "1" Then
Num1=Amount
Else
Num2=Amount
End If
Return Amount
End Function
Public Function GetDif()
Return Num1-Num2
End function
Right-click the second column to insert a third column with Outside Group-Right.
Then use the expressions below in the matrix.
=Code.GetAmount(Fields!Amount.Value,Fields!PeriodNumber.Value)
=code.GetAmount(Sum(Fields!Amount.Value),Fields!PeriodNumber.Value)
=Code.GetDif()
The report looks like below.
If you have any questions, please feel free to ask.
Thanks,
Katherine Xiong
Katherine Xiong
TechNet Community Support -
How to use C:when test... inside column in ADF table
I am using ADF table with below two columns
in First column i have to check the Type of document if it is doc type then i have to use commondlink to download that file ,Otherwise i need to show only text.
for that i added
*<c:when test="{boolean($favoriteType eq 'doc')}">*
which is not working .
please let me know how to use <C:when test... inside column in ADF table
<tr:column sortProperty="favoriteName" sortable="true"
headerText="#{res['favorite.favoritename']}"
width="500" noWrap="false">
<c:choose>
*<c:when test="{boolean($favoriteType eq 'doc')}">*
<tr:commandLink actionListener="#{bindings.downloadFile.execute}"
text="#{row.favoriteName}"
disabled="#{!bindings.downloadFile.enabled}"/>
</c:when>
<c:otherwise>
<af:outputText value="#{row.favoriteName}"/>
</c:otherwise>
</c:choose>
</tr:column>
<tr:column sortProperty="favoriteType" sortable="true"
headerText="#{res['favorite.favoriteType']}" rendered="true">
<af:outputText value="#{row.favoriteType}" id="favoriteType"/>
</tr:column>Hi Frank,
Thanks it is working like cham..
related to same page i am facing new problem which i posted at below thread
How to get row data runtime @ trinidad table , set rowSelection="multiple"
can u reply on same.
Thanks for all help.
Jaydeep -
Can i search multiple sheets in a document for text in column a to return the entire row if column a matches in a
separate sheet?Thank you, Barry. That was helpful, and am hopeful that what I want to do is possible.
I am creating a spreadsheet that currently has 20 sheets, of which certain sheets have more than one table. I will be adding more sheets. I would like to return results for all occurences of the search string. Preferably into a seperate spreadsheet. It would be perfect if that seperate sheet updated whenever I update information in the first spreadsheet. To give an example:
Sheet: baskets
Tables: straw
wire
Sheet: barware
Tables: glasses
decanters
coasters
My tables all have the same titiels:
Vendor Description Cost Selling Price
Since I will have upwards of 100 sheets, with multiple tables, and most of my vendors will fit into multiple sheet categories, it would be helpful if I could also see what my order will be from each vendor, not just who I will be ordering each item from. How would I do that?
I hope I conveyed that properly.
Thank you in advance for your help,
Rana -
Return the difference between two strings
Is it possible to return the difference between two strings?
For example
String one = "We went for a walk";
String two = "We went for a run";
compare "two" to "one" returns "walk"
compare "one" to "two" returns "run"I need to return the difference of two strings, one prior to editing and one after editing.
Then I want to return any difference between the two, which will be the effect of the editing.
Cheers AlexInteresting problem. Before you start writing any code, make up a whole bunch of test case pairs. Decide what the difference between the pairs is. When you start seeing patterns, analyse them. After a while you may have enough data to start writing your program. Here's one test case to get started with:"I saw the cat eat a rat"
"My cat is hungry" -
How do you get the sum of two columns multipied together?
I can't seem to figure out how to get the sum of two columns multiplied together without having to manually type out each location (example: A1*B1+A2*B2+A3*B3, etc..). Though the idea seems to be rather simple, everything I have tried has only given me an error. I know there must be an easier way of doing this, but I get lost in the explanations given in 'help' area of numbers, can someone help me?
C3=SUMPRODUCT(A3:A18, B3:B18)
the function SUMPRODUCT() takes ranges and multiplies corresponding cells in the ranges, then adds them together.
I the case I show SUMPRODUCT() performs:
A3*B3 + A4*B4 + A5*B5 + A6*B6 + ... + A18*B18
If you want to perform the same operation on the whole column (s) you could modify the formula:
C3=SUMPRODUCT(A, B)
Maybe you are looking for
-
This Apple ID has not yet been used...
with the iTunes store. First time logging onto iTunes, get this message after entering username and password. I click the review button and I get a new error message that reads YOUR REQUEST COULD NOT BE COMPLETED tells me to make sure date is set cor
-
Big Problem With Corrupted Image Library - Need Help Please
I recently updated my Aperture 2 (2.1) with the latest update. Upon doing so, most images in my library now show up in the 'ole maroon box with the message "Unsupported Image Format." This only began after the update. I've spent hours reading threads
-
Hi, We have service and which is called in batch. But when a service is called a dialog work process is initiated and the job is failing after 30 mins (1800 secs). Could you please suggest a way so that a service can be run in batch. Your help is rea
-
Have not been able to figure this one out, and it seems pretty bizarre. I begin with NO windows open on the desktop. My script begins with: tell application "Finder" open folder (path to applications folder as Unicode text) set WinID_ to item 1 of ev
-
Help Please! Dumb Mistake. Made No Recovery Disc
I have a Toshiba Satellite A355D-S6922 and unfortunately did not ccreate a recovery disc. I purchased the laptop in 2008 or 2009 and this morning it would not start. I repeatedly got a message that the problem could not be repaired automatically. I