Obtaining the last n values - possible to do non-procedurally?

I'm not sure if the following is possible without using PL/SQL instead garden variety SQL.
I'm looking for the last 4 or less (or some other arbitrary number) of LABVALUES for a PERSON. This is regardless of the time period in which they occur. Eventually I might AVERAGE these or do some other summary operation. The LABDATE might be a standard Oracle date, or might be a number (number of microseconds from 1/1/1960); however, these date forms would NOT be mixed in the same query.
The base query is:
SELECT DISTINCT
PERSON.NAME,
LAB.VALUE,
LAB.DATE
FROM
PERSON,
LAB
WHERE
PERSON.ID = LAB.ID
Does this have to be done procedurally or can it just be done as a SELECT statement
Thanks,
Will

Eric, Everything looks good assuming that we can go by Person.Id for order by clause, just couple of things,
1. William's spec does not have Person_Id column.
2. William also says he needs last 'n' rows for each Person, however he did not mention whether he passes 'Name' of the Person as a parameter. Following are couple of SQLs, one with Person Name as parameter and another one with out the parameter which brings all the persons with 'n' number of rows for each person.
/* Please note that I have not used analytical fn here, only because we can get away with out having to use it. Just an alternative ONLY. */
SELECT Name, Value, Date
FROM
   (Select P.Id
          , P.Name
          , L.Value
          , L.Date
    From PERSON P, LAB L
    Where P.Id = L.Id
    Group By P.Id, P.Name, L.Value, L.Date
    Order By P.Id Desc)
WHERE Name = :PersonName -- use Name of Person here as parameter
AND ROWNUM <= :n; -- number of rows required for the given Person
/* Please note that I have not used the group by here as PARTITION
does that for us*/
SELECT Name, Value, Date
FROM
   (Select  P.Name
          , L.Value
          , L.Date
          , row_number() over(PARTITION BY P.Name ORDER BY L.Id desc) Rn
   From PERSON P, LAB L
   Where P.Id = L.Id)
WHERE Rn <= :n; -- number of rows for each personThx,
SriDHAR

