Query to retrieve date depending on specific date

hi all,
i have a question regarding dates.
ihave a table with following data
with table1 as
  select 123 id, 'text' txt, to_date('9/16/2010','mm/dd/yyyy') date1 from dual union all
  select 111 id, 'text2' txt, to_date('9/16/2010','mm/dd/yyyy') date1 from dual union all
  select 222 id, 'text3' txt, to_date('9/16/2010','mm/dd/yyyy') date1 from dual union all
  select 333 id, 'text4' txt, to_date('9/16/2010','mm/dd/yyyy') date1 from dual union all
  select 444 id, 'text5' txt, to_date('9/20/2010','mm/dd/yyyy') date1 from dual union all
  select 555 id, 'text6' txt, to_date('9/20/2010','mm/dd/yyyy') date1 from dual union all
  select 666 id, 'text7' txt, to_date('9/20/2010','mm/dd/yyyy') date1 from dual union all
  select 777 id, 'text8' txt, to_date('9/20/2010','mm/dd/yyyy') date1 from dual
)i am creating a procedure that will insert data into a table and this procedure will run every day.
so for example, if procedure runs today, then it will check today's date against table1 and see we have
4 rows for 9/16/2010 and insert into a table. then tomorrow 9/17/2010 it will see if there is a 9/17/2010 data.
since there is no data, then the 4 rows with 9/16/2010 should be pick up and inserted with 9/17/2010 date.
now lets say that today is 9/20/2010 then the query should pick up 9/20/2010 data set.
for 9/21/2010 the same data set for 9/20/2010 should be pick since that is the latest date less than or equal to 9/21/2010
here is sample output.
lets say we run query today. then output should be
id     txt       date1
================================
123     text     09.16.2010  
111     text2     09.16.2010
222     text3     09.16.2010
333     text4     09.16.2010 now lets say today is 9/17/2010 then query will look at table for the 9/17/2010, since it is not found
it will be pick the latest less than or equal to 9/17/2010
output should be
id     txt       date1
================================
123     text     09.17.2010  
111     text2     09.17.2010
222     text3     09.17.2010
333     text4     09.17.2010 now lets say that today is 9/20/2010, the query will look at the table for date 9/20/2010, it found there there is an entry and will pick up that data set.
so output should be
444     text5     09.20.2010
555     text6     09.20.2010
666     text7     09.20.2010
777     text8     09.20.2010if you run for 9/21/2010 , query should pick up latest date less or equal to 9/21/2010 which is 9/20/2010 so output should be
444     text5     09.21.2010
555     text6     09.21.2010
666     text7     09.21.2010
777     text8     09.21.2010can someone help write a query that given a date can retrieve output above? thanks

with table1 as
  select 123 id, 'text' txt, to_date('9/16/2010','mm/dd/yyyy') date1 from dual union all
  select 111 id, 'text2' txt, to_date('9/16/2010','mm/dd/yyyy') date1 from dual union all
  select 222 id, 'text3' txt, to_date('9/16/2010','mm/dd/yyyy') date1 from dual union all
  select 333 id, 'text4' txt, to_date('9/16/2010','mm/dd/yyyy') date1 from dual union all
  select 444 id, 'text5' txt, to_date('9/20/2010','mm/dd/yyyy') date1 from dual union all
  select 555 id, 'text6' txt, to_date('9/20/2010','mm/dd/yyyy') date1 from dual union all
  select 666 id, 'text7' txt, to_date('9/20/2010','mm/dd/yyyy') date1 from dual union all
  select 777 id, 'text8' txt, to_date('9/20/2010','mm/dd/yyyy') date1 from dual
/* substitute trunc(sysdate) with your parameter */
select t.id, t.txt, trunc(sysdate)  as date1 from table1 t
where t.date1=(select max(t2.date1) from table1 t2 where t2.date1 <= trunc(sysdate) )

