SQL query to add a column based on matches in two rows?
-
I have fields as Name, Org ID, email, designation. Now, I want to add a new column with email where it matches the org ID and particular designation. For example, the user_table is like Name Org ID, Email, Designation Alex xx1 [email protected] Manager Tom xx2 [email protected] Supervisor Abc xx1 [email protected] Engineer Ron xx2 [email protected] Engineer The output that I want should look like this ------------------------ Name Org ID, Email, Designation New Alex xx1 [email protected] Manager [email protected] Tom xx2 [email protected] Supervisor [email protected] Abc xx1 [email protected] Engineer [email protected] Ron xx2 [email protected] Engineer [email protected] The idea here is to add a new column with email ID where Org ID matches and Designation = Engineer. I tried to make sql query like this: select Name, Org ID, Email, Designation, (select t1.Email from user_table t1 where t1.Org ID = Org ID and Designation = "Engineer") as New from user_table But, this seems to be not working. Please suggest me how I can do this? Also, how do I add the same thing in excel.
-
Answer:
select user_table.Name, user_table.Org ID, user_table.Email, Designation, t1.Email as New from user_table,t1 where t1.Org ID = user_table.Org ID and user_table.Designation = "Engineer" group by user_table.name
vikas p at Yahoo! Answers Visit the source
Other answers
Try this: select Name, Org ID, Email, Designation, (select t1.Email from user_table t1 where t1.[Org ID] = t2.[Org ID] and t1.Designation = 'Engineer') as New from user_table t2
I'm not sed to excel but in php I have worked SQL org I'd however doesn't look like a variable to me?
Related Q & A:
- How can I optimize this dynamic SQL query in oracle with PL/SQL?Best solution by docs.oracle.com
- How to add a column header to the dynamic table?Best solution by stackoverflow.com
- How to convert sql query to Hibernate Criteria query?Best solution by Stack Overflow
- How to output XML from a regular SQL query?Best solution by Stack Overflow
- How to convert a SQL query into hibernate criteria?Best solution by Stack Overflow
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.