using max in where clause

Hi all

I have data that looks like this.

PLAN_INSTANCE DATE_START, DATE_END TOTALS PLAN

1007 1 20150101 20151231 10

1007 2 20140101 20141231 25

1007 3 20130101 20131231 12

1008 1 20150101 20151231 22

1008 2 20140101 20141231 9

I would like you run a query that displays one line per plan based on max (date_start).

I tried

Select the total plan, t.date_start, date_end,

of t_totals t

where t.date_start= (select max (date_start)

of t_totals

where date_start= t.date_start);

but this does not seem to work that I get all the 5 rows instead of 2 rows.

Can you suggest me how can I change my query?

Here is the information of table

CREATE TABLE T_TOTALS

(

PLAN NUMBER (9) NOT NULL,

PLAN_INSTANCE NUMBER 4 NOT NULL,.

DATE_START NUMBER (8) NOT NULL,

DATE_END NUMBER (8) NOT NULL,

NUMBER (9) TOTALS NOT NULL,.

);

CREATE A UNIQUE I_TOTALS ON T_TOTALS INDEX

(SAK_PUB_HLTH, SAK_SHORT);

CREATE INDEX X_TOTALS_1 ON T_TOTALS

(SAK_PUB_HLTH, DTE_EFFECTIVE);

Thanks in advance,

sqlamateur

Hello

2886750 wrote:

Hi Pam,.

I would like to see the data set that results must be displayed

PLAN DATE_START, DATE_END TOTALS

1007 20150101 20151231 10

1008 20150101 20151231 22

How to modify the query to get the data above?

The solution you provided still shows all lines instead of simply max lines (date_start).

Thank you

Really?  It seems that the Pam should return only 1 package date_start.  Of course, we can not really test without valid CREATE TABLE and INSERT statements, but here's a query using the table scott.emp (which we all have, or can get easily) that uses the same approach:

SELECT ename, deptno, hiredate - you could include all the columns you want

FROM scott.emp m

WHERE hiredate =)

SELECT MAX (s.hiredate)

FROM scott.emp s

WHERE s.deptno = m.deptno

)

ORDER BY deptno

;

This query returns all the 14 rows of table scott.emp; She returned only 3 lines that were later hiredate in each of the 3 deptnos:

ENAME HIREDATE DEPTNO

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

MILLER 10 January 23, 1982

ADAMS 20 23 May 1987

JAMES 30 December 3, 1981

Tags: Database

