Select days in the future

Hello

I hope that anyone of you could help me out here.

I have an obligation to select a row in the table tbl_work_days with a calc_date of the column whose value is as trunc (sysdate) + 7.
I also have a table with a column free_date tbl_free_days. Now the problem:
I need to check if
  calc_date = trunc(sysdate)+7 and 
  when sysdate+8 = free_date then calc_date = trunc(sysdate)+8 or
  when sysdate+8 = free_date and sysdate+9 = free_date then calc_date = trunc(sysdate)+9
  when sysdate+8 = free_date and sysdate+9 = free_date and sysdate+10 = free_date then calc_date = trunc(sysdate)+10
  when sysdate+8 = free_date and sysdate+9 = free_date 
    and sysdate+10 = free_date and sysdate+11 = free_date then calc_date = trunc(sysdate)+11
  when sysdate+8 = free_date and sysdate+9 = free_date 
    and sysdate+10 = free_date and sysdate+11 = free_date 
    and sysdate+12 = free_date then calc_date = trunc(sysdate)+12
Today is the 30.08.2011 (August 30, 2011)

Source:
Table: tbl_work_days
customer_id    Name     calc_date   
1              Tobias   06.09.2011  
2              Hans     07.09.2011  
3              Lars     08.09.2011  
4              Dave     09.09.2011  
5              Mike     10.09.2011  

Example 1
Table: tbl_free_days
free_date   description
07.09.2011  happy DBA day
08.09.2011  a custom birthday
09.09.2011  another birthday

Example 2
Table: tbl_free_days
free_date   description
08.09.2011  a custom birthday
09.09.2011  another birthday

Example 3
Table: tbl_free_days
free_date   description
07.09.2011  happy DBA day
09.09.2011  another birthday

Output 1 based on Example 1
1              Tobias   06.09.2011  
2              Hans     07.09.2011  because of happy DBA day
3              Lars     08.09.2011  because of a custom birthday
4              Dave     09.09.2011  because of another birthday

Output 2 based on Example 2
1              Tobias   06.09.2011

Output 3 based on Example 3
1              Tobias   06.09.2011  
2              Hans     07.09.2011  because of happy DBA day
Hope you don't understand what I want. I'm just guessing that there is a nice analytical function for it?

Thanks in advance,

Tobias

Hi, Tobias,.

Tobias Arnhold wrote:
Hi Frank,.
Sorry for the wrong description.

The sample data are big now, but you did not improve the description at all. In particular, you have not answered my questions.
Time of day is not relevant to this problem? I guess it is. In your sample data, calc_date is always TRUNC (calc_date) and free_date is always TRUNC (free_date). If that's not true in your actual data, and then use TRUNC (calc_date) I used calc_date below and/or use TRUNC (free_date) I used free_date.
That means each line of output represent? There will always be either 0 or 1 for each line output lines in tbl_work_days? I guess that each output line represents a line of tbl_work_days where is
(a) calc_date = SYSDATE + 7 (no matter what is in tbl_free_days), or
(b) calc_date BETWEEN SYSDATE + 8 AND SYSDATE + 12 and daily between SYSDATE + 8 and calc_date are in tbl_free_dayss
This means that there is never more than 1 row in the output for each row in tbl_work_days. Not exactly. See my next message, below.

When tbl_free_days contains all these rows, then the results should be
customer_id    Name     calc_date
1              Tobias   06.09.2011  

When tbl_free_days contains all the rows except 200 , then the results should be
1              Tobias   06.09.2011  

When tbl_free_days contains all the rows except 100,200 , then the results should be
customer_id    Name     calc_date   free_id
1              Tobias   06.09.2011  -
2              Hans     07.09.2011  100
3              Lars     08.09.2011  200
4              Berni    08.09.2011  200

If tbl_free_days does not contain 100 or 200, then how do you get the numbers 100 and 200 in the output?
Did you mean "which contains tbl_free_days * only * ranks 100 200, while results are expected to be...". "in this last example?
Similarly, for the first example did you mean' when tbl_free_days contains * no of * these rows, then the results should be... » ?
For the second example, you mean ' when tbl_free_days contains * only * rank 200, while the results should be...» » ?

Here's a way to do it:

