Join without duplicates

hello,
i'm quite new to oracle and dbms in general, so please be patient ;)
so here goes:
i have two tables: people and cities
my task: select all people who live in a city that has a 'z' in its name or where the city_id field is empty.
so i thought i'd join the two tables and see what happens:
select people.* from people, cities
where (people.city_id = cities.city_id and cities.city_name like '%z%')
or (people.city_id is null);
the problem here is that there are duplicate records for the people who have no city_id. how can i change the where-clause in order to avoid this?
i was told not to use distinct, union, intersect.
greets and thx!

Processing ...
with people as (
     select 'a' as p_name,
          1 as city_id
     from dual
     union all
     select 'a' as name,
          1 as city_id
     from dual
), cities as (
     select 'z' as c_name,
          1 as city_id
     from dual
select people.* from people, cities
where (people.city_id = cities.city_id and
cities.c_name like '%z%')
or (people.city_id = cities.city_id (+) and
people.city_id is null)
Query finished, retrieving results...
P_NAME                   CITY_ID               
a                                               1
a                                               1
2 row(s) retrievedBye AlessandroThere are two different things here:
1) duplicate records because source have the same duplicate records i.e. as in your case a, 1 two times
2) duplicate records because join condition is too weak.
Your query Alessandro had duplicates because of (1).
OP original query have duplicates or triplicates or whatever else because he allowed cartesian product between peoples rows which haven't city_id and cities i.e. it was case (2)
It is easier to spot if you select not only people.* but also city columns.
Look what original query gives us:
SQL> with people as (
  2    select 1 p_id, 'Live in z1' name, 1 c_id from dual
  3    union all
  4    select 2 p_id, 'Live in z2' name, 1 c_id from dual
  5    union all
  6    select 3, 'Dont live in z1', 2 from dual
  7    union all
  8    select 4, 'Dont live in z2', 2 from dual
  9    union all
10    select 5, 'Dont live anywhere1', null from dual
11    union all
12    select 6, 'Dont live anywhere2', null from dual
13  ),
14  cities as (
15    select 1 city_id, 'bzz' city_name from dual
16    union all
17    select 2 city_id, 'aaa' city_name from dual
18    union all
19    select 3 city_id, 'bbb' city_name from dual
20  )
21  select * from people, cities
22  where (people.c_id = cities.city_id and cities.city_name like '%z%')
23  or people.c_id is null
24  /
                P_ID NAME                                C_ID              CITY_ID CIT
                   1 Live in z1                             1                    1 bzz
                   2 Live in z2                             1                    1 bzz
                   5 Dont live anywhere1                                         1 bzz
                   6 Dont live anywhere2                                         1 bzz
                   5 Dont live anywhere1                                         2 aaa
                   6 Dont live anywhere2                                         2 aaa
                   5 Dont live anywhere1                                         3 bbb
                   6 Dont live anywhere2                                         3 bbbWe have all combinations between city rows and rows in people, which city_id is null.
If source rows already have duplicates then there isn't any possibility to get rid of them just using joins. Then we need distinct, group by or some set operation.
Gints Plivna
http://www.gplivna.eu

