1. Welcome! Please take a few seconds to create your free account to post threads, make some friends, remove a few ads while surfing and much more. ClutchFans has been bringing fans together to talk Houston Sports since 1996. Join us!

Any SQL Nerds available?

Discussion in 'BBS Hangout' started by aeroman10, Aug 5, 2013.

Tags:
  1. aeroman10

    aeroman10 Contributing Member

    Joined:
    Nov 2, 2003
    Messages:
    2,695
    Likes Received:
    50
    Looking for someone who is good with SQL that may know a solution.

    Have 2 tables. Ex:

    T1
    Name Col1 Col 2
    John 1 5
    Joe 0 0
    Jack 0 0
    Jill 6 9
    Blah 0 0


    T1
    Name Col1 Col 2
    John 1 5
    Joe 3 7
    Jack 2 4


    I want to combine the 2 tables (UNION) but if there are same Names between T1 and T2 then only use the record from T2

    Is this possible?

    So the desired outcome would be


    Name Col1 Col 2
    John 1 5
    Joe 3 7
    Jack 2 4
    Jill 6 9
    Blah 0 0


    As it stands now UNION creates duplicate records based on Name because the rest of the data is different. I tried different JOINS and that didnt work either.

    Not sure if what I want is even possible.
     
  2. Kyakko

    Kyakko Contributing Member

    Joined:
    Aug 15, 2002
    Messages:
    2,161
    Likes Received:
    39
    select DISTINCT perhaps?
     
  3. DarkHorse

    DarkHorse Contributing Member

    Joined:
    Oct 9, 1999
    Messages:
    6,733
    Likes Received:
    1,247
    Between the IT thread and this, I think I'm going to start charging to do people's homework for them.
     
  4. Lil Pun

    Lil Pun Contributing Member

    Joined:
    Oct 6, 1999
    Messages:
    34,132
    Likes Received:
    1,021
    Can you use a LEFT JOIN

    Example:

    SELECT DISTINCT T1_NAME, T1_COL1, T1_COL2

    FROM
    T1


    LEFT JOIN T2

    ON T1_NAME = T2_NAME


    WHERE

    T1_COL1 <> 0 ;
     
  5. SwoLy-D

    SwoLy-D Contributing Member

    Joined:
    Jul 20, 2001
    Messages:
    37,617
    Likes Received:
    1,448
    Code:
    SELECT * FROM BOOKS_AEROMAN10_CAN_READ WHERE SUBJECT LIKE 'SQL'
    :p

    Lil Pun has a place for you to start.

    This is basic SQL stuff, man... it just seems to me like you could learn these things yourself FIRST before you ask peeps here. Also, don't use the word 'Nerd' if you want help. You can call us GURUs or GENIUSes, but... that's cool. :cool:
     
  6. aeroman10

    aeroman10 Contributing Member

    Joined:
    Nov 2, 2003
    Messages:
    2,695
    Likes Received:
    50
    Hey genius, this is not that simple. I was just making a joke in the thread title. Sorry to offend you Mr. Genius.

    Lil Pun
    Thanks for the suggestion but that won't work. I just used a basic example but the real data has 10 columns and the entry can be anything between 0 and 9

    Also, this is not homework. This is a real life example.

    I have already tried UNION and every possible JOIN

    Basically I am looking for a query that will do the following:
    -Union 2 tables but before that compare the 2 and if any values are the same in the 'Name' column between the two then to only use the entry from the 2nd table

    SwoLy-D - if that is really that simple then prove that you are a genius and post a query
     
  7. SwoLy-D

    SwoLy-D Contributing Member

    Joined:
    Jul 20, 2001
    Messages:
    37,617
    Likes Received:
    1,448
    One of those two tables has to be the "important" one with the data that is to be kept, if you're getting duplicates (basically, an "IF" statement that makes one the one to grab over the other one to say "this is the important one, if there is something in this one, grab it.") Now go learn more GENIUS stuff. ;)

    How about you post all those possible UNIONs and JOINs that you said you tried? We can decipher those and tell you what you're doing wrong. I will not post a query, because I want to see what you try. That's a lot easier because (supposedly), you've done the work. Right? Who's the nerd now?
     
  8. papadrunk

    papadrunk Member

    Joined:
    Nov 4, 2007
    Messages:
    86
    Likes Received:
    16
    Is there a need for Swoly-d*ckless to be an a-hole in every Clutchfans thread?
     
    1 person likes this.
  9. aeroman10

    aeroman10 Contributing Member

    Joined:
    Nov 2, 2003
    Messages:
    2,695
    Likes Received:
    50
    Seriously, what the hell is this? 3rd grade?
    I am not doing homework so its not like I am looking for someone to help me cheat. I am not an expert at SQL, just trying to figure something out on my own. I searched all over and couldn't find a solution because this is not a simple query type of thing. I will just wait for our DBA to return and ask him.

    You Swoly-D are certainly a genius at being a total @$$ but not much else.
     
  10. DrLudicrous

    DrLudicrous Contributing Member

    Joined:
    May 9, 2002
    Messages:
    3,936
    Likes Received:
    203
    What database system are you using? A join should work (you may have to use a full outer join if names don't exist in both tables) and then a series of case statements to determine which table to get data from. I'm not sure if case statements are specific to MSSQL or standard SQL but other systems will have something similar.

    Select
    case when t2.name is not null then t2.name else t1.name end as finalName,
    case when t2.col1 is not null then t2.col11 else t1.col1 end as finalValue
    From t1
    Full outer Join t2
    on t1.name = t2.name

    I haven't tested that code but the basic idea should work.
     
  11. SaFe

    SaFe Contributing Member

    Joined:
    Aug 30, 2000
    Messages:
    337
    Likes Received:
    7
    I'm not expert or anything, but isn't this a simple inner join...

    Select * From T1 inner join T2 on T1.Name = T2.Name

    Then just update T1.col# with T2.col#.

    Unless I'm missing something here... :confused:
     
    #11 SaFe, Aug 5, 2013
    Last edited: Aug 5, 2013
  12. SwoLy-D

    SwoLy-D Contributing Member

    Joined:
    Jul 20, 2001
    Messages:
    37,617
    Likes Received:
    1,448
    ^ Like I said before, it was a join and a union, like Pun said. But we need to see what he has.
    So... asking him for the queries he's tried... isn't a good thing?

    Prove to me you're really trying, aeroman. It's a simple request. We nerds will take it from where ever you're stuck. :eek:
     
  13. aeroman10

    aeroman10 Contributing Member

    Joined:
    Nov 2, 2003
    Messages:
    2,695
    Likes Received:
    50
    I think I figured it out. Not getting duplicates anymore and the total number of records match up. I just have to make sure that the correct records remained in the final product but here is the code I used where T1 is the table with the most records and T2 is a smaller table that contains some of the Names that T1 has. Goal is to UNION the 2 but if any 'Names' from T1 = T2 then only use the T2

    SELECT * INTO #Temp FROM T1
    WHERE NOT EXISTS (SELECT * FROM T2
    WHERE T1.Name = T2.Name)

    SELECT * FROM #Temp UNION SELECT * FROM T2
    DROP TABLE #Temp
     
    1 person likes this.
  14. Lil Pun

    Lil Pun Contributing Member

    Joined:
    Oct 6, 1999
    Messages:
    34,132
    Likes Received:
    1,021
    Thanks for posting what you used.
     
  15. aeroman10

    aeroman10 Contributing Member

    Joined:
    Nov 2, 2003
    Messages:
    2,695
    Likes Received:
    50
    Some more info - just in case it helps someone like me looking for the same thing...

    After setting up the code I posted earlier, I tried using the WHERE clause at the end and it did not work. It was almost as if it was ignored completely. So to get the WHERE to work I had to further modify the query to this:

    SELECT * INTO #Temp FROM T1
    WHERE NOT EXISTS (SELECT * FROM T2
    WHERE T1.Name = T2.Name)

    SELECT * INTO #Temp2 FROM ((SELECT * FROM #Temp UNION SELECT * FROM T2)) X

    SELECT *
    FROM #Temp2
    WHERE Name ='search term'

    DROP TABLE #Temp
    DROP TABLE #Temp2
     
  16. Dutchrudder!

    Dutchrudder! Member

    Joined:
    Mar 9, 2011
    Messages:
    214
    Likes Received:
    115
    Odd, I always thought being called a nerd was a compliment. I guess I'll have to slap my girlfriend around the next time she calls me that...
     
  17. DrLudicrous

    DrLudicrous Contributing Member

    Joined:
    May 9, 2002
    Messages:
    3,936
    Likes Received:
    203
    Putting the where clause in both parts of the union should work.
     
  18. dragician

    dragician Member

    Joined:
    Feb 27, 2011
    Messages:
    3,990
    Likes Received:
    131
    create table t1
    ( fname varchar2(10), rank integer );

    create table t2
    ( fname varchar2(10), rank integer );

    insert into t1
    select 'abc', 1 from dual
    union
    select 'xyz', 1 from dual;

    insert into t2
    select 'abc', 1 from dual
    union
    select 'xyz', 3 from dual
    union
    select 'opq', 2 from dual;

    commit;

    select * from t1;
    select * from t2;

    SELECT nvl(t1.fname, t2.fname),
    nvl(t2.rank, t1.rank)
    FROM t1, t2
    WHERE t1.fname(+) = t2.fname
     
  19. Jimes

    Jimes Member

    Joined:
    Mar 28, 2006
    Messages:
    868
    Likes Received:
    27
    Yeah, this should work. I would use coalesce for clarity if it's available in whatever version of SQL you are using.

    Select
    coalesce (t2.name, t1.name) as name,
    coalesce (t2.col1, t1.col1) as col1,
    coalesce (t2.col2, t1.col2) as col2
    From t1
    Full outer Join t2
    on t1.name = t2.name;

    This is cleaner than using temp tables.
     
  20. mrm32

    mrm32 Member

    Joined:
    Jun 26, 2006
    Messages:
    10,457
    Likes Received:
    2,327
    My company offers online training courses for various subjects, one of then being SQL. What would be a good to start learning first? What should I focus on?
     

Share This Page

  • About ClutchFans

    Since 1996, ClutchFans has been loud and proud covering the Houston Rockets, helping set an industry standard for team fan sites. The forums have been a home for Houston sports fans as well as basketball fanatics around the globe.

  • Support ClutchFans!

    If you find that ClutchFans is a valuable resource for you, please consider becoming a Supporting Member. Supporting Members can upload photos and attachments directly to their posts, customize their user title and more. Gold Supporters see zero ads!


    Upgrade Now