Similar Messages

  • FI: Payment condition calculating 2 due-dates depending on document-date

    Hello community!
    I'm searching for a definiton of a FI-payment-condition which is calculating two different due-dates depending on document-date.
    E.g.:
    Invoices dated from 01.03.08 till 09.03.08 should be paid on 10.04.08.
    Invoices dated from 10.03.08 till 31.03.08 should be paid on 10.05.08.
    Or must this be realized by a FI-substitution???
    Thanks for an answer.
    Kind regards,
    Bernd

    Hello,
    You didnot mention if u need posting date as base or document date. However your scenario, below is
    the answer for your query is
    1. Create one customised payment term only eg. XXX
        for first scenario,
       Day limit is 9 ;
       under payment terms tab , fixed date 10, Addl month 1
       for second scenario
       in the same payment term XXX
       Day limit is 31,
       under payment terms tab , fixed date 10, addl month 2.
    Explanation.
      as day limit is 9, what ever the dates of transaction between 1-9 it picks first setting
    and after 9th until 31st it picks second setting.
    I hope this clarifies you.
    Thanks
    G.Bharathi

  • Query to retrieve Identical names, same birth dates

    I have a table with these fields:
    table1 definition
    (ID varchar2(10),
    name1 varchar2(50),
    name2 varchar2(50),
    name3 varchar2(50),
    name4 varchar2(50),
    birthdt date)
    I need to write a query to retrieve the id's of those people that have identical first and last names AND were born on the same date. note that name is composed of four parts.
    I am trying this query
    (select id from table1 group by name1,name4,birthdt having count(name1)>1 and count(name4)>1 and count(birthdt)>1). I am not getting correct results.
    Any help appreciated.
    Ammar

    ¿...Doh...how could I miss that? smacks forehead
    Thanks ( for the N-th time ;) ) for setting straight, Solomon.
    Guess I wasn't completely recovered from yesterday ;)
    However, the basic approach wasn't too bad ;)
    But ofcourse the 'select *' should be 'select id'
    Using testdata:
    SQL> with table1 as ( -- generating sample data as I should have done in the first place:
      2  select 1 id, 'John' name1, 'James' name2, 'Joe' name3, 'Jameson' name4, to_date('15-05-1989','dd-mm-yyyy') birthdt from dual union all
      3  select 2 id, 'John' name1, 'James' name2, 'Joe' name3, 'Jackson' name4, to_date('15-05-1989','dd-mm-yyyy') birthdt from dual union all
      4  select 3 id, 'John' name1, 'James' name2, 'Joe' name3, 'Jameson' name4, to_date('25-05-1989','dd-mm-yyyy') birthdt from dual union all
      5  select 4 id, 'Zlad' name1, 'James' name2, 'Joe' name3, 'Jameson' name4, to_date('15-05-1989','dd-mm-yyyy') birthdt from dual union all
      6  select 5 id, 'John' name1, 'James' name2, 'Joe' name3, 'Jameson' name4, to_date('15-05-1989','dd-mm-yyyy') birthdt from dual
      7  )
      8  --
      9  -- actual query
    10  --
    11  select id
    12  from ( select id
    13         ,      name1
    14         ,      name2
    15         ,      name3
    16         ,      name4
    17         ,      birthdt
    18         ,      count(*) over (partition by name1, name2, name3, name4, birthdt) recs
    19         from   table1
    20       )
    21  where recs > 1;
            ID
             1
             5

  • Query That Retrieves New Customers & Leads By Date

    Hello All --
    We would like to create a Query that retrieves new customers and leads based on the date (or date range) they are entered into SAP. 
    Can we create a UDF named STDATE --- Start Date -- and have a Query that allows us to select based on this date range?
    Start dates are entered in this format --- YYYYMMDD.
    Then, the Query would pull out:
    Contact...Company...Bill To Address...Bill To City...Bill To State....Bill To Zip...Phone...Email
    Is this possible to do?
    Thanks!
    Mike

    Hi Mike ,
    You can add some other field as you like .This report will give you combination of both customer and lead .
    I couldn't understand the purpose ,but I like previous query as i have flexibilty to choose customer or vendor .
    SELECT T0.[CreateDate], T0.[CntctPrsn], T0.[CardName], T0.[Address], T0.[City], T0.[E_Mail], T0.[ZipCode], T0.[Phone1] FROM OCRD T0 WHERE T0.[CardType] in ('C','L') and T0.[CreateDate]=[%0]
    If you agree with me ,you can use this query
    SELECT T0.[CreateDate], T0.[CntctPrsn], T0.[CardName], T0.[Address], T0.[City], T0.[E_Mail], T0.[ZipCode], T0.[Phone1] FROM OCRD T0 WHERE T0.[CardType] =[%0] or T0.[Cardtype]=[%1] and
    T0.[CreateDate]=[%2]
    Regarding the date issue , since you are entering paramenter it will match with the date you are looking for .If you have more than one customer created on same day , you willreceive multiple client .
    Thank you
    Bishal

  • How to search on creation Date to get specific date records

    Hi,
    I have created a search page based on VO which contains creation Date field. Creation Date field stores date with time into database.
    af:query is created on this VO. Suppose I have created 10 records today and want to query that by providing today's date(6/21/2011) in CreationDate field. On click of search, it does not return any record.
    Since records in database are timestamp based, it does not return any record.
    How to achieve this functionality where user can search by specific date?

    799794 wrote:
    Hi,
    I have created a search page based on VO which contains creation Date field. Creation Date field stores date with time into database.
    af:query is created on this VO. Suppose I have created 10 records today and want to query that by providing today's date(6/21/2011) in CreationDate field. On click of search, it does not return any record.
    Since records in database are timestamp based, it does not return any record.
    How to achieve this functionality where user can search by specific date?did you created any view criteria? share with us more info

  • How to disable selection of  End Date depending on start Date.

    Hello,
    I have  2 DatePicker controls
    1) Start Date
    2) End Date
    When I select a start date  in  date picker depending on that  it should disable the  selection of  dates less than  Start date in End date Date Picker.
    Could anyone please provide solution for it.
    Thanks & regards,
    Viswanath

    You can always check UI5 core files and extend it. FYI, most of UI5 calendars are based on "MobileScroll" UI library, e.g.
    DateTimeInput - Mobiscroll - Demos and Examples - Date &amp;amp; Time - Date with min and max
    DatePicker - Mobiscroll 2.15.1 Documentation - Calendar
    Below you can find few extension examples:
    sap.m.DateTimeInput extend- JS Bin - Collaborative JavaScript Debugging&lt;/title&gt;  &lt;link rel=&quot;alternate&quot; type=&quot;application/jso…
    sap.m.DatePicker extend -> JS Bin - Collaborative JavaScript Debugging&lt;/title&gt;  &lt;link rel=&quot;alternate&quot; type=&quot;application/jso…
    Regards,
    Vladimir

  • Customer aging query - show balance as of a specific date

    I am working on a query for a vendor aging to show information that is not available in the vendor aging report. However, I want to show the balance that was open at a specific point in time. i.e. today is Aug 17 and i want to show what was due at June 30. All the June invoices have been paid as of Aug 17 so i cannot use docstatus. The aging report gives you the option to select Vendors with a zero balance and reconciled transactions. How do i show that information in a query?

    Hi Vicki,
    I'm trying to do the exact same thing. Get the aging balance at any point in time in verion 2007.
    Have you succeded so far?.
    I use to run a query to get the balance at any point in 2005 like this..
    SELECT     T2.CardCode, T2.CardName , T0.[BatchNum], T0.[TransId], T0.[TransType], T1.[CreatedBy], T1.[RefDate], T1.[DueDate], T1.[MthDate],
    T1.[LineMemo], T0.[Ref1], T1.[BaseRef], T1.[Credit], T1.[Debit], T1.[FCCredit], T1.[FCDebit], T1.[SYSCred],
    T1.[SYSDeb], T1.[SourceLine], T1.[Ref1], T1.[LineType], T1.[TaxDate], T1.[Account], T1.FCCurrency, T1.[ObjType]
    FROM     [dbo].[OJDT] T0
    INNER     JOIN [dbo].[JDT1] T1 ON T1.[TransId] = T0.TransId
    INNER     JOIN [dbo].[OCRD] T2 ON T1.[ShortName] = T2.CardCode
    WHERE     T1.[Account] IN (SELECT AcctCode FROM oact WHERE LocManTran = 'Y' AND GroupMask = 1)
    AND      T1.[RefDate] <= '20091026'
    AND      (T1.[MthDate] > '20091026'
    OR      T1.[IntrnMatch] = 0 )
    ORDER BY T0.[TransType], T1.[RefDate], T2.CardCode, T1.DueDate
    But this baby shows more documents than needed and a lot of work has to be done to place payments on the right invoices to como up with the right balance age.
    I'd appreciate if you could help me out or let me know what have you found so far.
    Best,
    Ricardo Sada

  • How to create a Date field with specific date limitations

    Hello, im new to creating forms in Acrobat.
    My question is, how do i create a date field and at the same time have a restriction on which dates to put.
    ie; i want the date field to only enter dates from Jan 01 2015 - Dec 31 2015.
    Thanks for your help.

    Thanks!
    I tried and it worked.
    but what are these numbers after the 2015?
    var minDate = new Date(2015, 0, 1, 0, 0, 0, 0); 
    var maxDate = new Date(2015, 11, 31, 23, 59, 59, 999); 

  • CTS+ Configuration in PI 7.4 for SLD specific data

    Hello Guys,
    I am doing CTS+ configuration for PI System SLD Vesrion 7.4 to transport J2ee as well as SLD specific data through transports. My CTS server is Solman System.
    I have created a CTS user in Solman System and in PI NWA I have defined destination to point to Solman System. But i am running into several errors.
    Could anyone please help me what user authorization are required for CTS user in Solman to transfer Non-Abap Data and SLD specific data through transports.
    Its very urgent.

    Did you happen to see this document of a CTS+ configuration for 7.3 (should be fairly the same for 7.4)?   CTS+ Configuration for PI 7.3
    Steps 1.2 and 8.2 have references to roles.

  • CTS+ configuration in PI 7.4 to transport SLD specific data

    Hello Guys,
    I am doing CTS+ configuration for PI System SLD Vesrion 7.4 to transport J2ee as well as SLD specific data through transports. My CTS server is Solman System.
    I have created a CTS user in Solman System and in PI NWA I have defined destination to point to Solman System. But i am running into several errors.
    Could anyone please help me what user authorization are required for CTS user in Solman to transfer Non-Abap Data and SLD specific data through transports.
    Its very urgent.

    Hi Bharatram,
    Apply the below roles to the user
    SAP_XI_CMS_SERV_USER
    SAP_XI_DEVELOPER_J2EE
    SAP_XI_CONFIGURATOR_J2ee
    and refer the below blog
    CTS+ Configuration for Process Orchestration (PI/PO) and AEX 7.31 (JAVA Only Stack)
    regards,
    Harish

  • View All Incidents Active on Specific Date Data Warehouse

    Hi
    I'm looking to find out if it is possible to view all incidents active on a specific date within the data warehouse. Obviously there is the created, resolved and closed dates however that doesn't present the correct information as filtering on the created
    by date with a specific date only shows incidents created on that specific date.
    What I am interested in is how many active incidents did I have at the time of the data warehouse processing and how I would go about that. Obviously it is possible to get this off the live database but it would be good to get this out of the data warehouse.
    Thanks

    Actually turns out the reason why none of the data was making sense was that the data in the cube was completely wrong. Despite the dwdatamart incidentdimvw showing the correct information the cube was out by a long way (cube said over 5000 active incidents
    where as dwdatamart had 17) so i'm guessing there was a problem in processing somewhere along the line. Not quite sure what would cause the cube to be so inaccurate though which is perhaps more concerning.

  • Retriving data depends on date

    Dear Experts,
    Im having 3 line items in schedule agreement. Im retreving data depends on delivery date of line items.
    for suppose 1st line item is on 01.05.2008,2nd line item is 10.05.2008,3rd line item is 27.05.2008.
    if user give options 10.05.2008 to 30.05.2008,2nd and 3rd item are displaying. but 1st line item quality should be displayed in pending qty(of course,new field).
    Right now i need suggestions regarding date calucalation, if user selects 2nd and 3rd line item, 1st line item qty should be displayed in pending. How store the line item qty that is out of select-options?
    Please need ur suggestions,
    Regards,
    Bharat

    Hi,
    Take two internal tables:-
    one with all existing fields that you are currently fetching (say itab)
    and
    other with all the previous fields and also a include other fields that you want to include for those records that are not in date value from select-options (say it_final)
    And now use these queries:-
    select <field1> <field2> ...
    from <table_name>
    into itab
    where
    date in s_date. "condition for date in select options
    "move records in date range to final internal table
    move-corresponding itab to it_final.
    refresh itab.
    clear itab.
    select <field1> <field2> ...
    from <table_name>
    into itab
    where
    date not in s_date. "condition for date not in select options
    "move records from itab to final internal table for records not in date range
    loop at itab.
      it_final-field = itab_field.
      "append other values also for fields not in select options for date
      append it_final.
      clear it_final.
      clear itab.
    endloop.
    Now this internal table will contain all records that are in date range and as well with the records not in date range.
    Hope this solves your problem.
    Thanks & Regards,
    Tarun Gambhir

  • How to query the opening balance for an specific account for an spec. date

    Hi all,
    for my user query I need to calculate the opening balance for an cash account for an specific date.
    Any ideas, how can I do this?
    Or may be you know a field in an SBO table that already contains information I need...
    Best Regards,
    Inna

    Hi Inna,
    to find the opening balance for an account or a business partner, please see SAP Note [1114253|https://service.sap.com/sap/support/notes/1114253] :
    Symptom
    How to find the opening balance of a business partner or G/L account.
    Other terms
    Account, customer, supplier, vendor, creditor, debitor, start, initial, first, opening balance, business partner, G/L, SAP Business One
    Reason and Prerequisites
    Consulting
    Solution
    There are several possible approaches:
    Approach 1 - Business Partner Opening Balance:
    1. Go to Business Partners -> Business Partner Master Data -> find the Business Partner
    2. Click on the orange link arrow next to the field 'Account Balance' in the header region of the window.
    3. Untick the boxes next to 'Posting date from', 'Display' and 'Display Unreconciled Trans. Only'.
    4. Click on the button 'Refresh'.
    5. Find 'OB' in the column 'Origin', this is the opening balance journal entry.
    Approach 2 - Business Partner Opening Balance:
    1. Go to Business Partners -> Business Partner Master Data.
    2. Click on the orange link arrow next to the field 'Account Balance' in the header region of the window.
    3. Untick the boxes next to 'Posting date from', 'Display' and 'Display Unreconciled Trans. Only'.
    4. Click on the button 'Refresh'.
    5. Double click on the column header 'Posting Date' to find the earliest transaction on the business partner account. This transaction could be opening balance, verify this with the company accountant.
    Approach 3 - G/L Account Opening Balance:
    1. Go to Financials -> Chart of Accounts -> click on the account name once.
    2. Click on the orange link arrow next to the field 'Balance'.
    3. Untick the boxes next to 'Posting date from', 'Display' and 'Display Unreconciled Trans. Only'.
    4. Click on the button 'Refresh'.
    5. Find 'OB' in the column 'Origin', this is the opening balance journal entry.
    Approach 4 - Business Partner and/or G/L Account Opening Balance:
    1. If using SAP Business One 2005 SP01 or earlier, go to Reports -> Query Generator -> click on 'Execute'.
    2. If using SAP Business One 2007 or later, go to Tools -> Queries -> Query Generator -> click on 'Execute'.
    3. Ignore the red system message in the bottom of the screen.
    4. Click on the pencil icon in the top left of the window, the field with 'SELECT *' will change colour to white, then yellow when activated by clicking in the field.
    5. Copy the query below:
               SELECT T0.[TransId], T0.[Debit], T0.[Credit] , T0.[CreatedBy], T0.[TransType] FROM [dbo].[JDT1]  T0 WHERE T0.[TransType] = '-2'  and T0.[ShortName]  = '[%1]'
    6. Click on 'Save' and give the query an appropriate name and select the appropriate category.
    7. Click on 'Execute'.
    8. Click on 'Existing Values'.
    9. Select the G/L account or the Business Partner name from the list and click on 'OK'.
    10. The system message 'Records retrieved by this query #' (# = number of records) will pop up. Click on 'OK'.
    All the best,
    Kerstin

  • Web Analysis Error -- Error while executing query and retrieving data

    Regarding Web Analysis:
    We have a number of reports that we created. yesterday they were all working fine. today we try to open them and most are generating an error.
    The error is:
    Error while executing query and retrieving data.; nested exception is:
    com.hyperion.ap.APException: [1033] Native:
    1013033[Thu Oct 22 09:08:17
    2009]server name/application name/database name/user name/Error91013033)
    ReportWriter exit abnormally
    Does anyone have any insight into what is going on?
    My version information is:
    Hyperion System 9 BI+ Analytic Administration Services 9.3.0.1.1 Build 2
    Web Analysis 9.3.0.0.0.286
    Thanks in advance for your help.
    DaveW

    Hi,
    And also click on check option by opening the query in Query designer,as Mr . Arun suggested.
    And if you get any error in checking, see the long message(detail).
    With rgds,
    Anil Kumar Sharma .P

  • Putting a specific date range in an SQL query

    Hello team,
    I have a query that i have to run every month but still i have to extract information for a given time period.
    Please advise.

    SELECT gcard.ncrd as "CARD NUMBER",
    GPCSTMER.TFNAMCSU || ' ' || TNAMECSU "CUSTOMER NAME",
    TO_CHAR(GCARD.DISSUCRD,'DD-MM-YYYY') AS "FIRST ISSUE DATE",
    case when GCARD.CRSVLCRD = '000' then 'VALID '
    when GCARD.CRSVLCRD = '001' then 'LOST CARD '
    when GCARD.CRSVLCRD = '002' then 'STOLEN CARD '
    when GCARD.CRSVLCRD = '004' then 'FAULTY CARD '
    when GCARD.CRSVLCRD = '005' then 'RETURNED CARD '
    when GCARD.CRSVLCRD = '006' then 'STOP CARD '
    when GCARD.CRSVLCRD = '007' then 'SUBSTITUTED CARD '
    when GCARD.CRSVLCRD = '008' then 'CARD NOT ACTIVATED'
    end "CARD STATUS ",
    CASE
    WHEN Gaccount.CSTATACT = '000' THEN 'NORMAL '
    WHEN Gaccount.CSTATACT = '001' THEN 'GREYLIST '
    WHEN Gaccount.CSTATACT = '002' THEN 'CLOSED '
    WHEN Gaccount.CSTATACT = '003' THEN 'BLOCKED '
    END "ACCOUNT STATUS",
    TO_CHAR(GCARD.DEXPICRD,'DD-MM-YYYY') AS "EXPIRY DATE" ,
    case when GCARD.CUTYPCRD = '001' then 'PRIMARY '
         when GCARD.CUTYPCRD = '002' then 'AUTHORISED '
    end "ACC TYPE"      
    FROM GCARD,GACCOUNT,GRACTCRD,GPCSTMER
    WHERE GCARD.NCRD = GRACTCRD.NCRD AND
    GCARD.NBIN = GRACTCRD.NBIN AND
    GACCOUNT.NACT = GRACTCRD.NACT AND
    GACCOUNT.NBIN = GRACTCRD.NBIN AND
    GCARD.CRSVLCRD = 000 and
    Gaccount.CSTATACT = 000 and
    GPCSTMER.NCST = GCARD.NCST AND
    GCARD.NBIN IN (515869) AND
    PAN_INDEX_RANGE_ID = 50
    ORDER BY GCARD.DISSUCRD,gcard.ncrd,PAN_INDEX_RANGE_ID;
    This my whole query and when i extract the information i have for the whole range.. but me i want to input for a specific date range

Maybe you are looking for

  • HT2736 How can I send a playlist from my library to my friend?

    How can I send a playlist from my Library to my friends phone.  She uses itunes on her phone. I thought that itunes used to do that, I remember it being somewhere on the program. Now when I would like to do it I can't find it.

  • PDFs not loading in Safari (OSX 10.8.2)

    Everytime I try to view a PDF through Safari, all I get is a dark grey screen. I can open PDFs through other browsers, and I can download them from Safari.

  • HT201364 I have a MacBook Pro 17" that meets all criteria on this list but it still won't let me upgrade... Help.

    I have a MacBook Pro 17" that meets all criteria on this list but it still won't let me upgrade... Help. To install Mavericks, you need one of these Macs: MacBook Pro (15-inch or 17-inch, Mid/Late 2007 or later)  **** Maybe my MacBook Pro is early 20

  • Read email Message and process it

    Hai friends.. I've just started working with the mail API.. I've gone through the code samples but couldn't get a clear picture on how to extract the body of an email message in some mailbox of mine.. Let me elabourate.. I have an email account say [

  • Cannot open Excel file over LAN

    I am trying to open an Excel file that is saved on the desktop of my iMac, from my iBook, but keeping getting a message that the file cannot be found. This seems like a strange message because I connected to the iMac over our home network, mounted my