Similar Messages

  • How to tune the SQL & solve UNIQUE Contraint issue using without duplicates

    CREATE TABLE REL_ENT_REF
      ROLL_ENT        VARCHAR2(4 BYTE)              NOT NULL,
      ROLL_SUB_ENT    VARCHAR2(3 BYTE)              NOT NULL,
      ROLL_ENT_DESCR  VARCHAR2(50 BYTE),
      ENT             VARCHAR2(4 BYTE)              NOT NULL,
      SUB_ENT         VARCHAR2(3 BYTE)              NOT NULL,
      ENT_DESCR       VARCHAR2(50 BYTE)
    CREATE UNIQUE INDEX REL_ENT_REF_IDX_PK ON REL_ENT_REF
    (ROLL_ENT, ROLL_SUB_ENT, ENT, SUB_ENT);
    ALTER TABLE REL_ENT_REF ADD (
      CONSTRAINT REL_ENT_REF_IDX_PK
    PRIMARY KEY
    (ROLL_ENT, ROLL_SUB_ENT, ENT, SUB_ENT);
    TOTAL NUMBER OF RECORDS FOR TABLE REL_ENT_REF : 123542
    CREATE TABLE REL_COA_REF
      ACCT                    VARCHAR2(9 BYTE)      NOT NULL,
      ACCT_LVL                VARCHAR2(2 BYTE)      NOT NULL,
      ACCT_ID                 VARCHAR2(9 BYTE)      NOT NULL,
      REL_TYPE                VARCHAR2(10 BYTE)     NOT NULL,
      ACCT_TYPE               VARCHAR2(2 BYTE)      NOT NULL,
      ACCT_DESCR              VARCHAR2(43 BYTE),
      POST_ACCT               VARCHAR2(9 BYTE)      NOT NULL,
      POST_ACCT_TYPE          VARCHAR2(2 BYTE),
      POST_ACCT_DESCR         VARCHAR2(43 BYTE),
      SIGN_REVRSL             NUMBER
    CREATE INDEX REL_COA_REF_IDX_01 ON REL_COA_REF
    (ACCT_ID, REL_TYPE, POST_ACCT);
    CREATE UNIQUE INDEX REL_COA_REF_IDX_PK ON REL_COA_REF
    (ACCT_ID, ACCT, REL_TYPE, POST_ACCT);
    TOTAL NUMBER OF RECORDS FOR TABLE REL_COA_REF : 4721918
    CREATE TABLE REL_CTR_HIER_REF
      ENT           VARCHAR2(4 BYTE)                NOT NULL,
      SUB_ENT       VARCHAR2(3 BYTE)                NOT NULL,
      HIER_TBL_NUM  VARCHAR2(3 BYTE)                NOT NULL,
      HIER_ROLL     VARCHAR2(14 BYTE)               NOT NULL,
      HIER_CODE     VARCHAR2(14 BYTE)               NOT NULL,
      SUM_FLAG      VARCHAR2(14 BYTE)               NOT NULL,
      CTR_OR_HIER   VARCHAR2(14 BYTE)               NOT NULL,
      CTR_DETAIL    VARCHAR2(14 BYTE)               NOT NULL,
      CTR_DESCR     VARCHAR2(50 BYTE)
    CREATE INDEX REL_CTR_HIER_REF_IDX_01 ON REL_CTR_HIER_REF
    (HIER_TBL_NUM, HIER_ROLL, SUM_FLAG);
    CREATE UNIQUE INDEX REL_CTR_HIER_REF_IDX_PK ON REL_CTR_HIER_REF
    (ENT, SUB_ENT, HIER_TBL_NUM, HIER_ROLL, SUM_FLAG,
    CTR_DETAIL, CTR_OR_HIER, HIER_CODE);
    CREATE INDEX REL_CTR_HIER_REF_IDX_02 ON REL_CTR_HIER_REF
    (ENT, SUB_ENT, HIER_TBL_NUM, CTR_OR_HIER, SUM_FLAG,
    CTR_DETAIL);
    TOTAL NUMBER OF RECORDS FOR TABLE REL_CTR_HIER_REF : 24151811
    CREATE TABLE REL_TXN_ACT_CM
      ENT               VARCHAR2(4 BYTE),
      SUB_ENT           VARCHAR2(3 BYTE),
      POST_ACCT         VARCHAR2(9 BYTE),
      CTR               VARCHAR2(7 BYTE),
      POST_DATE         DATE,
      EFF_DATE          DATE,
      TXN_CODE          VARCHAR2(2 BYTE),
      TXN_TYPE          VARCHAR2(1 BYTE),
      TXN_AMOUNT        NUMBER(17,2),
      TXN_DESCR         VARCHAR2(46 BYTE),
      TXN_SOURCE        VARCHAR2(1 BYTE)
    CREATE INDEX REL_TXN_ACT_CM_IDX_01 ON REL_TXN_ACT_CM
    (ENT, SUB_ENT, POST_ACCT, POST_DATE, EFF_DATE,
    TXN_AMOUNT);
    CREATE INDEX REL_TXN_ACT_CM_IDX_PK ON REL_TXN_ACT_CM
    (ENT, SUB_ENT, CTR, POST_ACCT, POST_DATE,
    EFF_DATE, TXN_AMOUNT);
    TOTAL NUMBER OF RECORDS FOR TABLE REL_TXN_ACT_CM : 111042301
    CREATE TABLE REL_CLPR_TBOX_GL_TXN
      ORGANIZATION  VARCHAR2(10 BYTE)               NOT NULL,
      ACCOUNT       VARCHAR2(10 BYTE)               NOT NULL,
      APPLICATION   VARCHAR2(10 BYTE)               NOT NULL,
      AMOUNT        NUMBER(17,2)                    NOT NULL
    CREATE UNIQUE INDEX REL_CLPR_TBOX_GL_TXN_IDX ON REL_CLPR_TBOX_GL_TXN
    (ORGANIZATION, ACCOUNT, APPLICATION);
        DELETE FROM REL_CLPR_TBOX_GL_TXN;
        INSERT INTO REL_CLPR_TBOX_GL_TXN
          ORGANIZATION,
          ACCOUNT,
          APPLICATION,
          AMOUNT
        SELECT  --+ INDEX(T REL_TXN_ACT_CM_IDX_PK)
          SUBSTR(REL_CTR_HIER_REF.HIER_CODE, 1, 5) || '.....',
          TXN.POST_ACCT,
          'GL-' || SUBSTR(TXN.TXN_DESCR, 1, 3),
          SUM
            CASE
              WHEN TXN.TXN_CODE IN ('01', '21') THEN 1
                    WHEN TXN.TXN_CODE IN ('02', '22') THEN -1
                    ELSE 0
            END
            CASE
              WHEN REL_COA_REF.ACCT_TYPE IN ('01', '25', '30', '40', '90', '95') THEN 1
                    WHEN REL_COA_REF.ACCT_TYPE IN ('05', '10', '20', '35') THEN -1
                    ELSE 0
            END
            REL_COA_REF.SIGN_REVRSL
            TXN.TXN_AMOUNT
        FROM
          REL_TXN_ACT_CM REL_TXN
            INNER JOIN
          REL_CTR_HIER_REF
            ON
              REL_TXN.ENT = REL_CTR_HIER_REF.ENT AND
              REL_TXN.SUB_ENT = REL_CTR_HIER_REF.SUB_ENT AND
              REL_TXN.CTR = REL_CTR_HIER_REF.CTR_DETAIL
            INNER JOIN
          REL_COA_REF
            ON REL_TXN.POST_ACCT = REL_COA_REF.POST_ACCT
            INNER JOIN
          REL_ENT_REF
            ON
              REL_CTR_HIER_REF.ENT = REL_ENT_REF.ENT AND
              REL_CTR_HIER_REF.SUB_ENT = REL_ENT_REF.SUB_ENT
        WHERE
          REL_TXN.EFF_DATE BETWEEN L_MONTH AND LAST_DAY(L_MONTH) AND
          REL_CTR_HIER_REF.HIER_TBL_NUM = '001' AND
          REL_CTR_HIER_REF.SUM_FLAG = 'D' AND
          REL_CTR_HIER_REF.CTR_OR_HIER = REL_CTR_HIER_REF.CTR_DETAIL AND
          REL_CTR_HIER_REF.HIER_CODE BETWEEN 'AAA' AND 'ZZZ' AND
          REL_COA_REF.REL_TYPE = ' ' AND
          REL_COA_REF.ACCT_ID = 'ALPTER' AND
          REL_COA_REF.ACCT_LVL = '9' AND
          REL_ENT_REF.ROLL_ENT = '999' AND
          REL_ENT_REF.ROLL_SUB_ENT = '111'
        GROUP BY
          SUBSTR(REL_CTR_HIER_REF.HIER_CODE, 1, 5),
          REL_TXN.POST_ACCT,
          SUBSTR(REL_TXN.TXN_DESCR, 1, 3)
        HAVING
          SUM
            CASE
              WHEN REL_TXN.TXN_CODE IN ('01', '21') THEN 1
                    WHEN REL_TXN.TXN_CODE IN ('02', '22') THEN -1
                    ELSE 0
            END
            CASE
              WHEN REL_COA_REF.ACCT_TYPE IN ('01', '25', '30', '40', '90', '95') THEN 1
                    WHEN REL_COA_REF.ACCT_TYPE IN ('05', '10', '20', '35') THEN -1
                    ELSE 0
            END
            REL_COA_REF.SIGN_REVRSL
            REL_TXN.TXN_AMOUNT
          ) <> 0;
    [\CODE]
    While try to run the query(only select statement), it is taking around 3+ hours & while try to insert the same query in the table, getting error called ORA-00001: unique constraint (INSIGHT.CLPR_TBOX_GL_TXN_IDX) violated.
    [\CODE]
    How to tune & resolve this UNIQUE Contraint issue using without duplicates?

    Should the SELECT statement be returning duplicate rows? If you know that there are duplicate rows in the underlying tables, you could add a DISTINCT to the select. Which forces Oracle to do an extra sort which will slow down the insert. If you don't expect duplicate rows, you would need to figure out what join criteria is missing from your query and add that criteria.
    Justin

  • How to link the same horizontal page for two different vertical pages without duplicate them?

    Hey guys,
    Is there a way to link the same horizontal page on two different vertical pages without duplicate the horizontal page?
    I have a doublepage of a book splitted in two parts in different vertical pages but i want link the fullsized image in the horizontal page for both of them. Got that? hahaha
    Thank you all

    Confusing But interesing.
    I think it's possible. I have a idea. It will need one advanced trick.
    To explain it, I need test simply haha.

  • Outer Join without Outer Join

    I'm trying to do is learn how to do an outer join without specifying right, left or full outer join. Why? Performance reasons mostly and to gain a better understanding of Oracle.
    Thanks,
    Charles.

    Another option would be a scalar sub-query in the select portion, like:
    SQL> select * from t;
            ID DESCR
             1 T One
             2 T Two
             3 T Three
    SQL> select * from t1;
            ID DESCR
             1 T1 One
             2 T1 Two
    SQL> select t.*, t1.descr t1_descr
      2  from t
      3     left join t1
      4        on t.id = t1.id;
            ID DESCR      T1_DESCR
             1 T One      T1 One
             2 T Two      T1 Two
             3 T Three
    SQL> select t.*, (select t1.descr
      2               from t1
      3               where t.id = t1.id) t1_descr
      4  from t;
            ID DESCR      T1_DESCR
             1 T One      T1 One
             2 T Two      T1 Two
             3 T ThreeThis can be faster if the correlated column in t1 is indexed, and there are a "large" number of records in t that do not match with t1.
    John

  • How to add 5.1 track without duplicate timelines?

    To add  a 5.1 track to my project I duplicate the movie timeline and change the stereo track for the 5.1. The problem is the program duplicate the capacity (3,50gb are now 7gb). I don't know if I can add the 5.1 as a second track in the same timeline to avoid the increasing in gb.
    is possible to add the 5.1 in a second track of the movie timeline? and how can I link a button in the menu to link to the movie with this second 5.1 track?
    If this isn't possible, how can I do a 5.1 option without duplicate the weight?
    sorry my poor English!
    Thanks

    Jeff, if I have one timeline with two audio tracks (stereo and 5.1) and the video track. If I have two buttons, stereo and 5.1, how I link the button to the video track with it correct audio track avoiding the other? I need to link the button direct to the audio track and this link also include the video track?
    Thanks for your help

  • Random numbers without duplicates

    Hello,
    I'm having trouble locating an explained example for selecting a set of random numbers without duplicates. Specifically, I'm working on this example from Ivor Horton's Beginning Java, chapter 3.
    3. A lottery program requires that you select six different numbers from the integers 1-49. Write a program to do this for you and generate five sets of entries.
    This is not homework, I'm just trying to teach myself Java to eventually work my way up to servlets and JSPs.
    This is what I have so far. I tried to use array sorting but it didn't work (the first three numbers in my entries was always 0), so I commented out that part. I've included sample output after the code. I don't necessarily need the exact code although that would be nice; I just need at least an explanation of how to go about making sure that if a number has been picked for an entry, not to pick it again.
    Thanks for any help,
    javahombre
    package model;
    import java.util.Random;
    import java.lang.Math.*;
    import java.util.Arrays;
    public class Lottery {
    public static void main(String[] args) {
    // Lottery example.
    // We need the following data:
    // - An integer to store the lottery number range (in this case 59).
    // - A mechanism to choose a number randomly from the range.
    // - An array to store the entries (6 different numbers per entry).
    // - A loop to generate 5 sets of entries.
    int numberRange = 59;
    int currentPick = 0;
    int[] entry = new int[6]; // For storing entries.
    //int slot = -1; // For searching entry array.
    Random rn = new Random();
    for (int n = 0; n < 5; n++)
    // Generate entry.
    for (int i = 0; i < 6; i++)
    currentPick = 1 + Math.abs(rn.nextInt()) % numberRange;
    //Arrays.sort(entry);
    //slot = Arrays.binarySearch(entry, currentPick);
    //System.out.println("i: " + i + "\t| slot: " + slot + "\t| pick: " + currentPick + "\t| compare: " + (slot < 0));
    // Add the current pick if it is not already in the entry.
    //if (slot < 0)
    entry[i] = currentPick;
    System.out.println("pick entered: " + currentPick);
    // Output entry.
    System.out.print("Entry " + (n + 1) + ": ");
    for (int j = 0; j < 6; j++)
    System.out.print(entry[j] + "\t");
    System.out.println("");
    // Set the array contents back to 0 for next entry.
    Arrays.fill(entry,0);
    Sample output (notice duplicates, as in Entry 3):
    pick entered: 11
    pick entered: 29
    pick entered: 33
    pick entered: 8
    pick entered: 14
    pick entered: 54
    Entry 1: 11     29     33     8     14     54     
    pick entered: 51
    pick entered: 46
    pick entered: 25
    pick entered: 30
    pick entered: 44
    pick entered: 22
    Entry 2: 51     46     25     30     44     22     
    pick entered: 49
    pick entered: 39
    pick entered: 9
    pick entered: 6
    pick entered: 46
    pick entered: 46
    Entry 3: 49     39     9     6     46     46     
    pick entered: 23
    pick entered: 26
    pick entered: 2
    pick entered: 21
    pick entered: 51
    pick entered: 32
    Entry 4: 23     26     2     21     51     32     
    pick entered: 27
    pick entered: 48
    pick entered: 19
    pick entered: 10
    pick entered: 8
    pick entered: 18
    Entry 5: 27     48     19     10     8     18     

    NOTE: the array reference to [ i ] seems to be misinterpreted as italics by the posting form. I've reposted the message here, hopefully it will show the code properly.
    Thanks again,
    javahombre
    Hello,
    I'm having trouble locating an explained example for
    selecting a set of random numbers without duplicates.
    Specifically, I'm working on this example from Ivor
    Horton's Beginning Java, chapter 3.
    3. A lottery program requires that you select six
    different numbers from the integers 1-49. Write a
    program to do this for you and generate five sets of
    entries.
    This is not homework, I'm just trying to teach myself
    Java to eventually work my way up to servlets and
    JSPs.
    This is what I have so far. I tried to use array
    sorting but it didn't work (the first three numbers in
    my entries was always 0), so I commented out that
    part. I've included sample output after the code. I
    don't necessarily need the exact code although that
    would be nice; I just need at least an explanation of
    how to go about making sure that if a number has been
    picked for an entry, not to pick it again.
    Thanks for any help,
    javahombre
    package model;
    import java.util.Random;
    import java.lang.Math.*;
    import java.util.Arrays;
    public class Lottery {
    public static void main(String[] args) {
    // Lottery example.
    // We need the following data:
    // - An integer to store the lottery number range (in
    this case 59).
    // - A mechanism to choose a number randomly from the
    range.
    // - An array to store the entries (6 different
    numbers per entry).
    // - A loop to generate 5 sets of entries.
    int numberRange = 59;
    int currentPick = 0;
    int[] entry = new int[6]; // For storing entries.
    //int slot = -1; // For searching entry
    array.
    Random rn = new Random();
    for (int n = 0; n < 5; n++)
    // Generate entry.
    for (int i = 0; i < 6; i++)
    currentPick = 1 + Math.abs(rn.nextInt()) %
    numberRange;
    //Arrays.sort(entry);
    //slot = Arrays.binarySearch(entry, currentPick);
    //System.out.println("i: " + i + "\t| slot: " + slot +
    "\t| pick: " + currentPick + "\t| compare: " + (slot <
    0));
    // Add the current pick if it is not already in the
    entry.
    //if (slot < 0)
    entry[ i ] = currentPick;
    System.out.println("pick entered: " + currentPick);
    // Output entry.
    System.out.print("Entry " + (n + 1) + ": ");
    for (int j = 0; j < 6; j++)
    System.out.print(entry[j] + "\t");
    System.out.println("");
    // Set the array contents back to 0 for next entry.
    Arrays.fill(entry,0);
    Sample output (notice duplicates, as in Entry 3):
    pick entered: 11
    pick entered: 29
    pick entered: 33
    pick entered: 8
    pick entered: 14
    pick entered: 54
    Entry 1: 11     29     33     8     14     54
    pick entered: 51
    pick entered: 46
    pick entered: 25
    pick entered: 30
    pick entered: 44
    pick entered: 22
    Entry 2: 51     46     25     30     44     22
    pick entered: 49
    pick entered: 39
    pick entered: 9
    pick entered: 6
    pick entered: 46
    pick entered: 46
    Entry 3: 49     39     9     6     46     46
    pick entered: 23
    pick entered: 26
    pick entered: 2
    pick entered: 21
    pick entered: 51
    pick entered: 32
    Entry 4: 23     26     2     21     51     32
    pick entered: 27
    pick entered: 48
    pick entered: 19
    pick entered: 10
    pick entered: 8
    pick entered: 18
    Entry 5: 27     48     19     10     8     18

  • How to sync iPhone photos with iPad and Mac without duplicates and deleted photos?

    I have a MacBook Pro (OS X 10.8.5, iPhoto 9.4.3), an iPad2 (ios 6.1.3) and iPhone (ios 7.0.4).  I take my pictures on the iPhone and use the iPad for viewing.  I use the MBP for storage and backup.  ios 7 seems to organize the photos nicely, so I no longer need iPhoto for organizing.
    What is the best, easiest and most efficient way to share, save and backup photos (iCloud prefered versus plugging devices into the MBP) between the 3 devices without duplicates and deleted photos?  With Photo Streaming turned on, I get duplicates and it retains photos that I have deleted.
    On all three devices, I turned on Photos & Camera > My Photo Stream and Shared Photo Streams and iCloud > Photo Stream.
    On the MBP, I have everything under Photo Stream EXCEPT Automatic Import (I took off recently hoping to eliminate duplicates).
    If someone else already asked this question, I apologize.  This is very confusing.

    While waiting for a reply, you can read the documentation that was in the box your iPad came in.  It's a book about a Spanish guy called Manual.
    The answer is iTunes.  Download it.  Install it.  Read the instructions.  Use it.

  • How to display all distinct values without duplicates.

    Hi Pros,
          I want to present a values list in dashboard, but this list have much duplicates, so when dispalying, I want to display all distinct values without duplicates.

    Hi,
    You can avoid the duplicates from the source side or use a filtered row option in the component.
    Arun

  • How do i restore photo albums from time machine backups without duplicates?

    how do i restore photo albums without duplicates from time machine backups? thx

    Worth updating?  Totally a personal decision. For some it would be. For others it would not?
    Upgrading, if there are no problems, does not affect your data. And since bad things do happen you always have a good backup. Especially when doing any upgrade.
    Your password question has nothing to do with iPhoto. Try it in the OS forum
    LN

  • New airport: can't get my macbook or my imac to join my wireless automatically, my other stuff still joins without problems but i have to join manually with the book and the imac ?? I've tried all the settings and every thing else I can think of.....

    new airport: can't get my macbook or my imac to join my wireless automatically, my other stuff still joins without problems but i have to join manually with the book and the imac ?? I've tried all the settings and every thing else I can think of.....
    Running on Lion, never had a problem before until the old airport died and I replaced it.

    try connecting the device in _*recovery mode*_, then restore from your backup.
    also, make sure [_*Apple Mobile Device Service*_|http://support.apple.com/kb/TS1567] is installed and started.
    JGG

  • How do you merge iphoto libraries without duplicates and maintaining events?

    I have a few iphoto libraries on my computer that I need to merge, without duplicates, and maintaining the seperate events.  Does anyone know how to do this efficiently?

    "Terence Devlin" does not work for Apple, for the makers of Library Manager and has no beneficial interest in whether or not you use or purchase this or any other app. If "Terence Devlin" did have such an interest the terms of use of the Forum would require that he add such information to the post. No, Aperture cannot merge iPhoto Libraries. Aperture can convert them to Aperture Libraries and merge them, but that would be of no use to an iPhoto user, as iPhoto can't open an Aperture Library.
    And the fact remains that the paid version of Library Manager is the only way to merge Libraries
    Regards
    "TD"

  • EXTRACT VALUES WITHOUT DUPLICATES

    How can I extract data without duplicates??
    I can't use DISTINCT....
    Thank you

    Hi,
    DISTINCT and UNIQUE keywords performs same in SQL and no difference in usage. Just write,
    SELECT UNIQUE <column_name>
    FROM <table_name>
    WHERE ...
    Regards,
    Dilek

  • Join multiple tables(6 tables) without duplicates

    Hi guys,
    I am having a problem with SQL query. I have 6 tables (Product,Manufacturer, ProductSpecs, GeneralSpecs, ProductConf, GeneralConf)
    1. Product and Manufacturer are referenced with ManufacturerID
    2. ProductSpecs and GeneralSpecs are referenced with SpecID
    3. ProductConf and GeneralConf are referenced with ConfID
    4. Product,ProductSpecs and ProductConf are referenced with Product_ID
    My requirement is to get the list of a particular product for each manufacturer with the list of unique Product Specification and Product Configuration. I had joined all the tables using LEFT OUTER JOIN, but i am getting numerous duplicate records. I need an efficient query so that I get all the information without any duplicates.
    Thanks in advance
    Arun

    Avery wild guess!
    Adjust the columns you want.
    SELECT P.columns_youwant,
           M.columns_youwant,
           ps.both_ps_and_gs_colummns_youwant,
           pc.both_pc_andgc_columns_youwant
      FROM Product P,
           Manufacturer M,
           (SELECT ps.product_id,
                   ps.specid,
                   gs.columns_you_want
              FROM ProductSpecs ps, GeneralSpecs gs
             WHERE ps.specid = gs.specid(+)) ps,
           (SELECT pc.productid,
                   pc.ConfID,
                   gc.columns_youwant
              FROM ProductConf pc, GeneralConf gc
             WHERE pc.ConfID = gc.ConfID(+)) pc
    WHERE P.ManufacturerID = M.ManufacturerID(+)
       AND P.prduct_id = ps.product_id(+)
       AND P.product_id = pc.product_id(+)G.

  • Sql join without combining all rows

    I've searched questions but can't seem to apply to my scenario.  Please see attached code below.  I am trying to join two subsets of this data but am getting duplicates/too many rows please see subsets below main query and main results (i'm hoping
    you can understand by providing data/examples without table defs as tables are enormous) (please excuse formatting i don't know how to line everything up in this) :
        IF OBJECT_ID(N'tempdb..#TRANS', N'U') IS NOT NULL
            BEGIN
                DROP TABLE #TRANS;
            END
        DECLARE @Item NVARCHAR(6) = 'AAS682' ,
            @ExpiryDate AS DATETIME = '2015-01-10 00:00:00.000'
        SELECT  ITO.REFERENCEID ,
                WP.PRODID ,
                WP.WMSPALLETID ,
                ITR.ITEMID ,
                ITR.QTY ,
                ITR.STATUSISSUE ,
                ITR.STATUSRECEIPT ,
                ITO.REFERENCECATEGORY ,
                PT.USEBYDATE
        INTO    #TRANS
        FROM    dbo.INVENTTRANS AS ITR
                JOIN INVENTDIM AS ID ON ID.INVENTDIMID = ITR.INVENTDIMID
                JOIN dbo.WMSPALLET AS WP ON WP.WMSPALLETID = ID.WMSPALLETID
                JOIN dbo.PRODTABLE AS PT ON PT.PRODID = WP.PRODID
                JOIN dbo.INVENTTRANSORIGIN AS ITO ON ITR.INVENTTRANSORIGIN = ITO.RECID
        WHERE   ITO.REFERENCECATEGORY IN ( 0, 2, 8 )
                AND PT.USEBYDATE = @ExpiryDate;
        SELECT * FROM #TRANS
    Which gives me :
        REFERENCEID PRODID WMSPALLETID ITEMID QTY STATUSISSUE STATUSRECEIPT REFERENCECATEGORY USEBYDATE
        M0000042 M0000042 1288390 BAS242 255.0000000000000000 0 2 2 2015-01-10 00:00:00.000
        M0000042 M0000042 1288391 BAS242 255.0000000000000000 0 2 2 2015-01-10 00:00:00.000
        M0000042 M0000042 1288392 BAS242 255.0000000000000000 0 2 2 2015-01-10 00:00:00.000
        M0000042 M0000042 1288393 BAS242 255.0000000000000000 0 2 2 2015-01-10 00:00:00.000
        M0000042 M0000042 1288394 BAS242 255.0000000000000000 0 2 2 2015-01-10 00:00:00.000
        M0000043 M0000043 1288395 BAS243 255.0000000000000000 0 2 2 2015-01-10 00:00:00.000
        M0000043 M0000043 1288396 BAS243 255.0000000000000000 0 2 2 2015-01-10 00:00:00.000
        M0000043 M0000043 1288397 BAS243 255.0000000000000000 0 2 2 2015-01-10 00:00:00.000
        M0000043 M0000043 1288398 BAS243 255.0000000000000000 0 2 2 2015-01-10 00:00:00.000
        M0000043 M0000043 1288399 BAS243 255.0000000000000000 0 2 2 2015-01-10 00:00:00.000
        M0000044 M0000044 1288400 AAS682 168.0000000000000000 0 2 2 2015-01-10 00:00:00.000
        M0000044 M0000044 1288401 AAS682 168.0000000000000000 0 2 2 2015-01-10 00:00:00.000
        M0000044 M0000044 1288402 AAS682 168.0000000000000000 0 2 2 2015-01-10 00:00:00.000
        M0000044 M0000044 1288403 AAS682 168.0000000000000000 0 2 2 2015-01-10 00:00:00.000
        M0000044 M0000044 1288404 AAS682 168.0000000000000000 0 2 2 2015-01-10 00:00:00.000
        M0000044 M0000042 1288390 BAS242 -255.0000000000000000 2 0 8 2015-01-10 00:00:00.000
        M0000044 M0000042 1288391 BAS242 -255.0000000000000000 2 0 8 2015-01-10 00:00:00.000
        M0000044 M0000042 1288392 BAS242 -50.0000000000000000 2 0 8 2015-01-10 00:00:00.000
        M0000044 M0000043 1288395 BAS243 -255.0000000000000000 2 0 8 2015-01-10 00:00:00.000
        M0000044 M0000043 1288396 BAS243 -255.0000000000000000 2 0 8 2015-01-10 00:00:00.000
        M0000044 M0000043 1288397 BAS243 -50.0000000000000000 2 0 8 2015-01-10 00:00:00.000
        0013949 M0000044 1288400 AAS682 -32.0000000000000000 2 0 0 2015-01-10 00:00:00.000
        0013949 M0000044 1288402 AAS682 -168.0000000000000000 2 0 0 2015-01-10 00:00:00.000
        0013951 M0000044 1288400 AAS682 -35.0000000000000000 2 0 0 2015-01-10 00:00:00.000
        0013951 M0000044 1288401 AAS682 -29.0000000000000000 2 0 0 2015-01-10 00:00:00.000
        0013952 M0000044 1288400 AAS682 -28.0000000000000000 2 0 0 2015-01-10 00:00:00.000
        0013953 M0000044 1288400 AAS682 -73.0000000000000000 2 0 0 2015-01-10 00:00:00.000
    I want to join these two query result sets
        WITH    ACODE
                  AS ( SELECT   *
                       FROM     #TRANS
                       WHERE    ITEMID = @Item
                                AND STATUSISSUE IN ( 1, 2 )
            SELECT  *
            FROM    ACODE
        SELECT  *
        FROM    #TRANS
        WHERE   REFERENCECATEGORY = 8
        REFERENCEID PRODID WMSPALLETID ITEMID QTY STATUSISSUE STATUSRECEIPT REFERENCECATEGORY USEBYDATE
        0013949 M0000044 1288400 AAS682 -32.0000000000000000 2 0 0 2015-01-10 00:00:00.000
        0013949 M0000044 1288402 AAS682 -168.0000000000000000 2 0 0 2015-01-10 00:00:00.000
        0013951 M0000044 1288400 AAS682 -35.0000000000000000 2 0 0 2015-01-10 00:00:00.000
        0013951 M0000044 1288401 AAS682 -29.0000000000000000 2 0 0 2015-01-10 00:00:00.000
        0013952 M0000044 1288400 AAS682 -28.0000000000000000 2 0 0 2015-01-10 00:00:00.000
        0013953 M0000044 1288400 AAS682 -73.0000000000000000 2 0 0 2015-01-10 00:00:00.000
    with this
        REFERENCEID PRODID WMSPALLETID ITEMID QTY STATUSISSUE STATUSRECEIPT REFERENCECATEGORY USEBYDATE
        M0000044 M0000042 1288390 BAS242 -255.0000000000000000 2 0 8 2015-01-10 00:00:00.000
        M0000044 M0000042 1288391 BAS242 -255.0000000000000000 2 0 8 2015-01-10 00:00:00.000
        M0000044 M0000042 1288392 BAS242 -50.0000000000000000 2 0 8 2015-01-10 00:00:00.000
        M0000044 M0000043 1288395 BAS243 -255.0000000000000000 2 0 8 2015-01-10 00:00:00.000
        M0000044 M0000043 1288396 BAS243 -255.0000000000000000 2 0 8 2015-01-10 00:00:00.000
        M0000044 M0000043 1288397 BAS243 -50.0000000000000000 2 0 8 2015-01-10 00:00:00.000
    I tried
        SELECT  ACODE.REFERENCEID ,
                    ACODE.PRODID ,
                    ACODE.WMSPALLETID ,
                    ACODE.ITEMID ,
                    ACODE.QTY ,
                    TRANS.REFERENCEID ,
                    TRANS.PRODID ,
                    TRANS.WMSPALLETID ,
                    TRANS.ITEMID ,
                    TRANS.QTY
            FROM    ACODE
                    CROSS APPLY ( SELECT    REFERENCEID ,
                                            PRODID ,
                                            WMSPALLETID ,
                                            ITEMID ,
                                            QTY
                                  FROM      #TRANS
                                  WHERE     ACODE.PRODID != #TRANS.PRODID
                                            AND REFERENCECATEGORY
    = 8
                                ) TRANS
    but this gave me 6 * 6 36 rows... I want to just bolt the right query on to the end of the left query.  exactly like a union
        SELECT  *
                                FROM    ACODE
                                UNION
                                ( SELECT    *
                                  FROM      #TRANS
                                  WHERE     REFERENCECATEGORY = 8
        REFERENCEID PRODID WMSPALLETID ITEMID QTY STATUSISSUE STATUSRECEIPT REFERENCECATEGORY USEBYDATE
        0013949 M0000044 1288400 AAS682 -32.0000000000000000 2 0 0 2015-01-10 00:00:00.000
        0013949 M0000044 1288402 AAS682 -168.0000000000000000 2 0 0 2015-01-10 00:00:00.000
        0013951 M0000044 1288400 AAS682 -35.0000000000000000 2 0 0 2015-01-10 00:00:00.000
        0013951 M0000044 1288401 AAS682 -29.0000000000000000 2 0 0 2015-01-10 00:00:00.000
        0013952 M0000044 1288400 AAS682 -28.0000000000000000 2 0 0 2015-01-10 00:00:00.000
        0013953 M0000044 1288400 AAS682 -73.0000000000000000 2 0 0 2015-01-10 00:00:00.000
        M0000044 M0000042 1288390 BAS242 -255.0000000000000000 2 0 8 2015-01-10 00:00:00.000
        M0000044 M0000042 1288391 BAS242 -255.0000000000000000 2 0 8 2015-01-10 00:00:00.000
        M0000044 M0000042 1288392 BAS242 -50.0000000000000000 2 0 8 2015-01-10 00:00:00.000
        M0000044 M0000043 1288395 BAS243 -255.0000000000000000 2 0 8 2015-01-10 00:00:00.000
        M0000044 M0000043 1288396 BAS243 -255.0000000000000000 2 0 8 2015-01-10 00:00:00.000
        M0000044 M0000043 1288397 BAS243 -50.0000000000000000 2 0 8 2015-01-10 00:00:00.000
    except the bottom 6 rows attached to the right of the query... Basically the 'B' items make up the 'A' item.  Which is the purpose of this report, but all the data exists in the same tables which is what i'm really struggling with... thanks!

    There's a simple answer to this. If the relationship between the tables does not exist to give you the results you want,
    you need to CREATE your own relationship. I used ROW_NUMBER OVER (ORDER BY PALLETID) to give me unique ID's for both datasets. THen simply join ACODEID = BCODEID. (make sure you check different joins to make sure you don't exclude rows from one
    side or the other) Simple!

  • How to transfer data from one table to another without duplicates

    This is what I use to quote bathroom remodels. The first thing I do is completely fill in the Master Item List with every Product or Service we can think of to do the project. The Type column has a pop-up menu which includes Combo because some Contractors don't separate the Product from Labor. The Category Totals Table is useful but since some Contractors provide Product and Service at the same time on one invoice I don't get accurate breakdowns for generating my invoices.
    What I would like is to transfer all Vendor names included in the project to the Vendor Totals Table but without any duplicates. Some Contractors might perform services in several categories. I've read many listings in the forum and am just not seeing the answer.
    I have a sample file ready to send. I don't see a way to send it with this message. First Post! Sorry!
    Thank You
    Jeff

    jwoods007 wrote:
    What I would like is to transfer all Vendor names included in the project to the Vendor Totals Table but without any duplicates.
    Hi Jeff,
    Welcome to Apple Discussions and the Numbers '09 forum.
    If your Vendor names (including duplicates) are all in the same column of the source table, it shouldn't be difficult to transfer them to a second table using the technique described below.
    For the example, the source table is named Main and has one header row, the Vendors are listed in column C (starting at C2), and column B is used as an index column. Note that to use VLOOKUP, the index column must be to the left of the Vendor column.
    The index is generated using the following formula in B2, and filled down to the end of the column:
    =IF(COUNTIF(C$1:C2,C)=1,MAX($B$1:B1)+1,"")
    On the second table, Vendor List, the following formula is used in Column A (starting at A2) to collect the indexed vendors from the Main table"
    =IFERROR(VLOOKUP(ROW()-1,Main :: B:C,2,FALSE),"")
    IFERROR is used to trap the "couldn't find" error in the 'empty' rows of Vendor List.
    'FALSE' displays as "exact-match" in the formula, and prevents the rpeated listing of the last vendor that would be created by 'close-match'.
    Regards,
    Barry

Maybe you are looking for

  • Images open in wrong version of Photoshop

    I have both Photoshop CC and Photoshop CC 2014 installed because some actions I have purchased only work in Photoshop CC due to them using the Oil Painting filter. But if I double click an image to open it, it opens in Photoshop CC. So I right click

  • How do I convert an arrayList to an array []

    I want to create a generic method that I can pass a Collection ( An ArrayList if I have to be specific ) and also tell this method the underlying Object type the Collection contains and have it call the Collections.toArray method and create an array

  • Unable to access  Remote EJB with jndi.properties in classpath

    Hi I'm trying to use remote interfaces with my adf web layer. Created remote datacontrol for my model part and my model EAR is deployed in another Oracle App Server instance(S1). My web layer is deployed in another Oracle App Server(S2). My page def

  • Ping 1202 error, also can't rate music or write a review

    I have been unable to ever get Ping to work. When I try to set up my profile I get a -1202 error. Around the same time Ping refused to work (since it was introduced) I notice I now also cannot rate a song, I get an error message, and I cannot write a

  • Efibootmgr without sudo

    Lets say I want to reboot into windows. I need to change the boot order using sudo efibootmgr -n, but the problem is I need to type the root password everytime. How to make this process passwordless? I tried making a sudoers rule for efibootmgr but n