Query help in select Distinct on one column.

CREATE GLOBAL TEMPORARY TABLE Table1 (
ID1 varchar2(100) ,
Name1 varchar2(100),
Name11 varchar2(100)
insert into Table1 values ('a','n1','h3');
insert into Table1 values('b','n2','h2');
insert into Table1 values('a','n3','h1');
insert into Table1 values('c','n4','h5');
insert into Table1 values ('c','n5','h4');
insert into Table1 values('d','n6','h6');
select * from Table1;
ID1,NAME1,     NAME11
a,     n1,     h3
b,     n2,     h2
a,     n3,     h1
c,     n4,     h5
c,     n5,     h4
d,     n6,     h6
I am trying to select distinct ID1 and all values associated with it which is max row.I want to result as -
ID1,NAME1,     NAME11
a,     n3,     h1
b,     n2,     h2
c,     n5,     h4
d,     n6,     h6
Can you please help me to write simple query to get above result.
Edited by: 871447 on Jul 25, 2011 9:42 AM
Edited by: 871447 on Jul 25, 2011 9:45 AM

Hi,
Do a self-join, to combine the two rows for each value of id1 onto one output row.
Make it an outer join, in case there is only one row with a vlaue for id1.
SELECT  l.id1
,     l.name1
,     NVL ( r.name11
         , l.name11
         )          AS name11
FROM              table1     l
LEFT OUTER JOIN     table1     r  ON  l.id1     = r.id1
                          AND l.name1     < r.name1
;Edited by: Frank Kulash on Jul 25, 2011 12:57 PM
Sorry, I mis-read the problem.
Lee's solution, above, assumes that name1 is unique, as it is in your sample data.
What output would you want if that's not the case?
If name1 is not unique, but the combination of (id1, name1) is unique, then you can modify Lee's solution like this:
SELECT  *
FROM    table1
WHERE   (id1, name1) IN (
                    SELECT    id1
                    ,       MAX (name1)
                           FROM          table1
                    GROUP BY  id1
;Or, if you can't make any assumptions about uniqueness, you might need something like this:
WITH     got_r_num     AS
     SELECT  id1, name1, name11
     ,     ROW_NUMBER () OVER ( PARTITION BY  id1
                               ORDER BY          name1     DESC
                         ,                name11     DESC
                       )      AS r_num
     FROM     table1
SELECT  id1, name1, name11
FROM     got_r_num
WHERE     r_num     = 1
;

Similar Messages

  • ALV to select more than one column by row using set_table_for_first_display

    Hello everyone,
    I am developing an application (ALV OO) to select more than 1 column by row ( one ). This is an a holiday application so the idea is:
    -One column will be the day, and the row will be the user.
    So I am trying to select more than one day by user (that would be the row).
    I am using the method set_table_for_first_display but when it shows the alv, doesn't let me to select more than one column with a click of the mouse.
    Does anybody know if I can do this (select more than one column, by row) in somehow?
    Please let me know if you need more clarification about this.
    Thanks in advance
    Diego

    Hi Diego,
    it's all in the documentation.
    set different selection modes through the value of the field u201CSEL_MODEu201D in the layout structure.
    SPACE
    same as 'B'
    see 'B'
    Default setting
    'A'
    Column and row selection
    Multiple columns
    Multiple rows
    The user selects the rows through pushbuttons at the left border of the grid control.
    'B'
    Simple selection, list box
    Multiple columns
    Multiple rows
    'C'
    Multiple selection, list box
    Multiple columns
    Multiple rows
    'D'
    Cell selection
    Multiple columns
    Multiple rows
    Any cells
    The user selects the rows through pushbuttons at the left border of the grid control
    Beyond setting this option, you can set u201CNO_ROWMARKu201D option to hide the mark column which is normally visible when the selection mode allows multiple row selection.
    One point to notice here is that if you set your ALV Grid as to be editable, it may override your selection mode regardless of your layout settings.
    This is from SDN Community Contribution "An Easy Reference for ALV Grid Control" By: Serdar ŞİMŞEKLER
    Sorry, no link,. it's on my disk.
    Regards,
    Clemens

  • ASP SQL Access distinct from one column

    I have a query from two tables: table company and tables
    subcategories
    A company can have many subcategories.
    In the query I have put the fields id_company, company,
    keywords from
    table company and subcategories from table categories.
    When I use the select distinct * (or field names) the sql
    returns all
    the data whit double company's. This is normal, there
    different by
    subcategories. I need the subcategories to use for a search
    field. I
    can delete the categories field from my query and put the
    names
    manually in the keywords field.
    Is there a way to use the distinct only on the id_comany
    field so the
    sql returns only one company whit many subcategories?
    I hope this makes sense, can somebody helping my out?
    Thanks in advance,
    Sam

    Table = companys - fields = id_company, company, keywords
    Table = subcategories - fields = id_subcategorie,
    subcategorie and
    id_company with a relation to the table companys.
    The companys can have many subcategories.
    I created a query "qry_search" with the fields id_company,
    company,
    keywords, subcategorie
    in the page with the text field "search" I created a
    Request.QuerySting.varcompany, Request.QuerySting.varkeywords
    and
    Request.QuerySting.varsubcategorie
    In the result page I have a recordset whit:
    SELECT id_company, company, keywords, subcategorie
    FROM qry_search
    WHERE company LIKE %varcompany% OR keywords LIKE
    %varkeywords% OR
    subcategorie LIKE %varsubcategorie%
    I leave the DISTINCT function for this moment away
    Example DB qry_search:
    ID company keywords
    subcategorie
    1 BMW bmw new sales luxe cars
    1 BMW bmw new sales luxe garage
    2 Sam sandwich lunch wine restaurant
    2 Sam sandwich lunch wine snack
    3 Audi Audi new sales luxe cars
    When you put in the text field "search" bmw, I have 2 results
    for the same
    company
    When you put in the text field "search" cars, I have 2
    results but different
    companys
    When you put in the text field "search" sales, I have 3
    results but 2 for
    the same company and 1 for the Audi company
    I hope you can help me otherwise I will ad the subcategories
    in the keyword
    field and don't search in the subcategorie field.
    Thanks for your time Bregent !!!
    "bregent" <[email protected]> a écrit
    dans le message de news:
    f8d7vn$4pg$[email protected]..
    > Sorry Sam, I am not completely understanding the
    problem. Can you please
    > provide more details about your database schema and
    include sample data?

  • Build dynamic query depending upon selection of table and columns

    Hi ,
    I want your views on following requirement :
    we r doing generic export to excel functionality .
    1.User will select multiple tables and according to tables ,columns on that table will select
    2.There can be multiple table
    3.depending upon column and table selection , we have to build dynamic query and execute .
    Please let me know is it possible .If yes then please tell me how to do above requirement.
    Thanks in advance

    Hi,
    Identifiers cannot be used as bind variables, query are parsed
    before evaluate bind variables. Identifiers like table name.
    For excel you can use some like this:
    SET MARKUP HTML ON ENTMAP ON SPOOL ON PREFORMAT OFF
    SPOOL test_xls.xls
    SELECT colum1||chr(9)||columN FROM tableName;
    or CSV:
    SELECT colum1|| ',' ||columN FROM tableName;
    SPOOL OFF
    SET MARKUP HTML OFF ENTMAP OFF SPOOL OFF PREFORMAT ON
    For construct the query i suggest to read "Dynamic SQL Statements":
    http://www.java2s.com/Tutorial/Oracle/0440__PL-SQL-Statements/0300__Dynamic-SQL.htm
    http://docs.oracle.com/cd/B28359_01/appdev.111/b28370/dynamic.htm
    http://docs.oracle.com/cd/B10500_01/appdev.920/a96590/adg09dyn.htm
    http://asktom.oracle.com/pls/asktom/f?p=100:11:0::::P11_QUESTION_ID:227413938857
    --sgc                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   

  • Need help with select distinct with group by

    RDBMS 10gr2
    I am trying achieve having a select distinct with a order by and after looking on the internet and trying different examples, I have been unsucessful.
    Here is the code working (not sorting - I wish to sort by pps.last_name however I can't seem to get it to work).
    select distinct pps.last_name || ', ' ||pps.first_name || ' ' ||pps.middle_initial || '.' d,
           emple_no r
      from cobr.vw_pps_payroll pps,
           projman pm
    where term_date is null
       and department = '0004400000'
       and pm.eid != pps.emple_no

    SQL> SELECT   ename || '-' || empno, sal
      2      FROM emp_test
      3  ORDER BY ename;
    ENAME||'-'||EMPNO                                                                         SAL
    BLAKE-7698                                                                              20000
    CLARK-7782                                                                              20000
    DAVID-7699                                                                              20000
    FORD-7902                                                                                6000
    JONES-7566                                                                               5950
    KING-7839                                                                               10000
    SCOTT-7788                                                                               6000
    7 ligne(s) sélectionnée(s).
    SQL>

  • Query Help - Embedded Select

    Hi
    The following Query will show me catalogue IDs and Descriptions of all the hired outfits
    for each Branch, with the revenue generated in descending order....
    SELECT Members_Order.Catalogue_ID, Item.Item_Description, Item.Colour Orders.Branch_ID, Item.Hire_Charge, Members_Order.Number_of_Days, (Hire_Charge*Number_of_Days) AS Total
    From Members_Order, Item, Orders
    WHERE Members_Order.Item_ID = Item.Item_ID
    AND Members_Order.Order_ID = Orders.Order_ID
    ORDER BY Total DESC;What i want to do now is to create a query to show the details of the outfits, which are the same colour as the outfit which has generated the most revenue. I'm guessing this will involve an embedded select, using the same basis of the query above, but i'm unsure as to what should go with the embedded select?
    I have the following so far, could anyone help to finish it off or point me the the right direction....?
    SELECT Members_Order.Catalogue_ID, Item.Item_Description, Item.Colour, Item.Hire_Charge, Members_Order.Number_of_Days, (Hire_Charge*Number_of_Days) AS Total
    From Members_Order, Item, Orders
    WHERE Members_Order.Item_ID = Item.Item_ID
    AND Colour = (SELECT Colour FROM Item WHERE
    ;Edited by: user10945931 on 15-Apr-2009 19:50

    Hello,
    You can add count(*) and group by clause and using WITH clause. If you can post your table structure and some sample data that will be easier for us to help you out and if you like the challlegne and want to do it on your own then run start with above options. Also when you post code and output enclose them between \ to preseve formatting.
    \your code or output goes here
    \Regards                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                       

  • Jsp form - inserting multiple selection in to one column of the database

    I have this JSP/ HTML form that has fields that I can select multiple values and in the backend there is only one table. But when I select these multiple value and submit, only one value gets to the database. How do I get all the values that I selected .
    By the way I am using jdeveloper 10g and oracle 9i database
    thank you,
    san

    thanks aleena. sorry for the late reply. Because I away during the weekend. so I saw u are also into a web application project. I use jdeveloper 10g as my developing tool. its free to download for personal use. It may help you with your class project.
    also thanks to dev_AZ
    angrycat here is a similar example of my problem
    <select size="8" name="fruit" multiple>
    <option value="apple">apple</option>
    <option value="orange">orange</option>
    <option value="mango">mango</option>
    <option value="cherry">cherry</option>
    <option value="pineapple">pineapple</option>
    <option value="grapes">grapes</option>
    </select>
    if i select apple, orange,mango and cherry and submit it to the database field 'fruit', only 'apple' gets inserted. none others.
    this is the line where it is processing the value.
    String fruit=request.getParameter("fruit");
    by the way I have only one database table
    I am going to try the for loop example that aleena has posted.
    one more time thanks everyone
    santhosh

  • How to select rows whose one column contains some specific characters ?

    These is a table say T1 with a column say C1
    Let the field C1's values be:
    row 1)DE/SPT/A/FWD
    row 2)G/SPT/DE/DE/SPT
    row 3)R/FWD/SPT/A/FWD/FWD
    row 4)A/A/DE/SPT/FWD
    row 5)FWD/SPT/E/DE/A
    How to get only those rows whose C1 column contains FWD in last ?

    We can solve this with the simple use of a wildcard:
        select *
        from t1
        where c1 like '%FWD'
        /Note that this query will execute a full table scan, because any index you may have on C1 will be ignored: indexes are organised on the leading characters.
    cheers, APC

  • More than one column retrieved in "report and form" page

    HELLO!
    let's see if anybody can help me with this... i'm sure it's a stupid problem but i really don't know how to solve it.
    when i create a new application with APEX, i want to add a "report and form" page. i select the table where de data is, and then click on add page. then i go to the page definition for the form that just has been created but there's no option for selecting more than one column as the link column...
    i mean, i want to show a report with all the columns from the table and then, by selecting one of them, the form should display that column but the problem is that the primary key of the table is formed by 6 columns and i always receive the "more than one column retrieved" message as it's trying to retrieve the information using only one column. is there any way to make the link column be composed by 6 or 7 columns??

    APEX can handle 2 primary key for one table (if you are using the automated fetch row and the automated process row).
    In your case, you'll have to create your own "fetch process (page rendering)" and "DML process (page processing)".
    Flex
    Homepage : http://www.insum.ca
    InSum Solutions' blog : http://insum-apex.blogspot.com

  • Bex Query report error in cal month /year column

    Hi Experts,
    We have a bex query on virtual provider with only one column in it i.e 'cal year / month'( This virtual provider ahs been created from planning area). I'm having the problem with this column.
    In the excel output of this report the cal year /month is displaying as follows.
    01/0000 02/0000 03/0000 04/0000 05/0000 06/0000 07/0000 08/0000 09/0000 08/2008 09/2008 10/2008 11/2008 12/2008 01/2009 02/2009 03/2009 04/2009 05/2009 03/2010 04/2010 05/2010 06/2010 07/2010 08/2010 09/2010 10/2010 11/2010 12/2010 01/2011 02/2011 03/2011 04/2011 05/2011 06/2011 07/2011 08/2011 09/2011 10/2011 11/2011 12/2011 01/2012 02/2012 03/2012 04/2012 05/2012 06/2012 07/2012 08/2012
    I'm not getting why the first 9 columns its displaying as 01/0000 02/0000 03/0000 04/0000 05/0000 06/0000 07/0000 08/0000 09/0000 .
    It should display as
    08/2008 09/2008 10/2008 11/2008 12/2008 01/2009 02/2009 03/2009 04/2009 05/2009
    06/2009 07/2009 08/2009 09/2009 10/2009 11/2009 12/2009 01/2010 02/2010 03/2010 04/2010 05/2010 06/2010 07/2010 08/2010 09/2010 10/2010 11/2010 12/2010 01/2011 02/2011 03/2011 04/2011 05/2011 06/2011 07/2011 08/2011 09/2011 10/2011 11/2011 12/2011 01/2012 02/2012 03/2012 04/2012 05/2012 06/2012 07/2012 08/2012
    My data source is fetching the correct data including this cal year/ month.
    Thanks in advance for your help.
    Regards,
    Brahma Reddy

    I assume you are running Crystal for enterprise 4.0 SP02 Patch 08 !!
    According to OSS note 1651521 , This issue has been fixed in FP3 which is planned to be released in first quarter of 2012 !!!!!! ouch !!!!! 
    I am facing same issue , Pls let me know if you already solved it.
    Regards,
    Miraj Desai.
    Edited by: Anne Howard on Nov 29, 2011 6:58 AM

  • Creating Select List with multiple columns

    I want to create a select list with multiple columns. I followed the demo application model described in the by Denes Kubicek (Currently my reference for APEX !!)
    The code is as follows:
    CREATE OR REPLACE FUNCTION getcrops(p_cropid IN NUMBER)
    RETURN VARCHAR2
    IS
    v_cropid VARCHAR2 (400);
    v_fcode VARCHAR2 (400);
    v_product VARCHAR2 (400);
    v_var VARCHAR2 (400);
    v_fname VARCHAR2 (400);
    v_acres VARCHAR2 (400);
    v_style_start VARCHAR2 (400);
    v_style_end VARCHAR2 (400);
    v_return VARCHAR2 (400);
    BEGIN
    FOR c IN (select "CROP"."CROPID" as "CROP ID",
         "CROP"."FIELDCODE" as "FIELD CODE",
         "CARROTPRODUCTLIST"."CARROTPRODUCTNAME" as "PRODUCT",
         "VARIETYLIST"."VARIETYNAME" as "VARIETY",
         "FIELD"."FIELDNAME" as "FIELD NAME",
         "CROP"."SIZEINACRES" as "ACRES"
    from     "FIELD" "FIELD",
         "CARROTPRODUCTLIST" "CARROTPRODUCTLIST",
         "VARIETYLIST" "VARIETYLIST",
         "CROP" "CROP"
    where "CROP"."CARROTPRODUCTTYPE"="CARROTPRODUCTLIST"."CARROTPRODUCTID"
    and     "CROP"."VARIETYID"="VARIETYLIST"."VARIETYLISTID"
    and     "CROP"."FIELDID"="FIELD"."FIELDID")
    LOOP
    v_cropid := TO_CHAR (c.'CROP ID', 'FML999G999G999G999G990');
    v_fcode := c.'FIELD CODE';
    v_product := c.'PRODUCT';
    v_var := c.'VARIETY';
    v_fname :=c.'FIELD NAME';
    v_acres :=c.'ACRES';
    FOR i IN 1 .. 12 - LENGTH (c."CROP ID")
    LOOP
    v_cropid := v_cropid || ' ';
    END LOOP;
    FOR i IN 1 .. 12 - LENGTH (c.'FIELD CODE')
    LOOP
    v_fcode := v_fcode || ' ';
    END LOOP;
    FOR i IN 1 .. 12 - LENGTH (c.'PRODUCT')
    LOOP
    v_product := v_product || ' ';
    END LOOP;
    FOR i IN 1 .. 12 - LENGTH (c.'VARIETY')
    LOOP
    v_var := v_var || ' ';
    END LOOP;
    FOR i IN 1 .. 12 - LENGTH (c.'FIELD NAME')
    LOOP
    v_fname := v_fname || ' ';
    END LOOP;
    FOR i IN 1 .. 12 - LENGTH (c.'ACRES')
    LOOP
    v_acres := v_acres || ' ';
    END LOOP;
    v_return := v_cropid || v_fcode || v_product || v_var || v_fname || v_acres;
    END LOOP;
    RETURN v_return;
    END;
    I created this anonymous Pl/SQL function at a application level ..Then I followed up to create a select list with a function inside. However I could not create select list with the command suggested in the demo which is
    select getcrops(cropid) d, cropid r from crop;
    APEX (2.1) returns a error message. What am I missing ? Should the function be called from somewhere else after creating a regular select list..? Where the functions (Pl/SQL) should be ideally stored for application level access..? and for across application level access ? How can I check the function works good. Help is appreciated.

    Still really one column.
    If they need to be independent then you've got to have three selection lists.
    %

  • Something´s going wrong with select distinct (Oracle 10g)

    Our database is oracle 10g release 2 and the query statement is:
    select distinct last_name
    from students;
    and the query returns all of the last_names without an specific order..
    If I execute the same query in oracle 9i the query returns all of the last_name in alphabetic order.
    Why is it different on Oracle 10g?

    See also this blog entry from Mr. Kyte.
    C.

  • Sort in Only One Column

    How can I sort selected cells in one column without affecting data in other columns?
    Thanks

    JP-FLA wrote:
    You've got to be kidding! (Of course, I know you are not.) What kind of brain-dead software have I spent good money for?
    I am unable to understand how you may spend your money before testing the program with the 30 days free trial demo.
    I'm glad I still have Excel 2007 via VMWare.
    I'm glad that this program never entered my home.
    Message to Apple: What are you thinking?! Stunning spreadsheets don't make up for poor functionality.
    If you want to speak to Apple, you use the wrong channel.
    Here we are end users like you.
    _Go to "Provide Numbers Feedback" in the "Numbers" menu_, describe what you wish.
    Then, cross your fingers, and wait _at least_ for iWork'09
    Yvan KOENIG (from FRANCE mercredi 31 décembre 2008 20:22:05)

  • Select Distinct (More than one column)

    Hi All,
    I have the following SQL statement written in MySQL that returns only one row per MODCODE with the associated DEPTCODE of AE:
    SELECT DISTINCT
    EL_MODULE.DEPTCODE, EL_MODULE.MODCODE, EL_MODULE.MODNAME, EL_MODULE.MODLEVEL, EL_DEPTLEVEL.DEPTLEVELHEADER
    FROM EL_DEPTLEVEL, EL_MODULE
    WHERE EL_MODULE.MODLEVEL = EL_DEPTLEVEL.LEVELCODE AND EL_MODULE.DEPTCODE='AE' ORDER BY EL_MODULE.MODLEVEL
    However when I attempt to use this in oracle it returns three rows, could somebody point out what it is I need to change to get this working correctly in oracle.
    Hope someone can help.
    Jon

    Duplicates? No way! This query returns unique comibination of
    EL_MODULE.DEPTCODE,
    EL_MODULE.MODCODE,
    EL_MODULE.MODNAME,
    EL_MODULE.MODLEVEL and
    EL_DEPTLEVEL.DEPTLEVELHEADER
    Cheers
    Sarma.

  • Select distinct('more than one column') from ....

    Why does this sql statement not work:
    select distinct(column_a, column_b, column_c), empno
    from emp;
    How can I 'select distinct' using more than one column.

    Actually, DISTINCT applies to all columns in the result.
    Distinct is not a function, you cannot do distinct(column1, column2, ...).
    As a matter of fact it doesn't make sense to expect distinct applies to only one column, because you do not specify any criteria on how to select the values for the second column.
    A final note: you can do distinct(column1), column2, but this will still apply to both columns!
    Here're some examples that will all return both rows from the set, where set is: (1,1), (1,2):
    select distinct val1, val2
    from
    select 1 as val1, 1 as val2 from dual
    union all
    select 1 as val1, 2 as val2 from dual
    select distinct val1||'-'||val2
    from
    select 1 as val1, 1 as val2 from dual
    union all
    select 1 as val1, 2 as val2 from dual
    select distinct(val1), val2 -- this is very misleading. distinct still applies to both columns
    from
    select 1 as val1, 1 as val2 from dual
    union all
    select 1 as val1, 2 as val2 from dual
    Edited by: user4010726 on Dec 1, 2009 11:06 AM
    Edited by: user4010726 on Dec 1, 2009 11:07 AM

Maybe you are looking for