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.
Between the IT thread and this, I think I'm going to start charging to do people's homework for them.
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 ;
Code: SELECT * FROM BOOKS_AEROMAN10_CAN_READ WHERE SUBJECT LIKE 'SQL' 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.
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
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?
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.
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.
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...
^ 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.
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
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
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...
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
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.
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?