How to write a SQL for this condition?

I have a table with columns v_sub, v_visit, and T_DATE and the structure is like this

v_sub v_visit T_DATE
1 visit-1 01 - mar - 09
1 visit-2 05 - mar - 09
1 visit-3 17 - mar - 09
2-visit-1-4 February 09
2 visit-2 12 - mar - 09
2 visit-3 20 - mar - 09

I want to write a query that control weather it is in chronological order or not. (for v_sub, v_visit and T_DATE must be in chronological order as above)

and I want to check the condition as below:

v_sub v_visit T_DATE
1 visit-1 01 - mar - 09
1 visit-2 5 February 09
1 visit-3 17 - mar - 09
2-visit-1-4 February 09
2 visit-2 12 January 09
2 visit-3 20 - mar - 09


Thanks in advance

Use the LAG function to get previous date

SQL> -- sample data
SQL> with t
  2  as
  3  (
  4     select 1 v_sub, 'visit-1' v_visit, to_date('01-mar-09','dd-mon-yy') v_date from dual union all
  5     select 1, 'visit-2', to_date('05-mar-09','dd-mon-yy') from dual union all
  6     select 1, 'visit-3', to_date('17-mar-09','dd-mon-yy') from dual union all
  7     select 2, 'visit-1', to_date('04-feb-09','dd-mon-yy') from dual union all
  8     select 2, 'visit-2', to_date('12-mar-09','dd-mon-yy') from dual union all
  9     select 2, 'visit-3', to_date('20-mar-09','dd-mon-yy') from dual
 10  )
 11  -- end of sample data
 12  select v_sub, v_visit, v_date, lag(v_date) over(partition by v_sub order by v_visit, v_date) v_previous_date
 13    from t
 14  /

     V_SUB V_VISIT V_DATE    V_PREVIOU
---------- ------- --------- ---------
         1 visit-1 01-MAR-09
         1 visit-2 05-MAR-09 01-MAR-09
         1 visit-3 17-MAR-09 05-MAR-09
         2 visit-1 04-FEB-09
         2 visit-2 12-MAR-09 04-FEB-09
         2 visit-3 20-MAR-09 12-MAR-09

6 rows selected.

Now you can check if the previous_date is less than T_DATE

Tags: Database

