Calculation on previous dates in sql

I need to find the age for each day, but I need for the previous set in a query

For example:

For SYSDATE:

SELECT SYSDATE AS DATE
((SYSDATE) - create_time) Age
Elements

(SYDATE-1):
SELECT (SYSDATE-1) AS DATE
((Sysdate-1)-create_time) Age
Elements


(SYDATE-2) AND SO ON: -
SELECT (SYSDATE-2) AS DATE
((Sysdate-2)-create_time) Age
Elements

Is there any method so that it automatically calculates all previous dates and gives the output. Final output should display like this:

Date_in age
24 JUNE 15 20
23 JUNE 15 19
22 JUNE 15 18

Note: this is not only for 3 days, I need for all previous dates

Help, please

Hello

assuming that your query on the tables of the elements is to return a single line, you can use the CONNECT BY.

Here I used a WITH statement instead of your entry table:

with items as
(
   select to_date('04-JUN-2015', 'dd.mon.yyyy') created_date from dual
)
 select trunc(sysdate) - level + 1 dt
      , trunc(sysdate) - level + 1 - created_date  age
   from items
connect by trunc(sysdate) - level + 1 - created_date > 0;

DT               AGE
--------- ----------
25-JUN-15         21
24-JUN-15         20
23-JUN-15         19
22-JUN-15         18
21-JUN-15         17
20-JUN-15         16
19-JUN-15         15
18-JUN-15         14
17-JUN-15         13
16-JUN-15         12
15-JUN-15         11
14-JUN-15         10
13-JUN-15          9
12-JUN-15          8
11-JUN-15          7
10-JUN-15          6
09-JUN-15          5
08-JUN-15          4
07-JUN-15          3
06-JUN-15          2
05-JUN-15          1

21 rows selected.

In case your query on the table of the elements to return several rows then please provide examples of data (CREATE TABLE and INSERT statement) as specified in: Re: 2. How can I ask a question on the forums?

Kind regards.

Alberto

Tags: Database

