10 most recent receipts

Have a table that contains data for items received into inventory.  Each record has an ItemNmbr,DateRecd, UnitCost etc.
Need to pull the 10 (or 12 etc.) most recent receipts for each item number.  The dates will vary widely between the part numbers and even for a single part number there could be large gaps.
Results should look like this
ItemNmbr                  
     UnitCost             
DateRecd
70000
24.2784
February 1, 2003
70000
30.77
February 25, 2003
70000
30.85
February 25, 2003
70000
30.85
March 27, 2003
70000
30.81
March 27, 2003
70000
30.85
March 27, 2003
70000
30.77
March 27, 2003
70000
30.85
May 13, 2003
70000
30.75
May 13, 2003
70000
30.85
May 27, 2003
70053
367.98
July 16, 2008
70053
368.69
July 16, 2008
70053
368.67
July 16, 2008
70053
369.41
August 6, 2008
70053
369.94
August 6, 2008
70053
368.71
August 19, 2008
70053
368.76
August 19, 2008
70053
307.56
November 17, 2008
70053
307.37
November 17, 2008
70053
312.42
February 2, 2009
I can do this for one ItemNmbr at a time using TOP but I need to do it for all existing items in one go.  The objective being to return the most recent 10 receipts for each part number.
I've been playing around with RANK but can't quite get to where I need to be.
Thanks

