Greatest-n-per-group problem
Hi folks.
Say I have the following (simplified):
[Parent] id, name, address
[Child1] id, parentId, lastUpdatedBy, lastUpdatedOn
[Child2] id, parentId, lastUpdatedBy, lastUpdatedOn
[Child3] id, parentId, lastUpdatedBy, lastUpdatedOn
I want to do a select of the parent and the last updated details of whichever child was most recently updated.
SELECT
parent.ID,
child1.lastUpdatedOn,
child2.lastUpdatedOn,
child3.lastUpdatedOn,
child1.lastUpdatedBy,
child2.lastUpdatedBy,
child3.lastUpdatedBy
FROM PARENT parent
LEFT OUTER JOIN CHILD1 child1
ON parent.id = child1.parentId
LEFT OUTER JOIN CHILD1 child2
ON parent.id = child2.parentId
LEFT OUTER JOIN CHILD1 child3
ON parent.id = child3.parentId
AND NOT EXISTS (
SELECT * FROM CHILD1 child1_b
WHERE child1_b.parentId = child1.parentId
AND child1_b.lastUpdatedOn> child1.lastUpdatedOn
AND NOT EXISTS (
SELECT * FROM CHILD1 child2_b
WHERE child2_b.parentId = child2.parentId
AND child2_b.lastUpdatedOn> child2.lastUpdatedOn
AND NOT EXISTS (
SELECT * FROM CHILD1 child3_b
WHERE child3_b.parentId = child3.parentId
AND child3_b.lastUpdatedOn> child3.lastUpdatedOn
So my final record contains the parent and the lastUpdatedDetails for each child type.
parent.ID, lastUpdatedOn_1, lastUpdatedOn_2, lastUpdatedOn_3, lastUpdatedBy_1, lastUpdatedBy_2, lastUpdatedBy_3
I now want to get the most recent lastUpdatedBy and lastUpdatedOn across the three child types.
I can do:
GREATEST(child1.lastUpdatedOn, child2.lastUpdatedOn, child3.lastUpdatedOn) as lastUpdatedOn
but I can not do the same (as far as I am aware) with the lastUpdatedBy records.
Does anyone know how I might achieve this?
If only there was a function like GREATEST that could take a list of values to compare but also output values. So the result of greatest is not the comparison value but rather some associated values.
GREATEST(PAIR(INPUT, OUTPUT)...) that could allow me to do:
GREATEST(PAIR(child1.lastUpdatedOn, child1.lastUpdatedBy), PAIR(child2.lastUpdatedOn, child2.lastUpdatedBy), PAIR(child3.lastUpdatedOn, child3.lastUpdatedBy)) as lastUpdatedBy
Any help greatly appreciated.
Thanks
Chris
Hi,
You can do something like this:
WITH got_r_num AS
SELECT p.id
, c.lastupdatedon
, c.lastupdatedby
, ROW_NUMBER () OVER ( PARTITION BY p.id
ORDER BY lastupdatedon DESC
, lastupdatedby -- See note below
) AS r_num
FROM parent p
LEFT OUTER JOIN child c ON p.id = c.parentid
SELECT id
, lastupdatedon
, lastupdatedby
FROM got_r_num
WHERE r_num = 1
ORDER BY id
;If the combination (parentid, lastupdatedon) is not unique in child, then this considers a row with the earlier lastupdatedby to be later other rows with the same parentid and lastudpdatedon. Depending on your data and your reequirements, you may need to change the analytic ORDER BY clause, and/or use RANK instead of ROW_NUMBER.
I hope this answers your question.
If not, post a little sample data (CREATE TABLE and INSERT statements, relevant columns only) for all the tables involved, and the results you want from that data.
Explain, using specific examples, how you get those results from that data.
Always say what version of Oracle you're using.
See the forum FAQ {message:id=9360002}
Similar Messages
-
Report design Grouping problem
I'm currently designing a report, something like an invoice. The invoice number is usually on the page header. But now i want to create a report to list down all the invoice numbers in the 'details' but when i drag the 'invoice number' data to 'details'
it only show 1 row of numbers, wheareas the data has few rows of it. How can i make it view all my numbers?Hi Eugene,
Thank you for posting in MSDN forum.
Since this forum is discuss the VS test issue, so
I’m afraid that it is not the correct forum for this issue.
As you said that the report design grouping problem, I suggest you could try to consult this issue directly to the VS Report Controls forum:
https://social.msdn.microsoft.com/Forums/en-US/home?forum=vsreportcontrols , maybe you will get better support.
Best Regards,
We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
Click
HERE to participate the survey. -
Percentage based on Status using per group
Hi Gurus,
Can you help me with this? You have any approach of getting the percentage based on status using per group?
Currently I have this code below but it doesn’t have result but no error. I am trying to get the % In Complete.
<?xdofx:(sum(current-group()/NUMINCOMPLETE)/(sum(NUMSUBMITTED) + sum(NUMCOMPLETED) + sum(NUMINPROGRESS) + sum(NUMINCOMPLETE)))*100?>
Thanks Much,
JP
Edited by: BIPnewbie on Feb 6, 2012 3:05 AMUse this:
<?xdoxslt:div(sum(current-group()/NUMINCOMPLETE), ((sum(NUMSUBMITTED) + sum(NUMCOMPLETED) + sum(NUMINPROGRESS) + sum(NUMINCOMPLETE)))*100?>
Thanks,
Bipuser -
How to limit row number per group without change new page in crystal report
Hi All Expert,
Is there any way to limit row number per group without change new page in crystal report 2008. The reason i do that is due to customer using printer EPSON LQ 300 (dot matrix) always will print in landscape if detech my layout in landscape. Because they need the record always print in 1 page (Letter size) for 2 groups and each group must show 5 records. Example:
But, in CR2008, if you set the row per group from group section expert, definitely it will change to new page, but I do not want it change to new page. Any Idea?
In one page (Letter size):
Group Name: Customer-ABC
NO INVOICE AMOUNT
1) INV001 USD100
2) INV002 USD100
3) INV003 USD100
4) INV004 USD100
5) INV005 USD100
Group Name: Customer-ABC
NO INVOICE AMOUNT
6) INV006 USD100
7) INV007 USD100
8) INV008 USD100
9) INV009 USD100
10) INV010 USD100Hi Angie.....
I guess it is not possible.
Because if you have one common group for all the 10 records then in one page it will show 10 records under one group.
I mean i think it is not possible but m not completely sure......
Lets wait for expert advise....
Regards,
Rahul -
Select latest two records per group
hi there,
i've been googling around and still can't really find an appropriate solution to retrieve two most recent records per group in one sql. Is this even possible?
Let's say there is a MyLife table :
ID, Event , RecordedDate
1. 1, 'Fell down the stairs', '20-DEC-07'
2. 1, 'Fell down the stairs', '22-DEC-07'
3. 1, 'Fell down the stairs', '23-DEC-07'
4. 2, 'Tried to kiss santa', '23-DEC-07'
5. 3, 'Reindeer stolen', '24-DEC-07'
6. 4, 'Chimney Broke', '25-DEC-07'
Output should be :
2. 1, 'Fell down the stairs', '22-DEC-07'
3. 1, 'Fell down the stairs', '23-DEC-07'
4. 2, 'Tried to kiss santa', '23-DEC-07'
5. 3, 'Reindeer stolen', '24-DEC-07'
6. 4, 'Chimney Broke', '25-DEC-07'I believe that something along these lines would be portable to most other databases.
SQL> SELECT id, event, recordeddate
2 FROM mylife o
3 WHERE recordeddate = (SELECT MAX(recordeddate) FROM mylife i
4 WHERE o.id = i.id)
5 UNION ALL
6 SELECT id, event, recordeddate
7 FROM mylife o
8 WHERE recordeddate = (SELECT MAX(recordeddate) FROM mylife i
9 WHERE o.id = i.id and
10 i.recordeddate < (SELECT MAX(recordeddate)
11 FROM mylife ii
12 WHERE i.id = ii.id))
13 ORDER BY id, recordeddate;
ID EVENT RECORDEDDAT
1 Fell down the stairs 22-DEC-2007
1 Fell down the stairs 23-DEC-2007
2 Tried to kiss santa 23-DEC-2007
3 Reindeer stolen 24-DEC-2007
4 Chimney Broke 25-DEC-2007John -
UME Roles/Groups problem
I have installed an ABAP +J2EE instance with the view of using it for Adobe Document Services.
While following the Adobe Document Services configuration guide, Step 3.2.1.1:
I'm creating a role in the ABAP engine, creating a user (ADSUser); creating and assigning the role (ADSCallers) to it.
When I start visual admin, i expect the user to be shown under the 'group': ADSCallers.
While I can see the user in visual admin, I'm unable to see the group (role in ABAP instance)
I'm on SP19 NW2004. Any views???There is a delay before roles show up as groups on the Java side. The delay runs about 30 minutes. See http://help.sap.com/saphelp_nw04s/helpdata/en/45/af3ac012d32e78e10000000a155369/frameset.htm
-Michael -
I have a project that is approximately 1 hour in length. This is the first project I am attempting to burn. I assigned a theme to the project and then proceeded to burn a DVD. Before the burn could actually start, PE crashed with a "blue screen" dump. I rebooted in Windows safe mode, then rebooted back into normal Windows mode. When I started PE and opened the project, nearly all of the themes were missing! It appears that only one theme per group remained.
What would have removed or hid the themes? I would have also expected all or none of the themes, rather than leaving just one per group. I was using a template in the "General" group, but the one that remained after the crash is not the one I had assigned to my project.
Does a reinstall restore the templates? If so, can I reinstall PE 4.0 from my CD without erasing, distroying, or losing the current projects I have created?
Thanks in advance for your comments!Hunt,
Before diving into the details of the items in the article, I did a few of the basic things:
stopped my anti-virus product (doh!)
stopped the temperature monitor software (doh!)
did a quick disk cleanup
turned off all trivial stuff in msconfig
I rebooted the system and started Premiere Elements. I had not attempted to reload the templates at that point, so I proceeded to "Burn to DVD". I selected the minimal quality and started the burn. That worked! Next I exited PE and restarted it and then was able to successfully "Burn to DVD" using the max setting for quality. As expected, it took a while to finish, but it also completed successfully! The resulting DVD looks and sounds great, so the rendering and burn phases seem to be working fine on my hardware.
I noticed on the CD that there is an option to load the themes, so I am going to try loading that today. I'll post more comments after I get the themes reloaded in case there are other steps involved in restoring those within the program. -
CSA 6.0 - policies in Audit mode - per groups
Hello,
Deploying CSA 6.0
Have several groups created but policies linked to these groups are the same.
I would like to have all of them work in Audit mode initially and move them
into non-Audit mode on per-group basis.
When I move policy out of Audit mode (uncheck box for specific policy) in group #1
I am getting messages that this policy will be not working in audit mode
in other groups as well because it is not in audit mode in group #1.
Is there way to work it around ?
and have policies be in Audit / non-Audit mode on per-group basis
thank you
AlexHi Tom
I agree, cloning rule modules is not an option.
But there would be no such thing as âaudit groupâ,
if I put policy in audit mode in one group (#1) and there is another group
where this policy is not in audit mode, then it becomes non-audit mode policy
even for group #1. This is what my experience tells me.
The ideal for our deployment (tens of groups, 1K users) would be able
to manipulate audit mode for policies on per-group basis.
Thanks
Alex -
Al tentativo di istallare boot camp il sistema risponde che non è possibile per un problema del network: che significa ?
Ciao Uno promo,
Lei escritto e linguale Engletare UK.
Dimaxum -
Crystal Reports Grouping Problem
Hello, I am using CRXIR2
I have a report of invoices that is grouped by PO Number. When I run the report, if there are more than one Invoice per PO. I can not see the second or third invoice. I can see the invoice numbers listed in the preview pane but when I click on the invoice number it will not show me the invoice in the report window.
Any ideas?
Thanks
JeffHey Jeff,
Maybe you could show some data. Seems like my head is needing more data, and asking:
Check the relationship between the PO and Invoice numbers.
Group on PO, and then invoice numbers. If you can't do that, you might need to start over with the query.
Try to "right join" the PO table to the invoice table. (If they are in the same table, that could be your problem). This will bring in all the PO's, and only the corresponing invoices.
Check that the data returned is truely incorrect. Sometimes it IS correct for the way the query is written. (...well, always actually....:)
The Panda -
Query to show stock value per group
Hi Experts
Would it be possible to create a query which would show me the stock value per item group in SAP? I know this can be done through Stock Audit report etc, but as we have over 300 item groups I would like to see a report which show me only the group name and the stock value within that group.
Thanks
GeoffHi Gordon,
The 2% will be because in almost all cases the query is using Last Purchase Price to calculate the value, but the stock balance on 130000 account in Financials is being generated from the FIFO values of stock. It is a problem we come up against a lot in calculating stock values - we probably wouldnt have gone down the FIFO route originally had we known!
Regards
Geoff -
[CS3] Radiobutton group problem
Hi, I'm creating a small quiz in Flash, and I have a nasty
problem I hope you can help me with.
I've set a group name like "group1" for the first group of
three radio buttons, and "group2" for the next, and so on. I have 2
groups per frame. The problem is that if I continue by having
"group3" and "group4" on frame 2, when it's exported; The first
frame is fine, but when I continue to the next frame, the radio
buttons label is gone, and the radio buttons are all selectable,
like check marks.
I would really appreciate some help on thisAnyone?
-
Report based on a reord group - PROBLEM
Using Reports Server / Forms/Reports 6i
I am trying to run a report based on a record group on the web. It fails giving the error 'Cannot run report.'
The report is called from a form and the record group is passed as a data parameter. It works fine in Client/Server.
I have tried to remove the data parameter and run the report without it but, despite working in the Client/Server environment, it still fails on the Web.
Does anyone have any suggestions?I have found the answer to my problem in the forms forum!
Apparently, this type of report won't work on the web and I have to use temporary tables! -
How do I resize pinned tabs/deal with tab group problems in Firefox 22?
With the automatic update to Firefox 22, my pinned tabs have ballooned in size; not only does this look horrible, but it reduces the real estate available for other tabs. While NoSquint has sorted the issue with websites randomly resizing themselves, everything else (add-on menu, toolbar icons) has also increased in size, which affects what can be displayed.
Another problem with 22 is that my tab groups are no longer behaving normally: I click on one group and another group moves around the screen, typically jumping behind the original group I clicked on. Any suggestions for how to improve these problems are much appreciated.hello, firefox 22 is now respecting the pixel density you've set on a system level in the windows control panel > appearance > display. more information about that is availbale at http://windows.microsoft.com/en-us/windows7/make-the-text-on-your-screen-larger-or-smaller
if you want to set the text size/pixel density in firefox different from that of your system's settings like it was handled in prior versions, enter '''about:config''' into the firefox address bar (confirm the info message in case it shows up) & search for the preference named '''layout.css.devPixelsPerPx'''. double-click it and change its value to '''1.0''' (or any other zoom factor that fits your purpose; 0.8 equals 80%, 1.15 is 115% and so on..., -1.0 is the default value and will adhere to the system settings). -
Hello All,
I have a DB that uses intermediate tables a lot. An intermediate table is one that breaks up a many-to-many relationship between two tables; by putting a table between them that uses the primary keys from both tables.
E.g. EMPLOYEE and PROJECT tables: Many Employees work on One Project; or One Employee works on Many Projects.
Here's the JHeadstart problem (9.0.4, Struts/UIX): Using the XML Application Editor, I created a Group called EMPLOYEE (select-form) and added some Lookups to it. I then added a Detail Group called EMPLOYEE_PROJECT and set it up as a table on a separate page. The application compiles without errors, but when I bring up the application and click on the "Employee-Project" tab, I receive the following error:
JBO-27122: SQL Error. "Select EMPPROJ.empid, EMPPROJ.projid, FROM EMPLOYEE_PROJECT empproj WHERE (EMPPROJ.empid =: 1) ORDER BY EMPPROJ.empid, EMPPROJ.projid"
The error comes from the WHERE clause, specifically the "=:1" part. SQL Worksheet calls the error a bad bind variable when I run the query in it. What's funny, is that the Master Detail page had Employee Number 5 up; so the query should have had the employee ID of 5, not 1. No matter which employee I bring up on the master view, I still get the same WHERE clause with the "=:1" error.
I tried adding other intermediate tables to the Master Employee detail group and get the same results.
Anyone see this before?
I suspect there's a JHeadstart setting somewhere related to the Master Detail, but I'm stumpred as to what it is.
Any thoughts or opinions would be appreciated.Tracy-Paul,
Are you using JDeveloper 9.0.3.3 or 9.0.3.4?
If so, you are probably running into BC4J bug 3214032. In the JHeadstart 9.0.4.5 release notes the following info is included:
3214032: JBO-27122: SQL error during statement preparation and ORA-1722 INVALID NUMBER IF PARENT VO ROW IN VIEW LINK REORDERS ATTRS. This bug can occur if you are using the BC4J libraries of JDeveloper 9.0.3.3 or 9.0.4.0 (or 9.0.5 Preview) and in one of your View Objects the Primary Key attribute(s) have a different index number than the Primary Key attribute(s) in the corresponding Entity Object. For example: in the Entity Object the primary key is the first attribute, but in the View Object the primary key is the last attribute. If this occurs, a workaround is to change the order of the View Object attributes.
So, you can try to reorder your attributes, or to use the BC4J libraries of JDeveloper 9.0.3.2.
We have been in close contact with BC4J product development about this bug, and they promised to publish a one-off patch for this bug on metalink as soon as possible. So keep your eye on metalink, you can also read the bug report there.
Steven Davelaar,
JHeadstart Team.
Maybe you are looking for
-
Will Late 2008 Unibody Macbook Pros support 8gb RAM
I was just wondering if now with the ram increase, the late 2008 Unibody Macbook Pros will be able to support 8 gb of RAM now that new model supports it? Thanks Message was edited by: ofon68
-
During upgrade iOS to 5 I got error 402636789, what to do in order finally upgrade
That hap end after the program was stopes with error 402653077 and I use the advice to start the connection to iTunes while pressing on the start botton .
-
I'm trying to set up firefox sync on my macbook. When setting up my account, the "prove you're not a robot" page loads with a blank captcha, so of course I can't go on. At first I thought that adblock might be interfering, so I disabled it. No dice.
-
Import .mod file - poor quality in Elements
I just installed Elements. My Panasonic SDR-H80 camcorder creates .MOD files. when i open these .MOD files in Elements the playback quality is very poor compared to playing the files Windows Media Player. the quality is equally poor when i export the
-
Able to receive calls or browse data - on "No Service"!!
I have experienced some peculiar observation & would appreciate to hear if someone else has faced similar situation (and knows the reason for this issue) 1. iPhone 5S used on VF Delhi: Phone shows "No Service" but I could receive a phone call 2. iPho