I need help in sql statement

Hello all,
im working in hr schema
i need sql statement to answer my question
i need all employees information
where average salary = average salary department 50
sorry i can't speak english very well
thank's all

There is no need to do it in two queries. All you need is:
select  *
  from  (
         select  e.*,
                 avg(case department_id when 50 then salary end) over() dept_50_avg_sal
           from  hr.employees e
  where salary <= dept_50_avg_sal
EMPLOYEE_ID FIRST_NAME           LAST_NAME                 EMAIL                     PHONE_NUMBER         HIRE_DATE JOB_ID         SALARY COMMISSION_PCT MANAGER_ID DEPARTMENT_ID DEPT_50_AVG_SAL
        198 Donald               OConnell                  DOCONNEL                  650.507.9833         21-JUN-07 SH_CLERK         2600                       124            50      3475.55556
        199 Douglas              Grant                     DGRANT                    650.507.9844         13-JAN-08 SH_CLERK         2600                       124            50      3475.55556
        115 Alexander            Khoo                      AKHOO                     515.127.4562         18-MAY-03 PU_CLERK         3100                       114            30      3475.55556
        116 Shelli               Baida                     SBAIDA                    515.127.4563         24-DEC-05 PU_CLERK         2900                       114            30      3475.55556
        117 Sigal                Tobias                    STOBIAS                   515.127.4564         24-JUL-05 PU_CLERK         2800                       114            30      3475.55556
        118 Guy                  Himuro                    GHIMURO                   515.127.4565         15-NOV-06 PU_CLERK         2600                       114            30      3475.55556
        119 Karen                Colmenares                KCOLMENA                  515.127.4566         10-AUG-07 PU_CLERK         2500                       114            30      3475.55556
        125 Julia                Nayer                     JNAYER                    650.124.1214         16-JUL-05 ST_CLERK         3200                       120            50      3475.55556
        126 Irene                Mikkilineni               IMIKKILI                  650.124.1224         28-SEP-06 ST_CLERK         2700                       120            50      3475.55556
        127 James                Landry                    JLANDRY                   650.124.1334         14-JAN-07 ST_CLERK         2400                       120            50      3475.55556
        128 Steven               Markle                    SMARKLE                   650.124.1434         08-MAR-08 ST_CLERK         2200                       120            50      3475.55556
EMPLOYEE_ID FIRST_NAME           LAST_NAME                 EMAIL                     PHONE_NUMBER         HIRE_DATE JOB_ID         SALARY COMMISSION_PCT MANAGER_ID DEPARTMENT_ID DEPT_50_AVG_SAL
        129 Laura                Bissot                    LBISSOT                   650.124.5234         20-AUG-05 ST_CLERK         3300                       121            50      3475.55556
        130 Mozhe                Atkinson                  MATKINSO                  650.124.6234         30-OCT-05 ST_CLERK         2800                       121            50      3475.55556
        131 James                Marlow                    JAMRLOW                   650.124.7234         16-FEB-05 ST_CLERK         2500                       121            50      3475.55556
        132 TJ                   Olson                     TJOLSON                   650.124.8234         10-APR-07 ST_CLERK         2100                       121            50      3475.55556
        133 Jason                Mallin                    JMALLIN                   650.127.1934         14-JUN-04 ST_CLERK         3300                       122            50      3475.55556
        134 Michael              Rogers                    MROGERS                   650.127.1834         26-AUG-06 ST_CLERK         2900                       122            50      3475.55556
        135 Ki                   Gee                       KGEE                      650.127.1734         12-DEC-07 ST_CLERK         2400                       122            50      3475.55556
        136 Hazel                Philtanker                HPHILTAN                  650.127.1634         06-FEB-08 ST_CLERK         2200                       122            50      3475.55556
        138 Stephen              Stiles                    SSTILES                   650.121.2034         26-OCT-05 ST_CLERK         3200                       123            50      3475.55556
        139 John                 Seo                       JSEO                      650.121.2019         12-FEB-06 ST_CLERK         2700                       123            50      3475.55556
        140 Joshua               Patel                     JPATEL                    650.121.1834         06-APR-06 ST_CLERK         2500                       123            50      3475.55556
EMPLOYEE_ID FIRST_NAME           LAST_NAME                 EMAIL                     PHONE_NUMBER         HIRE_DATE JOB_ID         SALARY COMMISSION_PCT MANAGER_ID DEPARTMENT_ID DEPT_50_AVG_SAL
        142 Curtis               Davies                    CDAVIES                   650.121.2994         29-JAN-05 ST_CLERK         3100                       124            50      3475.55556
        143 Randall              Matos                     RMATOS                    650.121.2874         15-MAR-06 ST_CLERK         2600                       124            50      3475.55556
        144 Peter                Vargas                    PVARGAS                   650.121.2004         09-JUL-06 ST_CLERK         2500                       124            50      3475.55556
        180 Winston              Taylor                    WTAYLOR                   650.507.9876         24-JAN-06 SH_CLERK         3200                       120            50      3475.55556
        181 Jean                 Fleaur                    JFLEAUR                   650.507.9877         23-FEB-06 SH_CLERK         3100                       120            50      3475.55556
        182 Martha               Sullivan                  MSULLIVA                  650.507.9878         21-JUN-07 SH_CLERK         2500                       120            50      3475.55556
        183 Girard               Geoni                     GGEONI                    650.507.9879         03-FEB-08 SH_CLERK         2800                       120            50      3475.55556
        186 Julia                Dellinger                 JDELLING                  650.509.3876         24-JUN-06 SH_CLERK         3400                       121            50      3475.55556
        187 Anthony              Cabrio                    ACABRIO                   650.509.4876         07-FEB-07 SH_CLERK         3000                       121            50      3475.55556
        190 Timothy              Gates                     TGATES                    650.505.3876         11-JUL-06 SH_CLERK         2900                       122            50      3475.55556
        191 Randall              Perkins                   RPERKINS                  650.505.4876         19-DEC-07 SH_CLERK         2500                       122            50      3475.55556
EMPLOYEE_ID FIRST_NAME           LAST_NAME                 EMAIL                     PHONE_NUMBER         HIRE_DATE JOB_ID         SALARY COMMISSION_PCT MANAGER_ID DEPARTMENT_ID DEPT_50_AVG_SAL
        194 Samuel               McCain                    SMCCAIN                   650.501.3876         01-JUL-06 SH_CLERK         3200                       123            50      3475.55556
        195 Vance                Jones                     VJONES                    650.501.4876         17-MAR-07 SH_CLERK         2800                       123            50      3475.55556
        196 Alana                Walsh                     AWALSH                    650.507.9811         24-APR-06 SH_CLERK         3100                       124            50      3475.55556
        197 Kevin                Feeney                    KFEENEY                   650.507.9822         23-MAY-06 SH_CLERK         3000                       124            50      3475.55556
37 rows selected.
SQL> SY.

Similar Messages

  • Need Help On SQL Statement

    I am using Sybase as my back end database. I need help on my SQL statement regarding datetime. The datetime is store as a 9 digit integer in the database (...I believe it is a Decimal(30,6) format, let me know if I am wrong).
    If I do this, "select * from mytable;" It works out fine (except I don't know what the date means e.g. 998919534)
    If I do this, "select * from mytable where datetime_col < '9/5/2002' ; I got an error. I even tried '9/5/02 11:00 000 am' but it didn't help.
    How do I specify a date or datetime in my query?
    Thanks in advance.

    I execute the sql statement
    select * from mytable where datetim_col < convert(datetime, '3/4/2002', 101) ;
    I got mix result. I got an error message "cannot convert 10375584 to a date.
    Yet, he statistics window of the I-sql says
    "estimated 493 rows in query (I/O estimate 87)
    PLan> mytable (seq)"
    It looks like I my the SQL statement is correct and then the system can't display it in the Data window.
    Any thought ?

  • Need help for sql statement

    Hi,
    Need help to write sql statement.
    create table t_dt ( dt_start date, dt_end date, amount number);
    insert into t_dt values('1-Jan-10','10-Feb-10',12);
    insert into t_dt values('11-Feb-10','10-Mar-10',10);
    insert into t_dt values('11-Mar-10','20-Apr-10',8);
    insert into t_dt values('21-Apr-10','28-Jun-10',10);
    insert into t_dt values('29-Jun-10','20-Sep-10',10);
    insert into t_dt values('21-Sep-10','10-Oct-10',10);
    insert into t_dt values('11-Oct-10','31-Dec-10',8);
    insert into t_dt values('1-Jan-11','10-Feb-11',8);
    insert into t_dt values('11-Feb-11','10-Mar-11',7);
    insert into t_dt values('11-Mar-11','20-Apr-11',6);
    insert into t_dt values('21-Apr-11','28-Jun-11',6);
    insert into t_dt values('29-Jun-11','20-Sep-11',6);
    insert into t_dt values('21-Sep-11','10-Oct-11',4);
    insert into t_dt values('11-Oct-11','31-Dec-11',8);
    Result should be like below..
    dt_start     dt_end     Amount
    1-Jan-10     10-Feb-10     12
    11-Feb-10     10-Mar-10     10
    11-Mar-10     20-Apr-10     8
    21-Apr-10     10-Oct-10     10
    11-Oct-10     10-Feb-11     8
    11-Feb-11     10-Mar-11     7
    11-Mar-11     20-Sep-11     6
    21-Sep-11     10-Oct-11     4
    11-Oct-11     31-Dec-11     8
    Just to explain the example, take a row with start date as 21-Apr-10 in the above insert statements, since it has the same amount for next two rows (i.e. with start date '29-Jun-10' and '21-Sep-10') these 3 rows should be converted to represent only 1 row in the result and the start date and end date should be changed per the result shown above.
    Thanks.

    Hello
    I think this gives yuo what you need....
    SELECT
        MIN(dt_start),
        MAX(dt_end),
        amount
    FROM
        (   SELECT
                dt_start,
                dt_end,
                MAX(marker) OVER(ORDER BY dt_start) marker,
                amount
            FROM
                    Select
                        dt_start,
                        dt_end,
                        amount,
                        CASE
                            WHEN LAG(amount) OVER(ORDER BY dt_start) <> amount THEN
                                ROW_NUMBER() OVER(ORDER BY dt_start)
                        END marker
                    from t_dt
    GROUP BY
         amount,
         marker
    order by     
         MIN(dt_start)
    MIN(DT_START)        MAX(DT_END)              AMOUNT
    01-JAN-2010 00:00:00 10-FEB-2010 00:00:00         12
    11-FEB-2010 00:00:00 10-MAR-2010 00:00:00         10
    11-MAR-2010 00:00:00 20-APR-2010 00:00:00          8
    21-APR-2010 00:00:00 10-OCT-2010 00:00:00         10
    11-OCT-2010 00:00:00 10-FEB-2011 00:00:00          8
    11-FEB-2011 00:00:00 10-MAR-2011 00:00:00          7
    11-MAR-2011 00:00:00 20-SEP-2011 00:00:00          6
    21-SEP-2011 00:00:00 10-OCT-2011 00:00:00          4
    11-OCT-2011 00:00:00 31-DEC-2011 00:00:00          8
    9 rows selected.HTH
    David
    Edited by: Bravid on Feb 23, 2012 12:08 PM
    Beaten to it by Frank! :-)

  • Need help on SQL Statement for UDF

    Hi,
    as I am not so familiar with SQL statements on currently selected values, I urgently need help.
    The scenario looks as follows:
    I have defined two UDFs named Subgroup1 and Subgroup2 which represent the subgroups dependent on my article groups. So for example: When the user selects article group "pianos", he only sees the specific subgroups like "new pianos" and "used pianos" in field "Subgroup1". After he has selected one of these specific values, he sees only the specific sub-subgroups in field "Subgroup2", like "used grand pianos".
    I have defined UDTs for both UDFs. The UDT for field "Subgroup1" has a UDF called "ArticleGroup" which represents the relation to the article group codes. The UDT for field "Subgroup2" has a UDF called "Subgroup1" which represents the relation to the subgroups one level higher.
    The SQL statement for the formatted search in field "Subgroup1" looks as follows:
    SELECT T0.[Name] FROM [dbo].[@B_SUBGROUP1]  T0 WHERE T0.[U_ArticleGroup]  = (SELECT $[OITM.ItmsGrpCod])
    It works fine.
    However, I cannot find the right statement for the formatted search in field "Subgroup2".
    Unfortunately this does NOT WORK:
    SELECT T0.[Name] FROM [dbo].[@B_SUBGROUP2]  T0 WHERE T0.[U_Subgroup1]  = (SELECT $[OITM.U_Subgroup1])
    I tried a lot of others that didn't work either.
    Then I tried the following one:
    SELECT T0.[Name] FROM [dbo].[@B_SUBGROUP2]  T0 WHERE T0.[U_Subgroup1] = (SELECT T1.[Code] FROM [dbo].[@B_SUBGROUP1] T1 WHERE T1.[U_ArticleGroup] = (SELECT $[OITM.ItmsGrpCod]))
    Unfortunately that only works as long as there is only one specific subgroup1 for the selected article group.
    I would be sooooo happy if there is anyone who can tell me the correct statement for my second UDF!
    Thanks so much in advance!!!!
    Edited by: Corinna Hochheim on Jan 18, 2010 10:16 PM
    Please ignore the "http://" in the above statements - it is certainly not part of my SQL.
    Please also ignore the strikes.

    Hello Dear,
    Use the below queries to get the values:
    Item Sub Group on the basis of Item Group
    SELECT T0.[Name] FROM [dbo].[@SUBGROUP]  T0 WHERE T0.[U_GroupCod] =$[OITM.ItmsGrpCod]
    Item Sub Group 1 on the basis of item sub group
    SELECT T0.[Name] FROM [dbo].[@SUBGROUP1]  T0 WHERE T0.[U_SubGrpCod]=(SELECT T0.[Code] FROM [dbo].[@SUBGROUP]  T0 WHERE T0.[Name] =$[OITM.U_ItmsSubgrp])
    Sub group 2 on the basis of sub group 1
    SELECT T0.[Name] FROM [dbo].[@SUBGROUP2]  T0 WHERE T0.[U_SubGrpCod1]=(SELECT T0.[Code] FROM [dbo].[@SUBGROUP1]  T0 WHERE T0.[Name] =$[OITM.U_ItmsSubgrp1])
    this will help you.
    regards,
    Neetu

  • Need Help In SQL Statement

    Hi,
    I need to combine many table only in one select statement.
    This is my table name
    - L20060101
    - L20060102
    - L20060103
    - L20060104
    - L20060105
    - L20060131
    - L20060201
    I dont how to do select statement to select all this table
    This is the wrong select statement.
    Select * from like '%L200601%
    Can i do like that?
    Can any body help me on how to solve my problem?

    You could do something like
    create or replace procedure sp_create_monthly_tabs
         (p_month in varchar2) is
    v_sql varchar2(32767);
    v_cnt number:=0;
    begin
    v_sql := null;
    for c1 in (select table_name from user_tables
    ---   put in the condition to select the tables by the month
    ---  passed in as the parameters
                  ) loop
       if v_cnt = 0 then
          v_sql := 'Create or replace VIEW Mon_Tab'||p_month ||
                       'AS Select * from '||C1.Table_Name ||' ' ;
       else
          v_sql := v_sql||'Union All '||'Select * from '||C1.Table_Name ||' ' ;
       end if;
       v_cnt := v_cnt + 1;
    end loop;
    execute immediate(v_sql)
    end;                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   

  • Need Help: Complex SQL statement

    select segment3, 0 "Other Income"
         , 0 "Sales of Services"
         , 0 "Personnel Costs"
         , 0 "Other Staff Cost"
         , 0 "General Admin"
         , 0 "Travel"
         , 0 "Collaborator"
         , 0 "Training"
         , 0 "Capital"     
         , nvl(round(sum (nvl(PERIOD_ACTIVITY_A,0) + nvl(PERIOD_ACTIVITY_E,0) + nvl(PRDOPEN_A,0) + nvl(PRDOPEN_E,0)),0),0) "Overhead"
         , 0 "Finance Charges"
         from PAYMAN.SNP_OP_DETAIL@OFA
         where segment3= x.new_cc and period_name = p_period
         and segment2 in (select acc_t from CRP_REPORT_ACCT_DETAIL_T where group_id = 10)
         group by segment3
    Please I don't understand select segment3, 0 "Other Income"....there's no column called 0, what is the 0s doing in the select statement?
    Secondly, I don't understand from PAYMAN.SNP_OP_DETAIL@OFA

    Hi,
    OlaTunde wrote:
    select segment3, 0 "Other Income"
         , 0 "Sales of Services"
         , 0 "Personnel Costs"
         , 0 "Other Staff Cost"
         , 0 "General Admin"
         , 0 "Travel"
         , 0 "Collaborator"
         , 0 "Training"
         , 0 "Capital"     
         , nvl(round(sum (nvl(PERIOD_ACTIVITY_A,0) + nvl(PERIOD_ACTIVITY_E,0) + nvl(PRDOPEN_A,0) + nvl(PRDOPEN_E,0)),0),0) "Overhead"
         , 0 "Finance Charges"
         from PAYMAN.SNP_OP_DETAIL@OFA
         where segment3= x.new_cc and period_name = p_period
         and segment2 in (select acc_t from CRP_REPORT_ACCT_DETAIL_T where group_id = 10)
         group by segment3
    Please I don't understand select segment3, 0 "Other Income"....there's no column called 0, what is the 0s doing in the select statement?Columns are one of the expressions that can be in a SELECT clause. There are many other kinds of expressions, including liteals (as in this example), pseudo-columns, functions, and compound expressions that are also allowed.
    Consider the following query:
    SELECT       deptno          AS raw_detpno
    ,       1 + deptno          AS one_plus
    ,       0 + deptno          AS zero_plus
    ,       0                  AS zero
    FROM       scott.dept
    ORDER BY  deptno
    ;Output:
    RAW_DETPNO   ONE_PLUS  ZERO_PLUS       ZERO
            10         11         10          0
            20         21         20          0
            30         31         30          0
            40         41         40          0No doubt you're familiar with using table columns (such as deptno) by themselves as an expression, as in the output column raw_deptno.
    You're probably familiar with using a table column and a numeric literal together, as in the ouptuput columns one_plus and zero_plus.
    It's also allowed to use numeric literals by themselves, as in the output column called zero.
    Expressions are part of the SQL language, so you can read about them in the SQL language manual:
    http://docs.oracle.com/cd/E11882_01/server.112/e26088/expressions001.htm#sthref1844
    Secondly, I don't understand from PAYMAN.SNP_OP_DETAIL@OFA@OFA is a database link. See
    http://docs.oracle.com/cd/E11882_01/server.112/e26088/queries010.htm#sthref2287

  • Need Help ASAP  my State tax form is in a PDF file and the attachment in my email says Please wait

    Need Help ASAP  my State tax form is in a PDF file and the attachment in my email says Please wait...
    I tried downloading updates like it said to but it still will not display the document.  How do I print the PDF file ASAP

    Can you give us a LOT more info?
    What email client? What version of Reader (I can only assume you even have Reader at this point)?
    Please wait? I'm sure it says more than that, right?
    Have you tried simply saving the PDF (it IS a PDF correct?) to your desktop and opening it from there?
    Did you get this form from the IRS or did it come from somewhere else? If the IRS again, what version of Reader?
    Help us help you.

  • Need help with SQL Query with Inline View + Group by

    Hello Gurus,
    I would really appreciate your time and effort regarding this query. I have the following data set.
    Reference_No---Check_Number---Check_Date--------Description-------------------------------Invoice_Number----------Invoice_Type---Paid_Amount-----Vendor_Number
    1234567----------11223-------------- 7/5/2008----------paid for cleaning----------------------44345563------------------I-----------------*20.00*-------------19
    1234567----------11223--------------7/5/2008-----------Adjustment for bad quality---------44345563------------------A-----------------10.00------------19
    7654321----------11223--------------7/5/2008-----------Adjustment from last billing cycle-----23543556-------------------A--------------------50.00--------------19
    4653456----------11223--------------7/5/2008-----------paid for cleaning------------------------35654765--------------------I---------------------30.00-------------19
    Please Ignore '----', added it for clarity
    I am trying to write a query to aggregate paid_amount based on Reference_No, Check_Number, Payment_Date, Invoice_Number, Invoice_Type, Vendor_Number and display description with Invoice_type 'I' when there are multiple records with the same Reference_No, Check_Number, Payment_Date, Invoice_Number, Invoice_Type, Vendor_Number. When there are no multiple records I want to display the respective Description.
    The query should return the following data set
    Reference_No---Check_Number---Check_Date--------Description-------------------------------Invoice_Number----------Invoice_Type---Paid_Amount-----Vendor_Number
    1234567----------11223-------------- 7/5/2008----------paid for cleaning----------------------44345563------------------I-----------------*10.00*------------19
    7654321----------11223--------------7/5/2008-----------Adjustment from last billing cycle-----23543556-------------------A--------------------50.00--------------19
    4653456----------11223--------------7/5/2008-----------paid for cleaning------------------------35654765-------------------I---------------------30.00--------------19
    The following is my query. I am kind of lost.
    select B.Description, A.sequence_id,A.check_date, A.check_number, A.invoice_number, A.amount, A.vendor_number
    from (
    select sequence_id,check_date, check_number, invoice_number, sum(paid_amount) amount, vendor_number
    from INVOICE
    group by sequence_id,check_date, check_number, invoice_number, vendor_number
    ) A, INVOICE B
    where A.sequence_id = B.sequence_id
    Thanks,
    Nick

    It looks like it is a duplicate thread - correct me if i'm wrong in this case ->
    Need help with SQL Query with Inline View + Group by
    Regards.
    Satyaki De.

  • I need help in sql insert statement

    sorry i cant speak English very well
    ======================
    i have a table contains two columns date_2011 and salary
    date_2011 || salary
    ====================
    1-1-2011 || 1000$
    2-1-2011 || 2000$
    7-1-2011 || 500$
    11-1-2011 || 200$
    my question is
    i need a sql statement to view Table as this table
    FROM || TO || Total Salary
    ==================================
    1-1-2011 || 2-1-2011 || 3000$
    7-1-2011 || 11-1-2011 || 700$

    try this
    SQL> with t as
      2  (
      3  select to_date('1-1-2011','dd-mm-yyyy') date_2011, 1000 salary from dual union all
      4  select to_date('2-1-2011','dd-mm-yyyy'), 2000 from dual union all
      5  select to_date('7-1-2011','dd-mm-yyyy'), 500 from dual union all
      6  select to_date('11-1-2011','dd-mm-yyyy'),  200 from dual
      7  ), t2 as(
      8  select
      9  date_2011 as date_from ,
    10  lead(date_2011, 1, null) over(order by date_2011) date_to,
    11  salary + lead(salary, 1, null) over(order by date_2011) sal
    12  , rownum rn
    13  from t order by date_2011)
    14  --
    15  select date_from as" from", date_to as "to", sal as "Total Salary"  from t2 where mod(rn,2) != 0;
    from    to       Total Salary
    01.01.11 02.01.11         3000
    07.01.11 11.01.11          700
    SQL>

  • Need Help in SQL command LIKE !

    Hi all,
    plz go through the following sql statement :
    select last_name
    from employees
    where last_name like '%a%e%' or last_name like '%e%a%';
    This gives an output where the last_name contains a or e in it.
    Now what would be the best solution to find the records in a table where last_name contains a,e,i,o,u (in anyorder). Do I need to write all the combinations ? Or is there any solution.
    Thanks,
    Sandeep

    Thnaks all for your help. However, the issue is not yet resolved.
    Ok let me more clear.
    Now consider a table called Employees which has a column called LAST_NAME.
    All the last_names in that column are of 10-20 letters. I want to findout the names
    which has the letters q,o,p,r,s (in anyorder).
    What would be the SQL statment ? From my point of view, I have to use LIKE keyword and use different combinations like '%q%0%p%r%s' or '%o%p%q%r%s' .,.....etc.
    Or do we have any other better solution ?
    Thnaks a lot for your solutions

  • Need help on case statements to validate records

    Hi Experts ,
    My table :
    seq_num
    col2
    col3
    col4
    1
    A
    12345
    P
    2
    B
    1
    123%23
    3
    C
    1
    23AB
    4
    D
    1
    20131001
    5
    E
    1
    6
    A
    13245
    Q
    7
    B
    1
    12345
    8
    C
    2
    1234*AB
    9
    D
    5
    20140112
    10
    E
    1
    00020
    my output
    seq_num
    col2
    col3
    col4
    Status
    Reason
    1
    A
    12345
    P
    Valid
    2
    B
    1
    123%23
    invalid
    Special Character for col4
    3
    C
    1
    23AB
    Valid
    4
    D
    1
    20131001
    Valid
    5
    E
    1
    invalid
    null for col4
    6
    A
    13245
    Q
    invalid
    Invalid character col4 || invalid number for col3
    7
    B
    1
    12345
    Valid
    8
    C
    2
    1234*AB
    Invalid
    Special Character col4 ||invalid col3
    9
    D
    5
    20140112
    invalid
    Future dates col4 ||invalid col3
    10
    E
    1
    00020
    Valid
    Sql :
    with t as
    ( select 1 as seq_num,'A' as col2 ,12345 as col3 ,'P' as col4 from dual
    union all
    select 2 ,'B',1,'123%23' from dual
    union all
    select 3,'C',1,'23AB' from dual
    union all
    select 4,'D',1,'21-02-2013' from dual
    union all
    select 5,'E',1,null from dual
    union all
    select 6,'A,13245,'Q' from dual
    union all
    select 7,'B',1,12345 from dual
    union all
    select 8,'C',2,'1234*AB' from dual
    union all
    select 9,'D',5,'25-01-2014' from dual
    union all
    select 10,'E',1,20 from dual
    I am applying rules on col3 and col4 for each records row-wise.
    I need case statements to populate status and reason columns after applying below rules
    Rules
    Col3 :
    For A record ,it should be 12345 always .
    For B,C,D,E , record should be always 1
    col4
    For A record , it should be either P or R
    No null values for all A, B,C,D,E records
    for B record , it dont contain special charecters
    for C RECORD ,  it dont contain special charecters
    for D record ,it should not contain future dates (dates are in yyyymmdd format and  less than  sysdates are valid )
    I have other columns as well ,as i not included here
    .It would be great if you Could  help on case statements
    Thanks and Regards,
    Sumanth

    I've adjusted Gregs nice example a bit. This should work:
    with w_base as (
          select seq_num, col2, col3, col4,
                 case when (col2 = 'A'                 AND col3 = 12345 )
                        OR (col2 in ('B','C','D','E')  AND col3 = 1)
                            then '' else '||invalid col3' end ||
                 case when (col2 = 'A'        AND col4 not IN ( 'P', 'R' ) )
                            then '||invalid col4' else '' end ||
                 case when (col2 IN ( 'B', 'C' )   AND col4 != translate(col4, 'a!@#$%^*()','a') )
                            then '||special character for col4' else '' end ||
                 case when (col2 = 'D'        AND col4 >= to_char(sysdate,'yyyymmdd') )
                            then '||future dates col4' else '' end
                   reason
          from ( select 1 as seq_num, 'A' as col2, 12345 as col3, 'P' as col4  from dual union all
                 select 2,            'B',         1,             '123%23'     from dual union all
                 select 3,            'C',         1,             '23AB'       from dual union all
                 select 4,            'D',         1,             '20130212'   from dual union all
                 select 5,            'E',         1,             null         from dual union all
                 select 6,            'A',         13245,         'Q'          from dual union all
                 select 7,            'B',         1,             '12345'      from dual union all
                 select 8,            'C',         2,             '1234*AB'    from dual union all
                 select 9,            'D',         5,             '20140125'   from dual union all
                 select 10,           'E',         1,             '20'         from dual )
    Select seq_num, col2, col3, col4,
           case when reason is null then 'Valid' else 'Invalid' end status,
           substr(reason, 3 ) reason
    from w_base
    It returns
    SEQ_NUM
    COL2
    COL3
    COL4
    STATUS
    REASON
    1
    A
    12345
    P
    Valid
    2
    B
    1
    123%23
    Invalid
    special character for col4
    3
    C
    1
    23AB
    Valid
    4
    D
    1
    20130212
    Valid
    5
    E
    1
    Valid
    6
    A
    13245
    Q
    Invalid
    invalid col3||invalid col4
    7
    B
    1
    12345
    Valid
    8
    C
    2
    1234*AB
    Invalid
    invalid col3||special character for col4
    9
    D
    5
    20140125
    Invalid
    invalid col3||future dates col4
    10
    E
    1
    20
    Valid
    edited some bugs :) now it should be fine! really

  • Help creating sql statement

    I need help in creating a sql statement. For example lets say i have 3 tables one(key_id, os), two(key_id, type, desc), three(type, desc). I need to create a statement that is kind of like this but i need all rows in table one returned also.
    select a.key_id, b.key_id, c.desc
    from one a, two b, three c
    where a.key_id = b.key_id and b.type = c.type;
    Message was edited by:
    user457357

    Or
    michaels>  with one as
    (select 10 key_id from dual union all
      select 20 from dual union all
      select 30 from dual union all
      select 40 from dual union all
      select 50 from dual),
    two as
    (select 10 key_id,'aa' type from dual union all
      select 20, 'aa' from dual union all
      select 30, 'bb' from dual),
    three as
    (select 'aa' type ,'Artificial' des from dual union all
      select 'bb','Bulk' from dual)
    select a.*, b.key_id, c.des
      from one a, two b, three c
    where a.key_id = b.key_id(+)
        and b.type = c.type(+)
        KEY_ID   KEY_ID_1 DES      
            10         10 Artificial
            20         20 Artificial
            30         30 Bulk     
            40                     
            50                     

  • Need help :  Select in statement

    Hi all,
    i am passing a query from SQL server to Oracle using the openquery..
    i need to pass a variable as a string that has more than one parameter ..here is the example i ma uding for one value..
    DECLARE @OBJECTID varchar(max);
    SET @OBJECTID = '423_23';
    SET @OBJECTID =' ''423_23''' '+','+' ''423_24'' '; this is giving error when usingmore than 1 string value
    -- here is the query that works just fine using only one value :
    @MYSTRING ='SELECT * FROM OPENQUERY(LINKED_PCTIDM,''SELECT p1.* FROM ADM.day p1 WHERE p1.time_key >= ''''' + @Report_Date + ''''' and p1.time_key <= ''''' + @Report_Date2 + ''''' and p1.QUEUE <> ''''' + @zerocall + ''''' and p1.object_id in ''''' + @OBJECTID + ''''' '')'
    EXEC (@MYSTRING)
    what i want : is to add the '' select in'' statement where the @OBJECTID='423_23','423_24','423_25'; (more than one value)..
    i am getting error on the sql statement
    incorrect syntax...
    the regular select instatement should be like this : select * in ('value1,'value2',....)
    any help to fix that will be appreciated...

    it shoud be:
    SET @OBJECTID = '''''423_23'''',''''423_24''''' ;
    @MYSTRING ='SELECT * FROM OPENQUERY(LINKED_PCTIDM,''SELECT p1.* FROM ADM.day p1 WHERE p1.time_key >= ''''' + @Report_Date + ''''' and p1.time_key <= ''''' + @Report_Date2 + ''''' and p1.QUEUE <> ''''' + @zerocall + ''''' and p1.object_id in (' + @OBJECTID + ') '')'

  • Need help regarding SELECT statement

    Hello, first time here but need help badly.
    I been using SQL syntax with another SQL server by the following statement doesnt seem to work in Oracle database.
    SELECT firstname+" "+lastname AS fullname FROM customers
    basicially, I just want to display date from two column as one column.
    Thanks

    Oracle has pipe sign for concate
    SELECT firstname||' '||lastname AS fullname
    FROM customers;Khurram

  • Need help in SQL (DENSE_RANK) function

    Hello All,
    I need the help in SQL.
    We have a table called status and the column are
    status_id number
    account_id number
    status_cd varchar2(10)
    created_id varchar2(10)
    created_by date
    and data is as follows
    insert into status values (1,101,'ENTER','ABC',to_date('21-JAN-2007 11:15:14','DD-MON-YYYY HH:MI:SS'));
    insert into status values (2,101,'REVIEW','DEF',to_date('21-JAN-2007 11:30:25','DD-MON-YYYY HH:MI:SS'));
    insert into status values (3,101,'APPROVE','GHI',to_date('21-JAN-2007 11:30:25','DD-MON-YYYY HH:MI:SS'));
    insert into status values (4,102,'ENTER','ABC',to_date('21-JAN-2007 11:18:14','DD-MON-YYYY HH:MI:SS'));
    insert into status values (5,102,'REVIEW','DEF',to_date('21-JAN-2007 11:33:25','DD-MON-YYYY HH:MI:SS'));
    insert into status values (6,102,'CANCEL','GHI',to_date('21-JAN-2007 11:33:25','DD-MON-YYYY HH:MI:SS'));
    insert into status values (7,103,'ENTER','ABC',to_date('21-JAN-2007 11:21:14','DD-MON-YYYY HH:MI:SS'));We have different status as follows
    1. ENTER
    2. REVIEW
    3. APPROVE
    4. CANCEL
    5. REJECT
    My requirement ..
    I need the max of created_id column for the status in ('APPROVE','CANCEL') and if there is no status in ('APPROVE','REVIEW') than it should be NULL.
    I wrote an SQL as
    select account_id,max(created_id) keep (dense_rank first order by decode(status_cd,'APPROVE',created_dt,'REVIEW',created_dt,NULL) DESC NULLS LAST,
          decode(status_cd,'APPROVE',status_id,'REVIEW',status_id,NULL) DESC NULLS LAST) last_app_rev_user
    from status
    group by account_id and gives me the output like
    ACCOUNT_ID LAST_APP_R
           101 GHI
           102 DEF
           103 ABCBut I want the Output like
    ACCOUNT_ID LAST_APP_R
           101 GHI
           102 DEF
           103 NULLAs the account 103 has no status called 'REVIEW' and 'APPROVE'
    My DB Version in 10.2.0.3.0.
    Hope I explain it properly. And if you have any other option without dense_rank still i will be happy.
    Thanks in advance for your help.
    AB
    null
    Message was edited by:
    AB

    instead of max(created_id) keep... use
    smth like max(case when status_cd in ('APPROVE','REVIEW') then created_id end) keep...

Maybe you are looking for