Eliminating Duplicates in a Union situation

Hi guys,
Just realized and stumbled upon a situation here and I need some help. I have a Query that gets all the departments and Students who took a survey by saying "Yes" or "No". Some of the Students could also be part of a "Board" and hold some special position.
Here is my UNION :
SELECT '"'||d.dept_type
       ||' - '||d.dept_code||'"'
       ||','||'"'||d.dept_desc||'"'
       ||','||'"'||0||'"'
from   department      d
group by d.dept_type, d.org_code, d.dept_desc
union
SELECT '"'||d.dept_type
       ||' - '||d.dept_code||'"'
       ||','||'"'||d.dept_desc||'"'
       ||','||'"'||COUNT(distinct(s.student_id))||'"'
from   student s,
         department d,
         survey sv
where  s.student_id = d.student_id
and      s.student_id = sv.student_id
and      sv.response = 'YES'
group by d.dept_type, d.org_code, d.dept_descBy doing this, I am being able to achieve departments that don't have any Students who said "Yes" in the survey but then again, when there is a department that did have a student that took the survey and said "Yes", I am getting two records.
Here is what the output is looking like :
"DEP - 01","Department 1","0"                                                                 
"DEP - 02","Department 2","0"                                                                                
"DEP - 02","Department 2","1"                                                                                
"DEP - 03","Department 3","0"                                                                                    
"DEP - 04","Department 4","0"                                                                     
"DEP - 04","Department 4","1"                                                                      As you can see, there are duplicates which I don't want. This is what the accurate result shoudl be like :
"DEP - 01","Department 1","0"                                                                 
"DEP - 02","Department 2","1"                                                                                
"DEP - 03","Department 3","0"                                                                                    
"DEP - 04","Department 4","1"                                                                      Any help would be awesome !

Hi,
As Toon suggested, this looks like an outer join problem, not a UNION.
If you did have to do a UNION, you could GROUP BY twice; once where you're doing it now, and again in the main query. Do the conversion to strings in the main query, only.
WITH     union_data     AS
     SELECT DISTINCT
               dept_type,   dept_code,   dept_desc,
            0                         AS sub_total
     FROM      department
UNION
     SELECT    d.dept_type, d.dept_code, d.dept_desc,
            COUNT (DISTINCT  s.student_id)     AS sub_total
     FROM      student         s,
                department       d,
                survey       sv
     WHERE         s.student_id       = d.student_id
     AND       s.student_id       = sv.student_id
     AND       sv.response       = 'YES'
     GROUP BY  d.dept_type, d.dept_code, d.dept_desc
SELECT       '"'     || dept_type          ||
       ' - '     || dept_code          ||
       '","'     || dept_desc          ||
       '","'     || SUM (sub_total)     ||
FROM       union_data
GROUP BY  dept_type,   dept_code,   dept_desc
;When you said org_code, did you mean dept_code?
Edited by: Frank Kulash on Aug 27, 2009 10:53 AM