DEFINE     base_date     = "DATE '2011-08-30'"

WITH     got_free_group     AS
(
     SELECT     free_id
     ,     free_date
     ,     free_date - DENSE_RANK () OVER (ORDER BY free_date)
               AS free_group
     FROM     tbl_free_days
     WHERE     free_date     BETWEEN     &base_date + 8
                    AND     &base_date + 12
     AND     free_id     IN (100, 400)     -- For testing only
)
SELECT       w.*
,       f.free_id
FROM            tbl_work_days     w
LEFT OUTER JOIN     got_free_group     f  ON  w.calc_date = f.free_date
WHERE       &base_date + 7  IN ( w.calc_date
                    , f.free_group
                    )
ORDER BY  customer_id
;

I used the variable base_date of substitution because SYSDATE is no longer on 30 August. In your code, you must use trunc (sysdate) every time I've used & base_date, or you can define base_date as

DEFINE     base_date     = "TRUNC (SYSDATE)"

In addition, the condition of the WHERE clause in got_free_group

     AND     free_id     IN (100, 400)     -- For testing only

is here to make it easy to test scenarios as 'when tbl_free_days contains only the lines... ". ». You don't need this condition in your production code.

I realize that it is not easy to understand it as free_group, but because you hate the explanations, I'm not going to explain it. (Also, I'm out of time right now. Maybe later.)

Published by: Frank Kulash, August 31, 2011 09:22

Tags: Database

