Need to write a different SQL query

Oracle version: 11.2.0.4

create visitors from the table (name varchar2 (10), dated $vdate);

Insert values ('John', sysdate-3) visitors;

Insert values ('John', sysdate-2) visitors;

Insert values visitors ('John', sysdate + 1);

Insert values visitors ('Thomas', sysdate-5);

Insert values visitors ('Thomas', sysdate-4);

Insert values visitors ('Thomas', sysdate + 2);

Insert values visitors ('Adam', sysdate + 1);


Now I need to find the max for each visitor with the name $vdate. I wrote the query below and I'm
My expected results.

My query:

Select * from (name, $vdate, dense_rank (select) on mxdate (partition by name $vdate desc order)
visitors)
where mxdate = 1;

Output:

NAME $VDATE
John May 14, 2015
Thomas may 15, 2015
Adam, 14 may 2015

Can someone write this query using the EXISTS or NOT EXISTS with below terms?

1. without using the aggrgeate function
2. without using the analytic function

Thanks for your help in advance...

Note: This is not the original data. This is only the test data...

Something like

Select * from v1 visitors

where does not exist (select 1 of visitors v2 where v1.name = v2.name and v1.vdate<>

NOT TESTED.

Tags: Database

Similar Questions

  • Conditional display of interactive report based on the different SQL query

    Hello

    I have two drop-down list at the top of my page and below I have an interactive report.
    Based on the selection of the user from the drop-down list values, interactive report should change based on different SQL queries.

    Is it possible to have different SQL queries based on the drop-down list values and generate interactive report based on that?

    Thank you

    Hello

    You can't have IR based on function returning the query as you can have classic report

    But here is a workaround
    http://www.oracleapplicationexpress.com/tutorials/71

    Kind regards
    Jari

  • Need help to write a better SQL query

    Hi gurus,

    Please let me know your best query for the problem below:

    Suppose we have 2 tables in Oracle 10 G database:

    IM1: product and warranty sales records

    A: sales_id

    B: product QTY.

    C: quantity of product

    D: Service QTY.

    E: Service amount

    IM1 table:

    a b c d and e

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

    1 1 100 1 10

    2 1 150 1 70

    3 2 500 1 100

    4 1 100 0 0

    IM2: brand and product guarantee service term associated with record sales

    A: sales_id

    G: the Service life

    P: brand product of

    Z: flag of product/Service

    IM2 table:

    a    g               p          z

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

    1 sony NULL p

    1 monthly NULL g

    Dell 2 p, NULL reference

    Reg 2 g NULL

    NULL lenovo 3 p

    3 monthly NULL g

    Sony NULL 4 p

    Then, he must bring the "term of Service" and "Brand product" in line with the sales table, as below:

    ID h_qty h_amnt g_qty g_amnt brand term

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

    1 1 100 1 10 sony monthly

    2 1 150 1 70 dell Reg

    3 2 500 1 100 lenovo monthly

    4 1 100 0 0 sony NULL

    What I wrote myself is as below, but I want to know if there is a better idea to make the query more reliable, because real paintings are big and my query is slow.

    SELECT DISTINCT

    ID of the s.a,

    s.b h_qyt,

    s.c. h_amnt,

    Virginia g_qty,

    Sager g_amnt

    brand of the PDP,

    term of r.g

    Of

    (SELECT s.*, PDP FROM s im1, im2 p WHERE s.a = p.a. PDP AND IS NOT NULL).

    IM2 p,

    IM2 g

    WHERE s.a. = p.a.

    AND s.a. = g.a

    AND (p.z = 'p' AND WINS = 'g')

    OR (p.z = 'p' AND WINS = 'p' AND sagehomme = 0 AND s.p = pp)

    ORDER BY 1;

    Here you will find the table scripts:

    create table im1 (a number, b, c number, number d, e number);
    create table im2 (a number g char (10), char (10) p, z (1) tank);
    insert into values im1 (1,1,100,1,10);
    insert into values im1 (2,1,150,1,70);
    insert into values im1 (3,2,500,1,100);
    insert into values im1 (4,1,100,0,0);

    Insert in im2 values (1, null, 'sony', 'p');
    Insert in im2 values (1, 'Monthly', null, 'g');
    Insert in im2 values (2, null, 'dell', 'p');
    Insert in im2 values (2, 'Reg', null, 'g');
    Insert in im2 values (3, null, 'lenovo', 'p');
    Insert in im2 values (3, 'Monthly', null, 'g');
    Insert in im2 values (4, null, 'sony', 'p');

    something like below

    Select x.a as id, x.b as h_qty, x.c as h_amnt, Eliane as g_qty, x.e as g_amnt, min (y.p) as min (y.g) as mark

    -min (y.p) compared to (x.a partition) as min (y.g) on (x.a partition) as the term mark
    of im1, im2 XY
    where x.a = y.a
    Group of x.a, x.b, x.c, Eliane x.e

    Edited: putting aggregation function instead of the function min min Analytics

  • Need to PROFILE options in SQL query

    SELECT ffvs.FLEX_VALUE_SET_NAME
    , ffvs.FLEX_VALUE_SET_ID
    , ffv.FLEX_VALUE_ID
    , ffv.FLEX_VALUE
    FROM fnd_flex_value_sets ffvs
    , fnd_flex_values ffv
    WHERE 1=1
    AND ffvs.FLEX_VALUE_SET_ID=ffv.FLEX_VALUE_SET_ID
    AND ffvs.FLEX_VALUE_SET_ID=1012493  -- SCHEME_GL_OPTION
    AND ffvs.FLEX_VALUE_SET_ID IN (select FLEX_VALUE_SET_ID from FND_FLEX_VALUE_RULE_LINES 
    where 
    FLEX_VALUE_RULE_ID 
    in 
    (select FLEX_VALUE_RULE_ID from FND_FLEX_VALUE_RULES where FLEX_VALUE_RULE_NAME='MDS ZA Option' ))
    

    I try to run the particular qyery of SQL to create the VIEW.

    Concern:-he will return all columns of values associated with all the rules and count GL

    -> I want to filter by running the or by using the PROFILE option so that he returned for a single RESP_ID.

    It is anyway to use in creating in the QUERY view to pick up the only RESP_ID.

    flex.png

    Hope I made clear in my question.

    I agree with you people.

    This table, I used fnd_flex_value_rule_usages to join the fnd_flex_values for responsibility according to the users want by passing the value.

    No need to use the PROFILE option.

    Thanks to all of you guyz.

  • pls I need your help in my SQL query statement

    Hi brothers

    pls guys I need someone help me on this issue

    3.1.1.
    List of all columns in all the lines in the table STOCKITEM Crescent primary key sequence.
    Only list the lines where the selling price is higher than the price of purchase multiplied by 3 or the
    selling price is lower than the price of purchase multiplied by 2?



    I made this stastment but the ruselt doesn't come with multiplied by 2 or 3.

    SELECT *.
    IN stockitem
    where sellprice > purchprice * 3
    or sellprice > purchprice * 2
    stkid asc agenda;

    Something like that?

    SQL> SELECT stkid,
      2  sellprice*3 sellprice,
      3  purchprice*2 purchprice
      4  FROM stockitem
      5  where sellprice > purchprice *3
      6  or sellprice < purchprice *2
      7  order by stkid asc
      8  /
    
         STKID  SELLPRICE PURCHPRICE
    ---------- ---------- ----------
            78         33        5.5
            82       16.8       3.36
            85        120         44
            88       97.5         37
    
    SQL>
    

    Concerning
    Girish Sharma

  • Need a Suggestion tune the SQL query

    Hello


    There is a view that is created that takes 17 seconds to run the query, when I split the query and run each query prend.15 seconds to run. Below the query I need to improve the performance of the code if you need your suggestion please order to tweek so this time take for execution to become less

    SELECT "Parature'"Source. "
    NULL 'retailer ',.
    reseller_name "reseller."
    NULL city "reseller."
    NULL state "reseller."
    A.Country "dealer Country."
    DECODE (a.state, "ACPA", "ACPA", 'EMEA', 'EMEA', NULL, 'Unknown', 'US') "Dealer Theater",
    NULL "Customer Name,"
    NO city 'customer ',.
    'Client state', NULL
    NULL 'country of the customer. "
    NULL "customer Theater."
    NULL "sales book Date."
    NULL "Ship Date."
    c.sales_order_number 'Sales Order Number',
    C.Po_Number "Number"
    controller_model_number_1 "product name."
    TRIM (UPPER (serial_number_1)) 'product serial number.
    c.contract_id "Contract ID",
    product_name 'name of support. "
    To_date (c.contract_purchase_date) "Start Date"
    To_date (c.contract_expiration_date) 'Expiry Date',
    NULL "header Status."
    NULL "status of line."
    NULL 'line Type. "
    NULL "reason for termination of employment."
    NULL 'location_id. "
    NULL 'Product_id '.
    Of edb_assets c,.
    edb_accounts one
    WHERE controller_model_number_1! = 'NULL '.
    AND Serial_Number_1! = 'NULL '.
    AND C.Amid = A.Amid (+)

    UNION
    SELECT "Parature'"Source. "
    NULL 'retailer ',.
    reseller_name "reseller."
    NULL city "reseller."
    NULL state "reseller."
    A.Country "dealer Country."
    DECODE (a.state, "ACPA", "ACPA", 'EMEA', 'EMEA', NULL, 'Unknown', 'US') "Dealer Theater",
    NULL "Customer Name,"
    NO city 'customer ',.
    'Client state', NULL
    NULL 'country of the customer. "
    NULL "customer Theater."
    NULL "sales book Date."
    NULL "Ship Date."
    c.sales_order_number 'Sales Order Number',
    C.Po_Number "Number"
    controller_model_number_2 "product name."
    TRIM (UPPER (serial_number_2)) 'product serial number.
    c.contract_id "Contract ID",
    product_name 'name of support. "
    To_date (c.contract_purchase_date) "Start Date"
    To_date (c.contract_expiration_date) 'Expiry Date',
    NULL "header Status."
    NULL "status of line."
    NULL 'line Type. "
    NULL "reason for termination of employment."
    NULL 'location_id. "
    NULL 'Product_id '.
    Of edb_assets c,.
    edb_accounts one
    WHERE controller_model_number_2! = 'NULL '.
    AND serial_number_2! = 'NULL '.
    AND C.Amid = A.Amid (+)
    UNION

    SELECT "Parature'"Source. "
    NULL 'retailer ',.
    reseller_name "reseller."
    NULL city "reseller."
    NULL state "reseller."
    A.Country "dealer Country."
    DECODE (a.state, "ACPA", "ACPA", 'EMEA', 'EMEA', NULL, 'Unknown', 'US') "Dealer Theater",
    NULL "Customer Name,"
    NO city 'customer ',.
    'Client state', NULL
    NULL 'country of the customer. "
    NULL "customer Theater."
    NULL "sales book Date."
    NULL "Ship Date."
    c.sales_order_number 'Sales Order Number',
    C.Po_Number "Number"
    controller_model_number_3 "product name."
    TRIM (UPPER (serial_number_3)) 'product serial number.
    c.contract_id "Contract ID",
    product_name 'name of support. "
    To_date (c.contract_purchase_date) "Start Date"
    To_date (c.contract_expiration_date) 'Expiry Date',
    NULL "header Status."
    NULL "status of line."
    NULL 'line Type. "
    NULL "reason for termination of employment."
    NULL 'location_id. "
    NULL 'Product_id '.
    Of edb_assets c,.
    edb_accounts one
    WHERE controller_model_number_3! = 'NULL '.
    AND serial_number_3! = 'NULL '.
    AND C.Amid = A.Amid (+)

    UNION
    SELECT "Parature'"Source. "
    NULL 'retailer ',.
    reseller_name "reseller."
    NULL city "reseller."
    NULL state "reseller."
    A.Country "dealer Country."
    DECODE (a.state, "ACPA", "ACPA", 'EMEA', 'EMEA', NULL, 'Unknown', 'US') "Dealer Theater",
    NULL "Customer Name,"
    NO city 'customer ',.
    'Client state', NULL
    NULL 'country of the customer. "
    NULL "customer Theater."
    NULL "sales book Date."
    NULL "Ship Date."
    c.sales_order_number 'Sales Order Number',
    C.Po_Number "Number"
    controller_model_number_4 "product name."
    TRIM (UPPER (serial_number_4)) 'product serial number.
    c.contract_id "Contract ID",
    product_name 'name of support. "
    To_date (c.contract_purchase_date) "Start Date"
    To_date (c.contract_expiration_date) 'Expiry Date',
    NULL "header Status."
    NULL "status of line."
    NULL 'line Type. "
    NULL "reason for termination of employment."
    NULL 'location_id. "
    NULL 'Product_id '.
    Of edb_assets c,.
    edb_accounts one
    WHERE controller_model_number_4! = 'NULL '.
    AND serial_number_4! = 'NULL '.
    AND C.Amid = A.Amid (+)

    UNION
    SELECT "Parature'"Source. "
    NULL 'retailer ',.
    reseller_name "reseller."
    NULL city "reseller."
    NULL state "reseller."
    A.Country "dealer Country."
    DECODE (a.state, "ACPA", "ACPA", 'EMEA', 'EMEA', NULL, 'Unknown', 'US') "Dealer Theater",
    NULL "Customer Name,"
    NO city 'customer ',.
    'Client state', NULL
    NULL 'country of the customer. "
    NULL "customer Theater."
    NULL "sales book Date."
    NULL "Ship Date."
    c.sales_order_number 'Sales Order Number',
    C.Po_Number "Number"
    controller_model_number_5 "product name."
    TRIM (UPPER (serial_number_5)) 'product serial number.
    c.contract_id "Contract ID",
    product_name 'name of support. "
    To_date (c.contract_purchase_date) "Start Date"
    To_date (c.contract_expiration_date) 'Expiry Date',
    NULL "header Status."
    NULL "status of line."
    NULL 'line Type. "
    NULL "reason for termination of employment."
    NULL 'location_id. "
    NULL 'Product_id '.
    Of edb_assets c,.
    edb_accounts one
    WHERE controller_model_number_5! = 'NULL '.
    AND serial_number_5! = 'NULL '.
    AND C.Amid = A.Amid (+)
    UNION
    SELECT "Source", "ERP"
    Imv.Bill_To 'retailer ',.
    Imv.Ship_To "dealer."
    Ship_To_City "Dealer City."
    Ship_To_State "State dealer."
    Edb2_Global.Country_Name_Theater (Imv.Ship_To_Country, 'COUNTRY') 'dealer country. "
    Edb2_Global.Country_Name_Theater (Imv.Ship_To_Country, 'THÉÂTRE') 'dealer Theater. "
    Cl.Company_Name "name of the customer."
    Cl.City "customer's city."
    Cl.State "State of the customer."
    Edb2_Global.Country_Name_Theater (cl.) Country, 'COUNTRY') 'country of the customer. "
    Edb2_Global.Country_Name_Theater (cl.) Country, 'THEATER') "customer Theater."
    Imv.So_Booked_Date "Date from the sale of book."
    Imv.Ship_Date "Delivery Date"
    Imv.Order_Number "client order number."
    Imv.Cust_Po_Number "Number"
    Cp.Item_Name "product name."
    Cp.Product_Serial_Number "serial number of product."
    To_char (Imv.contract_number) "Contract ID"
    Imv.Service_Description "name of support."
    Imv.Service_Start_Date "Start Date"
    Imv.Service_End_Date "expiration Date."
    Imv.Header_Status "header Status."
    Imv.Line_Status "status of line."
    Imv.Line_Type 'line Type. "
    Imv.Termination_Reason "reason for termination of employment."
    Cl.Location_Id 'location_id. "
    CP.ID "Product_id".
    Of ib_support_contracts_mv imv.
    customer_products cp,
    customer_locations cl
    WHERE superior (Trim (Imv.Serial_Number)) = Upper (Trim (Cp.Product_Serial_Number (+)))
    AND Cp.Location_Id = Cl.Location_Id (+)
    /


    There is a Union for each request, please suggest how tweek

    Thank you
    Sudhir

    A quick look indicates that first 5 select queries are that even accepting the 2 conditions

    WHERE controller_model_number_1! = 'NULL '.
    AND Serial_Number_1! = 'NULL '.

    Use SEPARATE and Clause OR reduce these 5 queries in one like that

    SELECT DISTINCT
               .....
               ......  
    
      FROM edb_assets       c
          ,edb_accounts     a
    WHERE  C.Amid = A.Amid(+)
      AND  (
              (     controller_model_number_1 != 'NULL'
                AND Serial_Number_1 != 'NULL'
              )
             OR
              (     controller_model_number_2 != 'NULL'
                AND Serial_Number_2 != 'NULL'
              )
             OR
              (     controller_model_number_3 != 'NULL'
                AND Serial_Number_3 != 'NULL'
              )
             OR
              (     controller_model_number_4 != 'NULL'
                AND Serial_Number_4 != 'NULL'
              )
             OR
              (     controller_model_number_5 != 'NULL'
                AND Serial_Number_5 != 'NULL'
              )
    
           )
    

    Concerning
    Arun

    Published by: Arun Kumar Gupta on May 9, 2012 12:10

  • Need help to convert a sql query to a stored procedure

    I have a task to convert a script sql in a stored precedure. The sql with different pr_id test_type are below. And the stored procedure must accept a pr_id and a test_type as an argument. Test type will be 3 for ATI and 2 for TI.  It must return a single value, the greatest distance calculated to fill the value of tap.  It is possible that they will pass a prid, test_type combination that will have all the records in the dsltdr table.

    {
    SELECT pw,
    Round ((DECODE (t.rfltype1, 5, t.rfldist1,)))
    DECODE (t.rfltype2, 5, t.rfldist2,)
    DECODE (t.rfltype3, 5, t.rfldist3,)
    DECODE (t.rfltype4, 5, t.rfldist4,)
    DECODE (t.rfltype5, 5, t.rfldist5,)
    DECODE (t.rfltype6, 5, t.rfldist6,)
    DECODE (t.rfltype7, 5, t.rfldist7,)
    DECODE (t.rfltype8, 5, t.rfldist8,)
    NULL)))
    (- launch_start) * distk * vop, 2) dist
    OF dsltdr t
    WHERE pr_id = 464529581
    AND test_type = 3
    ORDER BY pw CSA;
    }



    SELECT pw,
    Round ((DECODE (t.rfltype1, 5, t.rfldist1,)))
    DECODE (t.rfltype2, 5, t.rfldist2,)
    DECODE (t.rfltype3, 5, t.rfldist3,)
    DECODE (t.rfltype4, 5, t.rfldist4,)
    DECODE (t.rfltype5, 5, t.rfldist5,)
    DECODE (t.rfltype6, 5, t.rfldist6,)
    DECODE (t.rfltype7, 5, t.rfldist7,)
    DECODE (t.rfltype8, 5, t.rfldist8,)
    NULL)))
    (- launch_start) * distk * vop, 2) dist
    OF dsltdr t
    WHERE pr_id = 464528353
    AND test_type = 3
    ORDER BY pw CSA;


    SELECT pw,
    Round ((DECODE (t.rfltype1, 5, t.rfldist1,)))
    DECODE (t.rfltype2, 5, t.rfldist2,)
    DECODE (t.rfltype3, 5, t.rfldist3,)
    DECODE (t.rfltype4, 5, t.rfldist4,)
    DECODE (t.rfltype5, 5, t.rfldist5,)
    DECODE (t.rfltype6, 5, t.rfldist6,)
    DECODE (t.rfltype7, 5, t.rfldist7,)
    DECODE (t.rfltype8, 5, t.rfldist8,)
    NULL)))
    (- launch_start) * distk * vop, 2) dist
    OF dsltdr t
    WHERE pr_id = 464527142
    AND test_type = 2
    ORDER BY pw CSA;


    SELECT pw,
    Round ((DECODE (t.rfltype1, 5, t.rfldist1,)))
    DECODE (t.rfltype2, 5, t.rfldist2,)
    DECODE (t.rfltype3, 5, t.rfldist3,)
    DECODE (t.rfltype4, 5, t.rfldist4,)
    DECODE (t.rfltype5, 5, t.rfldist5,)
    DECODE (t.rfltype6, 5, t.rfldist6,)
    DECODE (t.rfltype7, 5, t.rfldist7,)
    DECODE (t.rfltype8, 5, t.rfldist8,)
    NULL)))
    (- launch_start) * distk * vop, 2) dist
    OF dsltdr t
    WHERE pr_id = 464528984
    AND test_type = 2
    ORDER BY pw CSA;



    >

    SELECT pw,
    Round ((DECODE (t.rfltype1, 5, t.rfldist1,)))
    DECODE (t.rfltype2, 5, t.rfldist2,)
    DECODE (t.rfltype3, 5, t.rfldist3,)
    DECODE (t.rfltype4, 5, t.rfldist4,)
    DECODE (t.rfltype5, 5, t.rfldist5,)
    DECODE (t.rfltype6, 5, t.rfldist6,)
    DECODE (t.rfltype7, 5, t.rfldist7,)
    DECODE (t.rfltype8, 5, t.rfldist8,)
    NULL)))
    (- launch_start) * distk * vop, 2) dist
    OF dsltdr t
    WHERE pr_id = 464529918
    AND test_type = 2
    ORDER BY pw CSA;

    How to start with?

    Thanks in advance.

    I removed the dbms_output statement because it does not seem to turn it on. If you do this from sqlplus
    Make sure that you type "serverout on" and press return.

    declare
    vPW number := 0 ;
    vDistance number := 0 ;
    begin
    sp_dsl_test(3,'12345',vDistance,vPW) ;
    DBMS_OUTPUT.PUT_LINE('Distance = '|| vDistance || ' PW = '|| vPW);
    end;
    

    Published by: FlyingGuy on March 3, 2011 14:24

  • Dynamic lists of values - 2 different SQL query

    Hello

    I use dynamic values lists as mentioned below. This will return 2 different results depending on the conditions.
    This does not work, when I run the page, it gives a blank page to Popup when I click on the shortcut menu, it gives me error... * HTP 404 error *... web page not found

    P1_ITEM1 elements is of type - Popup LOV key (description of the poster, returns the key value)
    if :P1_ITEM1 = 'ABC' then
     return 'select ename d, empno r from emp';
    else
     return 'select dname d, deptno r from dept';
    end if;
    No idea why it does not work

    Thank you
    Deepak

    Published by: Deepak_J on January 27, 2010 19:31

    Published by: Deepak_J on January 27, 2010 19:32

    Published by: Deepak_J on January 27, 2010 19:32

    Hello

    I don't know why it does not work.

    But you can use this query LOV solution in this case

    SELECT ename d,
      empno r
    FROM emp
    WHERE :P1_ITEM1 = 'ABC'
    UNION
    SELECT dname d,
      deptno r
    FROM dept
    WHERE :P1_ITEM1 <> 'ABC' OR :P1_ITEM1 IS NULL
    

    BR, Jari

  • Need help to build the SQL query

    Hi all

    I struggle to create the select statement.

    This is my expected outcome:

    Insert into tmp (aa, bb, cc, dd)

    values('CUST1','10100001',2,'10-Jan-2013');

    Insert into tmp (aa, bb, cc, dd)

    values('CUST1','10100002',4,'11-Jan-2013');

    Insert into tmp (aa, bb, cc, dd)

    values('CUST2','10200001',2,'19-Jan-2013');

    Insert into tmp (aa, bb, cc, dd)

    values('CUST2','10200002',6,'28-Jan-2013');

    Insert into tmp (aa, bb, cc, dd)

    values('CUST2','10200003',1,'28-Mar-2013');

    My result is:

    AA COUNT (1) MAX (DD) by cc dd AA AA

    CUST1 2 10100002 4 11 January 2013

    CUST2 3 10200003 1 28 MARCH 2013

    Can someone help with the select statement?

    Thank you and best regards,

    Iwan

    Just change it to the column:

    SELECT

    AA

    COUNT (aa)

    , MAX (bb) keep (last dense_rank command by JJ)

    , MAX (cc) keep (last dense_rank command by JJ)

    MAX (dd)

    OF THE TMP

    GROUP BY aa

    ORDER BY aa

    Note that by default sort order is ascending with the last NULL values.

    If in case your line-date-max will contain null value in the bb or cc columns those will be returned.

    Post edited by: Correction of chris227

    Post edited by: chris227 added note

  • Write a SQL query with lines in columns

    All the

    I need help in writing a SQL query with lines in columns, let give u an example...

    drop table activity;

    CREATE TABLE 'ACTIVITY '.

    (

    "PROJECT_WID" NUMBER (22.0) NOT NULL,

    VARCHAR2 (150 CHAR) "PROJECT_NO."

    VARCHAR2 (800 CHAR) 'NAME '.

    );

    Insert in the ACTIVITY (PROJECT_WID, PROJECT_NO, NAME) values (1683691, '10007', 12-121');

    Insert in the ACTIVITY (PROJECT_WID, PROJECT_NO, NAME) values (1684994, '10008', 12-122');

    Insert in the ACTIVITY (PROJECT_WID, PROJECT_NO, NAME) values (1686296, '10009', 12-123');

    Insert in the ACTIVITY (PROJECT_WID, PROJECT_NO, NAME) values (2225222, '9040', 12-124');

    drop table lonet;

    CREATE TABLE 'LONET.

    (

    VARCHAR2 (150 CHAR) "NAME."

    NUMBER OF THE "ROOT."

    VARCHAR2 (150 CHAR) "ENTRYVALUE".

    );

    INSERT INTO LONET (NAME, ROOT, ENTRYVALUE) VALUES ("GAC", 1683691, "LDE");

    INSERT INTO LONET (NAME, ROOT, ENTRYVALUE) VALUES ('NAM', 1683691, 'LME');

    INSERT INTO LONET (NAME, ROOT, ENTRYVALUE) VALUES ('BAG', 1683691, 'ICE');

    INSERT INTO LONET (NAME, ROOT, ENTRYVALUE) VALUES ('PAP', 1683691, 'IKE');

    INSERT INTO LONET (NAME, ROOT, ENTRYVALUE) VALUES ('NAM', 1686291, "QTY");

    INSERT INTO LONET (NAME, ROOT, ENTRYVALUE) VALUES ('PAP', 1686291, 'MAX');

    INSERT INTO LONET (NAME, ROOT, ENTRYVALUE) VALUES ("GAC", 1684994, "MTE");

    INSERT INTO LONET (NAME, ROOT, ENTRYVALUE) VALUES ('PAP', 1684994, 'MAC');

    INSERT INTO LONET (NAME, ROOT, ENTRYVALUE) VALUES ('FMT', 1684994, 'NICE');

    INSERT INTO LONET (NAME, ROOT, ENTRYVALUE) VALUES ('FMR', 1684994, 'RAY');

    INSERT INTO LONET (NAME, ROOT, ENTRYVALUE) VALUES ('BAG', 1686296, "CAQ");

    INSERT INTO LONET (NAME, ROOT, ENTRYVALUE) VALUES ("PAP", 1686296, "QAQ");

    INSERT INTO LONET (NAME, ROOT, ENTRYVALUE) VALUES ("VANESSA", 1686296, "THEW");

    INSERT INTO LONET (NAME, ROOT, ENTRYVALUE) VALUES ("ANDR", 1686296, "REYL");

    commit;

    Link: activity.project_wid = lonet.root

    look like output

    Project_wid Project_no NAME GAC NAM BAG RAC
    16836911000712-121LDELMELCELKE
    16849941000812-122MTEnullnullMAC
    16862961000912-123nullnullCAQQAQ
    2225222904012-124nullnullnullnull

    two problems, in that I am running

    1. I dono how simply we can convert rows to columns

    2. for root = 1683691, there are double NAM and RAC in lonet table... ideally these data should not be there, but since its here, we can take a MAX so that it returns a value

    3. There are undesirables who should be ignored

    Once again my thought process is that we join the activity and 4 alias table lonet.

    ask for your help in this

    Thank you

    Hello

    This is called pivoting.

    Here's a way to do it:

    WITH relevant_data AS

    (

    SELECT a.project_wid, a.project_no, b.SID

    , l.name AS lonet_name, l.entryvalue

    Activity one

    LEFT OUTER JOIN lonet l.root = a.project_wid l

    )

    SELECT *.

    OF relevant_data

    PIVOT (MAX (entryvalue)

    FOR lonet_name IN ("GAC" IN the gac

    "NAM" AS nam

    'BAG' IN the bag

    "RAC" AS cars

    )

    )

    ORDER BY project_wid

    ;

    Output:

    PROJECT_WID PROJECT_NO GAC NAM BAG RAC NAME

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

    1683691 12 - 10007 121 LDE LME LCE LKE

    1684994 MAC MTE 10008 12-122

    1686296 12 - 10009 123 QAC QAQ

    2225222 9040 12 - 124

    To learn more about swivel, see the FAQ in the Forum: Re: 4. How can I convert rows to columns?

    Thanks for posting the CREATE TABLE and INSERT statements; It's very useful!

  • need to customize SQL query verification

    Friends,

    I need to create a custom sql query. Please see the following scenarios:

    I want to audit all users who are successfully and unsuccessfully to logon to my oracle database. Tahts why I enbale 2 following options:

    SQL > session audit every time that successful;
    Verification succeeded.

    SQL > noaudit session each time it fails.
    Verification succeeded.

    And I found all the users using the following sql query:

    "Select username, to_char (timestamp," MM/DD/YY HH24 ' ") Timestamp, obj_name, returncode, action_name of."
    "dba_audit_trail where action_name in ('LOGIN', 'LOGOUT') order by timestamp desc '.

    USERNAME TIMESTAMP OBJ_NAME RETURN_CODE ACTION_NAME
    -------- --------- -------- ------------- -----------
    RIPON 14-12-08 11:51 0 LOGON
    SHIPON 14-12-08 11:50 1017 LOGON
    RIPON 14-12-08 11:50 1017 LOGON


    Here, return_code = 1017 means the user cannot successfully loged on
    and, return_code = 0 the average user can successfully connected


    Now, I want to custom to the query that is readable to others easily. As

    I want to add a column, which meets the follwing If condition.

    Return_code = 0, then the column is 'USER CORRECTLY CONNECTED ON' and
    If return_code = 1017, then the column displays "USER NOT CORRECTLY CONNECTED ON".


    can someone please help with this?

    Edited by: shipon_97 14 December 2008 13:28

    A CASE statement is probably the easiest option

    select username,
           to_char(timestamp,'MM/DD/YY HH24:MI') Timestamp,
           obj_name,
           returncode,
           action_name,
           (CASE WHEN return_code = 0 THEN 'User logged in successfully'
                 WHEN return_code = 1017 THEN 'User specified an invalid password'
                 ELSE 'User failed to log in for some other reason'
                 END) reason_desc
      from dba_audit_trail
     where action_name in ('LOGON','LOGOFF')
     order by timestamp desc
    

    Note that any nonzero return code indicates a failed to connect. ORA-01017 indicates an invalid user name or password, which is probably the most common error.

    Justin

  • This is not correct in SQL Query?

    Hello world

    I was reading some tutorials on job cuts. And I discovered the query to be something like:

    RUN DBMS_JOB. REMOVE (2);

    2 where is the job number. I ran the same command and I get the error like 'SQL statement' invalid. Does this mean the tutorial is wrong? :(

    The link to the tutorial is:
    http://www.dbasupport.com/Oracle/ora9i/jobqueue2.shtml

    the place where you run the job...?

    exec (UTE) is a sql... more command you can use from several sql...
    All publishers that allows sql commands will accept over the command. Developer SQL only accepts the exec command. If you need to write a pl/sql block it as

    begin
    DBMS_JOB.REMOVE(12);
    end;
    

    But since sql like

    EXECUTE DBMS_JOB.REMOVE(2); 
    

    is sufficient.

    Ravi Kumar

  • A necessary sql query

    Hi all

    I have two record values described.

    Select 1 as seq_no, "test1" as double data_set
    Union
    Select 1, '12test' from dual
    Union
    Select 2, 'abcd' from dual
    Union
    Select 2, 'ilm' from dual
    Union
    Select 2, '12test444' from dual

    Here, I need to extract the o/p from the DataSet above below

    1, 12test
    1, test1
    2, 12test444

    Select 1 as seq_no, "test1" as double data_set
    Union
    Select 1, '12test' from dual
    Union
    Select 2, 'abcd' from dual
    Union
    Select 2, 'ilm' from dual

    Here, I need to extract the o/p from the DataSet above below

    1, 12test
    1, test1
    2, abcd
    2, ilm

    The logic behind both above the o/p is

    If the search string 'test' is present in any data set (data set is defined by the same seq_no values), and then print the entire line containing the search string. As in the first data set of record value with seq_val 1 and 2 have two test string 'test', o/p is all the lines contating the search string 'test '.

    If the search string 'test' is not present in any data set (data set is defined by the same values seq_no) then print all lines not containing not the chain sought. As in the second set of data record value with seq_val 2 at once is not test string 'test', o/p is all lines not contating the search string 'test' in the test with 1as and all seq_val data the lines contating the search string 'test' in a set of data with seq_val 2

    Hope I could explain my logic o/p and I need to do in a sql query.

    Waiting for your answers.

    Thanks in advance.

    Hello

    Here's one way:

    WITH     got_rnk          AS
    (
         SELECT     seq_no, data_set
         ,     DENSE_RANK () OVER ( PARTITION BY  seq_no
                                   ORDER BY          CASE
                                       WHEN  INSTR (data_set, 'test') > 0
                                       THEN  1
                                       ELSE  2
                                     END
                           )     AS rnk
         FROM    table_x
    )
    SELECT     seq_no, data_set
    FROM     got_rnk
    WHERE     rnk     = 1
    ;
    

    The CASE expression returns 1 if data_set contains 'test', and it returns 2 if it is not. We will call this number x.
    You are not really interested in x itself. On the contrary, you want all the lines that have the lowest value of x between the lines with the same seq_no. In other words, if a row with a given_seq_num x = 1, then you want all the lines with this seq_no and x = 1, but it the lowest x for a seq_no is 2, then you want the lines with x = 2 instead.
    DENSE_RANK returns the number 1, if the riow data is the more low (or tied for the lowest) in its partition, so if him are all rows with x = 1 in the group, rnk = 1 corresponds to x = 1. But if all lines with the same seq_no have x = 2, then rnk = 1 corresponds to x = 2.

  • Need help to write a SQL query complex

    I have the source tabe as below

    -> SOURCE_TABLE
    NAME     CUST_ID     SVC_ST_DT     SVC_END_DT 
    TOM        1               31/08/2009      23/03/2011 
    DOCK       2               01/01/2004      31/05/2010 
    HARRY      3               28/02/2007      31/12/2009 
    I want to load as target table below
    -> TARGET_TABLE
    NAME     CUST_ID                     SVC_ST_DT      SVC_END_DT 
    TOM      1           31/08/2009      31/12/2009 
    TOM      1           01/01/2010      31/12/2010 
    TOM      1           01/01/2011      23/03/2011 
    DOCK      2           01/01/2004      31/12/2004 
    DOCK      2           01/01/2005      31/12/2005 
    DOCK      2           01/01/2006      31/12/2006 
    DOCK      2           01/01/2007      31/12/2007 
    DOCK      2           01/01/2008      31/12/2008 
    DOCK      2           01/01/2009      31/12/2009 
    DOCK      2           01/01/2010      31/05/2010 
    HARRY      3           28/02/2007      31/12/2007 
    HARRY      3           01/01/2008      31/12/2008 
    HARRY      3           01/01/2009      31/12/2009 
    Is it possible to write a SQL query that returns the data in the same way above the target table.

    Published by: AChatterjee on April 30, 2012 07:14

    Published by: AChatterjee on April 30, 2012 07:14

    Or like this...

    SQL> ed
    Wrote file afiedt.buf
    
      1  with t as (select 'TOM' as NAME, 1 as CUST_ID, date '2009-08-31' as SVC_ST_DT, date '2011-03-23' as SVC_END_DT from dual union all
      2             select 'DOCK', 2, date '2004-01-01', date '2010-05-31' from dual union all
      3             select 'HARRY', 3, date '2007-02-28', date '2009-12-31' from dual)
      4  --
      5  -- end of test data
      6  --
      7  select name, cust_id, svc_st_dt, svc_end_dt
      8  from (
      9        select name
     10              ,cust_id
     11              ,greatest(svc_st_dt, add_months(trunc(svc_st_dt,'YYYY'),yr*12)) as svc_st_dt
     12              ,least(svc_end_dt, add_months(trunc(svc_st_dt,'YYYY'),(yr+1)*12)-1) as svc_end_dt
     13        from t
     14             cross join (select rownum-1 as yr
     15                         from   dual
     16                         connect by rownum <= (select extract(year from max(svc_end_dt)) - extract(year from min(svc_st_dt)) + 1 from t)
     17                        )
     18       )
     19  where svc_st_dt <= svc_end_dt
     20* order by 2, 3
    SQL> /
    
    NAME     CUST_ID SVC_ST_DT            SVC_END_DT
    ----- ---------- -------------------- --------------------
    TOM            1 31-AUG-2009 00:00:00 31-DEC-2009 00:00:00
    TOM            1 01-JAN-2010 00:00:00 31-DEC-2010 00:00:00
    TOM            1 01-JAN-2011 00:00:00 23-MAR-2011 00:00:00
    DOCK           2 01-JAN-2004 00:00:00 31-DEC-2004 00:00:00
    DOCK           2 01-JAN-2005 00:00:00 31-DEC-2005 00:00:00
    DOCK           2 01-JAN-2006 00:00:00 31-DEC-2006 00:00:00
    DOCK           2 01-JAN-2007 00:00:00 31-DEC-2007 00:00:00
    DOCK           2 01-JAN-2008 00:00:00 31-DEC-2008 00:00:00
    DOCK           2 01-JAN-2009 00:00:00 31-DEC-2009 00:00:00
    DOCK           2 01-JAN-2010 00:00:00 31-MAY-2010 00:00:00
    HARRY          3 28-FEB-2007 00:00:00 31-DEC-2007 00:00:00
    HARRY          3 01-JAN-2008 00:00:00 31-DEC-2008 00:00:00
    HARRY          3 01-JAN-2009 00:00:00 31-DEC-2009 00:00:00
    
    13 rows selected.
    
  • need help with sql query dates

    Hello

    I have a sql query I need to extract some info between data dates. Where clause in this query is:

    WHERE CPD_BUS_UNIT =: ESI_PRM_1
    AND CPD_VOUCHER_DATE > =: P_DATE_FROM
    AND CPD_VOUCHER_DATE < (: P_DATE_TO + 1).

    When I run the query into a toad, I can view the data, but not the execution plan. It gives an error ORA-00932 inconsistent Datatypes.
    But when I remove (+ 1): P_DATE_TO, I can c the execution plan and data. The data will be different from the previous.

    Please suggest how to rewrite the query.

    Can you please give it a try?

    WHERE CPD_BUS_UNIT=:ESI_PRM_1
    AND CPD_VOUCHER_DATE >= :to_date(P_DATE_FROM)
    AND CPD_VOUCHER_DATE < (to_date(:P_DATE_TO)+1) 
    

    Concerning

Maybe you are looking for

  • Satellite L655 - 17 H - won't turn on properly

    I just bought a new Satellite L655 - 17H and it is already causing problems. The laptop sometimes turns. When you press the power button on the laptop lights will Flash and then turn off quickly, but the laptop does not light. If you press the power

  • Equium A60 with defective built in memory

    Hello I tried to recover my Equium A60 with the recovery DVD and it does not boot to windows. I tried to install a number of different versions of windows and the error messages lead me to the conclusion that the module memory is faulty. The system c

  • 5.5.2 error Message - recipient address rejected: need full address

    I get this error message that slows down my Outlook whenever he attempts to deal with e-mail. He apparently keeps trying to send this message that I am NOT author. How can I stop this?

  • Simulator for BB Pearl Flip?

    Hello guys ' I need a simultator for BB Pearl Flip. What JDE has a simulator for BB Pearl Flip? TNX guys'

  • The sum of more of two results of subject area

    HelloOBIEE 10 g.I have to club two reports from two disciplines into one, and I'm trying to use the results of two features of areas (Union) topic replies.But the problem is its production two records each on a SA. I need a sum more than as follows.Y