locked
Joining two query to get the result (multiple join) RRS feed

  • Question

  • User1052662409 posted

    Hi All,

    Here I have wrote two query to get some results. As am not much user of sql queries am not finding the way to join both of them. Please help.

    Queries are as below

    --1st query
    
    select  mtblProjects.Cost_Code, mtblProjects.Short_Name, mtblProj_Cost.Emp_Code from mtblProjects right join mtblProj_Cost on  mtblProj_Cost.Cost_Code =mtblProjects.Cost_Code where Emp_Code='ICLEI-0026' and mtblProjects.Status='Open'
    
    
    
    -- second query
    select Cost_Code, Short_Name from mtblProjects where Type='Internal'

    There are only 2 tables. The second query also from the first table.

    Thanks

    Friday, September 23, 2016 7:40 AM

Answers

  • User77042963 posted
     --1st query
    
    select  mtblProjects.Cost_Code, mtblProjects.Short_Name, mtblProj_Cost.Emp_Code from mtblProjects right join mtblProj_Cost on  mtblProj_Cost.Cost_Code =mtblProjects.Cost_Code where Emp_Code='ICLEI-0026' and mtblProjects.Status='Open'
    
    UNION ALL
    
    -- second query
    select Cost_Code, Short_Name , null as Emp_Code  from mtblProjects where Type='Internal'

    Post some sample data and it will help to get what you need quick.  Thanks.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, September 23, 2016 7:23 PM

All replies

  • User77042963 posted
     --1st query
    
    select  mtblProjects.Cost_Code, mtblProjects.Short_Name, mtblProj_Cost.Emp_Code from mtblProjects right join mtblProj_Cost on  mtblProj_Cost.Cost_Code =mtblProjects.Cost_Code where Emp_Code='ICLEI-0026' and mtblProjects.Status='Open'
    
    UNION ALL
    
    -- second query
    select Cost_Code, Short_Name , null as Emp_Code  from mtblProjects where Type='Internal'

    Post some sample data and it will help to get what you need quick.  Thanks.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, September 23, 2016 7:23 PM
  • User-1672470423 posted

    Also, if you want to eliminate duplicate use UNION rather than UNION ALL

    The difference between Union and Union all is that Union all will not eliminate duplicate rows, instead it just pulls all rows from all tables fitting your query specifics and combines them into a table.

    Friday, September 23, 2016 7:31 PM
  • User1052662409 posted

    Thank you so much Limno.

    Saturday, September 24, 2016 2:02 AM