Similar Questions

  • How can I get a list of jobs that are planned for the days of the future

    How can I get a list of jobs that are planned for the days of the future?

    In a previous article, I found a query that lists the scheduled tasks that are already running. Can I get a similar request for the future days which is compiled?

    Hi sreedevir,

    SELECT jobmst.jobmst_prntname, jobmst.jobmst_name, jobrun.*

    FROM jobrun JOIN jobmst ON jobmst.jobmst_id = jobrun.jobmst_id -joining tables jobmst and jobrun

    WHERE jobrun_proddt > = dateadd (dd, 1, datediff (dd, getdate())) 0, -future dates

    AND jobmst.jobmst_type = 2 -given jobs (and not groups)

    ORDER BY 1, 2 -Sort by name of the parent, then by task name

    Feel free to make any changes to your reporting needs

    ARO

    The Derrick

  • Set a date of two days in the future

    Hi all

    This code works very well to set a value if there is no value in the form. MEETING (so I can insert the value in my db with sql):

    < cfset isMEETINGNull = iif (len (trim (form. (False (MEETING)) EQ 0, true) >

    How can I set the value to a date of two days from now if it is empty (instead of the null value because it is at the moment, seomthing like that?)

    < cfset isMEETINGNull = DateFormat (DateAdd ("d", + 2, now(), ' MM/DD/YYYY') >

    Thank you!

    Message has been changed due to the possible response from google search

    Here is another suggestion. It includes a validation.

    The value to store in the database is then meetingDate.

  • Thunderbird is save messages sent with a date 3 days in the future, what is with that?

    I noticed this morning that an email I sent from another computer yesterday, 05/28/15, in the afternoon around 16:00 sitting in my inbox sends file dated 05/31/15. Today 29/05/15. What's new? I'm under TB 31.7.0 in Windows 7.

    have you checked the date information, time and time zone of the computer that you used to send the mail?

  • Selection field of the day of the week as the appointment screen

    Hello

    I am developing a screen exactly similar to the ' screen new appointment "in the calendar application. But I can't find any appropriate UI component to expand the field of selection of the day of the week.

    In the 'new appointment screen of origin', this field of choice in week appears when you select "weekly" in the choicefield "recidivism". There are 7 characters, one for each day of week & you can select / deselect a day of week by pressing the space bar.

    Now, can someone please give me an idea on how to develop this field?

    Thank you.

    Hello

    Add HorizontalFieldManager and add ButtonFields on measurement (method overload f.e paint.).

    ButtonFieldListener is added to the custom field button.

    If selected the day of the week (given by the manufacturer) paint "BOLD", and another not.

    Kind regards

    Jochen

  • When purchasing a subscription, if I know that I used to use the product until a certain date in the future, am I able to set up a future subscription start date as a way to not lose all day in the duration of my subscription?

    When purchasing a subscription, if I know that I used to use the product until a certain date in the future, am I able to set up a future subscription start date as a way to not lose all day in the duration of my subscription?

    Not according to the terms and conditions

    Adobe - General conditions of subscription

    Service starts as soon as your first payment is processed.

    Subscribe when you are ready to use it.

  • by selecting specific days of the week for the date range?

    I have a table with a number of clients for about a year, but I want to only select Wednesday and Thursday of
    every week, since the beginning of the dates. Table is simple and has two columns. Each line is separate from a Date
    There is no duplicate of Date, it is counted for every day of the year.

    column 1: number of clients, count (customer_id)
    column 2: Date

    Need help with the best way to achieve this.

    Not sure if it is even possible to select a date in the name of the day?

    Basically, I want to select every Wednesday and Thursday of each week and compare the counts during the week, the week during
    week for the whole week see if charges go upwards or downwards, to get trends, thank you!

    Hello

    Kodiak_Seattle wrote:
    I have a table with a number of clients for about a year, but I want to only select Wednesday and Thursday of
    every week, since the beginning of the dates. Table is simple and has two columns. Each line is separate from a Date
    There is no duplicate of Date, it is counted for every day of the year.

    column 1: number of clients, count (customer_id)
    column 2: Date

    Need help with the best way to achieve this.

    Not sure if it is even possible to select a date in the name of the day?

    Sorry, we don't know what you want.

    To see if a date given (dt) is a Wednesday or Thursday, you can use:

    WHERE   TO_CHAR ( dt
              , 'DY'
              , 'NLS_DATE_LANGUAGE=ENGLISH'     -- If necessary
              )  IN ('WED', 'THU')
    

    I hope that answers your question.
    If not, post a small example data (CREATE TABLE and only relevant columns, INSERT statements) for all of the tables involved and also publish outcomes from these data.
    Explain, using specific examples, how you get these results from these data.
    Always say what version of Oracle you are using (for example, 11.2.0.2.0).
    See the FAQ forum {message identifier: = 9360002}

  • Select the first 20 days of the month for a given date

    Hi gurus,

    Please let me know to select the first 20 dates for a date supplied by using a parameter.

    For ex -.

    Suppose I provided a date by using a parameter - January 13, 2012

    The query should return me - 01-Jan-2012, January 2, 2012... January 20, 2012

    or if I get home on 23 March 2012

    The query should return me - 01-Mar-2012, March 2, 2012... 20 March 2012

    Thanks in advance.

    Hello

    Here's one way:

    VARIABLE  target_date     VARCHAR2 (11)
    EXEC     :target_date := '13-Jan-2012';
    
    SELECT      TO_DATE ( SUBSTR (:target_date, 4)
                , 'Mon-YYYY'
               ) + LEVEL - 1     AS a_date
    FROM     dual
    CONNECT BY     LEVEL     <= 20
    ;
    

    Note that this does not take account the part of: target_date that represents the day of the month. When you use TO_DATE without giving a date of the month, it defaults to the 1st of the month.

  • Select SQL - last day of the month, a year from now

    Hello
    How do we get the last day of the month, a year from now.

    For example-sysdate-12/2/2009
    Result - 31/12/2009

    Thank you

    Hello

    SELECT  LAST_DAY ( ADD_MONTHS ( SYSDATE
                                  , 12
                )           )
    FROM    dual;
    

    Remember, all DATEs, including the results of this query, include the hours minutes and seconds.
    The hours, the minutes and the seconds returned by the above expression are the same as SYSDATE: If you run it at 15:43:30 today, it returns 15:43:30 on December 31, 2010. This can be important if you want to use an expression like this as the cut-off point in a WHERE clause.

  • Select the last value for each day of the table

    Hello!

    I have a table that contains several measures for each day. I need two queries on this table, and I'm not sure how to write them.

    The table stores the rows (sample data)
    *DateCol1                 Value       Database*
    27.09.2009 12:00:00       100           DB1
    27.09.2009 20:00:00       150           DB1
    27.09.2009 12:00:00       1000          DB2
    27.09.2009 20:00:00       1100          DB2
    28.09.2009 12:00:00       200           DB1
    28.09.2009 20:00:00       220           DB1
    28.09.2009 12:00:00       1500          DB2
    28.09.2009 20:00:00       2000          DB2
    Explanation of the data in the sample table:
    We measure the size of the data files belonging to each database to one or more times a day. The value column indicates the size of the files of database for each database at some point (date in DateCol1 European model).


    What I need:
    Query 1:
    The query must return to the last action for each day and the database. Like this:
    *DateCol1       Value      Database*
    27.09.2009        150          DB1
    27.09.2009       1100          DB2
    28.09.2009        220          DB1
    28.09.2009       2000          DB2
    Query 2:
    The query should return the average measurement for each day and the database. Like this:
    *DateCol1       Value      Database*
    27.09.2009       125          DB1
    27.09.2009      1050          DB2
    28.09.2009       210          DB1
    28.09.2009      1750          DB2
    Could someone please help me to write these two queries?

    Please let me know if you need further information.

    Published by: user7066552 on September 29, 2009 10:17

    Published by: user7066552 on September 29, 2009 10:17

    Why two queries when it suffice ;)

    SQL> select dt
      2       , db
      3       , val
      4       , avg_val
      5    from (
      6  select dt
      7       , val
      8       , db
      9       , row_number () over (partition by db, trunc (dt)
     10                                 order by dt desc
     11                            ) rn
     12       , avg (val) over (partition by db, trunc (dt)) avg_val
     13    from test)
     14   where rn = 1
     15  order by dt
     16  /
    
    DT        DB           VAL    AVG_VAL
    --------- ----- ---------- ----------
    27-SEP-09 DB2         1100       1050
    27-SEP-09 DB1          150        125
    28-SEP-09 DB2         2000       1750
    28-SEP-09 DB1          220        210
    
  • Yahoo mail and yahoo Web sites not be loading not on safari April 10, 2916. How to get around this problem? Œuvres of chrome! What is the future of mac?

    Yahoo mail and yahoo Web sites not to load not on safari as of April 10, 2016. How to get around this problem? Œuvres of chrome! What is the future of mac?

    Could be a cookie problem...

    In your Safari menu bar, click Safari > Preferences then select the Privacy tab, then click on: Remove all data from the Web site

    If this does not help, you can have this Yahoo pages preventing to open a Safari extension or third party plugin installed.

    In the Safari menu, click Safari > Preferences , and select the tab ' Extensions '. If there are installed extensions, one at a time uninstall and exit then restart Safari to test.

    If it is not a question of extensions, try troubleshooting third-party plug-ins.

    Back to Safari > Preferences. This time select the tab Security Deselect: plug-ins Allow. Quit and restart Safari to test.

    If it makes a difference, the instructions of troubleshooting plugins here.

  • My wrt400n worked very well, but since those days, as the internet continues to drop.

    My wrt400n worked very well, but since those days, as the internet continues to drop. My modem is all good and up and running. Its router just guard fall internet. SO I reset the router and thn it works. Pls suggest a soltuion. Its very frustrating and fiddles with the work.

    THX in advance

    How about if you change the settings of the router wireless. Change the width of the channel at 20 Mhz and select 9 as the standard channel under the 2.4 Ghz settings. For 2.4 GHz wireless security settings, you must set the security settings wireless personal WPA2 and AES as the encryption. BTW, you have problems in your laptop only, or with computers portable mutiple because if you don't have a laptop then you must try to update drivers wireless card from your cell phone.

  • Once I reset my battery CMOS from one day to the next. I tried to install my recovery disk this morning and can't get it done

    Once I reset my battery CMOS from one day to the next.  I tried to install my recovery disk this morning and can't get it done.  I kept receiving an error of 100.
    Don't know what to do.
    Are there out there can HELP?

    Please, I beg you!

    Thank you

    Hello

    Would you please ask the same questions more then once in these Forums.

    What you're doing is confusing in these Forums, as nobody else knows what advice are envisaged for you in other responses.

    Other questions, it seems you have an HP computer.

    Here is the information of HP for Vista from recovery to factory settings:

    "Recover Windows Vista operating system using HP Recovery"

    http://h10025.www1.HP.com/ewfrf/wc/document?DocName=c00809678&LC=en&DLC=en&cc=us&product=18703

    Restore the PC to its original state with the recovery discs

    If the PC cannot access the Windows desktop, and the Recovery Manager cannot be started by selecting the f11 at startup key, you can use either the recovery disc custom that you created, or you can order a recovery CD to replace HP, to boot the PC and restore the hard drive to its original operating state.
    NOTE: perform the recovery process by using the custom recovery disk you created can take less than an hour, whilst using the replacement disks ordered directly from HP can take four to six hours and includes several reboots.
    If the HP Recovery Disk Manager can access the hard drive, it will attempt to repair the recovery partition. If successful, a prompt to continue execution of the recovery using the Recovery Disk Manager.
    Use the recovery discs to restore the PC by following the steps below.
    1. Insert the recovery disc into the optical disc drive and start the computer.
      If the computer is running, insert the reinstallation disc and immediately click on Start and select restart to force the computer to boot from the recovery disc.
    2. When prompted, Press any key to boot from the CD or DVD, press any key on the keyboard.
    3. Leave the recovery disk boot and load the files required, while the Microsoft loading screen appears. It may take several minutes to load the required files.
    4. When the HP Recovery Manager window appears, click Advanced options , System Recovery , and then click Next to open the Recovery Manager.
    5. etc.

    If you are having problems using the HP recovery process, you will need to contact HP, because this is Recovery process HP, not Microsoft.

    Details of HP:

    http://WWW8.HP.com/us/en/contact-HP/contact.html

    You can also try the Forums from the HP Support:

    http://h30434.www3.HP.com/

    See you soon.

  • Hard drive inexplicably doubled from one day to the next

    My hard drive inexplicably doubled from one day to the next.  I now have only 3 GB free.  I did disk cleaning, defrags, uninstalls, deleted files (just 23 GB of personal files, music, etc. 99 GB drive), etc.  I downloaded TreeSize and see what the size of my files are, but don't know what is normal.  Common sense tells me that clearly something has changed the last time I used my computer to create this problem, but the only change I made was moving my personal files in my user folder to a Public folder, and then in my computer with my other laptop's network.  I checked three times, and my previous user folder files have definitely missing.  Could a bunch of file system are duplicated or something like that?  I know that I cannot delete them, but is it possible to clean them safely?

    To clean all the files on the computer

    1. Click to open Disk Cleanup.

    2. In the drives list, click the drive you want to clean, and then click OK.

    3. In the Disk Cleanup dialog box, click on clean up system files. If you are prompted for an administrator password or a confirmation, type the password or provide confirmation.

    4. In the disk cleanup: drive selection dialog box, select the drive you want to clean, and then click OK.

    5. In the dialog box disk cleanup, on the Disk Cleanup tab, select the check boxes for the types of files that you want to remove, and then click OK.

    6. In the message that appears, click on delete files.

      The other Options tab is available when you choose to clean up the files of all users on the computer. This tab includes two additional ways to free disk space even more:

      • Programs and features. This option opens programs and features in Control Panel, where you can uninstall programs you no longer use. The column size in programs and features shows how much disk space each program uses.

      • System Restore and shadow copies. With this option, you can remove all but the most recent point on the restore disc.

        Use system restore to return your system files to an earlier point in time, restore points. If your computer is running normally, you can save disk space by removing the previous restore points.

        In some editions of Windows 7, restore points can include previous versions of files, called the shots and backup images created with Windows complete PC Backup. These files and images will also be deleted. For more information about system restore, see what is system restore?

  • Cancellation: "do not show this message in the future".

    A Tech support guy selected "do not show this message in the future" on the page uninstall programs...  Well... I didn't do this to him.

    How to reverse the "selection" do not show this message in the future? '"

    Thank you

    Well found the answer.  Posting it here in case anyone else is curious

    1. type regedit into the dialog box RUN and press ENTER. Now goto:

    HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\DontShowMeThisDialogAgain

    2 in the pane, delete the following string value:

    {948e51fb-0a48-44f0-86ac-33c36def540c}

    Alternatively, you can change its value to Yes instead of delete it. That'll work too.

    3 this is it. Now you answer the prompt confirmation of uninstallation.

Maybe you are looking for