Similar Messages

  • Is it possible to maintain the last AO value after the DAQmx task is done on device DAQ USB-6341 ?

    Hi all,
    I use the device DAQ USB-6341 to generate the desired voltage waveform by the "finite samples" sample mode.
    I notice that the channel resets to zero volts after the DAQmx task is done and the DAQ USB-6341 does not have 
    the property AO.idleOutputBehavior to select "Maintain Existing Value".
    How can I maintain the last AO value after the DAQmx task is done on device DAQ USB-6341 ?
    Thanks.
    Godel

    How many computers do you have in the network?
    What is the IP address and default gateway number on your computer?
    In order to connect to the wireless network, WVC80N should get valid IP address from the access point.
    If the access point has capacity to assign a private IP address to the computers and devices then I think you can use WVC80N.

  • How to get the last "reported" value in a cross tab

    Dear all,
    based on a BeX Query Universe I have built a report with a cross tab in webI.
    Maybe you can help me with the following issue. There I have a problem: I want to get in the Total-column always the last reported value, e.g. for Number Total Customers "66", not for Mar or Apr, because they are null.
    Calendar Month                      |Jan| |Feb| |Mar| |Apr| |Total|
    Number Total Customers         |54|    |66|  | |      | |       66
    Number Active Customers      |57|    |71|  | |      | |       71
    I hope that you understand my problem and could help as soon as possible.
    Thanks a lot in advance for your support.
    Best regards.
    Edited by: HOLWUE on Feb 8, 2010 1:40 PM

    Hi,
    If this question is related to Webi then post in the below forum.
    http://scn.sap.com/community/businessobjects-web-intelligence/content?filterID=content~objecttype~objecttype[thread]
    Regards,
    Sujit.

  • Using an index to access the last n values of a certain combination

    I have a large table of the following form:
    Name Typ
    ID NUMBER
    M_DATE DATE
    M_LOC NUMBER
    M_ARTICLE VARCHAR2(30)
    M_TYPE VARCHAR2(30)
    M_MEASURE VARCHAR2(50)
    VALUE NUMBER
    In this table there is only a small number of different locations, articles, types and maesures but a large number of m_date values.
    I want to access the last 10 values for a certain combination of locations, articles, types and measures.
    I have an index
    create index i_res_2 on results (m_loc, m_article, m_type, m_measure, m_date);
    In my opinion it should be possible to access the last 10 values directly by this index, but
    when I do my select in the following way it seems to me that first all datasets with the same
    locations, articles, types and maesures are selected, then sorted by date and then the last 10
    dates are selected. As those datasets are spread over the whole table it needs a higher number
    of physical reads. Is there a way to have an index on m_date and an other form of this query
    that selects the requested values with less phyical reads? (Database version is 10.2)
    SQL> select
    2 id,
    3 m_date,
    4 value
    5 from (
    6 select
    7 id,
    8 m_date,
    9 value,
    10 rank() over (order by m_date desc) r
    11 from results
    12 where m_loc = '14001'
    13 and m_article = '11211-00-00'
    14 and m_type = '0002'
    15 and m_measure = '032')
    16 where r <= 10
    17 order by m_date asc;
    ID M_DATE Value
    3931958 05.03.10 69.00
    3931960 05.03.10 69.00
    3905712 10.03.10 68.60
    3999535 10.03.10 69.70
    3985125 11.03.10 69.40
    3957851 11.03.10 69.60
    3949799 21.03.10 68.70
    4017369 21.03.10 69.00
    3951981 22.03.10 68.80
    3983554 22.03.10 68.80
    Abgelaufen: 00:00:03.00
    | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
    | 0 | SELECT STATEMENT | | 1 | 48 | 6 (34)| 00:00:01 |
    | 1 | SORT ORDER BY | | 1 | 48 | 6 (34)| 00:00:01 |
    |* 2 | VIEW | | 1 | 48 | 5 (20)| 00:00:01 |
    |* 3 | WINDOW SORT PUSHED RANK | | 1 | 39 | 5 (20)| 00:00:01 |
    | 4 | TABLE ACCESS BY INDEX ROWID| RESULTS | 1 | 39 | 4 (0)| 00:00:01 |
    |* 5 | INDEX RANGE SCAN | I_RES_2 | 1 | | 3 (0)| 00:00:01 |
    Predicate Information (identified by operation id):
    2 - filter("R"<=10)
    3 - filter(RANK() OVER ( ORDER BY INTERNAL_FUNCTION("M_DATE") DESC )<=10)
    5 - access("M_LOC"=14001 AND "M_ARTICLE"='11211-00-00' AND "M_TYPE"='0002' AND
    "M_MEASURE"='032')
    Statistiken
    1 recursive calls
    0 db block gets
    547 consistent gets
    474 physical reads
    0 redo size
    766 bytes sent via SQL*Net to client
    380 bytes received via SQL*Net from client
    2 SQL*Net roundtrips to/from client
    2 sorts (memory)
    0 sorts (disk)
    10 rows processed

    I found a solution, which is much better in terms of physical reads and consistent gets even if the optimizer calculates higher costs. But I still think, that there should be an easier way to do this:
    SQL> select
    2 id, m_date, value from results
    3 where
    4 (m_loc, m_article, m_type, m_measure, m_date)
    5 in
    6 (select m_loc, m_article, m_type, m_measure, m_date
    7 from (
    8 select /*+ first_rows(10) */ m_loc, m_article, m_type, m_measure, m_date
    9 from results
    10 where m_loc = '14001'
    11 and m_article = '11370-00-00'
    12 and m_type = '0002'
    13 and m_measure = '032'
    14 order by m_loc,m_article,m_type,m_measure,m_date desc
    15 ) where rownum < 10);
    ID M_DATE VALUE
    2495995 01.02.09 70,4
    2457657 19.01.09 66,9
    2556262 13.02.09 67,4
    2496232 01.02.09 67,6
    2465051 20.01.09 67
    2454994 19.01.09 67,4
    2545951 13.02.09 67,4
    2502216 01.02.09 67,5
    2497533 01.02.09 67,2
    9 Zeilen ausgewählt.
    Ausführungsplan
    Plan hash value: 3924867000
    | Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
    | 0 | SELECT STATEMENT | | 1 | 122 | 8 (25)| 00:00:01 |
    | 1 | TABLE ACCESS BY INDEX ROWID| RESULTS | 1 | 39 | 3 (0)| 00:00:01 |
    | 2 | NESTED LOOPS | | 1 | 122 | 8 (25)| 00:00:01 |
    | 3 | VIEW | VW_NSO_1 | 1 | 83 | 4 (25)| 00:00:01 |
    | 4 | HASH UNIQUE | | 1 | 31 | | |
    |* 5 | COUNT STOPKEY | | | | | |
    | 6 | VIEW | | 1 | 31 | 4 (25)| 00:00:01 |
    |* 7 | SORT ORDER BY STOPKEY| | 1 | 31 | 4 (25)| 00:00:01 |
    |* 8 | INDEX RANGE SCAN | I_RES_2 | 1 | 31 | 3 (0)| 00:00:01 |
    |* 9 | INDEX RANGE SCAN | I_RES_2 | 1 | | 2 (0)| 00:00:01 |
    Predicate Information (identified by operation id):
    5 - filter(ROWNUM<10)
    7 - filter(ROWNUM<10)
    8 - access("M_LOC"=14001 AND "M_ARTICLE"='11370-00-00' AND "M_TYPE"='0002'
    AND "M_MEASURE"='032')
    9 - access("M_LOC"="$nso_col_1" AND "M_ARTICLE"="$nso_col_2" AND
    "M_TYPE"="$nso_col_3" AND "M_MEASURE"="$nso_col_4" AND "M_DATE"="$nso_col_5")
    Statistiken
    1 recursive calls
    0 db block gets
    36 consistent gets
    14 physical reads
    0 redo size
    748 bytes sent via SQL*Net to client
    381 bytes received via SQL*Net from client
    2 SQL*Net roundtrips to/from client
    1 sorts (memory)
    0 sorts (disk)
    9 rows processed

  • Formula to calculate the last active value in a row?

    When using a simpe table, what formula will display the last active value in the row? Some of the cells are blank. I want a column to show the most recent value, which in this case will be the rightmost, non-blank cell.

    B2 through J2 are your data
    B3 =IF(B2="","",COLUMN())
    Fill that to the rest of row 3
    A2 =INDEX(B2:J2,MATCH(MAX(B3:J3),B3:J3,1))
    If there are no values in the row, the answer will be an error triangle. You can put the IFERROR function around the formula and give it an answer when there is an error.

  • While P.O creation material automatic capture the last Purchase value

    Dear all
    Please guide me for the following :
    I need a scenario like while creation of new P.O if material already purchase then system automatic capture the last purchase value as per the given material....
    Thanks & Regards,
    Neha

    Prompt the last Purchase Order Price for the Material
    If you want the system to take the price from the last Purchase Order, then do not maintain the conditions in the info record because it has precedence over the last PO. This means that the netprice field in the info record should be left blank.
    In the case when you have already maintained the conditions in the info record (or netprice), try to delete them or make them invalid by changing the validity date.
    Even though no price was maintained, the info record will still keep track of the Order price history.
    To check the Order price history, go into the material info record and click Environment -> Order price history
    Edited by: Sachein on Oct 20, 2011 6:54 AM

  • New measurement starts with the last measurement value before program stop

    New measurement starts with the last measurement value before program stop. This is very embarrassing because we finish at a high measured value and start with small value, which can not be seen on the autoscale screen due to the memorized high first value. We use NI PCI-6220 DAQ board and software written in LabView 7.1 using Express VIs and running in the LabView environment. The program (attached) every half second measures 3 AI channels, filters and scales the data, visualizes, averages and saves it in a file. Please advise me how can I avoid this problem. Please note that I am not very experienced in LabView.
    Attachments:
    OLED_Monitor2.vi ‏785 KB

    I can not run your vi, but if you initialize your controls you should be ok.
    Look at the picture
    Attachments:
    Clipboard-2.jpg ‏16 KB

  • Making an average of the last 100 values?

    Hi everyone,
    I am acquiring a signal every 1000mS with Labview 7.1. I need to make an average of the last 100 values every 1000mS. I know how to do this with shift registers but it would be a bit long to do it this way...There must be a better way
    Would anybody have an example of an array that takes into consideration the new value that has just been acquired, so that I can make an average of the array?
    Thank you very much for your help,
    Toto

    Hi Toto,
    Here is the first thing I thought of.  Initialize an array of 1000 elements, and replace an element each iteration of the loop.  Use the Quotient and Remainder function along with the iteration count of the loop to replace the oldest element in the array.  Add the array and divide by 1000 in each iteration to get the current average.  I've attached the VI (saved in LabVIEW 7.1), along with a screenshot for anyone who wants to see it but doesn't have LabVIEW 7.1 or later.
    Good luck,
    -D
    Message Edited by Darren on 02-05-2006 07:40 PM
    Darren Nattinger, CLA
    LabVIEW Artisan and Nugget Penman
    Attachments:
    Running_average.vi ‏11 KB
    Running_average.jpg ‏32 KB

  • How to find the last string value in dynamic object?

    Hi All,
    I am trying to find the last string value in dyanamic objects,Any one have solution for this.
    Ex:
    my data :12347-ebjdone-525-ecgfjf-25236-defdafgdeg
    And i want to show the output is :defdafgdeg
    Any ideas:
    Thanks
    Srini

    For oracle try using oracle function.
    e.g.
    SELECT  reverse(substr(reverse('12347-ebjdone-525-ecgfjf-25236-defdafgdeg'),1,instr(reverse('12347-ebjdone-525-ecgfjf-25236-defdafgdeg'),'-','1'))) from dual
    Object definition might look like:
    reverse(substr(reverse({ObjectName}),1,instr(reverse({ObjectsName}),'-','1')))
    Regards,
    Kuldeep
    Edited by: Kuldeep Chitrakar on Feb 12, 2010 8:12 AM

  • Getting the last selected value of the combo  - very very urgent...........

    On my panel i have a jcombobox in upper half(which displays say the address type.) and few text fields on the lower half(which displays the add details). when the user selects a value in the combo box the text fields are populated based on the selection. The user can change the values displayed in the text fields. So before setting the new values in the text fields the last displayed values need to be saved.
    I have an ActionListener for my combo which gives me the currently selected value by calling getSelecetdItem()
    How can i get the last selcted value of the combobox without using any variable of my own.

    To update the last displayed details(the address details say) i need to check what type of address the combo had previousely.....
    when i display the panel first time the home address is set by default and i have a variable to hold this value..as the user changes the selection i update the variable...I don't want to use this variable..instead if i can get the last selcted value from the event that would be great...
    thanks

  • DAQmx :: Continous reading of the last 10 values of a continous data-aquisition at a fixed point

    Hello,
    I am using a NI-PCI 6259 for generating and aquiring data.
    The other configuration is:
    - LabVIEW 7.1
    - DAQ 7.3
    I need to measure three anaolg channels, and generate an analog output. Additonally I need to read some values over the RS232. The whole activity should repeat every 30ms. To get this, I am using a timed loop at the moment. I generate a clock with counter 0 and read it with counter 1 for the timed loop. Inside the timed loop I start/stop a finite aquisition for the three analog channles and the RS232 reading. The fastest aquiring supported by the RS232 is an iteration every 30ms. Without the analog aquisition the system runs without any dropped loops. By adding the analog aquisition the timed loop dropps 2-5 loops every 1000 iterations.
    I think the problem is the repeated start/stop of the aquisition. Maybe a solution is a continous aquistion by reading out the last 10 values of the buffer every start of the timed loop. But all my tests failed, because of the FIFO design of the buffer. When I start a continous aquisition outside the timed loop and read 10 values inside, I always have the first 10 values of the buffer, not the last ones.
    Does anyone has an idea, how to read loop the last 10 values of a continous aquisition inside a timed ?
    Thanks a lot for your help.
    Best regards,
    Michael
    Attached you will find the actual state of the testprogram. Because the aqusition should be in the last quarter of the generated output value, the values of the loaded waveform (output) are four times resampeled and the clock has four times the speed of the aquisition, which generates a timing event every four clocks.
    Attachments:
    Datenerfassung.zip ‏3047 KB

    To address your main issue you might try using the DAQmx control Task VI right after you call the Timing VI (outside the timed loop) and set its value to commit. This allows for more efficient starts and stops. Some other things I saw in your program which might make it a little easier and use less HW. First, you shouldn't need to create a CO task on ctr0 and then feed it to the timed loop using ctr1. Why don't you just use the Frequency personality of the timed loop? That way you will only use one counter. If you want to use the 2 counter method then to avoid using external wiring as the source for counter 1 in the timing source VI use CTR0/internaloutput. This is an advanced property for the terminal I/O control so you will need to right click on the constant and select I/O filtering. Finally, when you specify your create channel for the AI Voltage why are you doing this in a for loop. You can just have one channel string Dev1/Ai16:18 and that will create all 3 channels with the same information. Hope this helps
    StuartG

  • Get every 10 sec a int.value and need to take the sum of the last 18 values

    Hi,
    i get every 10 sec a int.value and need to take the sum of the last 18 values (3 minutes).
    the programm should work permanently.
    I tried with a 1d-array but didn´t get a result. Could anyone help me?
    Best regards
    kasche

    Use the example in the posted link, then add another shift register for your sum of all elements. Dont add all the elements every time, add them to a running total when they come in. You will need to evaluate how big this number is going to get, however, and decide if you should use an I64 for your running total of all elements. Even this will overflow eventually. 
    So your code would look like that posted by GerdW in the above link, then add a shift register with a starting value of 0. Then add your input value to the value comming from this shift register and send the output to the shift register's output terminal. This creates your running total of all values. The code by GerdW provides your "last 18 elements" total in a very efficient manner, just change the 15 to an 18.
    I have attached a sample bit of code to make it clear. I saved it in LV 8.0 so more people can open it.
    CyberTazer
    Software Systems Engineer
    Attachments:
    Running 18 total.vi ‏11 KB

  • Is it possible to obtain the last IP address from which the device has displayed a message from Find My iPhone ?

    Hello, a couple of months ago my macbook got stolen so when yesterday i recieved an email my message was displayed on it i was wondering if there is a possibility to get the last IP address used on that machine .So if there is such possibility could someone direct me how to do it ?

    I'm sorry, but I'm still not clear on the problem. Do you mean that you have both "Smith Family" and "John Smith" tied to the same email address? If so, again, that "friendly" name isn't sent to the recipient, so the person receiving the email will get exactly the same thing regardless of whether you select the "Smith Family" entry or the "John Smith" entry. The "friendly" name doesn't go along as part of the email.
    It would only matter on your end when looking at your Sent email, and if that's an issue, it should be no problem to select which one you want; just start entering in the email address/name and wait a second; you'll get a dropdown with all the addresses that meet that criteria and you can just pick the correct one.
    Forgive me if I'm still not understanding what the difficult is or what you're trying to accomplish.

  • How can I obtain the last purchase price

    Hi,
    there is a method to obtain the list of last purchase price??
    Thx
    Best Regards

    hi,
    I've tried the PriceList Obj and GetItemPrice but nothing to do....
    I would like obtaining the second Price List row in the Price Lists Form (Inventory\Price Lists\Price Lists).
    If I enable Debug Information I can obtain ListNum but the first two rows have the ListNum set to -2 and -1
    With the PriceList.GetByKey method if I set the parameter with -1 or -2 doesn't work... There is a way to obtain this PriceList?
    Thanks a lot
    Best Regards

  • How to Finding the Last Row Value in Datagrid?

    Hi Everyone,
    Thanks in Advance.
    I need your help, to find the last row data in Datagrid.
    Actually i am using Datagrid to display my Data in flex. In my data i stored the gender value of employees. So if the last row in my datagrid is "female" i need to be highlight that particular row. So please help me to solve this issue.
    Thanks,
    Charles. J

    datagrid.selectedIndex = datagrid.dataprovider.length;
    ^ something like this will select the last row in the datagrid.
    if you need to check it's value, you might need to cast an object here, based on the index value, and check it's gender value.
    datagrid.selectedIndex = datagrid.dataprovider.length;
    if (datagrid.selectedItem["gender"] == "female") {
    //handle here

Maybe you are looking for

  • Low Disk Space

    Dear Experts, One of mine DEV server is running out of space in the SAP drive ( D: ) ECC 6.0 and SQL SERVER 2005 Total size Allocated  : 270 GB SIDData1 : 24 GB SIDData2 : 23 GB SIDData3 : 24 GB SIDLOG1 : 170 GB Now the message showing in Low Disk Sp

  • Export Numbers Document to Editable .pdf

    Forgive me if the answer is obvious, but I am a newbie to numbers. I have created a sales reporting form in numbers that I would like to host online or send out as a .pdf document. The problem is, I would like my clients to have the ability of enteri

  • Attachments in module pool

    HI iam working on a Module pool, is it possible to have an attachments on the screen where we can upload or download any file.please help me in this if it is possible thanks in advance. Regards

  • Ipod classic 5th gen 80 GB, cannot add more files past 12.9 GB

    I have an ipod classic 5th gen and I can't add any songs or videos past 12.9 GB even though its an 80 GB capacity.  I keep getting the error messages: "Attempting to copy to the disk "ipod" failed.  the disk could not be read from or written to."  "T

  • My iPod touch home button doesn't work what can i do about it and I got it about a year ago and wat can apple do to fix it

    I got my iPod about a year ago and now the home buttOn won't work so every time I try to go back to the home Screen I have to power off and turn back on and what can I do to fix it and what can apple do to fix it and how much will it cost also the ho