Unique row based on two columns and single column

Dear Members,
I have a table which contains duplicate rows, for which a query should be able to fetch the unique row from the table. Here the unique is not based on one column, but it should be on two columns and also check for uniqueness on one column.
create table addr ( firstname varchar2(10), lastname varchar2(10), area varchar2(3));
insert into addr values('bob', 'james', '1');
insert into addr values('bob', 'james', '1');
insert into addr values('harry', 'bert', '1');
insert into addr values('jimmy', 'bert', '1');
insert into addr values('sam', 'mac', '1');
insert into addr values('sam', 'knight', '1');
insert into addr values('tom', 'sand', '1');
insert into addr values('cat', 'mud', '1');
The output of query should contain 3 rows.
bob - james
harry - bert or jimmy - bert [ either one of them, but not both ]
sam - mac or sam - knight [ either one of them, but not both ]
tom - sand
cat - mud
SELECT firstname, lastname as total from addr WHERE area = '1' GROUP by firstname,lastname; This does not take of single column duplication..
Any suggestions..

SQL> with t_data
as
select 'bob' as firstname, 'james' as lastname, '1' as area from dual union all
select 'bob', 'james', '1' from dual union all
select 'harry', 'bert', '1' from dual union all
select 'jimmy', 'bert', '1' from dual union all
select 'sam', 'mac', '1' from dual union all
select 'sam', 'knight', '1' from dual union all
select 'tom', 'sand', '1' from dual union all
select 'cat', 'mud', '1' from dual
SELECT
        firstname,
        lastname,
        area
FROM
                SELECT
                        t.*,
                        row_number() over(partition BY firstname order by 1) rn,
                        row_number() over(partition BY lastname order by 1) rn1
                FROM
                        t_data t
WHERE
        rn     = 1
AND rn1 =1 ; 
FIRSTNAME       LASTNAME        AREA
bob             james           1
cat             mud             1
jimmy           bert            1
sam             knight          1
tom             sand            1
SQL>