Similar Questions

  • How can I write the SQL for this result?

    Hello my dear,
    Here first of all the script.
    CREATE TABLE ACC_TEST(
    AD_ID NUMBER,
    AD_NAME VARCHAR2(50),
    AD_SPM_ID NUMBER);
    /
    the data are
    Insert into ACC_TEST (AD_ID,AD_NAME,AD_SPM_ID) values (136,'Saleh Ahmed',129);
    Insert into ACC_TEST (AD_ID,AD_NAME,AD_SPM_ID) values (142,'Hamidur Rahman',136);
    Insert into ACC_TEST (AD_ID,AD_NAME,AD_SPM_ID) values (124,'Jasim Uddin',null);
    INSERT INTO ACC_TEST (AD_ID,AD_NAME,AD_SPM_ID) VALUES (129,'Sazib',124);
    I need the following result, when passing a value of Ad_Id. For example I go to 142 then result must be
    Select Ad_Id,Ad_Name
    From..
    ...
    where ad_id=142
    
    Ad_Id   Ad_Name
    136     Saleh Ahmed
    129     Sazib
    124     Jasim Uddin
    
    If I Pass Ad_Id=136 Then Result Should Be
    Ad_Id   Ad_Name
    129     Sazib
    124     Jasim Uddin
    
    
    If I Pass Ad_Id=129 Then Result Should Be
    Ad_Id   Ad_Name
    124     Jasim Uddin 
    Database 10 G XE

    Any help will be useful

    Hello

    HamidHelal wrote:
    WoW! You are totally my point. How do you understand that? l

    Guess luck. Guessing is generally not the best way to solve problems. It is generally faster and more reliable to say exactly what you want, as well as give an example.

    fallen little more that I want to know, if I want to restrict the output not more then 2, which would be sql?

    Now, you're not even giving for example!
    Maybe you want something like this:

    SELECT     ad_id
    ,     ad_name
    FROM     acc_test
    WHERE     LEVEL     BETWEEN 2 AND 3          -- Changed
    START WITH     ad_id     = :target_ad_id
    CONNECT BY     ad_id     = PRIOR ad_spm_id
    ;
    

    which will show just the mother and grandmother of the given line.

    I work with the developer of forms a lot. Knowledge of SQL is like sql oracle (9i cerfitication) book. But this type of sql is not available here.
    where can I learn this type of sql? SQL different then regular?

    Certification is another matter entirely.
    There are books and web sites dealing with more advanced techniques. Sorry, I don't know any good enough to recommend. Some authors (such as Tom Kyte) are always good.

    Here are a few sites that explain CONNECT BY queries:
    http://www.adp-GmbH.ch/ora/SQL/connect_by.html
    http://www.oradev.com/connect_by.jsp

  • How to build a SQL for this logic.

    Hello

    I have a requirement like, I need to derive a column with a calculation.

    Here is the psedocode,

    number of occurrence (deptno = 10) / count (deptno = 10)

    Expected result:

    If the deptno = 10 occurs 3 times in the table, then at the time of fist should be the calcuation

    second 1/3, 2/3 of the time, the third time 3/3 and so on.

    How can I build in a sql query. Help, please.

    Any help will be appreciated with points.

    Thank you.

    Try this.

        SELECT LEVEL / CNT CALC
          FROM (SELECT COUNT (*) CNT
                  FROM EMP
                 WHERE DEPTNO = 10)
    CONNECT BY LEVEL <= CNT
    

    G.

  • How to write a query for this data?

    In this table, I dob column that is of type DATE, for example, 12 January 89 I want output like this

    Day of the year

    January 12, 89 Thusrs_day

    January 12, 90 Friday

    up to

    12 January 14 Sunday

    Select add_months (d, ((level*12)-12)), to_char (add_months (d, ((level*12)-12)), 'DY')

    de)

    Select d double to_date('12-jan-89','dd-mon-rr')

    )

    connect by level<=>

    ----------------------------- ------------------------------

    12.01.1989 GAME

    12.01.1990 FRI

    12.01.1991 SAT

    12.01.1992 SUN

    12.01.1993 MAR

    12.01.1994 SEA

    12.01.1995 GAME

    12.01.1996 FRI

    12.01.1997 SUN

    12.01.1998 LUN

    MAR 12.01.1999

    12.01.2000 SEA

    12.01.2001 FRI

    12.01.2002 SAM

    12.01.2003 SUN

    12.01.2004 LUN

    12.01.2005 WED

    12.01.2006 GAME

    12.01.2007 FRI

    SAM 12.01.2008

    12.01.2009 LUN

    12.01.2010 MAR

    12.01.2011 WED

    12.01.2012 GAME

    24 selected lines

    ----

    Ramin Hashimzade

  • How to write a script for date get to the Clipboard

    Hi experts,

    How to write a script for date get to the Clipboard.

    the date format will be like this:

    05 - may

    respect of

    John

    Thanks guys, thanks Sanon

    I finally use the .bat doc

    like this:

    @@echo off
    for /f "delims =" % in (' wmic OS Get localdatetime ^ | find ".") "") Set "dt = %% a"
    the value "YYYY = % dt: ~ 0, 4%.
    the value "MM = % dt: ~ 4, 2%.

    If MM % is 01 set MM = January
    If % MM == 02 set MM = February
    If MM % is MM value = March 03
    If MM % is 04 MM value = April
    If MM % is 05 MM value = may
    If MM % is 06 MM value = June
    If MM % == 07 set MM = July
    If MM % is MM value = August 08
    If MM % is MM value = September 09
    If MM % is 10 MM value = October
    If MM % is 11A set MM = November
    If MM % is game MM 12 = December

    the value "DD = % dt: ~ 6, 2%.
    the value "HH = % dt: ~ 8, 2%.
    the value "Min = % dt: ~ 10, 2%.
    Set "s = % dt: ~ 12, 2%.

    Echo DD - MM HH % %% % Min | Clip

    It works

    respect of

    John

  • How to write a query for the given scenario?

    Hi all

    I have two tables EMP, DEPT with data below.
    EMP TABLE: -.
    SAL DEPID EMPLOYMENT ENAME EID
    111 RAM 1500 10 MANAGER
    222 SAM ASST MANAGER 2000 20
    KALA 333 2500 10 REGISTRAR
    444. BIMA 20 3000 MANAGER
    CHALA 555 MANAGER 3500 30
    RANI 666 ASST MANAGER 4000 10
    777 KAMAL MANAGER 2400 10

    DEPT TABLE: -.

    DEPID DNAME
    XX 10
    AA 20
    30 ZZ

    Q1: I want the sum of the salary of each Department and for the particular job. Here, in each Department manager, Assistant Manager, clerk positions are there.
    I want to display the result as below...
    10-20-30 JOBS
    --------------------------------------------------------------------------
    MANAGER OF 3900 3000 3500
    ASST MANAGER 4000 2000 NULL
    THE CLERK 2500 NULL NULL

    Please tell me how to write a sql query?

    Thank you
    SAI
    SQL> SELECT job,
      2         MAX(DECODE(deptno, 10, sum_sal)) "10",
      3         MAX(DECODE(deptno, 20, sum_sal)) "20",
      4         MAX(DECODE(deptno, 30, sum_sal)) "30"
      5  FROM (SELECT deptno, job, SUM(sal) sum_sal FROM emp GROUP BY deptno, job)
      6   GROUP BY job;
    
    JOB               10         20         30
    --------- ---------- ---------- ----------
    CLERK           1300       1900        950
    SALESMAN                              5600
    PRESIDENT       5000
    MANAGER         2450       2975       2850
    ANALYST                    6000
    
    SQL> 
    
  • How to write a query for the data exchange between two columns?

    How to write a query for the data exchange between two columns?

    I tried a request, does NOT work.
    update tmp t1 set t1.m1=t1.m2 and t1.m2=(select t2.m1 from tmp t2 where t2.student_id = t1.student_id)
    Thank you.

    Published by: user533361 on October 23, 2009 14:04

    Just plain and simple:

    update tmp t1
     set t1.m1=t1.m2,
         t1.m2=t1.m1
    /
    

    SY.

  • How to write an if else logical expression for this condition

    Hai All

    I wrote some other logic if for my condition

    If the weather is betweeb 0145 and 0630 and timeout is a column in a table is null that updated sysdate-1
    If the time is bet0645 and 0900 then timein is null then insert into timein in a table named dail_att
    and time bet 1130 and 1300 then if barcode null then insert timein else update timeout
    and bet 1645 and1730 then if null of barcodes and timein null then insert timein or update expiration time

    It comes to my condition to generate presences. Ive tried a logic and pls tell executed case faster than logical ifelse
    and give me a soluton



    This is my encoding

    If: bartime between 0145 and 0630 and: barcode is not null then
    Update dail_att
    the value of timeout =: bartime
    where = barcode: barcode
    and timein is not null
    and ATTEND_DATE =: BARDATE-1;
    otherwise if: bartime between 0645 and 0900 and: bar code is null then
    insert into dail_att (barcode, timein, attend_date)
    values(:Barcode,:Bartime,:bardate);

    otherwise if: bartime between 1145 and 1300 and: bar code is null then
    insert into dail_att (barcode, timein, attend_date)
    values(:Barcode,:Bartime,:bardate);
    on the other
    Update dail_att
    the value of timeout =: bartime
    where = barcode: barcode
    and ATTEND_DATE =: BARDATE;
    end if;
    otherwise if: bartime between 1645 and 1730 and: bar code is null then
    insert into dail_att (barcode, timein, attend_date)
    values(:Barcode,:Bartime,:bardate);
    on the other
    Update dail_att
    the value of timeout =: bartime
    where = barcode: barcode
    and ATTEND_DATE =: BARDATE;
    end if;
    Another thing: bartime > 1730 and: bar code is null then
    Update dail_att
    the value of timeout =: bartime
    where = barcode: barcode
    and ATTEND_DATE =: BARDATE;
    end if;
    WHEN THE OUTPUT: SYSTEM. LAST_RECORD = "TRUE" OR: BAR CODE IS NULL;
    NEXT_RECORD;
    forms_ddl ('commit');
    end loop;



    Concerning

    Srikkanth.M

    1 your code has bugs... the word "else if" should be elsif

    and in what format you store time?

  • I am a newbie to this and I was wondering how to write CSS Code for text, text color, size text and text effects...

    Please keep in mind that I am a newbie. For the moment, I don't know how to write code, so I use Muse to try to build a Web site that is not super fancy writing know, then improve or build or create a new site later. I called Adobe and they said someone in the forums may know how to write code and that he could help me. That said, if someone could answer my questions and help me somehow, I'd be really grateful.

    1. How can I write code, what code should I write to

    Text?

    Color of the text?

    Text size?

    Text/Font Style

    Text effects (such as fly in or fly out, bounce in and out bounce, spin in and spin out, fade in and fade out, etc)?

    Direction of text effect?

    Time that the effect occurs, if it is applied, initiated, comes in? example fly in or fade?

    Time than the last.

    After the effect stops, turns off? example: fly away or fade out?

    2. how to enter the code Muse?

    3. should I have a program to write the code?

    4. is there a function I could use on my computer, such as Notepad, to write the code, copy and paste it into Muse?

    5. What is the best book or books to learn how to write css, code and everything I need to do to be able to do the above and more?

    6. is there anything else I need to know or do to be able to do these things with the text?

    You can find more information on the style of your text (paragraphs, titles, etc. in some of the tutorials here: TUTORIALS |) Adobe Muse CC

    You can also check out tutorials on this page for scrolling effects that can do some of the other effects you need. Without exactly knowing your use case it's hard to give specific advice.

    Basically if it is not covered undressed under the text, scrolling Effects tutorials or in a third party widget already made, then you will need to create it yourself using CSS. You will need to use some sort of browser Firebug like developer tools in Firefox to find the ID of the element you want to add the CSS to write the CSS code and insert it into the head section of the page properties.

  • How to write the SQL without using dynamic SQL?

    How can you write this under SQL without using execute immediately?

    You can use static SQL (using something like a CASE statement)?

    test procedure (one in varchar2, b number, each number) is

    v_num_recs pls_integer;

    Start

    Select count (*)

    in v_num

    FROM table1

    where

    col1 = one and

    If b is not null then col2 = b

    If c is not null then col3 = c;

    / * i.e. If b is not null, where condition to add this line only. If c is not null, where condition should add only this line. OR condition No. it * /.

    dbms_output.put_line (v_num);

    end;

    Or should I use dynamic SQL statements for this?

    I was wondering if the two are NOT NULL? Well check this.

    Select count (*) in v_num

    table

    where col1 = one

    and col2 = (CASE WHEN (b is not null) THEN b ELSE END col2)

    and col3 = (CASE WHEN (c is not null) THEN ELSE END col3 c)

  • How to write the function for bottom of logic...

    I have a picture in the picture I have a records like below
    code value
    1 20
    2 null
    3 30
    4 null
    5 40
    6 null


    The output need to have the values displayed in the fields first, followed by the NULL values as below

    code value
    1 20
    2 30
    3 40
    4 null
    5 null
    6 null

    I think the suspicion is that the values must be concatenated with the replacement of NULL values with (;).
    as
    20, 30, 40
    Get single values and insert into the table...

    for this I don't know how to write function for who can please help me on these...

    Published by: Claudia on June 22, 2011 22:45

    Published by: Claudia on June 22, 2011 22:51

    Create or replace function test_fun
    is
    cursor fun1 is the value select from table by CSA 2;-"this select query will return 20,30,40, null, null, null".
    v_out varchar2 (20);
    Start
    fun1 open cursor;
    loop
    extract the fun1 in v_out;
    dbms_output.put_line (v_out); - this is the command display output on the screen.
    output when fun1 not found %;
    end loop;
    close;
    end;

    Please try it and tell us if no query...

    Thank you
    IQ

  • How to write the Sql values in the Application Engine

    I am trying to write dynamic sql statements in AE
    E.g.
    I have SQL n Action
    DELETE
    OF %Table(%BIND(RECNAME))
    WHERE Bind (FIELDNAME) = % (AMIT_VALUE_TO_DEL) Bind %;

    at runtime, it converts into

    DELETE
    OF PS_TABLE1
    WHERE 'EMPLID '=' AJ23;

    THE QUOTATION MARK SINGLE AROUND EMPLID IN WHERE CLAUSE CREATES PROBLEM BCZ, IT IS A COLUMN NAME, IS THERE A SOLUTION THIS?

    Add NOQUOTES to your % Bind(). For example, % (FIELDNAME, NOQUOTES) Bind.

    I must warn you however. When generating dynamic SQL, make sure that you have absolute control over the input text used to generate the SQL statement. For example, make sure that you, not a user, determines the value of RECNAME and FIELDNAME. It is too easy to create a SQL Injection flaw in converting the values entered by the user in SQL statements.

  • How to write the sql command in the batch file

    Jin
    I m using oracle 9i on windows. I want to know how I can write the sql on the command file command. I want to use a batch file to use the sql like shutdowm command & start command and any select command too.
    so please tell how can I do?


    thxs

    Tried looking for what he superdba.

    Can't connect to sqlplus using sys using a batch file

    HTH
    Aman...

  • How to write the trigger for update or delete multiple columns in a table?

    Hello

    I create one in the form of table of sample_emp. In that, every time I want to change of name, team_id, team_leader_id, supervisor_id, manager_id it must store the update-able and old values of those in the job_history table. When I write the trigger for which it shows "ORA-04082 new or the old value not table level triggers. Here is my emp_table. My table also similar job_history like this. Need your help.

    Header 1 Header 2 Header 3 Header 4 Header 5 Heading 6 Heading 7 8 header Header 9
    EMP_IDEMP_NAMEDESIGNATIONTEAM_IDTEAM_LEADER_IDEMPLOYEEMANAGER_IDHIRE_DATERELIEVED_DATE

    --

    Thank you.

    Hi Joel,.

    Venky_prs wrote:

    Hello

    I create one in the form of table of sample_emp. In that, every time I want to change of name, team_id, team_leader_id, supervisor_id, manager_id it must store the update-able and old values of those in the job_history table. When I write the trigger for which it shows "ORA-04082 new or the old value not table level triggers. Here is my emp_table. My table also similar job_history like this. Need your help.

    Header 1 Header 2 Header 3 Header 4 Header 5 Heading 6 Heading 7 8 header Header 9
    EMP_ID EMP_NAME DESIGNATION TEAM_ID TEAM_LEADER_ID EMPLOYEE MANAGER_ID HIRE_DATE RELIEVED_DATE

    --

    Thank you.

    You can try something like this given below to complete the historical table on update and delete.

    create or replace TRIGGER  "CLONE_EMP_UPDATE_DELETE"
    BEFORE DELETE OR UPDATE ON EMP
    FOR EACH ROW
    DECLARE
        PRAGMA autonomous_transaction;
    BEGIN
    INSERT INTO JOB_HISTORY("EMP_ID","EMP_NAME","DESIGNATION","TEAM_ID","TEAM_LEADER_ID","SUPERVISOR_ID","MANAGER_ID"
    ,"HIRE_DATE","RELIEVED_DATE")
      VALUES(:OLD.EMP_ID,:OLD.EMP_NAME,:OLD.DESIGNATION,:OLD.TEAM_ID,:OLD.TEAM_LEADER_ID,:OLD.SUPERVISOR_ID,
    :OLD.MANAGER_ID,:OLD.HIRE_DATE,
    :OLD.RELIEVED_DATE);
    COMMIT;
    END;
    

    Hope this helps you,

    Kind regards

    Jitendra

  • How to run wfstatus.sql for i-expenses

    How to run wfstatus.sql i-expenses in EBS 11i.

    Select this check box, hope it helps.

    How to find and solve the reports on Internet expenditures within workflow [ID 185004.1]

    How wfstatus.sql to run on a statement of expenses Internet [134895.1 ID]

Maybe you are looking for

  • No games (solitaire, etc.) on xp pro

    Just bought a new computer pre-loaded with xp pro. But there is no games. I tried "Add / Remove windows components" but the games option is simply not there. This new computer didn't come with any disks, I had to make my own backups when I got it.How

  • How can I get a program icon for the office high back on the menu drop-down?

    I accidentally pulled the icon 'Windows Explorer' on the menu drop-down 'Programs' (instead of create a shortcut).  Now I can not understand how to get it back.  If I display the menu drop-down then try to sneak back, as soon as I click the icon, the

  • AProblems printing with HP Officejet 6500 a Plus

    I have a new HP Officejet 6500 has more, and have had the following problems, since it was installed. I'm on an iMac computer using Mac OS 10.6.6. The power adapter is plugged into a wall outlet as suggested. I turn my computer off every night. When

  • my windows 8 not boot/restore at all

    My pc won't start not restore does not have options been screwing with it for days uploaded a video to give a better idea of the problem youtube.com/watch? v = Rr579F9OKZ4

  • Impossible to restore the catalog LR6

    I am running LR6 on PC. I accidentally deleted my current catalog, as well as the .lrcat files that allow me to open/reinstall my catalog. I seem to have backup catalogs, but when I try to open them to restore, Lr tells me "no element not correspondi