Is the field having date values a varchar field in your case? Seeing the data I feel you've declared it as varchar. Then I would say you should make it datetime so as to get date sequence correctly
Once you do it you can make a logic like this
SELECT *
FROM TableName t
WHERE NOT EXISTS (SELECT 1
FROM TableName
WHERE ItemNmbr = t.ItemNmbr
AND DateRecd > t.DateRecd
GROUP BY ItemNmbr
HAVING COUNT(*) >= 10
Please Mark This As Answer if it solved your issue
Please Vote This As Helpful if it helps to solve your issue
Visakh
My Wiki User Page
My MSDN Page
My Personal Blog
My Facebook Page

Similar Messages

  • Ten most recent

    I'm trying to find the ten (or any number) most recent receipts for items.  TOP works fine for one item at a time but I need all items with their ten most recent receipt dates.
    Sample Data
    ItemNmbr
    DateRecd
    UnitCost
    70000
    2/1/2003
    24.2784
    70000
    2/25/2003
    30.85
    70000
    2/25/2003
    30.77
    70000
    3/27/2003
    30.85
    70000
    3/27/2003
    30.81
    70000
    3/27/2003
    30.85
    70000
    3/27/2003
    30.77
    70000
    5/13/2003
    30.85
    70000
    5/13/2003
    30.75
    70000
    5/27/2003
    30.85
    70000
    5/27/2003
    30.83
    70000
    7/14/2003
    30.84
    70000
    7/14/2003
    30.85
    70000
    8/13/2003
    30.85
    70000
    8/13/2003
    30.82
    70000
    10/31/2003
    30.85
    70000
    10/31/2003
    30.81
    70000
    11/14/2003
    30.85
    70035
    1/2/2007
    289.21
    70035
    1/2/2007
    289.22
    70035
    1/23/2007
    291.52
    70035
    1/23/2007
    291.54
    70035
    5/16/2007
    277.66
    70035
    5/16/2007
    277.66
    70035
    7/5/2007
    267.68
    70035
    7/5/2007
    267.69
    70035
    8/30/2007
    264.45
    70035
    9/11/2007
    264.45
    70035
    9/11/2007
    264.44
    70035
    10/29/2007
    240.8
    70035
    10/29/2007
    240.8
    70035
    2/27/2008
    249.67
    70053
    3/15/2006
    323.44
    70053
    3/15/2006
    323.53
    70053
    3/22/2006
    323.59
    70053
    3/22/2006
    323.44
    70053
    7/8/2006
    312.9
    70053
    7/8/2006
    312.78
    70053
    12/7/2006
    332.3
    70053
    12/7/2006
    332.18
    70053
    1/10/2007
    342.71
    70053
    1/10/2007
    342.77
    70053
    2/16/2007
    348.54
    70053
    2/16/2007
    348.43
    70053
    4/30/2007
    355.29
    70053
    4/30/2007
    355.26
    70053
    5/17/2007
    355.46
    70053
    5/17/2007
    355.52
    70053
    6/7/2007
    341.28
    70053
    6/7/2007
    341.19
    70053
    6/11/2007
    345.54
    70053
    6/11/2007
    345.28
    70053
    7/4/2007
    341.27
    70053
    7/4/2007
    341.56
    70053
    10/11/2007
    329.51
    I've been trying to use RANK to do this but can't quite get it. 
    Thanks

    Select ItemNmbr, DateRecd,UnitCost from
    (select *, row_number() Over(Partition by ItemNmbr Order by DateRecd desc) rn from test) t
    where rn<=10

  • Query most recent date

    I do not know how to accomplish querying a table on an index for a single value representing the record with the most recent datetime.
    The table looks like:
    CREATE TABLE PartReceipt(
    Id [int] IDENTITY(1,1) NOT NULL,
    PartId [int] NOT NULL,
    UnitCost decimal(8,2) NOT NULL,
    DateInserted [datetime] NOT NULL)
    PartId is a foreign key into the Part table.
    I tried:
    select unitcost from partreceipt  where partid=583  having max(dateinserted)
    but received this error:
    An expression of non-boolean type specified in a context where a condition is expected, near ')'.

    >> The table looks like: <<
    I hope not! 
    A table models a set, so its name has to be plural or a collective name. Do you really have only one receipt? 
    A table has to have a key by definition. 
    We do not use IDENTITY in RDBMS. Why would the proprietary table property that counts physical insertion attempts be part of a valid data model? 
    Identifiers are never numeric. What math do you do with it? None. How do you get validation and verification with a numeric? 
    Why did you use an old Sybase DATETIME and not a DATE or even a DATETIME2(0)?
    Why did you fail to post the DRI in your narrative? 
    Insertion is a physical event and has nothing to do with a logical data model.  Your “DateInserted” violates both ISO-11179 rules and logic. 
    Here is one guess, but in another design we could have (receipt_date, part_id) as a key. This would mean that you get a daily shipment.
    CREATE TABLE Part_Receipts
    (receipt_nbr CHAR(10) NOT NULL PRIMARY KEY,
     receipt_date DATE DEFAULT CURRENT_TIMESTAMP NOT NULL,
     part_id CHAR(10) NOT NULL
       REFERENCES Parts(part_id),
     unit_cost DECIMAL(8,2) NOT NULL
      CHECK (unit_cost >= 0.00),
    SELECT unit_cost, MAX(receipt_date)AS max_receipt_date
      FROM Part_Receipts
     WHERE part_id = '0000000583';
    Your HAVING clause made no sense; there was no predicate in it. 
    --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

  • Collapsed view: how to see "most recent unread message" instead of "first unread message" in a thread in Mail

    Hey all,
    I use the inbuilt Mail-software on my Macbook with Lion 10.7 and I constantly overlook new messages in a thread, as I organize by conversation and usually have all my threads in 'collapsed view'. For some reason Mail seems to prefer showing the 'first', i.e. the earliest unread message as the one message that I see in collapsed. If there are newer messages already in the thread, I don't see them, unless I un-collapse the thread. I find this really unintuitive and annoying, my inbox seems far more static than it really is.
    Is there any way to change this and have Mail show me the most recent unread message as the message I see in collapsed view?
    I appreciate any advice.
    Thanks, Carl
    PS: BTW, I have the same problem on my iPhone, double props to you if you can solve it for both Mail and iPhone!

    Mail
    Preferences
    Viewing
    Check put most recent message at top

  • I've updated to the most recent version of iTunes and I can no longer see any of my old music on my mac.  All the music is still available on my ipad.  How can i download the music on my mac?

    I've updated to the most recent version of iTunes and I can no longer see any of my old music on my mac.  All the music is still available on my ipad.  How can i download the music on my mac?

    First, are you positive it is really not there?  Have you looked in your iTunes folder in your Music folder, and in the media folders there? Do you see your media files?
    If your media are gone it is a lot easier to restore from a proper computer backup.  Your i-device was not designed for unique storage of your media. It is not a backup device and media transfer was planned with you maintaining a master copy of your media on a computer which is itself properly backed up against loss. Syncing is one way, computer to device, updating the device content to the content on the computer, not updating or restoring content on a computer. The exception is iTunes Store purchases which can be transferred to a computer.
    iTunes Store: Transferring purchases from your iOS device or iPod to a computer - http://support.apple.com/kb/HT1848 - only purchases from iTunes Store
    For transferring other items from an i-device to a computer you will have to use third party commercial software.  See this document by turingtest2: Recovering your iTunes library from your iPod or iOS device - https://discussions.apple.com/docs/DOC-3991

  • I just tried to install the 11.4 update (or whichever one is the most recent update as of 1/26/2014) and when it failed i tried to install manually and now whenever i try to use it, i get the following error: the application has failed to start because MS

    i just tried to install the 11.4 update (or whichever one is the most recent update as of 1/26/2014) and when it failed i tried to install manually and now whenever i try to use it, i get the following error: "The application has failed to start because MSVCR80.dll was not found. Re-installing the application may fix this problem." Right after i click ok i then get this error: "Itunes was not installed correctly. Please reinstall Itunes. Error 7 (Windows error 126)." I tried to uninstall Itunes and then reinstall the 11.03 version but that didnt work either. I want to know if i copy all of the music in my itunes folder to an external without consolidating can i still transfer all my itunes music from my current windows xp pc to a brand new one and have my current itunes library in my new pc? Basically i just want to know 3 things: What exactly does consolidating the itunes library do? Can i copy, paste, and transfer my itunes library to an external and from there to a new pc? Will i be able to transfer my itunes library without consolidating the files?

    I have found a temporary solution, allowing the previous version of iTunes (v. 11.1.3 (x64) in my case) to be re-installed.  It will allow you to re-establish use of iTunes until the Apple software engineers fix the most recent disasterous upgrade (v. 11.1.4).  Please see and follow the procedure in the following article:http://smallbusiness.chron.com/reverting-previous-version-itunes-32590.html   The previous version works beautifully.

  • My Mac OS, 10.5.8, won't support the most recent Firefox and I need the most recent version it WILL support and how to get it, as it's not on this site.

    I need a way to get a more recent but not current version of Firefox that my computer, a PowerPC processor Mac, will support. I am running OS X version 10.5.8. This processor/computer will not run Lion and the most recent version of Firefox will not run on it either.
    On my second computer, same type, a newer version of Firefox crashes constantly while in Yahoo!

    Firefox 3.6.x is the last available from Mozilla for PPC Macs. <br />
    http://www.mozilla.com/en-US/firefox/all-older.html

  • My iPhone 4 will not sync my new voice memos from the "Voice Memos" app to my computer. This is frustrating, should not be so hard, can someone please help. I use PC with windows 7 with iPhone version 6.1.3 and iTunes most recent. Thanks.

    My iPhone 4 will not sync my new voice memos from the "Voice Memos" app to my computer. This is frustrating, should not be so hard, can someone please help. I use PC with windows 7 with iPhone version 6.1.3 and iTunes most recent. Thanks.

    In the Music tab of iTunes, do you have 'Include Voice Memos' checked?

  • Downloading images used to go automatically to the last folder used to save images, even when reopening Firefox. In just the past few days, possibly coinciding wih the most recent updates, the folder defaults to the 'Downloads' folder. This is most annoyi

    Downloading images used to go automatically to the last folder used to save images, even when reopening Firefox. In just the past few days, possibly coinciding wih the most recent updates, the folder defaults to the 'Downloads' folder. This is most annoying. What happened? Internet Explorer 8.0 did the same thing. This was one of the reasons why I started using Firefox to download all images. I went into Tools>Options and it only lets me set another folder. I dont want to set a specific folder I want it to always go to the last folder used. So what gives?
    == This happened ==
    Every time Firefox opened
    == possibly when the most recent updates were installed, a few days ago

    Thanks jscher 2000. I guess I didn't make it clear. "It restarts with all the addons activated, and resumes with the tabs that were open before closing it." IE, it's running fine now with all the extensions activated. Everything is OK now.
    So something in the Firefox code was causing the bad behavior. It's not essential that I find out what the problem was - I'm just curious. And if anybody else has this same problem, it might be nice to have it corrected at the source.

  • HT4623 I have followed the directions and my iphone is not installing the iOS 7.0.2.  My iphone is plugged into my lap top that has the most recent version of itunes.  It is a dsl connection.  Is that my problem?

    I have followed the directions and my iphone is not installing the iOS 7.0.2.  I have the most recent version of itunes on my laptop commuter.  my iphone is attached to my laptop via usb port.  The laptop uses dsl not wifi.  Is that my problem?

    Exactly what version of iTunes are you running? And DON'T say 'the latest version'... CHECK the version number. You need to be running 11.1 or higher.

  • After the most recent FF update, FF 3.6.6 will not load, citing the error message: "XULRunner / Error: Platform version '1.9.2.3' is not compatible with minVersion =1.9.2.6 / maxVersion". Tried updating XULRunner with no luck. Downloaded fresh copy and

    After the most recent FF update, FF 3.6.6 will not load, citing the error message: "XULRunner / Error: Platform version '1.9.2.3' is not compatible with minVersion>=1.9.2.6 / maxVersion". Tried updating XULRunner with no luck. Downloaded fresh copy and installed. Still no luck.
    == This happened ==
    Every time Firefox opened
    == FF updated to 3.6.6 ==
    == User Agent ==
    Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; GTB6.4; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)

    Do a clean reinstall and download a fresh Firefox copy from http://www.mozilla.com/firefox/all.html and save the file to the desktop.
    Uninstall your current Firefox version and remove the Firefox program folder before installing that copy of the Firefox installer.
    It is important to delete the Firefox program folder to remove all the files and make sure that there are no problems with files that were leftover after uninstalling.
    You can skip the step to create a new profile, that is not necessary for this issue.
    See http://kb.mozillazine.org/Standard_diagnostic_-_Firefox#Clean_reinstall

  • Since downloading the most recent version of iTunes, I have to accept the software terms and conditions every time I open iTunes

    Since installing the most recent version of iTunes, I have to accept the software terms and conditions every time I go in to iTunes.  I sign in to my account, but when I re-open iTunes, as well as having to accept the terms and conditions before continuing, I find that iTunes has logged me out of my account.  None of this has happened before.  I am using a PC with Windows 8.

    See Empty/corrupt iTunes library after upgrade/crash.
    tt2

  • Help with getting the most recent transaction

    I have a system that keeps track of part repairs. Unfortunately a part my come in for repair multiple times. I want my query to pull back the most recent time/ request id that a part came in. This is my basic query, but it pulls back multiple records for the given part/serial number combo. How can I re-write this? Thanks
    SELECT M4OWNER.STOCK_SERIAL_ID.BIN_ID AS "Bin",
    M4OWNER.RECEIVING_UNIT.CREATED_DTTM AS "Date Placed in Bin",
    M4OWNER.STOCK_SERIAL_ID.SERIAL_ID AS "Serial ID",
    M4OWNER.STOCK_SERIAL_ID.PART_ID AS "Part ID",
    M4OWNER.STOCK_SERIAL_ID.USEABLE AS "Usable Status",
    M4OWNER.RECEIVING_UNIT.REQUEST_ID AS "Request ID",
    M4OWNER.RECEIVING_UNIT.REQUEST_LINE AS "Line",
    M4OWNER.STOCK_SERIAL_ID.PLACE_ID AS "Warehouse",
    M4OWNER.STOCK_SERIAL_ID.LOCATION AS "Location"
    FROM M4OWNER.STOCK_SERIAL_ID INNER JOIN
    M4OWNER.RECEIVING_UNIT ON M4OWNER.STOCK_SERIAL_ID.SERIAL_ID = M4OWNER.RECEIVING_UNIT.SERIAL_ID AND
    M4OWNER.STOCK_SERIAL_ID.PART_ID = M4OWNER.RECEIVING_UNIT.PART_ID INNER JOIN
    M4OWNER.RECEIVING ON M4OWNER.RECEIVING_UNIT.RECEIVING_ID = M4OWNER.RECEIVING.RECEIVING_ID INNER JOIN
    M4SUPP1.PART ON M4OWNER.STOCK_SERIAL_ID.PART_ID = M4SUPP1.PART.PART_ID
    WHERE (M4OWNER.STOCK_SERIAL_ID.BIN_ID = 'LOCATION')
    AND (M4OWNER.STOCK_SERIAL_ID.SERIAL_ID = 'ISSUE360')

    I have a system that keeps track of part repairs. Unfortunately a part my come in for repair multiple times. I want my query to pull back the most recent time/ request id that a part came in. This is my basic query, but it pulls back multiple records for the given part/serial number combo. How can I re-write this? Thanks
    SELECT M4OWNER.STOCK_SERIAL_ID.BIN_ID AS "Bin",
    M4OWNER.RECEIVING_UNIT.CREATED_DTTM AS "Date Placed in Bin",
    M4OWNER.STOCK_SERIAL_ID.SERIAL_ID AS "Serial ID",
    M4OWNER.STOCK_SERIAL_ID.PART_ID AS "Part ID",
    M4OWNER.STOCK_SERIAL_ID.USEABLE AS "Usable Status",
    M4OWNER.RECEIVING_UNIT.REQUEST_ID AS "Request ID",
    M4OWNER.RECEIVING_UNIT.REQUEST_LINE AS "Line",
    M4OWNER.STOCK_SERIAL_ID.PLACE_ID AS "Warehouse",
    M4OWNER.STOCK_SERIAL_ID.LOCATION AS "Location"
    FROM M4OWNER.STOCK_SERIAL_ID INNER JOIN
    M4OWNER.RECEIVING_UNIT ON M4OWNER.STOCK_SERIAL_ID.SERIAL_ID = M4OWNER.RECEIVING_UNIT.SERIAL_ID AND
    M4OWNER.STOCK_SERIAL_ID.PART_ID = M4OWNER.RECEIVING_UNIT.PART_ID INNER JOIN
    M4OWNER.RECEIVING ON M4OWNER.RECEIVING_UNIT.RECEIVING_ID = M4OWNER.RECEIVING.RECEIVING_ID INNER JOIN
    M4SUPP1.PART ON M4OWNER.STOCK_SERIAL_ID.PART_ID = M4SUPP1.PART.PART_ID
    WHERE (M4OWNER.STOCK_SERIAL_ID.BIN_ID = 'LOCATION')
    AND (M4OWNER.STOCK_SERIAL_ID.SERIAL_ID = 'ISSUE360')

  • I just found my old ipod touch (i think 1st generation) and would like to let my toddler use it instead of my phone.  I am trying to download apps but it say I need to update to 4.3 but it won't let me update.  I have the most recent itunes. any idea why?

    I just found my old ipod touch (i think 1st generation) and would like to let my toddler use it instead of my phone.  I am trying to download apps but it say I need to update to 4.3 but it won't let me update.  I have the most recent itunes. any idea why? I saw a thread saying to purchase the newest software (that was posted a few years ago) I paid 4.95 for the software and it's still saying it can't be updated.  Am I just SOL??

    The 1G iPod can only go as high as 3.1.3. The 1G does not have an internal speaker or volume buttons on the upper left edge.
    Identifying iPod models
    To more easily find compatible apps:
    iOSSearch - search the iTunes store for compatible apps.
    Apple Club - filter apps by iOS version.

  • Why is it that when I go to the search screen to find a particular message in a conversation it comes up with the message but when I click on it it takes me to the conversation where the most recent message was sent and not the message I wanted

    Why is it that when I go to the search screen and type in a particular message I want to view, my iPhone finds it but when I click on it it only takes me to the conversation at the most recently thing texted and not the particular message I clicked on?

    Never occurred to me you can search messages that way, thanks.
    Another way to do it is to scroll from the most recent message to the top of the screen and tap load more messages, repeatedly till you reach the one you want.
      I like your method better, but with both methods it refreshes to the most recent message.

Maybe you are looking for