Similar Questions

  • Can I use truncated and where clause in the statement even?

    Hello guys,.

    Can I use truncated with where clause in the same statement.


    For ex:

    TRUNCATE TABLE my_table WHERE id < 500;

    If not, that you recommend me to overtemperature to the level of high water line?


    By the way
    select * from v$version;
    Oracle Database 11 g Enterprise Edition Release 11.2.0.1.0 - 64 bit Production
    PL/SQL Release 11.2.0.1.0 - Production
    "CORE 11.2.0.1.0 Production."
    AMT for 64-bit Windows: Version 11.2.0.1.0 - Production
    NLSRTL Version 11.2.0.1.0 - Production
    >

    Thank you very much.

    Published by: 970992 on 11:14 13.Ara.2012

    >
    Okay I need an increasing amount of space. However, at the same time I want to recover the spaces empty below HWM. So, I need to use less amount of space I am inserting. Therefore, shouldn't use retractable for this?
    >
    Why waste the effort. Oracle will reuse the space below the HWM when doing inserts. You already said that you insert more lines you delete so all these will be more accustomed to the top.

    The only time where that oracle may not use space is when the space is too small to be useful. It is documented in the doc, I told you
    >
    Understanding recoverable unused space
    Over time, Update, and Delete on objects within a tablespace can create pockets of empty space that individually are not large enough to be reused for the new data.
    >
    Most uses of CONTRACTION I've seen are totally useless. If you follow the doc and run the Advisor of segment it will tell you if there is space that can be retrieved and how much. But, even once, for your use even if case will get you will Oracle while you need to reassign the new extensions, then the HWM will just go still higher.

    If pay you a heavy price in Oracle did a full table scan, update of many blocks from the table, update THE index, impact performance for other users to access the table during the SHRINK and reallocate extensions (maybe even some of the same areas) when you insert more data.

    Do not SHRINK unless you know it's necessary.

  • Using decode in where clause

    Hello

    I need to use decode condition in where clause in such a way that if the respective column is null then consider the corresponding column

    for example

    Fee_dateFee_refund_date
    10 OCTOBER 05
    12 NOVEMBER 05
    11 JANUARY 06
    16 FEBRUARY 06

    I have a request in place as below where p_fee_flag will be passed as either FEES or set to null, I check another condition as if p_fee_flag is null then fee_date must be set to null when comparing fee_refund_date between TRUNC (BKT. ACT_START_DATE) AND TRUNC (BKT. ACT_END_DATE

    SELECT 1

    BUCKET BKT

    WHERE DECODE (P_FEE_FLAG, 'FEES', TRUNC(FS.) FEE_DATE), TRUNC (FS. FEE_REFUND_DATE))

    BETWEEN TRUNC (BKT. ACT_START_DATE) AND TRUNC (BKT. ACT_END_DATE)

    944524 wrote:

    Hello Alberto,.

    Thank you that's what I ask but is it possible to verify the condition even using decode

    Yes, it is possible, but why?

    Just replace the query in this way:

    select *
      from fee_table f
    where exists(select 1
                    from bucket b
                  where decode(:p_fee_flag, 'FEE', f.fee_date, f.fee_refund_date) between b.act_start_date and b.act_end_date)
      and decode(:p_fee_flag, 'FEE', null, f.fee_date) is null;
    

    Kind regards.

    Alberto

  • using aliases in where clause

    I am using the following query as part of my effort to

    Select b.account_number,
    b.restriction_code,
    a.cusip_number,
    a.Symbol,
    c.Shares,
    Round(((c.shares/NULLIF(TO_NUMBER(a.shares_outstanding),0))*100),4) outstanding_share_percent
    br_securities has
    b br_accounts,
    (select account_number, cusip_number, sum (shares) shares
    of br_positions
    where account_number not in (select account_number
    of br_house_accounts)
    Account_number group, cusip_number) c
    where a.cusip_number = c.cusip_number
    and b.account_number = c.account_number
    and b.open_flag = 'Y '.
    and a.security_type. a.security_sub_type ("A01", "B01", "B02", "C02")
    and round(((c.shares/nullif(to_number(a.shares_outstanding),0))*100),4) (> 10
    or round(((c.shares/nullif(to_number(a.shares_outstanding),0))*100),4) < 0
    (or round(((c.shares/nullif(to_number(a.shares_outstanding),0))*100),4) is null)
    outstanding_share_percent desc order;


    Given that the expression round(((c.shares/nullif(to_number(a.shares_outstanding),0))*100),4) occurs again in
    where clause, I tried to use the query as shown

    Select b.account_number,
    b.restriction_code,
    a.cusip_number,
    a.Symbol,
    c.Shares,
    a.shares_outstanding,
    Round(((c.shares/NULLIF(TO_NUMBER(a.shares_outstanding),0))*100),4) outstanding_share_percent
    br_securities has
    b br_accounts,
    (select account_number, cusip_number, sum (shares) shares
    of br_positions
    where account_number not in (select account_number
    of br_house_accounts)
    Account_number group, cusip_number) c
    where a.cusip_number = c.cusip_number
    and b.account_number = c.account_number
    and b.open_flag = 'Y '.
    and a.security_type. a.security_sub_type ("A01", "B01", "B02", "C02")
    and outstanding_share_percent (> 10
    outstanding_share_percent < 0 or
    or outstanding_share_percent is null)
    outstanding_share_percent desc order;

    SQL * more returned this error

    ERROR at line 21:
    ORA-00904: "OUTSTANDING_SHARE_PERCENT": invalid identifier

    Is it possible to modify the query for readability?

    Try this:

    select * FROM
    (SELECT
    b.account_number,
    b.restriction_code,
    a.cusip_number,
    a.symbol,
    c.shares,
    a.shares_outstanding,
    round(((c.shares/nullif(to_number(a.shares_outstanding),0))*100),4) outstanding_share_percent
    from br_securities a,
    br_accounts b,
    (select account_number,cusip_number,sum(shares) shares
    from br_positions
    where account_number not in (select account_number
    from br_house_accounts)
    group by account_number,cusip_number) c
    where a.cusip_number = c.cusip_number
    and b.account_number = c.account_number
    and b.open_flag = 'Y'
    and a.security_type||a.security_sub_type in ('A01','B01','B02','C02'))
    WHERE ( outstanding_share_percent > 10
    or outstanding_share_percent < 0
    or outstanding_share_percent is null)
    order by outstanding_share_percent desc; 
    

    PS: Not tested

  • Size of partition on interval range partitioned Table happens when SYSDATE is used in a Where Clause

    We have tables interval range partitioned on a DATE, with a partition for each day column - very standard and straight out of doc Oracle.

    A 3rd party application queries the tables to find the number of rows based on the date range that is located on the column used for the partition key.

    This application uses the date range specified from the current date - i.e. for last two days would be «...» StartDate > SYSDATE-2 "- but the partition size is irrelevant and the explain command plan shows that each partition is included."

    In presenting the request uses the date in a variable partition size location and query table is obviously much better.

    DB is 11.2.0.3 on RHEL6, and default settings - i.e. nothing that could influence the behavior of the optimizer to something unusual.

    I can't work on why this would be the case. It is very easy to reproduce with cases of simple test below.

    I would be very interested to hear any views on why it's that way and if anything can be done to allow the size of the partition to work with a query including SYSDATE because it would be difficult to get the application code has changed.

    In addition to make a case to change the code, I need an explanation of why query using SYSDATE is not advisable and I know this information.

    (1) create a simple partitioned table

    CREATETABLE part_test
       (id                      NUMBER NOT NULL,
        starttime               DATE NOT NULL,
        CONSTRAINT pk_part_test PRIMARY KEY (id)) 
    PARTITION BY RANGE (starttime) INTERVAL (NUMTODSINTERVAL(1,'day')) (PARTITION p0 VALUES LESS THAN (TO_DATE('01-01-2013','DD-MM-YYYY')));
    

    (2) fill in the rows of the table 1 million distributed among 10 partitions

    BEGIN
        FOR i IN 1..1000000
        LOOP
            INSERT INTO part_test (id, starttime) VALUES (i, SYSDATE - DBMS_RANDOM.value(low => 1, high => 10));
        END LOOP;
    END;
    /
    EXEC dbms_stats.gather_table_stats('SUPER_CONF','PART_TEST');
    

    (3) to query the Table of data from the last 2 days using SYSDATE in paragraph

    EXPLAIN PLAN FOR 
    SELECT  count(*) 
    FROM    part_test
    WHERE   starttime >= SYSDATE - 2;
    

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

    | ID | Operation | Name | Lines | Bytes | Cost (% CPU). Time | Pstart. Pstop |

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

    |   0 | SELECT STATEMENT |           |     1.     3 ×  7895 (1) | 00:00:01 |       |       |

    |   1.  GLOBAL TRI |           |     1.     3 ×            |          |       |       |

    |   2.   RANGE OF PARTITION ITERATOR.           |   111K |   867K |  7895 (1) | 00:00:01 |   KEY | 1048575.

    |*  3 |    TABLE ACCESS FULL | PART_TEST |   111K |   867K |  7895 (1) | 00:00:01 |   KEY | 1048575.

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

    Information of predicates (identified by the operation identity card):

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

    3 - filter("STARTTIME">=SYSDATE@!-2)

    (4) now do the same query, but with SYSDATE - 2 presented as a literal value.

    This query returns the same response but very different cost.

    EXPLAIN PLAN FOR
    SELECT count(*) 
    FROM part_test
    WHERE starttime >= (to_date('23122013:0950','DDMMYYYY:HH24MI'))-2;
    

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

    | ID | Operation | Name | Lines | Bytes | Cost (% CPU). Time | Pstart. Pstop |

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

    |   0 | SELECT STATEMENT |           |     1.     8.   131 (0) | 00:00:01 |       |       |

    |   1.  GLOBAL TRI |           |     1.     8.            |          |       |       |

    |   2.   RANGE OF PARTITION ITERATOR.           |   111K |   867K |   131 (0) | 00:00:01 |   356 . 1048575.

    |*  3 |    TABLE ACCESS FULL | PART_TEST |   111K |   867K |   131 (0) | 00:00:01 |   356 | 1048575.

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

    Information of predicates (identified by the operation identity card):

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

    3 filter ("STARTTIME" > = TO_DATE (' 2013-12-21 09:50 ',' syyyy-mm-dd hh24:mi:ss'))))

    Thank you in anticipation

    Jim

    sysdate is not constant and its value may change from time optimization of execution; but the optimizer can deduce that he will be a known running value and then produce a plan that will make the size of partition running.  This is the meaning of 'KEY' in the column pstart – the partition of departure will be known at run time and the size of partition will take place in order to eliminate the previous partitions.

    Concerning

    Jonathan Lewis

  • How to use DATE in where clause

    I need to select the list of records in a table where the available date is greater than or equal to the current date. This is the structure of the table and
    Select the query that is used to get the list of records
    CREATE TABLE TEMP (ITEM_ID NUMBER(20),ITEM_NAME VARCHAR2(100),CREATION_DATE DATE,AVAILABLE_DATE DATE);
    
    INSERT INTO TEMP (ITEM_ID,ITEM_NAME,CREATION_DATE,AVAILABLE_DATE) VALUES(1,'ITEM1',SYSDATE,SYSDATE);
    INSERT INTO TEMP (ITEM_ID,ITEM_NAME,CREATION_DATE,AVAILABLE_DATE) VALUES(2,'ITEM2',SYSDATE,SYSDATE+10);
    INSERT INTO TEMP (ITEM_ID,ITEM_NAME,CREATION_DATE,AVAILABLE_DATE) VALUES(3,'ITEM3',SYSDATE,SYSDATE-10);
    INSERT INTO TEMP (ITEM_ID,ITEM_NAME,CREATION_DATE,AVAILABLE_DATE) VALUES(4,'ITEM4',SYSDATE,SYSDATE);
    INSERT INTO TEMP (ITEM_ID,ITEM_NAME,CREATION_DATE,AVAILABLE_DATE) VALUES(5,'ITEM5',SYSDATE,SYSDATE+5);
    
    SELECT ITEM_NAME, available_date FROM TEMP WHERE available_date > SYSDATE OR available_date LIKE SYSDATE;
    I get the documents expected but I'm not able to find a condition where I can use * > = * a query as the below a date data type, is
    do not return only documents not expected
    SELECT ITEM_NAME, available_date FROM TEMP WHERE available_date >= SYSDATE ;
    Published by: Barro on March 19, 2012 21:13

    Hello

    Barros says:
    I need to select the list of records in a table where the available date is greater than or equal to the current date. This is the structure of the table and
    Select the query that is used to get the list of records

    CREATE TABLE TEMP (ITEM_ID NUMBER(20),ITEM_NAME VARCHAR2(100),CREATION_DATE DATE,AVAILABLE_DATE DATE);
    
    INSERT INTO TEMP (ITEM_ID,ITEM_NAME,CREATION_DATE,AVAILABLE_DATE) VALUES(1,'ITEM1',SYSDATE,SYSDATE);
    INSERT INTO TEMP (ITEM_ID,ITEM_NAME,CREATION_DATE,AVAILABLE_DATE) VALUES(2,'ITEM2',SYSDATE,SYSDATE+10);
    INSERT INTO TEMP (ITEM_ID,ITEM_NAME,CREATION_DATE,AVAILABLE_DATE) VALUES(3,'ITEM3',SYSDATE,SYSDATE-10);
    INSERT INTO TEMP (ITEM_ID,ITEM_NAME,CREATION_DATE,AVAILABLE_DATE) VALUES(4,'ITEM4',SYSDATE,SYSDATE);
    INSERT INTO TEMP (ITEM_ID,ITEM_NAME,CREATION_DATE,AVAILABLE_DATE) VALUES(5,'ITEM5',SYSDATE,SYSDATE+5);
    
    SELECT ITEM_NAME, available_date FROM TEMP WHERE available_date > SYSDATE OR available_date LIKE SYSDATE;
    

    Both operands for AS are supposed to be strings. Do not try to use a DATE, such as SYSDATE, with AS.

    I get the documents expected but I'm not able to find a condition where I can use * > = * a query as the below a date data type, is
    do not return only documents not expected

    SELECT ITEM_NAME, available_date FROM TEMP WHERE available_date >= SYSDATE ;
    

    It returns the results I expect.
    If you'd say what results you expected, could someone help you wite a query to get.

    Assuming that 1 second elapses between the time "ITEM4" is inserted and that 2nd query execution, then, running, available_date will be 1 second less than SYSDATE, so it shouldn't be included.
    If you want to find lines that are on the same calendar day as SYSDATE or later, then use

    WHERE    available_date >= TRUNC (SYSDATE)
    
  • date = sysdate-18 max in where clause!

    Hi all

    Please,

    My request is I have a lot of mit_date for a perticular stu_name and I wanted to max (mit_date), but this mit_date should not be in the last 18 months of sysdate.

    Here's what I wanted to say as a query

    SELECT MAX (MIT_DATE), STU_NAME FROM USA_MIT_TBL WHERE MIT_DATE < = (SYSDATE-18) - it's what I dream.
    or
    Select mit_date, stu_name from mit_usa_tbl where mit_date =)
    Select MAX (MIT_DATE) in the USA_MIT_TBL UD UD WHERE. MIT_DATE < = (SYSDATE-18))-IS ALSO NOT feasible
    the current data as follows

    I have a stu_name like John Smith, who mit_date as follows
    20/01/2001
    20/01/2002
    20/01/2007
    2008-01-20. This perticular stu_name, I'm looking for the date "20/01/2007" is not with 18 months of sysdate and which is max (mit_date)-01/20/2001 and 01/20/2002.

    Note:-this table has more than one stu_name

    Somone please help me in this.
    appreciate any help.
    SQL> create table t (stu_name,mit_date)
      2  as
      3  select 'John Smith', date '2001-01-20' from dual union all
      4  select 'John Smith', date '2002-01-20' from dual union all
      5  select 'John Smith', date '2007-01-20' from dual union all
      6  select 'John Smith', date '2008-01-20' from dual union all
      7  select 'Another Name', date '2003-01-01' from dual union all
      8  select 'Another Name', date '2007-01-01' from dual union all
      9  select 'Another Name', date '2007-04-01' from dual union all
     10  select 'Another Name', date '2007-07-01' from dual union all
     11  select 'Another Name', date '2007-10-01' from dual
     12  /
    
    Tabel is aangemaakt.
    
    SQL> select stu_name
      2       , max(mit_date)
      3    from t
      4   where mit_date <= add_months(sysdate,-18)
      5   group by stu_name
      6  /
    
    STU_NAME     MAX(MIT_DATE)
    ------------ -------------------
    John Smith   20-01-2007 00:00:00
    Another Name 01-04-2007 00:00:00
    
    2 rijen zijn geselecteerd.
    

    Kind regards
    Rob.

  • How to use a function in a Where Clause?

    Hello
    I have a doubt. If MY_FUNCT is a function that returns a Boolean value, can I use in a where clause clause to write a query like this? :

    Select...
    table one
    where the...
    *...*
    and MY_FUNC (a.field) = true
    *...*
    *;*

    Thank you!

    Published by: Mark1970 on 3.27 lug-2-2010

    But is it possible that the function does not return a Boolean value?

    Yes (as sysdate, substr and instr etc.) you can also use your own function in a where clause clause, as long as it does not return a BOOLEAN value.

    I mean how to use a function in a where clause clause?

    Your own pseudocode seems a good example for me.
    So, just give it a try.

  • where clause to query the hierarchy in the tree Apex area does not

    Hi all

    I am building a tree Menu in Apex 5.0.3 (Oracle 11 g XE), when using when the clause, it does show all the data, but without him, showing all the data.

    I'm trying to populate Tree Menu with data specific to a user successfully logged on.

    (A) table created as HR2. TREE_MENU

    menu_name varchar2 (50).

    number of menu_parent

    number of menu_child

    menu_id varchar2 (12),

    menu_path varchar2 (100),

    number of user_no

    (B) SQL statement for the tree in the Apex region

    Select case when connect_by_isleaf = 1 then 0

    When level = 1 then 1

    else                           -1

    end the status,

    level,

    'Menu_name' as the title,

    NULL as an icon,

    "MENU_CHILD" as a value.

    NULL as ToolTip,

    NULL as link

    to HR2. "" TREE_MENU ".

    -WHERE USER_NO =: P_USERNO

    Start with 'MENU_CHILD' is null

    connect prior "MENU_PARENT" = "MENU_CHILD."

    brothers and sisters of order by 'MENU_ID '.

    Note: also used static value where clause but is not workd.

    that the mistake of me or something else? When using the wizard, she asks (possibly) where clause, this means that when the clause can be used in the SQL statement.

    Help, please.

    Kind regards.

    If you want to prune your tree to show only the elements of the No. 7 usermenu, copy the following code helps you

    select case when connect_by_isleaf = 1 then 0
                when level = 1             then 1
                else                           -1
           end as status,
           level,
           "MENU_NAME" as title,
           null as icon,
           "MENU_CHILD" as value,
           null as tooltip,
           null as link
    from HR2."TREE_MENU"
    start with "MENU_CHILD" is null
    connect by
        prior "MENU_PARENT" = "MENU_CHILD"
        and "USER_NO" = 7
    order siblings by "MENU_ID"
    

    Concerning

    Mahmoud

  • several instructions box in where clause

    Hello

    I'm trying to create report filters using substitution variables in a case statement in a where clause clause. I have several instructions box running on the same column. I don't really know how to write it. Here's what I have so far but I know I'm going in the wrong direction.

    where

    i.compute_zone =

    case

    When: P14_zone is NOT NULL

    then: P14_zone

    of other i.compute_zone

    end

    AND

    i.compute_zone =

    case

    When: P14_zone_2 is NOT NULL

    then: P14_zone_2

    other: P14_zone

    end

    AND

    i.compute_zone =

    case

    When: P14_zone_3 is NOT NULL

    then: P14_zone_3

    of other i.compute_zone

    end

    AND

    i.compute_zone =

    case

    When: P14_zone_4 is NOT NULL

    then: P14_zone_4

    of other i.compute_zone

    end

    any suggestions?

    Thank you

    MICAH

    Hi, Micah,

    User12611868-Oracle wrote:

    Hello

    I'm trying to create report filters using substitution variables in a case statement in a where clause clause. I have several instructions box running on the same column. I don't really know how to write it. Here's what I have so far but I know I'm going in the wrong direction.

    where

    i.compute_zone =

    case

    When: P14_zone is NOT NULL

    then: P14_zone

    of other i.compute_zone

    end

    AND

    i.compute_zone =

    case

    When: P14_zone_2 is NOT NULL

    then: P14_zone_2

    other: P14_zone

    end

    AND

    i.compute_zone =

    case

    When: P14_zone_3 is NOT NULL

    then: P14_zone_3

    of other i.compute_zone

    end

    AND

    i.compute_zone =

    case

    When: P14_zone_4 is NOT NULL

    then: P14_zone_4

    of other i.compute_zone

    end

    any suggestions?

    Thank you

    MICAH

    How get the desired results of your data depends on the desired results and your data.  What do you do?  (I can't tell just by looking at the aat code that do not do).  After CREATE TABLE and INSERT statements for some sample data and a couple of sets of parameters (variables such as p14_zone) and the exact results that your choices in each set of parameters, given the same sample data.

    Check out the Forum FAQ: Re: 2. How can I ask a question on the forums?

    CASE expressions are generally not useful in a WHERE clause.  CASE expressions are a convenient way to IF-THEN-ELSE logic of the places where you can't do anything other than (the SELECT clause), but WHERE the clauses allow IF-THEN-ELSE logic in any case

    Maybe you want something like

    WHERE MERGE (: p14_zone,: p14_zone_2,: p14_zone_3,: p14_zone_4) IS NULL

    OR IN i.compute_zone (: p14_zone,: p14_zone_2,: p14_zone_3,: p14_zone_4)

    It returns TRUE if all 4 parameters are set to NULL.  If 1 or more of them are not NULL, then it will return TRUE if i.compute_zone is equal to one of them.

  • ORA-01722: number not valid only in where clause

    I'm getting ORA-01722: invalid number when you use to_number in where clause:
    select to_number(txt_field) from tbl where 100>=to_number(txt_field);
    I don't get it without where clause (300 rows returned):
    select to_number(txt_field) from tbl;
    No logical explanation?

    Published by: totalnewby on February 21, 2012 04:54

    totalnewby wrote:
    No logical explanation?

    Let me guess - tbl is actually a point of view:

    SQL> create table tbl(code varchar2(10),val varchar2(10));
    
    Table created.
    
    SQL> insert into tbl values('string','abc');
    
    1 row created.
    
    SQL> insert into tbl values('number','123');
    
    1 row created.
    
    SQL> create or replace
      2    view tbl_vw
      3      as
      4        select  *
      5          from  tbl
      6          where code = 'number'
      7  /
    
    View created.
    
    SQL> select  to_number(val)
      2    from  tbl_vw
      3  /
    
    TO_NUMBER(VAL)
    --------------
               123
    
    SQL> select  to_number(code)
      2    from  tbl_vw
      3    where to_number(val) > 100
      4  /
    select  to_number(code)
            *
    ERROR at line 1:
    ORA-01722: invalid number
    
    SQL> 
    

    Reason is first selection from the first filters view (find out where clause) out of the non-numeric values and then only apply to_number. When the second select is run optimizer merges the view definition and where would adopt becomes:

    where where code = 'number '.
    and to_number (val) > 100

    Since there is no order of predicate, it happens that to_number (val) > 100 is applied first, and he obviously fails.

    SY.

  • CASE in a WHERE clause: ORA-00905

    Hello world
    I try to use a BOX in a WHERE clause clause and I got the error ORA-00905: lack of keyword. Here is my code:

    SELECT id_reserv,
    Concat (name, Concat ('_', index)) as name,
    Libelle,
    num_lot,
    resa_keyword1,
    resa_keyword2,
    resa_keyword3,
    resa_keyword4,
    date_creation,
    comm_creation,
    id_util,
    assets,
    id_env_act,
    (SELECT connection of user u where u.id_util = r.id_util) AS nom_util,
    (SELECT name e environment where e.id_env = r.id_env_act) AS nom_env,
    (SELECT count (*) of rc reserv_comp where rc.id_reserv = r.id_reserv) AS nbComp,
    (SELECT count (*) MC reserv_modif where mc.id_reserv = r.id_reserv) AS nbModif
    BOOKING r
    WHERE the r.nom NOT LIKE 'RESERV_LOT_ % '.
    AND id_util = '1'
    AND active = '1'
    AND id_env_act > = '-1'.
    AND the MATTER sansdemande
    WHEN true THEN id_reserv not in some id_reserv from demande_livraison where id_env_dep > = '-1'.
    ELSE id_reserv = id_reserv
    END
    AND name LIKE '% '.
    ORDER BY date_creation;

    I already looked at the CASE statement and it seems that the syntax is correct, so I don't know I can use in a WHERE clause.
    Any help would be nice!

    Andalusians

    Hello

    It should be something like this:

    AND CASE
      WHEN 'false'='true' THEN (select id_reserv from demande_livraison where id_env_dep>='-1')
      ELSE id_reserv
         END = id_reserv 
    

    The subquery must return a line

    But I think that it is better to write your query in the form:

    SELECT id_reserv,
         concat(nom,concat('_',indice)) as nom,
         libelle,
         num_lot,
         resa_keyword1,
         resa_keyword2,
         resa_keyword3,
         resa_keyword4,
         date_creation,
         comm_creation,
         id_util,
         actif,
         id_env_act,
         (SELECT login from utilisateur u where u.id_util=r.id_util) AS nom_util,
         (SELECT nom from environnement e where e.id_env=r.id_env_act) AS nom_env,
         (SELECT count(*) from reserv_comp rc where rc.id_reserv=r.id_reserv) AS nbComp,
         (SELECT count(*) from reserv_modif mc where mc.id_reserv=r.id_reserv) AS nbModif
    FROM reservation r
         WHERE r.nom NOT LIKE 'RESERV_LOT_%'
         AND id_util='1'
         AND actif='1'
         AND id_env_act>='-1'
         AND
                       (
                          (  'false'='true' and id_reserv not in (select id_reserv from demande_livraison where id_env_dep>='-1')
                          )
                          or
                          ( 'true'= 'true' and id_reserv=id_reserv
                          )
                       )
         AND nom LIKE '%'
    ORDER BY date_creation;
    

    This coding in SQL something as if a = b, then c, d also in the simplest form.

    Herald tiomela
    http://htendam.WordPress.com

  • several conditional parameters in where clause

    How to select records in a table, when I say emp, a parameter for where the condition as
    If empno is known then use in where clause,
    If ename is known then use ename in where clause,
    If both are known then use two parameters in where clause.

    I use Oracle 9i.
    psuedo code.
    ...
    if p_empno is not null and p_ename is not null then
    select * from emp 
    where empno=p_emp and ename = p_ename ;
    elsif p_empno is null and p_ename is not null then
    select * from emp 
    where ename = p_ename ;
    elsif p_empno is not null p_ename is null then
    select * from emp 
    where empno=p_emp ;
    else 
    select * from emp 
    end if ;
    ...
    How can I do this in sql and plsql


    Thank you

    Published by: user10999538 on July 15, 2009 03:29

    Solution

    SELECT *
    FROM   emp
    WHERE  Nvl(empno,-9999) = Coalesce(p_empno,empno,-9999)
           AND Nvl(ename,'X') = Coalesce(p_ename,ename,'X')
    /
     
    

    SS

  • long data type in where clause

    Hello

    I need to select in an Oracle table with a column having
    Long data type and Oracle does not use this column in the
    WHERE clause. Doco Oracle suggests that the columns with LARGE data types may
    only be used in the select statement (not in the CASE or any other
    conditional clause).

    Anyone know any work around for this problem?

    What is the way to see the value of long data type?

    How can we use them in where clause?

    We can all utility to display the specified records? There are Lakes of records...


    I ned use it in where clause...

    Thanks and greetings
    VD

    Published by: vikrant dixit on November 23, 2008 22:47

    So can OK we use CLOB/BLOB in where clause?

    Yes, you can query the LOB columns using the package (F.ex.SUBSTR or INSTR) DBMS_LOB functions.

    Why u say that the result is FTS?

    This would result in FTS as you cannot index LARGE data or create a function based on the column index.

    http://download.Oracle.com/docs/CD/B19306_01/server.102/b14200/sql_elements001.htm#sthref149


    LONG columns cannot be indexed.

    No other way to my knowledge.

    You can convert the LOB column with function TO_LOB as described in

    http://download.Oracle.com/docs/CD/B19306_01/server.102/b14200/functions185.htm#i79464

    Published by: Sissi Kandi on November 24, 2008 12:28

  • Max value return value despite where clause is not not really looking for the reason

    Hello

    Using oracle 11.2.0.3

    Select 'A '.

    of the double

    where 1 = 2

    No returns no row, as expected

    Select max ('A')

    of the double

    where 1 = 2 - that is to say isn't true

    Returns the line with null

    Why is this?

    Thank you

    Hello

    user5716448 wrote:

    Hello

    Thanks for the reply.

    I guess that's rule ageneral wait even if no line because of the place where the condition.

    where 1 = 2 - that is to say isn't true

    Thank you

    Yes, if there are no rows in the table, or no line meets the conditions in the WHERE clause, it would be logical that the query return lines exactly 0, but it is also logical that the query returns exactly 1 row.  People who have designed SQL apperantly thought return 1 row was most helpful.

    If you want the query to return 0 rows when there is no data, you can GROUP BY a constant.  For example:

    SELECT MAX (hiredate) AS last_hitedate

    FROM scott.emp

    WHERE deptno = & deptno_wanted

    GROUP 0

    ;

    If there are any rows that meet the condition in the WHERE clause, then the query will produce 1 row, but if no rows meet this condition, then the query will return 0 rows.

    Personally, I'm happy that they designed this way SQL.  I find it rather convenient that the aggregates without GROUP BY clause will return 1 row.  I do things like

    SELECT COUNT (*)

    FROM table_x;

    often, when I want to know if a table has been filled or not.  Seeing a 0 for the output is much clearer that does not get any results.

Maybe you are looking for

  • Cradle mobile apple iphone 5s not recognized

    good evening to all of you that I would like someone I contribute in the next problem... .i bye recently an iphone 5s drives 16 gb with that when I plug in for my first time in the pc I have windows 10 actually regularly their installation and pc for

  • Apple vs spotify music

    my free trial music apple sold out, ive been very fluid I should use my 2 year old free spotify or use an mp3 youtube converter to continue using the application of apple's music. I prefer the apple's music software, but sometimes itunes synchronizat

  • I don't know what to do with what you send me

    you keep sending me Email and I don't know what to do with it.

  • Is "qwerty" supposed to be listed as a user on my computer?

    My site has been infected by the virus exploit Blackhole Kit,. Coincidentally, I got some weird messages.  One was a site asking if we could download a code on my computer - to use in a downtime - I said no! Another was when I started my computer, th

  • where can I find the copy paste, print, menu etc. on Windows 7?

    Windows XP had the drop top with the options, but I was not able to find it in Win7. How can I access the copy, print preview, edit, etc. on Win7?  Help, please!