Similar Messages

  • How to fetch the junk values of a column and the column name in oracle

    Hello,
    Can anyone help me in writing a procedure/dynamic SQL to fetch the column where the junk values appears and its value. Eg: If emp table contains ID and Name columns, and ID column contains junk values, the result should be the Id column and the junk value/s. It should be dynamic because next time if the other column contains junk values(like $,%...), the query should display the other column too..
    Thanks in advance..

    Try this, I'm not sure if it'll help... and get some idea...
    try to modify it as per your requirement -
    select * from junk_vals;
    1     ranit1
    2#     ranit2
    3     ranit3
    4$     ranit@4
    5     ranit5
    6     r^anit6
    select 'ID', id, val from junk_vals
        where regexp_like(id,'[^a-zA-Z0-9]')
    UNION
    select 'VALUE', id, val from junk_vals
        where regexp_like(val,'[^a-zA-Z0-9]');
    -- "Output :"
    ID     2#     ranit2
    ID     4$     ranit@4
    VALUE     4$     ranit@4
    VALUE     6     r^anit6
    NOTE - I've considered anything other than Alphabets and Numbers as a Junk Value
    Edited by: ranit B on Jan 4, 2013 5:07 PM
    -- o/p added

  • How to add one date column and charecter column

    hi all,
    i have 3 column start_date(date),end_date( date),duration (varchar2)
    i am trying to add start_time and duration like this
    end_date := to_char(start_time) + duration;
    but its showing value_error
    how to add one date column and charecter column.
    Thanks

    you need something that does:
    end_date (DATE) := start_date (DATE) + <number of
    days> (NUMBER)Not necessarily, because if the duration is just a string representation of a number then it will be implicitly converted to a number and not cause an error
    e.g.
    SQL> ed
    Wrote file afiedt.buf
      1  with t as (select to_date('01/07/2007','DD/MM/YYYY') as start_dt, '3' as duration_days from dual)
      2  -- END OF TEST DATA
      3  select start_dt + duration_days
      4* from t
    SQL> /
    START_DT+
    04-JUL-07

  • I want to know the difference between Days column and Day1 Column

    Hi All,
    I used this query:
    SELECT to_char(return_date_time,'dd/mon/yyyy hh24:mi:ss') Test,
    DECODE ('I', 'I', DECODE (1, NULL, NULL, 0, NULL, SYSDATE + 1)) Days,
    DECODE ('I', 'I', SYSDATE + 1) Day1
    FROM fm_curr_locn
    WHERE patient_id = 'DU00002765'
    output: 
    Test
    Days
    Day1
    26/oct/2013 00:00:00
    26-OCT-13
    10/26/2013 3:06:59 PM
    I want to know the difference between Days column and Day1 column and why the days column didnt show the time
    Please anyone help.....
    Regards
    Shagar M

    Pleiadian wrote:
    It is the decode statement that is doing this.
    The format of decode is:
    decode(expression, search1, result1, search2, result2, searchn, resultn, default)
    All result fields must be of the same datatype and will be of the datatype of the first result (result1 in this example)
    In your example (the decode for the field Days) the first result is a NULL. I suspect (gurus? anyone?) that Oracle will use the varchar2 overload of the decode statement, and the date field will be converted to varchar2 using your NLS_DATE_FORMAT settings.
    In the second decode statment (Day1) the first result is of datatype date, so the result of the decode statement will be a date field (and will be parsed as such by your sql development tool)
    I agree, as NULL is the first result returned the datatype is undetermined, so Oracle is picking VARCHAR2 over others, and causing an implicit datatype conversion on the resultant date value, whereas the other which is returned as a date will use the local settings of the client tool being used.
    I can replicate the 'issue' in Toad, and it's rectified if we cast the first returned value of the decode statement...
    SELECT DECODE ('I', 'I', DECODE (1, NULL, CAST(NULL AS DATE), 0, NULL, SYSDATE + 1)) Days,
           DECODE ('I', 'I', SYSDATE + 1) Day1
    FROM dual

  • Change URL of Edit column and Title column link that appear on AllItems.aspx

    Hi there,
    In the below snapshot. I am trying to change the URL of the Edit column and Title Column. By default when click on Edit Icon on each item, it opens the EditForm.aspx for that item. Similary by clicking on title of an item, it opens DispForm.aspx.
    I need to change it to my custom forms, CustomEditForm.aspx and CustomDispForm.aspx . Could anybody please suggest if it can be done by any Jquery or using SPD
    I know I can change the default forms from SPD.
    I don't want to change the default forms. Because for some users I need to show them default forms. for some users I need to show custom forms.
    Many Regards,
    Khushi
    Khushi

    Hi Alex - I understand that I need to identify users. I have the following script for that. But Where should i write so that redirection applies to all the condition items in AllItem.aspx 
    function IsGroupMember(GroupName)
    var isGroupMember = false;
    $().SPServices({
    operation: "GetGroupCollectionFromUser",
    userLoginName: $().SPServices.SPGetCurrentUser(),
    async: false,
    completefunc: function(xData, Status) {
    if($(xData.responseXML).find("Group[Name='" + GroupName + "']").length == 1)
    isGroupMember = true;
    else
    return isGroupMember;
    Khushi

  • Filter a column and a row based on two labels

    Hi
    Having a slight problem with an excel table and how to either filter or lookup the value I require.
    Basically the table is a skill matrix that I am trying to create into an app. The columns of the table are Name, and then all the separate areas (Area 1, Area 2, Area 3 etc). The rows are then the persons name in the name column and then a grading of that
    persons skills for each area. So the table looks look this:
    Name.       Area 1.        Area 2.      Area 3.
    John.            1.                1.             3
    Paul.             2.                1.             1
    I have created a gallery with all the names, and another gallery with all the areas - and I would like the values for the areas to show based on these 2 criteria. Tried Lookup which kept throwing up errors and also filter which again threw up errors. Is
    there any way to combine these to get the values showing in a label?
    Thanks

    Thanks. I tried that method and it works but I have too many areas to make it functional within the app experience.
    A bit more information:
    I have a gallery which is linked to Table 1 - this consists of the name of each person and an image - the image is what controls the gallery, touch it and you go to screen 2 which is that person's individual 'page'.
    On this page is the persons image, a label with their name, a drop down box and a text gallery. The drop down box and text gallery are linked to Table 2 - The dropdown is directed by a column in the table which is Zones and the text gallery is filtered by
    these zones into the areas within the zones:
    Zones            Areas            Name1            Name2             
    Name3 ....
    Zone 1           Area 1            3                     
    2                       3
    Zone 1           Area 2            1                     
    3                       2
    Zone 2           Area 3            3                     
    3                       3
    Zone 3           Area 4            1                     
    1                       3
    (This is an example of the table - the areas actually go into close to 100 and split into 6 zones).
    The Text gallery filter works upto a point - it gives me a Header which is based on the correct area - and only shows the Areas for the Zone picked from the drop down box. However, I would like the Body of the text gallery to give me the number for the name
    (which is the same name as picked from gallery 1).
    This is where I am having major problems (spent a week on this upto now and its nearly cost me 2 tablets when I think i have solved it and that little yellow triangle pops up in the body box).
    I have tried Lookup on Table2, using the name from gallery 1 as a the argument but it asks me each time to pick the right column to look in, which would work if I wanted the gallery only to work for a single Name but I would like it to show the rankings
    for the person who is picked from gallery1.

  • Update multiple rows based on two columns in same row

    I have a 1000 rows in a table I would like to update with a unique value. This unique value is a cocatenation of two columns in teh same row.
    Each row has a (i) date and a (ii) time and a (iii) date_time column. I would like to update the date_time (iii) column with a cocatenation of the (i) date and (ii) time columns.
    I know how I would update a single row but how can I update multiple rows with a cocatenation of each of the two columns - i.e put a different value into the date_time column for each row?

    this?
    update table tab_name
    set date_time =date||time
    where your_condition

  • How can I select and delete rows based on the value in one column?

    I searched through the discussion board, and found a thread on deleting blank rows, but not sure how to modify it to work with my issue.
    I have put together a rather complicated spreadsheet for designing control systems, it calculates parts needed based on check boxes selected in a second spreadsheet.
    Since not all systems require all parts there are many rows that have a 0 quantity value, I would like to select these rows and delete them once I have gone through the design phase (checking off required features on a separate sheet).
    I like the way the other thread I found will gather all the blank rows at the bottom without changing the order of the rows with data in them.
    I don't understand exactly how the formula in the other thread works well enough to modify it to look for a certain column.
    I hope I made myself clear enough here, to recap, I would like to sort the rows based on a zero value in one (quantity) column, move them (the zero quantity rows) to the bottom of the sheet, and then delete the rows with a zero quantity (I can delete them manually, but would like to automate the sorting part).
    Thanks for any help anyone can provide here.
    Danny

    I apologize but, as far as I know, Numbers wasn't designed by Ian Flemming.
    There is no "this column will be auto-destructing after two minutes"
    You will have to use your fingers to delete it.
    I wish to add a last comment :
    if your boss has the bad habit to look over your shoulder, it's time to find an other one.
    As I am really pig headed, it's what I did. I became my own boss so nobody looked over my shoulder.
    Yvan KOENIG (VALLAURIS, France) mercredi 13 juillet 2011 20:30:25
    iMac 21”5, i7, 2.8 GHz, 4 Gbytes, 1 Tbytes, mac OS X 10.6.8
    Please : Search for questions similar to your own before submitting them to the community
    To be the AW6 successor, iWork MUST integrate a TRUE DB, not a list organizer !

  • Merging values of CKFs in two columns into single column

    Hi ,
    I want to merge values present in two different columns.The values present in rows of one column are not present in rows of other column, I have to merge the values into a single column.so that in the single column i can get all the values.how to do it in BEx.
    regards ,
    man.

    Hi,
    Dear murali , the solution you gave has helped me a bit as i am able to get the values for both the formula KF's in the same column,But another problem has arisen.
       For both the formula KF's I had to choose 'New Selection' (and not 'New formula')after R-cllick on the structure, as I had to use a Characteristic as restriction for Each of the KF's, and also to use the Characteristic I had to associate it with some KF so that the Characteristic wil be used as a KF in the formula.
       Now what is happening is that I am getting the values of that associated KF also in the empty rows.(i.e. the rows where the value of both the KF1 and KF2 is not present)
       What has to be done to remove them and keep the empty rows empty and not show the value of the KF taken with the Characteristic for restriction. Also its value is summing up with KF1 and KF2 in the coulmn which is having both KF1 and KF2 together.
    Regards
    man

  • Display two heading in single column using ALV report

    Hi Experts,
    I got a requirement for displaying 2 rows heading in a single column report. Is it possible that I can perform this task using ALV. How to get this 2 rows in ALV.
    This is a criteria that need to be output in ALV REPORT.
    MATERIAL CODE--MATERIAL NUMBERSTORAGE LOC----SLOC1     SLOC2
    --DATE--DATE1      DATE2
    123445--TEST MATERIAL22--
    3
    As mentioned above storage loc and date will be changing with respect to data dynamically and under this double heading qty will be displayed.
    Just want to know how to get double heading. It is clear that how to display dynamically but unaware of double heading using fieldcatalog.
    Regards,
    Yahya

    Hi Yahya,
    Please pass row position in fieldcatalogue for the respective columns.
    E.g  MOVE '2' to w_fieldcat-row_pos.  " This will display the field in 2nd row.
    Thanks,
    Rupali

  • Normal text and single column order issue

    I have a long document text for a textbook, some paragraph I need to use two column (single column), but  the order wrongly changes between text and footnote.
    Footnote should be in the end of the page.

    Check the scripts by Peter Kahrel and see if one of them applies to your case: http://www.kahrel.plus.com/indesign/footnotes.html

  • Merge multiple columns into single column?

    Hi,
    I need to execute queries dynamically and return the values. I need to fetch the values of the queries in single column only by concatenating the multiple columns. I cannot use PL/SQL in my scenario.
    is there any way to execute a query and the result will be fetched in single column (values of multiple columns needs to be concatenated)?
    Thanks,
    Raja.

    hi,
    do you mean this??
      1* select EMPNO||' '||ENAME||' '||JOB||' '||MGR||' '||HIREDATE||' '||SAL||' '||COMM||' '||DEPTNO||
    SQL> /
    MULTIPLE_COL
    100 JDF DIR  05-SEP-09 200 1000 10
    7497 MILLER CLERK 7782 23-JAN-82 25000 195 35
    7566 JONES MANAGER 7839 02-APR-81 3175 446.25 20
    7654 RAR SALESMAN 7698 28-SEP-81 1450 1587.5 30
    7698 BLAKE MANAGER 7839 01-MAY-81 3050 427.5 30
    7782 CLARK MANAGER 7839 09-JUN-81 2650 367.5 10
    7788 SCOTT ANALYST 7566 09-DEC-82 3200 450 20
    7839 KING PRESIDENT  17-NOV-81 5200 8250 10
    7844 TURNER SALESMAN 7698 08-SEP-81 1700 225 30
    7876 ADAMS CLERK 7788 12-JAN-83 1300 165 20
    7900 JAMES CLERK 7698 03-DEC-81 1150 85.5 30 [email protected]
    MULTIPLE_COL
    7902 FORD ANALYST 7566 03-DEC-81 3200 450 20
    8000 KINGBABA PRESIDENT  17-NOV-81 5200 8250 10
    8001 TURNER RAV SALESMAN 8000 08-SEP-81 1700 450 30
    1001 KITTU DOR  05-SEP-09 1200 100 40
    15 rows selected.Or
    Mean this??
    SQL> ed
    Wrote file afiedt.buf
      1  With T As
      2     ( Select Level col1 From dual Connect By Level<=10
      3     )
      4     Select Max(SYS_CONNECT_BY_PATH(COL1||',',' ')) Multi_col
      5       From
      6     ( Select COL1, Lag(COL1) Over (Order By COL1) As Lag
      7             From   T  )
      8      Start With Lag Is Null
      9        Connect By
    10*    Prior col1 = LAG
    SQL> /
    MULTI_COL
    1, 2, 3, 4, 5, 6, 7, 8, 9, 10,Edited by: user291283 on Sep 8, 2009 10:10 PM

  • How to add multiple columns to single column header

    hi,
    i wans to add two columns under a single column header in jtable how can i do this.

    By following some of these clever samples:
    http://www.crionics.com/products/opensource/faq/swing_ex/JTableExamples1.html
    Lots of different ideas on manipulating headers and columns there and on the proceeding pages.

  • How to insert from one table to another (multiple columns to single column)

    I want to insert data from table1 into table2 and the data looks in 3 columns as below
    Table1     
    RepNm ObjNm DbNm
    123     abc def
    456     def xyz
    789     123 456
    and the data in table2 should be display as
    Table 2
    ObjNm
    123
    456
    789
    abc
    def
    xyz
    the dupes should get eliminated and only the distinct values should be inserted into a single column in table2 from multiple columns in table1

    How do you want to handle nulls? If there is a null in any value of a column, do you want to insert it?
    Then,
    WITH T
         AS (SELECT LEVEL colnum
               FROM DUAL
             CONNECT BY LEVEL <= 3)
    SELECT DECODE (colnum,  1, RepNm,  2, ObjNm,  3, DbNm) as ObjNm
      FROM table1, T;if you dont want to insert nulls then,
    WITH T
         AS (SELECT LEVEL colnum
               FROM DUAL
             CONNECT BY LEVEL <= 3)
    SELECT DECODE (colnum,  1, RepNm,  2, ObjNm,  3, DbNm) AS ObjNm
      FROM table1, T
    WHERE DECODE (colnum,  1, RepNm,  2, ObjNm,  3, DbNm) = DECODE (colnum,  1, RepNm,  2, ObjNm,  3, DbNm)G.

  • Converting multiple column in single column

    Hi All,
    I need to create a table with single column by using select statement with multiple columns
    For Ex- i have 1 row with 10 columns (may be more than 10) like
    'A','B','C','D','E','F','G','H',I','J'
    i written sql like
    select 'A','B','C','D','E','F','G','H','I','J' from dual
    result is - 'A','B','C','D','E','F','G','H','I','J' with 10 columns
    Now i need output lik this using SQL
    Text
    'A'
    'B'
    'C'
    'D'
    'E'
    'F'
    'G'
    'H'
    'I'
    'J'
    Please help me to sort out this problem. Your help would highly appropriated.
    Regards
    Raju
    Edited by: 888667 on Apr 8, 2013 10:54 PM
    Edited by: 888667 on Apr 8, 2013 10:56 PM

    Welcome to the forum!!! You can convert column to row like this
    SQL>with my_table
      2  as
      3  (
      4  select 'A' col1,'B' col2,'C' col3,'D' col4,'E' col5,'F' col6,'G' col7,'H' col8,'I' col9,'J' col10
      5    from dual
      6  )
      7  select decode(no , 1, col1, 2, col2, 3, col3, 4, col4, 5, col5, 6, col6, 7, col7, 8, col8, 9, col9, 10, col10) col
      8    from my_table
      9    cross join (select level no from dual connect by level <= 10)
    10  /
    C
    A
    B
    C
    D
    E
    F
    G
    H
    I
    J
    10 rows selected.
    SQL> To ask question in a better way please read {message:id=9360002} from FAQ

Maybe you are looking for

  • "Save for Web" exporting very slowly

    I appologize if this question has been answered somewhere already, I didn't find a solution for my problem, so here goes. When saving images for web, photoshop (or is it still considered image ready?) can take as long as 10-15 seconds per slice while

  • Fonction sketch and line art

    Qui à des informations sur la fonction sketch and line art se trouvant dans le menu objet des dernières version de CC ??? Google et l'aide illustrator ne sont pas avares en info sur cette fonctionnalité passé sous silence...

  • Display problems after update

    I recently updated- on balance things went ok- How ever for some odd reason something is causing KDE (3.5) to behave oddly- I used to have a app in the kde control panel that'd let me fine tune settings  (fonts and display size for instance)- I see t

  • No system in the system landscape exports transport requests

    Hi gurus, We are trying to implement the Change Request Management scenario in Solution Manager 4.0 SP12 in a POC. The TMS are created with Solution Manager as Controller and with the settings are correct. We have created a Maintenance Project with t

  • BuildDatabase.Execute failed

    I'm using awxml.jar in my Java application to create and populate analytic workspace. In my code I created 1 Analytic Workspace, 1 cube, 3 dimensions and 1 measure. The code also handles all the mappings and associations. I then call AW.Create( )and