Similar Questions

  • Select previous date table

    Hi all

    My need is to choose the present previous date in the table. For example, I select '' 2012-03-30 in my first article, Sun' 2011-12-30' should be in the second column. How can I make it?
    with t1 as (
    select date '2012-03-30' as arcdate from dual union all
    select date '2011-12-30' as arcdate from dual union all
    select date '2011-09-30' as arcdate from dual union all
    select date '2011-06-30' as arcdate from dual union all
    select date '2011-03-31' as arcdate from dual union all 
    select date '2010-12-31' as arcdate from dual 
                   )
    select arcdate, '' as previous_date_from_table  
    from t1 
    where arcdate = date '2012-03-30' 

    Hello

    Use analytics LAG fucntion, like this:

    WITH    got_prev  AS
    (
         SELECT  arcdate
         ,      LAG (arcdate) OVER (ORDER BY arcdate)     AS previous_date_from_table
         FROM      t1
    )
    SELECT     arcdate
    ,     previous_date_from_table
    FROM     got_prev
    WHERE      arcdate     = DATE '2012-03-30'
    ;
    

    Analytical functions are calculated after the clause WHERE is applied; This is why a subquery is needed here. If we use the same query as the LAG

    WHERE      arcdate     = DATE '2012-03-30'
    

    then the result set will that one line, and LAG returns NULL.

    Published by: Frank Kulash, August 26, 2011 11:18

  • user input windows OS when loading data using sql loader

    Hello
    I have the oracle 11g Linux database.
    I have excel sheets with data that must be loaded into the database.
    I have oracle running on my windows client11g.

    When I or anyone else is running the load sql script to load the data, I need to capture the user OS (the one that runs the script of their windows login) and pass it via the sql loader and insert into the table as a column value outside the columns in the excel sheet.

    I think that this could be a very simple solution, I did some research but not just anywhere.
    Can someone guide me on this.

    Thanks in advance.
    Philip.

    Well, I built this example on the fly to show the answer. But it is based on documentation and previous experience with sql * loader or sys_context.

    SQL * Loader Control File Reference
    http://download.Oracle.com/docs/CD/E11882_01/server.112/e16536/ldr_control_file.htm#SUTIL005

    SQL * Loader field list reference
    http://download.Oracle.com/docs/CD/E11882_01/server.112/e16536/ldr_field_list.htm

    SQL - SYS_CONTEXT language reference
    http://download.Oracle.com/docs/CD/E11882_01/server.112/e17118/functions184.htm#SQLRF06117

    This forum is a good source of examples, as well as asktom.oracle.com

  • calculation on line data

    Hello world

    I have just started using the tiara and I have no idea how I can do a few calculations, I want to have chanell with result depends on the other chanells, but not the same data line, I can use the SMC (line, no.) and I can do to make it function in a cell with my result chanell (example for the first row of data (: SMC (1, Ch("[4]/Channel1")) = (ChD (1, Ch("[4]/Channel")) + SMC (2, Ch("[4]/Channel"))) / 2), but I don't know how I can do this function automatically for the other line (for example, line 2 ChD (2, Ch("[4]/Channel1")) = (ChD (2, Ch("[4]/Channel")) + SMC (3, Ch("[4]/Channel"))) / 2) I have a line of 500,000 of data so I need know some functions that will be clalculated this function on the lines of my data. I will be grateful for your help.

    Hello!

    For performance of good calculations on your data of type long, you must use a formula in tiara. In the past, it was the FormulaCalc command. Today, the ChnCalculate is recommended. The similar oddity in the two commands, they can only do calculations on one line (it's repeat them verry fast for all lines). For you the problem this behavior results in a no aproach obvoius. You must copy your data to a second channel, remove the number of values that gives your shift and then use the two channels for the calculation. Here's the script for offset = 1:

    Call ChnCopy("[1]/Channel","[1]/ChannelCopy")
    Call DataBlDel("[1]/ChannelCopy", 1, 1)
    Call ChnCalculate("Ch(""[1]/Result"" )= Ch(""[1]/Channel"" ) + Ch(""[1]/ChannelCopy"" )/2")
    

    Matthias

  • cannot restore from the previous date says computer is unprotected I just get rid of the virus of audit system

    rid of virus check system but cannot restore the computer to the previous date

    You never said how you got rid of the "System Check" malware. As far as we know, he is still there. Follow these instructions (if you have not already done so):

    Also, as Jose pointed out, there are programs that are designed to interfere with the system restore (Norton is one of the most common). But there are really two questions. First of all, we will treat your malware problem. We can always revisit SR at a later date.
    Finally, even once as Jose pointed out, you NEVER run the system restore after the removal of the malware! Which could only bring him back! That you have to turn it off and then put it again, which will REMOVE all previous restore points. You really all gone!
  • How can I configure the system to a previous date

    I have windows vista and cannot find how to restore the system to a previous date

    Hello

    Here's how to do a system restore:

    http://www.windowsvistauserguide.com/system_restore.htm

    If necessary do in safe mode

    Windows Vista

    Using the F8 method:

    1. Restart your computer.
    2. When the computer starts, you will see your computer hardware are listed. When you see this information begins to tap theF8 key repeatedly until you are presented with theBoot Options Advanced Windows Vista.
    3. Select the Safe Mode option with the arrow keys.
    4. Then press enter on your keyboard to start mode without failure of Vista.
    5. To start Windows, you'll be a typical logon screen. Connect to your computer and Vista goes into safe mode.
    6. Do whatever tasks you need and when you are done, reboot to return to normal mode.

    and if you have problems doing, read this:

    There are a variety of reasons for system restore problems

    Norton and Norton product Tamper Protection is the main

    Read about it:

    http://us.Norton.com/support/kb/web_view.jsp?wv_type=public_web&docURL=20101101224849EN&LN=en_US

    http://Service1.Symantec.com/support/sharedtech.nsf/pfdocs/2005113009323013

    You can try restoring the system in safe mode

    http://www.windowsvistauserguide.com/system_restore.htm

    Windows Vista

    Using the F8 method:

    1. Restart your computer.
    2. When the computer starts, you will see your computer hardware are listed. When you see this information begins to tap theF8 key repeatedly until you are presented with theBoot Options Advanced Windows Vista.
    3. Select the Safe Mode option with the arrow keys.
    4. Then press enter on your keyboard to start mode without failure of Vista.
    5. To start Windows, you'll be a typical logon screen. Connect to your computer and Vista goes into safe mode.
    6. Do whatever tasks you need and when you are done, reboot to return to normal mode.

    and malware can interfere with the restoration of the system

    Download update and scan with the free version of malwarebytes anti-malware

    http://www.Malwarebytes.org/MBAM.php

    You can also download and run rkill to stop the process of problem before you download and scan with malwarebytes

    http://www.bleepingcomputer.com/download/anti-virus/rkill

    If it does not remove the problem and or work correctly in normal mode do work above in safe mode with networking

    Windows Vista

    Using the F8 method:

    1. Restart your computer.
    2. When the computer starts, you will see your computer hardware are listed. When you see this information begins to tap theF8 key repeatedly until you are presented with theBoot Options Advanced Windows Vista.
    3. Select the Safe Mode with networking with the arrow keys.
    4. Then press enter on your keyboard to start mode without failure of Vista.
    5. To start Windows, you'll be a typical logon screen. Connect to your computer and Vista goes into safe mode.
    6. Do whatever tasks you need and when you are done, reboot to return to normal mode.
  • I installed SQL Server 2014. I was wondering if I am able to uninstall previous versions of SQL if there is no database (live) used by these versions?

    * Original title: SQL Versions

    I installed SQL Server 2014.  I was wondering if I am able to uninstall previous versions of SQL if there is no database (live) used by these versions?

    This issue is beyond the scope of this site (for consumers) and to be sure, you get the best (and fastest) reply, we have to ask either on Technet (for IT Pro) or MSDN (for developers)
    *
  • Loading data from SQL to Essbase table

    Hello

    I'm loading data from SQL to Essbase table by using a rules file. Number of rows in the source table is 7 million. I use the SUNOPSIS MEMORY ENGINE as area transit, LKM SQL for SQL and SQL IKM for Hyperion Essbase (DATA).

    Question:

    1 can I not use any other LKM as MSSQL for MSSQL (PCBS) to load data to the staging instead of LKM SQL for SQL? What I have to change the transit area then? Loading data using LKM SQL for SQL seems quite slow.

    2 it is mandatory to use LKM SQL for SQL, can someone please tell me what I can change to make this quick support parameters?

    3. is it compulsory to use the SUNOPSIS MEMORY engine loading data from SQL server to Essbase?

    Thank you...

    (1) Yes, I highly recommend watching using a KM which uses native as database technology these will usually be more efficient than the generic (like LKM SQL for SQL) KM especially when large volumes of data are involved. Your staging will change depends on where you organize data for example if you are using a SQL server specific KM as - MSSQL for MSSQL (PCBS) - you must have a lay-by available on a MSSQL database and have access to the utility of PCBS.

    (2) it is not mandatory to use this KM you can use a KMs supported by your database technology

    (3) it is absolutely not obligatory to use the SUNOPSIS MEMORY engine. This should only be used when you have relatively small amounts of data, as well as all the processes in memory, or in the case where you have no other relational technology to perform the staging on. However, in your case to use wherever you are processesing these large volumes of data you should be staged on a physical such as SQL Server or Oracle database if they are available.

  • Loads of parallel data through SQL connect and replace existing data

    I'm doing a load of multiple data through SQL Connect using the following statement.  Just try to put in place a .bat file to automate the process but not sure that whenever it executes this statement adds data to existing or replaces the data? I tried to write an override for this but it works for instructions which contain the import section.

    Import of database data App.DB connect as username identified by password

    with the help of several rules_file, rul1, rul2, rul3

    load_buffer_block starting with buffer_id 10 on error write to 'C:\\dataload.err ';

    We can add spec commit buffer such as "replace all data ', 'create group', 'Add' to this import statement? The default declaration overrides the value whenever the data is loaded?

    Thanks in advance!

    you have an option in the rules file "Overwrite out of values."

    rulefile > setting dataload > loading values

  • view the data in SQL

    I was able to use ASP to retrieve data from SQL database by using something like the one below:

    SQL = "SELECT PageName",

    SQL = SQL & "CONVERT (NUMERIC (6,2), AVG(Rating * 1.00))" AVERAGE ".

    SQL = SQL & 'COUNT (Rating) AS Total',

    SQL = SQL & "SUM(CASE WHEN Rating = 1 THEN 1 ELSE 0 END) AS [Star1Total]"

    SQL = SQL & "SUM(CASE WHEN Rating = 2 THEN 1 ELSE 0 END) AS [Star2Total]"

    SQL = SQL & "SUM(CASE WHEN Rating = 3 THEN 1 ELSE 0 END) AS [Star3Total]"

    SQL = SQL & "SUM(CASE WHEN Rating = 4 THEN 1 ELSE 0 END) AS [Star4Total]"

    SQL = SQL & "SUM(CASE WHEN Rating = 5 THEN 1 ELSE 0 END) AS [Star5Total].

    SQL = SQL & "FROM [SDBI]. [dbo]. [GnieRatePage] "

    SQL = SQL & "GROUP BY PageName".

    SQL = SQL & "ORDER BY PageName".

    I then post on the help page:

    Response.Write ("PageName") Recordset

    What I need, it of to transmit these data to Flash and let Flash to view the coast. How to do by way of ASP?

    Thank you

    I don't use the proper syntax for writing couples variable/value with asp, but, if this is the case, use:

    var myTextLoader:URLLoader = new URLLoader();

    myTextLoader.dataFormat = pouvez;

    myTextLoader.addEventListener (Event.COMPLETE, onLoaded);

    function onLoaded(e:Event):void

    {

    for {(var s:String in e.target.data)

    trace (s, e.Target.Data [s]);

    }

    myTextLoader.load (new URLRequest ("read_page_rating.asp"));

  • data via sql dev store

    Hello
    I've given cluster and can store files and it is clustering ID in the new database using the ODM. I group these data via SQL dev. Oracle again but I'm not for any folder for the storage of cluster ID. How do I? can I put something in response or detail node?

    Hi Nasiri,
    In your description, you have only the Cluster node as entry in the node to apply it. You should also connect a node type of source of data as well. Just to get this to work, why not connect the source of input data to your Cluster node to the node to apply it. If the node to apply it should have 2 inputs, a node of type of data source, but also a model node.
    Thank you, Mark

  • Logic to find particular weekdays after a given date, using SQL

    Hello Experts,

    Version: Oracle 11G

    Could you please help me with logic to find a day of special week after a date in SQL
    For example, I need to know the first Friday after today's date.

    Thank you for your help.

    I hope that under query will be useful for you.

    SELECT NEXT_DAY(SYSDATE,'FRIDAY') "NEXT DAY"
    FROM DUAL
    
    Output:
    
            NEXT DAY
         2/24/2012 12:06:48 PM
    
  • How only migrate data between SQL Server 2008 Oracle 11?

    According to our requirement, we only have to migrate data from a SQL Server database to an existence
    Oracle database user.
    (1) I tried to do with the wizard of Migration SQL Developer 3.0.04, but find a problem.
    My SQL Server database name is SCDS41P2 and my Oracle database user name is CDS41P2;
    When I used SQL Developer move offline data by the Migration Wizard, I found all the oracle user
    name in movedata files who obtained by running the Migration Wizard
    is dbo_SCDS41P2. If the Oracle user name is not the same as my Oracle username existed.
    the data cannot be moved to my user Oracle existed when I run oracle_ctl.bat in the command line window.
    So I had to change the Oracle username in all the movedata files, but it is difficult to change because there are a lot of tables in
    databases. So could you please tell me how to get the movedata files which is the name of user oracle in them my
    WHEREAS Oracle user name?

    (2) I also tried to use the "copy to Oracle" to copy the data to SQL Server database tables
    the user database was Oracle. When you click on 'copy to Oracle', I selected the option 'Include data' and 'replace '.
    But I found some tables cannot be copied, the error info is as below:
    SPSSCMOR_CONTROLTABLE table failed. Message: ORA-00955: name is already used by an existing object

    Could you please tell me how to deal with this kind of mistake?

    Thank you!

    Published by: 870587 on July 6, 2011 02:57

    Hello
    After following with development the following should work too-

    1 use the migration option to capture and convert the SQL * database server
    2. go in SQL * Developer and remove the converted model.
    3. click on the captured model and choose 'convert '.
    4. click Next and you should get a screen with 2 options - "Data Type Mapping" and "Object Naming.
    5. choose "Object Naming" and scroll down the list until you see the name of the user who will be transformed. For example, in my test, I got the names scott_test and dbo_test - where the owners are 'scott' and 'dba' and 'test' was the name of the database.
    6. change the name of oner_database to just owner - scott_test to scott
    7 complete the conversion process and the owners must be created without the name of the database.
    8. you should now be able to generate the SQL * scripts of charger with only the name of the owner in the CTL files.

    Please try this and let us know if it works for you.

    Kind regards
    Mike

  • strange problem in shape after the transfer of data from sql server2000

    I successfully transferred data from sql server 2000 to oracle 10 g 2 for my forms application development...

    Now I have when I create a sample form using this data block and run I get an error something like

    1. cannot run the query... don't know why... :(

    2. when I query my table in select * from tblmaterial all values are displayed

    but

    When I run select tblmaterial matid

    He throws me an error like invalid identifier... when the column is actually present in the table...

    its really make me go nuts

    Hello
    You may be similar to this problem...

    SQL> CREATE TABLE "AMD"
      2  ("TID" NUMBER,
      3   "Tname" VARCHAR2(10),
      4   "TLOC" VARCHAR2(10));
    
    SQL> INSERT INTO "AMD"
      2  VALUES (1,'TEST','L1');
    
    SQL> SELECT * FROM "AMD";
    
           TID Tname      TLOC
    ---------- ---------- ----------
             1 TEST       L1
    
    SQL> SELECT Tname
      2  FROM "AMD";
    SELECT Tname
           *
    ERROR at line 1:
    ORA-00904: "TNAME": invalid identifier
    
    SQL> SELECT "Tname"
      2  FROM "AMD";
    
    Tname
    ----------
    TEST
    
    SQL> 
    

    If so, while creating the table remove the double quotes in column names and the name of the table.

    -Clément

  • How to view clob data using sql

    Hello

    In our database we have a table that is to have a column of type CLOB but now I want to display the data using sql select query but it throws error: "unsupported data Type.

    Could someone let me know how to view the clob data using the select query.

    Oracle DB version: 10.2.0.3

    Thank you

    user562223 wrote:
    Hello

    In our database we have a table that is to have a column of type CLOB but now I want to display the data using sql select query but it throws error: "unsupported data Type.

    Could someone let me know how to view the clob data using the select query.

    Oracle DB version: 10.2.0.3

    Thank you

    Usually a to_char (CLOBDFIELD) circled

    HTH,

    FJFranken

Maybe you are looking for