Finding the most current row where it the row meets all multiple conditions

Need to find the latest barcode to check if it is expired previous to today.  Currently getting barcode that has expired but that happened after the stateID changed to 2 not earlier when the stateID  is 1 and it needs to be
the current record by date.  The expired is done by a scalar function that uses the barcode that calculates an expired container. The expired container from the cte  is either 1 for expired or 0 for non expired containers
from the containerHistory table
stateID    barCode           dateEntered
1             901178-2240   2014-11-15 10:34:09.693
2             901178-2240   2014-11-19 22:07:45.0230
From the cte table
stateID    dateEntered                       barcode            containerExpired        
1            2014-11-19 22:07:45.000   901178-2240              1
From the global ##Temp table
2              2014-11-19 22:07:45.000  901178-2240 
;with cte AS(
SELECT stateID, barcode, dateEntered, containerExpired
FROM barcode.dbo.ContainerHistory
WHERE dateEntered >= DATEADD(DAY, DATEDIFF(DAY, 0, CURRENT_TIMESTAMP) -7, 0)
AND dateEntered < DATEADD(DAY, DATEDIFF(DAY, 0, CURRENT_TIMESTAMP), 0)
AND stateID = 1 AND containerExpired = 1
SELECT stateID, barcode, dateEntered, containerExpired
INTO ##Temp
FROM
(SELECT barcode, stateID, dateEntered, containerExpired, row_number() over(partition by barcode, containerExpired order by dateEntered) seq
FROM cte
WHERE dateEntered <= (select max(dateEntered) from cte where containerExpired = 1 AND stateID = 1) AND containerExpired = 1
) d
WHERE seq = 1 ORDER BY dateEntered
SELECT * FROM ##Temp ORDER BY barcode
SELECT barcode, stateID, dateEntered WHERE containerID IN (SELECT barCode AS containerID FROM ##Temp) ORDER BY containerID, dateEntered
DROP TABLE ##Temp

Youir approach is wrong. To track the history of, say, Foobars we need to see time as a continuum and model it as (begin_date, end_date) pairs that define when a foobar had a particular value. Here is the skeleton. 
CREATE TABLE Foobar_History 
(foo_id CHAR(9) NOT NULL, 
 start_date DATE NOT NULL, 
 end_date DATE, --null means current 
 CHECK (start_date <= end_date),
 foo_status INTEGER NOT NULL, 
 PRIMARY KEY (foo_id, start_date)); 
When the end_date is NULL, that state of being is still current. You use a simple query for the status on any particular date;
SELECT * 
  FROM Foobar
 WHERE @in_cal_date
     BETWEEN start_date
      AND COALESCE (end_date, CURRENT_TIMESTAMP);
There are more tricks in the DDL to prevent gaps, etc
CREATE TABLE Events
(event_id CHAR(10) NOT NULL,
 previous_event_end_date DATE NOT NULL  
 CONSTRAINT Chained_Dates  
  REFERENCES Events (event_end_date), 
 event_start_date DATE NOT NULL, 
 event_end_date DATE UNIQUE, -- null means event in progress
  PRIMARY KEY (event_id, event_start_date), 
 CONSTRAINT Event_Order_Valid 
  CHECK (event_start_date <= event_end_date), 
 CONSTRAINT Chained_Dates 
  CHECK (DATEADD(DAY, 1, previous_event_end_date) = event_start_date)
-- CHECK (previous_event_end_date + INTERVAL '01' DAYS) = event_start_date)
-- disable the Chained_Dates constraint
ALTER TABLE Events NOCHECK CONSTRAINT Chained_Dates;
GO
-- insert a starter row
INSERT INTO Events(event_id, previous_event_end_date, event_start_date, event_end_date)
VALUES ('Foo Fest', '2010-01-01', '2010-01-02', '2010-01-05');
GO
-- enable the constraint in the table
ALTER TABLE Events CHECK CONSTRAINT Chained_Dates;
GO
-- this works
INSERT INTO Events(event_id, previous_event_end_date, event_start_date, event_end_date)
VALUES ('Glob Week', '2010-01-05', '2010-01-06', '2010-01-10');
-- this fails
INSERT INTO Events(event_id, previous_event_end_date, event_start_date, event_end_date)
VALUES ('Snoob', '2010-01-09', '2010-01-11', '2010-01-15'); 
--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

Similar Messages

  • How do I alter the announcements list so that it shows the most current news event at the top?

    I have the announcent list module on my home page and it shows 25 news/announcements however you can only see a few on the home page without scrolling down. I would like it to display those events that are coming up soon (and then when the date has passed, remove them) rather than events that are six months away.
    How do I do this please?
    Regards
    Kate

    Hi Kate, depends on the setup. You mentioned scrolling so I guess your using one of those old BC templates?
    By default announcements will run in date order so the new ones are at the top. There are options in the module to set it datereverse and by alphabetical.
    {module_announcement}  
    {module_announcement,filter,id,noTemplate,effect,targetFrame,reserved,reserved,reserved,reserved,sort} 
    After adding the announcement module to a web page, you can make changes to it to slightly alter its behavior. Normally the module would look like:
    {module_announcement,filter,id,noTemplate,effect,targetFrame,reserved,reserved,reserved,reserved,sort}
    Parameters
    filter - filtering criteria for display and can be one of the following:
    i - individual item
    a - all items
    c - all items in some category
    cl - latest items in some category
    l - latest items
    r - random item
    id – system generated (do not change)
    noTemplate – if you want to force the item not to use a site-level template when displayed pass in true, otherwise leave empty
    effect – Enter ajax if you want to take advantage of the latest web technologies for a better customer experience
    targetFrame – the frame in which to open the item in
    reserved - for use in a future release
    sort - if you leave this parameter blank, by default announcements are sorted from the latest to the oldest where the latest is displayed first
    alphabetical - sorts announcements alphabetically
    datereverse - sorts announcements in the reverse order, that is from the oldest to the latest
    Examples of use
    {module_announcement,a,,,,_parent} - displays all announcements and open them in parent frame
    {module_announcement,l,5} - displays the latest five announcements.
    {module_announcement,r,,true,ajax,_blank,,,,,datereverse} - opens a random item in a new window and doesn't apply the template. It uses the Ajax effect, where the detail content opens right underneath the announcement name and all the announcements are displayed from the oldest to the latest
    {module_announcement,cl,34392,,ajax,,,,,5} - displays the five latest announcements in category 34392
    Check to see what the module has been set as

  • My IPhone stopped working right after I did the most current update, yesterday.  The screen has a picture of the usb cord, pointing towards the ITunes logo. I cannot use the phone at all.  I have tried turning it off and on.ITunes won't recognize device

    Hi there,
    Can someone please help.  My IPhoneS stopped working right after I did the latest update which was yesterday.  Now, the phone does not work at all.  When turned on, the screen shows a picture of USB cable pointing to the ITunes logo.  I have tried just about everything, and cannot get it working. It's like it is frozon.  My ITunes does not recognize my device.  Cannot dial out either. I desperately need to get it working.  Can someone please help me. 
    Thanking you in advance for your help.  :-)

    The update did not do anything.
    AT&T cannot and will not do anything, they do not provide support for iPhones, Apple does.
    The issue is not with the iPhone or iOS if you've reset network settings and tried forgetting the Wi-Fi Network, it is specific to the network.
    Check the settings on the router, try disabling the security settings first... that will clarify the issue is not with the device but rather the Network.

  • Is the most current Premiere Pro CS6 the same as the CC version?

    I just used Application Manager to update my Premiere Pro CS6, which I originally installed through my CC account (which is still active). However, a tutorial I want to use makes a distinction between Premiere CS6 and Premiere CC. The CC version does not appear as an option in my App Manager.

    Kevin which operating system are you using?  Do you meet or exceed the system requirements for Premire Pro CC listed at http://www.adobe.com/products/premiere/tech-specs.html?

  • How to highlight lines of ALV of the most current delimited records?

    Hi all,
    I used the ALV to display annual salary records of certain employees. Now the ALV will show the past annual salary as well as the most current records. I need to highlight all the current records. Anyone have any idea on this? Thanks

    You need to add a field into your output table for the color
    example
    data: begin of tb_out occurs 0,
    line_color(4) type c,
    end of tb_out.
    Then, in your build layout form
    gd_layout-info_fieldname =      'LINE_COLOR'.
    When you build your output table, you fill the field LINE_COLOR with the color you want to use (you can choose a standard color for everything, like C100, and a different color for the row you want to highlight, like C300).

  • Is Mountain Lion the most current operating system for MacBook Pro?

    I am wondering if Mountain Lion is the most current operating system for the MacBook Pro.  I am under the impression that 'Mavericks' is for the desktop Mac, am I correct?  Thank You.

    Actually, Mavericks is the current version of OS X replacing Mountain Lion. Mavericks will be replaced later this year by Yosemite. The upgrade to Mavericks is free. You can download the installer from the App Store.

  • How do I get the numbers ipad app to plot a graph of dates against values, where the most recent date is on the right of the graph, but the most recent date is at the top rather than bottom of the rows in the table?

    How do I get the numbers ipad app to plot a graph of dates against values, where the most recent date is on the right of the graph, but the most recent date is at the top rather than bottom of the rows in the table?
    Also how can it be a line graph without plotting a circle at each value?
    Thanks this is very frustrating

    Make a copy of the table and produce your Line chart or Bar chart from the copy. Sort the copy into the order you want to see in your chart.
    Alternately you could use a Scatter Chart...
    Jerry

  • Finding the Most Current Visio Stencils

    To find the most current Visio stencils for each product, please go to the individual file download links on the Visio download page:
    http://www.cisco.com/en/US/products/hw/prod_cat_visios.html
    The "All Visio Products" file has grown very large and is difficult to keep up to date.  We are considering options on how to break this file into more manageable pieces but this has not been done. 
    Regards,
    Brett Newman
    Cisco Visio Production
    Visimation Inc.
    www.shapesource.com

    To find the most current Visio stencils for each product, please go to the individual file download links on the Visio download page:
    http://www.cisco.com/en/US/products/hw/prod_cat_visios.html
    The "All Visio Products" file has grown very large and is difficult to keep up to date.  We are considering options on how to break this file into more manageable pieces but this has not been done. 
    Regards,
    Brett Newman
    Cisco Visio Production
    Visimation Inc.
    www.shapesource.com

  • Can I use Flash Player plugin version 11.2.202.228 with my Firefox ESR 10.0.7 instead of the most current Flash Player plugin?

    I am trying the Tor web browser software pack and it comes with Firefox ESR 10.0.7.
    Right now, I'm trying to install a Flash Player plugin because it doesn't have one in this Firefox ESR 10.0.7 already, or this browser just doesn't see the slightly older Flash Player 11.2.202.228 plugin that I already have already installed on this machine.
    '''Is there a way to make this Firefox ESR 10.0.7 browser see the Flash Player plugin 11.2.202.228 that I already have installed on this machine without installing the newest, possibly problematic Flash Player plugin?'''
    I already have the Flash Player 11.2.202.228 plugin installed on this machine, a PC with Windows 7 ULT, because the most current versions of the Flash Player plugin had some problems and they wouldn't allow Flash content to play at all, so I reverted back to a slightly older version of the Flash Player that was reliable, crash free, and most importantly, it works.
    But when I try to play any Flash content in this Tor-Firefox ESR 10.0.7 browser it tells me I don't have a Flash Player on this machine, or installed as a browser plugin or add-on, and it wants me to install the most current Flash Player on this machine, which I DO NOT want to do for the reasons I stated above.
    My other Firefox browser, along with Google's newest Chrome browser and a newer version of the Safari browser for Windows, plus an older Internet Explorer 8, all WORK JUST FINE with the Flash Player 11.2.202.228 that I have installed on this computer and Google Chrome and Safari didn't even require having newer Flash Player plugins installed at all... they just see the Flash Player 11.2.202.228 plugin that I already have on this machine and they play all Flash content perfectly.
    So can I just make this Tor-Firefox ESR 10.0.7 browser see the existing Flash Player 11.2.202.228 plugin that I already have on this machine?... and if so, how do I do that?
    Thanks for any help.
    Also, I'm not able to find an actual community help forum for Tor, even via their "Torproject.org" website... is there one?
    Thanks,
    numetro

    You should actually be using Flash 10.3 (11.2 is insecure).[[Flash 11.3 doesn't load video in Firefox#w_solution-2-downgrade-flash|Solution 2: Downgrade Flash]].
    Also, is there a reason you are using Firefox ESR? It's mean for organizations, and doesn't include all the security and stability fixes that Firefox latest (15.0.1) has.

  • I have uninstalled and reinstalled the most current edition of Adobe Reader, including the patch. When I open a PDF file, the screen is first grey, then viewable, but the message "Adobe Reader has stopped working" comes up. How do I remedy this?

    I have uninstalled and reinstalled the most current edition of Adobe Reader (11.9), including the patch. When I open a PDF file, the screen is first grey, then viewable, but then the message "Adobe Reader has stopped working" comes up. How do I remedy this?

    RR,
    One thing often tried first is to create a new document and File>Place the corrupted one to see how much may be rescued that way (remember to tick Paste remembers Layers in the Layer palette flyout/dropdown first, and to untick it afterwards).
    Here is a website where you can see whether it can rescue the file, and if it can, you may pay for a subscription to have it done,
    http://www.recoverytoolbox.com/buy_illustrator.html
    and another similar website,
    http://markzware.com/adobe-software/fix-illustrator-file-unknown-error-occurred-pdf2dtp-fi le-recovery/
    As far as I remember, the former is for Win and the latter for Mac.
    Here are a few pages about struggling with it yourself:
    http://daxxter.wordpress.com/2009/04/16/how-to-recover-a-corrupted-illustrator-ai-file/
    http://helpx.adobe.com/illustrator/kb/troubleshoot-damaged-illustrator-files.html
    http://kb2.adobe.com/cps/500/cpsid_50032.html
    http://kb2.adobe.com/cps/500/cpsid_50031.html
    http://helpx.adobe.com/illustrator/kb/enable-content-recovery-mode-illustrator.html
    Failing a full recovery, hopefully you have one or more earlier versions and/or bits of artwork saved before it happened.

  • Downloaded the most current version and PNG images will not show

    I'm running XP with SP3 and the most current versions of Firefox and IE8. I have the AdBlock add on. PNG images do not appear (on either browser) only blank spaces where a picture should be.
    == This happened ==
    Every time Firefox opened
    == I updated my computer to SP2 and current browsers

    Hello Scott.
    Try reading these support articles:
    http://support.mozilla.com/en-US/kb/Websites+look+wrong
    https://support.mozilla.com/en-US/kb/Images+or+animations+do+not+show
    I hope it helps.

  • I keep getting a new tab asking me to update Firefox & JAVA but I already have the most current versions. I also am getting ads on the homepage

    After updating the most current Firefox I now get Ads on the home page that I can't get rid of even with the "X" box on the window. I have to shut down Firefox and start over to get anywhere. I didn't have this problem before the update.
    I also had a change to the new tab/page feature. I used to click for a new tab and a series of boxes would show where I've been so I can perhaps select one. That feature has changed and I now get a tab/page that says it is owned by AVG. Where is my old page

    If it opens in a new tab it is likely fake.
    Can you post a link of it, you may also want to check for malware.
    You can try these free programs to scan for malware, which work with your existing antivirus software:
    * [http://www.microsoft.com/security/scanner/default.aspx Microsoft Safety Scanner]
    * [http://www.malwarebytes.org/products/malwarebytes_free/ MalwareBytes' Anti-Malware]
    * [http://support.kaspersky.com/faq/?qid=208283363 TDSSKiller - AntiRootkit Utility]
    * [http://www.surfright.nl/en/hitmanpro/ Hitman Pro]
    * [http://www.eset.com/us/online-scanner/ ESET Online Scanner]
    [http://windows.microsoft.com/MSE Microsoft Security Essentials] is a good permanent antivirus for Windows 7/Vista/XP if you don't already have one.
    Further information can be found in the [[Troubleshoot Firefox issues caused by malware]] article.
    Did this fix your problems? Please report back to us!

  • Error message that Flash Play 11.5 isn't the most current version

    When trying to install 11.5 flash player (I use Windows7 and Firefox), I keep getting an adobe installer message that the version of the flash player is not the most current, and I'm routed back to the flash player download page where I just downloaded flash player.  Any suggestions?  Thanks!

    Try using the offline installer from http://helpx.adobe.com/flash-player/kb/installation-problems-flash-player-windows.html#mai n-pars_header

  • What's the most current version that runs on SP1?

    I need to find the most current version of QuickTime that will run on SP1. It is a work laptop and our IS department is reluctant to install SP2 on our systems, but I need to have QuickTime for an upcoming presentation that I am supporting. So any suggestions?

    iPhoto 11, v9.4.3.
    If you don't have iPhoto 11 already then you'll need to purchase an iLife 11 install disk, install the app and then update it.
    You may be able to purchase an iLife 11 disk on ebay or amazon.

  • My mac os x is currently 10.5.8. What is the cost to upgrade to the most current?

    My macbook pro os x is currently running 10.5.8. What is the cost to upgrade to the most current?

    Assuming you have the upgrade system requirements you would first purchase a Snow Leopard DVD and upgrade to Snow Leopard. Then you update Snow Leopard to 10.6.8 to gain access to the App Store where you would then purchase and download Lion. Costs are $29.00 for Snow Leopard and $30.00 for Lion.
    Lion System Requirements
    Mac computer with an Intel Core 2 Duo, Core i3, Core i5, Core i7, or Xeon processor
    2GB of memory
    OS X v10.6.6 or later (v10.6.8 recommended)
    7GB of available space
    Some features require an Apple ID; terms apply.
    See the following as well:
    How to Install OS X Updates Successfully
    A. Repair the Hard Drive and Permissions:
    Boot from your current OS X Installer disc. After the installer loads select your language and click on the Continue button. Then select Disk Utility from the Utilities menu. After DU loads select your hard drive entry (mfgr.'s ID and drive size) from the the left side list.  In the DU status area you will see an entry for the S.M.A.R.T. status of the hard drive.  If it does not say "Verified" then the hard drive is failing or failed. (SMART status is not reported on external Firewire or USB drives.) If the drive is "Verified" then select your OS X volume from the list on the left (sub-entry below the drive entry,) click on the First Aid tab, then click on the Repair Disk button. If DU reports any errors that have been fixed, then re-run Repair Disk until no errors are reported. If no errors are reported click on the Repair Permissions button. Wait until the operation completes, then quit DU and return to the installer. Now restart normally. 
    If DU reports errors it cannot fix, then you will need Disk Warrior (4.0 for Tiger) and/or TechTool Pro (4.5.2 for Tiger) to repair the drive. If you don't have either of them or if neither of them can fix the drive, then you will need to reformat the drive and reinstall OS X.
    B. Make a Bootable Backup Using Restore Option of Disk Utility:
    Open Disk Utility from the Utilities folder.
    Select the destination volume from the left side list.
    Click on the Restore tab in the DU main window.
    Check the box labeled Erase destination.
    Select the destination volume from the left side list and drag it to the Destination entry field.
    Select the source volume from the left side list and drag it to the Source entry field.
    Double-check you got it right, then click on the Restore button.
    Destination means the external backup drive. Source means the internal startup drive.
    C. Important: Please read before installing:
    If you have a FireWire hard drive connected, disconnect it before installing the update unless you will boot from this drive and install the update on it. Reconnect it and turn it back on after installation is complete and you've restarted.
    You may experience unexpected results if you have installed third-party system software modifications, or if you have modified the operating system through other means. (This does not apply to normal application software installation.)
    The installation process should not be interrupted. If a power outage or other interruption occurs during installation, use the standalone installer (see below) from Apple Downloads to update.  While the installation is in progress do not use the computer.
    D. To upgrade:
    Purchase the Snow Leopard Retail DVD.
    Boot From The OS X Installer Disc:
    Insert OS X Installer Disc into the optical drive.
    Restart the computer.
    Immediately after the chime press and hold down the "C" key.
    Release the key when the spinning gear below the dark gray Apple logo appears.
    Wait for installer to finish loading.
    E. If updating:
    Download and install update(s) 
    Use Software Update, or
    Download standalone updater(s).
    Once you then purchase and download Lion from the App Store consider this before you actually install Lion:
    Make Your Own Lion Installer
    1. After downloading Lion you must first save the Install Mac OS X Lion application. After Lion downloads DO NOT click on the Install button. Go to your Applications folder and make a copy of the Lion installer. Move the copy into your Downloads folder. Now you can click on the Install button. You must do this because the installer deletes itself automatically when it finishes installing Lion.
    2. Get a USB flash drive that is at least 8 GBs. Prep this flash drive as follows:
    Open Disk Utility in your Utilities folder.
    After DU loads select your hard drive (this is the entry with the mfgr.'s ID and size) from the left side list. Note the SMART status of the drive in DU's status area.  If it does not say "Verified" then the drive is failing or has failed and will need replacing.  SMART info will not be reported  on external drives. Otherwise, click on the Partition tab in the DU main window.
    Under the Volume Scheme heading set the number of partitions from the drop down menu to one. Set the format type to Mac OS Extended (Journaled.) Click on the Options button, set the partition scheme to GUID then click on the OK button. Click on the Partition button and wait until the process has completed.
    Select the volume you just created (this is the sub-entry under the drive entry) from the left side list. Click on the Erase tab in the DU main window.
    Set the format type to Mac OS Extended (Journaled.) Click on the Options button, check the button for Zero Data and click on OK to return to the Erase window.
    Click on the Erase button. The format process can take up to several hours depending upon the drive size.
    3. Locate the saved Lion installer in your Downloads folder. CTRL- or RIGHT-click on the installer and select Show Package Contents from the contextual menu. Double-click on the Contents folder to open it. Double-click on the SharedSupport folder. In this folder you will see a disc image named InstallESD.dmg.
    4. Plug in your freshly prepared USB flash drive. You are going to clone the InstallESD.dmg disc image to the flash drive as follows:
    Open Disk Utility.
    Select the USB flash drive from the left side list.
    Click on the Restore tab in the DU main window.
    Check the box labeled Erase destination.
    Select the USB flash drive volume from the left side list and drag it to the Destination entry field.
    Drag the InstallESD.dmg disc image file into the Source entry field.
    Double-check you got it right, then click on the Restore button.
    When the clone is completed you have a fully bootable Lion installer that  you can use without having to re-download Lion.

Maybe you are looking for