Similar Messages

  • What's the best approach/program for finding and eliminating duplicate photos on my hard drive?

    What's the best approach/program for finding and eliminating duplicate photos on my hard drive? I have a "somewhat" older version of iPhoto (5.0.4), and it doesn't seem to offer anything like that except during the importing phase of syncing my phone...

    I wonder, is there room to transfer them to your phone, & then back to filter them?

  • Question. Eliminating duplicate identical rows.

    Question. Eliminating duplicate identical rows.
    --1. assume we have this table
    create table T
    tabid int not null,
    crdate datetime not null
    go
    --2.Now load the following data:
    insert into T values (1, '1/1/2003')
    insert into T values (1, '1/1/2003')
    insert into T values (2, '2/1/2003')
    insert into T values (2, '2/1/2003')
    insert into T values (3, '3/1/2003')
    insert into T values (3, '3/1/2003')
    insert into T values (4, '4/1/2003')
    insert into T values (4, '4/1/2003')
    go
    -- 3. Run this query to discover duplicates
    select tabid, crdate, count(*)
    from T
    group by tabid, crdate
    having count(*) > 1
    go
    --4. Result is something like this
    1     2003-01-01 00:00:00.000     2
    2     2003-02-01 00:00:00.000     2
    3     2003-03-01 00:00:00.000     2
    4     2003-04-01 00:00:00.000     2
    Question
    How do I eliminate duplicates and reduce table by 50%?

    I think this what you want:
    SQL> create table test ( c1 varchar2(5));
    Table created.
    SQL> insert into test values ( 'a' );
    1 row created.
    SQL> insert into test values ( 'b');
    1 row created.
    SQL> insert into test values ( 'a');
    1 row created.
    SQL> insert into test values ( 'c');
    1 row created.
    SQL>
    SQL> select c1 from test group by c1 having count(*)>1;
    C1
    a
    SQL> delete from test where c1 in
    2 ( select c1 from test group by c1 having count(*)>1);
    2 rows deleted.
    SQL>
    SQL> select * from test;
    C1
    b
    c
    SQL>
    Joel P�rez

  • Eliminating duplicates in iPhoto

    How do I eliminate duplicates in iPhoto 08? If this has been answered before, I couldn't find it - sorry.
    I have up to 4 copies showing of hundreds and hundreds of pics. I don't relish the thought of eliminating dups one at a time. Please advise and thank you.

    Matro
    Welcome to the Apple user to user assistance forums
    back up your iPhoto library and run duplicate annihilator -http://www.brattoo.com/propaganda/
    LN

  • IC ELIMINATIONS DUPLICATE RECORDS

    Hi friends
    I have a issue with the package for intercompany elimination, when I run the package the first time the eliminations is correct but when I run the same packege other wise the system generate duplicate records, is there any way to prevent this?
    Example
    first execution
    intercompany input 100
    intercompany elim (100)
    second execution
    intercompany input 100
    intercompany elim (200)
    third execution
    intercompany input 100
    intercompany elim (300)
    this is wrong because I want to execute the package any times and the result should be the same (100)
    thanks in advance

    Hi vara and collet
    thanks for your prompt reply but I still have the same problem, and check the dimension and I have set datasrc Y to the members to be eliminated, plus I have the flow dimension as closing.
    The workaround I'm doing is deleting and re-run the eliminations Interunit
    cheers

  • Eliminating duplicate loops when Soundtrack installed

    I've noticed in GarageBand that I have many duplicate items in the loops browser. Almost every sample, if not all, is present in the list twice. Soundtrack (and FCE) came preinstalled on my Mac when I bought it from Apple, but I don't use Soundtrack. I rarely use GarageBand. I'd like to get rid of the duplicates to free up space, if possible.
    Where are the music loops and sample sounds stored for Soundtrack and GarageBand? Can I easily remove one of the two sets? Is it better to remove the GarageBand-related sounds or the Soundtrack ones?
    Thanks.

    See here and here for information about the loops provided with both GarageBand and Soundtrack.
    (19201)

  • Eliminating duplicate images in my catalog

    I have duplicate images in my Adobe Photoshop Elements 6.0 catalog that I want to eliminate.  When I check properties the image is identical except for the text format describing the image e.g.  img 001.jpg, IMG_001.jpg  and IMG_001.JPG.  I am looking for a way to get rid of duplicates without clicking on them individually and deleteing them. Also I want to prevent this happening again.  Any suggestions?  

    I don't know of any way to automatically eliminate duplicates -- all programs require manually deciding which photo is the duplicate and which is the one to keep.
    Two programs I use are:
    DupScout, which has a freeware version:
    http://www.dupscout.com/
    BeyondCompare:
    http://scootersoftware.com/
    Ken

  • Eliminating duplicates from subquery...

    What is the best way to eliminate duplicates from a subquery:
    SELECT dept_no, dept_name
    FROM dept D
    WHERE EXISTS (
    SELECT 'X'
    FROM emp E
    WHERE E.dept_no = D.dept_no);
    OR
    SELECT dept_no, dept_name
    FROM dept D
    WHERE ( SELECT 'X'
    FROM emp E
    WHERE E.dept_no = D.dept_no AND ROWNUM < 2);
    Thanks!

    >
    UPDATE TABLE1
    SET COL1 = (
    SELECT DISTINCT COL1
    FROM TABLE2, TABLE3
    WHERE TABLE2.ID = TABLE3.ID
    You need to refine your example. At present you appear to be updating every row in table1 to the same value - but only if col1 (which could be from table2 or table3 - or might be accidental capture from table1) holds just one distinct value across the query; but it looks as if you're likely to get 'single row subquery returns more than one row[ as an error.
    I guess you're trying to do something LIKE:
    update t1
    set t1.col1 = (
      select t2.col2
      from t2
      where  t2.colx = t1.coly
      and  exists (
        select null
        from t3
        where t3.id = t2.id
    )The most efficient access path depends on how many rows will have to be examined in each table, and how many times you will have to jump to another table to find related data - and if your query is roughly the shape of this one, the optimizer may be able to transform it in a variety of ways to find an efficient access path.
    As it stands, my example will be setting col1 to null whenever there is no match for coly in table t2 - and the optimizer would have to drive off t1 looking at every row to do this. Your requirement, and available predicates, indexes and constraints, may allow, or force, a completely different strategy.
    Regards
    Jonathan Lewis
    http://jonathanlewis.wordpress.com
    http://www.jlcomp.demon.co.uk

  • Eliminating duplicates in arraycollection with multiple values in them

    I have an array collection with 4 values in it. It doesnt have a key but each row in the array collection has to be different.
    Could someone help me out with the code to eliminate all the duplicates?
    I have the array collection
    anchorGroupDetailAC  and each object in the array collection has anchorGroupId, assignmentStep, dimensionMasterId and dimensionValueId.
    i need each cell in the anchorGroupDetail to be unique. I found some examples online that talk about using one element in the object to be a key but how can we do it for all the 4 elements combined?
    Your help is greatly appreciated.
    Thank you
    Nikhil

            private function cc(event:FlexEvent) : void {
                for(var i:int = 0; i < 4; i++) {
                    var obj:Object = new Object();
                    if(i < 3) {
                        obj['anchorGroupId'] = i;
                        obj['assignmentStep'] = ("step" + i);
                        obj['dimensionMasterId'] = (i + 1) * 2;
                        obj['dimensionValueId'] = (i + 1) * 3;
                    } else {
                        obj['anchorGroupId'] = i - 1;
                        obj['assignmentStep'] = ("step" + (i - 1));
                        obj['dimensionMasterId'] = i * 2;
                        obj['dimensionValueId'] = i * 3;
                    _test.addItem(obj);
            private function filter() : void {
                for(var i:int = 0; i < _test.length; i++) {
                    if(getNbrTimesInList(Object(_test.getItemAt(i))) > 1) {
                        _test.removeItemAt(i);
                        i--;
            private function getNbrTimesInList(object:Object) : int {
                var nbr:int = 0;
                for each (var obj:Object in _test) {
                    if(   obj['anchorGroupId'] == object['anchorGroupId']
                        && obj['assignmentStep'] == object['assignmentStep']
                        && obj['dimensionMasterId'] == object['dimensionMasterId']
                        && obj['dimensionValueId'] == object['dimensionValueId'] ) {
                        nbr++;
                return nbr;
            private var _test:ArrayCollection = new ArrayCollection();
            private function handleClick():void {
                trace("collection before filter: " + ObjectUtil.toString(_test));
                filter();
                trace("collection after filter: " + ObjectUtil.toString(_test));
    Trace Result:
    collection before filter: (mx.collections::ArrayCollection)#0
      filterFunction = (null)
      length = 4
      list = (mx.collections::ArrayList)#1
        length = 4
        source = (Array)#2
          [0] (Object)#3
            anchorGroupId = 0
            assignmentStep = "step0"
            dimensionMasterId = 2
            dimensionValueId = 3
          [1] (Object)#4
            anchorGroupId = 1
            assignmentStep = "step1"
            dimensionMasterId = 4
            dimensionValueId = 6
          [2] (Object)#5
            anchorGroupId = 2
            assignmentStep = "step2"
            dimensionMasterId = 6
            dimensionValueId = 9
          [3] (Object)#6
            anchorGroupId = 2
            assignmentStep = "step2"
            dimensionMasterId = 6
            dimensionValueId = 9
        uid = "DC1E4607-4E77-90E9-D189-D87022CF6189"
      sort = (null)
      source = (Array)#2
    collection after filter: (mx.collections::ArrayCollection)#0
      filterFunction = (null)
      length = 3
      list = (mx.collections::ArrayList)#1
        length = 3
        source = (Array)#2
          [0] (Object)#3
            anchorGroupId = 0
            assignmentStep = "step0"
            dimensionMasterId = 2
            dimensionValueId = 3
          [1] (Object)#4
            anchorGroupId = 1
            assignmentStep = "step1"
            dimensionMasterId = 4
            dimensionValueId = 6
          [2] (Object)#5
            anchorGroupId = 2
            assignmentStep = "step2"
            dimensionMasterId = 6
            dimensionValueId = 9
        uid = "DC1E4607-4E77-90E9-D189-D87022CF6189"
      sort = (null)
      source = (Array)#2
    This is not pretty...but it works.
    T.K.

  • Best solution for finding and eliminating duplicate files? 10.8.3

    Good morning.  I just upgraded to 10.8.3 (from, 10.6.8) and have many external hard drives with tons of duplicate photos, music and general documents.
    I've been looking into what the best way is to pull all this together and have it all backed up in one spot, with duplicate deleted.
    Probably around 3TB's of data spread over 5 external drives.
    Any suggestions on best way to achieve / softweare to use woiuld be greatly appreciated.
    Thanks

    Another option that is free is DupeGuru: http://www.hardcoded.net/dupeguru/
    I have not used these personally so I am not sure how effective they are, but they do have some positive reviews by others.

  • Problem in eliminating duplicates

    hi EXPERT
    i want to take off the duplicates
    eg
    Emp Name
    BALAJI
    BALAJI
    Bala
    nela
    anbu
    MUMBAI
    R
    R
    Amman
    Amman
    i have used
    1. SORT it_final.
    DELETE ADJACENT DUPLICATES FROM it_final.
    and
    2. select distict
    but i dont find any change
    it appears with duplicates
    CAN YOU PLS HELP ME OUT
    THANX IN ADVANCE.\
    RACHEL

    Hi
    DATA :
       BEGIN OF fs_name,
          empnam TYPE string,
       END OF fs_name.
    DATA :
      t_name LIKE STANDARD TABLE OF fs_name.
    CLEAR fs_name.
    fs_name-empnam = 'XYZ'.
    APPEND fs_name TO t_name.
    CLEAR fs_name.
    fs_name-empnam = 'XYZ'.
    APPEND fs_name TO t_name.
    CLEAR fs_name.
    fs_name-empnam = 'ABC'.
    APPEND fs_name TO t_name.
    CLEAR fs_name.
    fs_name-empnam = 'ABC'.
    APPEND fs_name TO t_name.
    CLEAR fs_name.
    fs_name-empnam = 'XYZ'.
    APPEND fs_name TO t_name.
    CLEAR fs_name.
    fs_name-empnam = 'XYZ'.
    APPEND fs_name TO t_name.
    SORT  t_name.
    LOOP AT t_name INTO fs_name.
      WRITE :
        / fs_name-empnam.
    ENDLOOP.
    DELETE ADJACENT DUPLICATES FROM t_name.
    skip 5.
    LOOP AT t_name INTO fs_name.
      WRITE :
        / fs_name-empnam.
    ENDLOOP.
    Regards
    Pavan

  • HT2486 eliminating duplicate contacts

    I have multiple contacts which are duplicates. I want to delete all my contacts in my phone and then download a updated contact list from my Imac

    Welcome to the Apple Community.
    The following article(s) may help you.
    Resolving duplicate contacts

  • Eliminating duplicates in albums

    I have some large albums in Elements 9 Organizer and for various reasons some contain duplicates.  Will I have to manually eliminate them or is there a feature in Organizer that will recognize and eliminate the duplicates?  Thanks.

    See:
    http://www.johnrellis.com/psedbtool/photoshop-elements-faq.htm#_Find_and_delete

  • Finding and Eliminating Duplicates

    I'm sure there has already been discussion on this topic and I really did try to find something that specifically addrssed my issue but could not find any.
    So, with that being said: How do I find and eliminate duplicates in my iTunes?

    It is too tedious to go through a delete all the duplicates, one by one.
    Especially since to delete is not just a single click or key stroke.
    I am sure that I have read about or used a program that will do it automatically.
    Can anyone help me to remember the name of that program or suggest applications that will do this. I have thousands of duplicates and even many quadruplicates.

  • Eliminating Duplicate Messages While Consolidating Impotered Mailboxes

    I'm recovering from a complete OS Erase/Reinstall, and the last step is to move old messages (backed up on another volume, natch..) from the imported mailboxes into the newly created In/Out boxes. This will create duplicates, which I will then have to manually weed out. Mail (2.0.5) has no provision for this. Any strategies for this?
    TIA.
    mrbl

    Andreas Amann’s Mail Scripts has a Remove Duplicates script that may do what you want.
    Not sure at which point of reinstalling Mac OS X you’re now, but if you’ve already installed the system updates and are running Mac OS X 10.4.11, as your profile indicates, the version of Mail should be at least 2.1 (2.1.3 if you’ve installed the latest Security Update), not 2.0.5...

Maybe you are looking for

  • Crystal Report images not showing up.

    Post Author: cfloss CA Forum: JAVA I am displaying reports in my web application using the Viewers SDK. The reports are being displayed with a frame where the image would be and a small red X in the corner. I have entries in my web.xml (shown below)

  • Itunes not seeing ipod shuffle

    I have an older ipod shuffle . Its the 4 generation i think the small blue shuffle . It is fully charged and plays all the music on the shuffle . When I mount the shuffle and plug it into my imac desk computer running snow leopard 10.6.8 it is not re

  • MacBook Pro 15" Retina Not Charging

    My MacBook Pro is not charging. I've plugged in the charger connected to a working power supply and the LED light does not light up like it always does in green. My macbook does not charge; it does not display any "not charging" message too. I need m

  • Why does my printer not work when i plug in the internet? (ethernet)

    hey guys, i have the Mac connected to a large digital printer via the ethernet connection. works fine, no problems untill i plug in the internet. I get an error message in my software and can't print any jobs, but the internet works fine. I have a fe

  • Disk space error when copying/pasting clips between iMHD projects

    Anyone know how to get around this problem? I have emptied all my trash, but continue to get a "not enough disk space" error message when I attempt to paste one copied iMHD clip to another iMHD project. How do I check my available disk space? I assum