简体   繁体   中英

SQL using inner join

So this is the situation I have. I have 3 tables ( tblEmployeesinfo , sqlSumrepMTC , sqlSumrepMTC15th ) I display this info using inner join:

SELECT
       SQLSummRepMTC."RepCompany", SQLSummRepMTC."WHTax",
       SQLSummRepMTC."Company", SQLSummRepMTC."MonthName",
       SQLSummRepMTC."YearVal", SQLSummRepMTC."Basis",
       tblEmployeesInfo."LastName", tblEmployeesInfo."FirstName",
       tblEmployeesInfo."Company", tblEmployeesInfo."MInitial", 
       tblEmployeesInfo."Division", sqlSumrepMTC15th."WHTax",
       sqlSumrepMTC15th."Basis"
FROM
   {
     oj ("BIOMETRICS"."dbo"."SQLSummRepMTC" SQLSummRepMTC INNER JOIN
     "BIOMETRICS"."dbo"."tblEmployeesInfo" tblEmployeesInfo 
     ON SQLSummRepMTC."EmployeeNo" = tblEmployeesInfo."EmployeeNo")
     INNER JOIN "BIOMETRICS"."dbo"."sqlSumrepMTC15th" sqlSumrepMTC15th
     ON tblEmployeesInfo."EmployeeNo" = sqlSumrepMTC15th."EmployeeNo"
  }
  ORDER BY
      SQLSummRepMTC."Basis" ASC,
      tblEmployeesInfo."Company" ASC,
      tblEmployeesInfo."LastName" ASC

Let's say one employee has his record on sqlSumrepMTC with its field Taxvalue of 50 but he does not exist on sqlSumrepMTC15th my problem is that this record will not be displayed in the inner join since it does not have value on both tables. What i want to achieve is just display a 0 value when it does not exist in the other table. This is my report looks like.

    Employeeno      employeename   15th      30th
       01               james       10        20
       02                Chris      NULL      50

first record will appear in the report since it has both record existing in the two tables, the second will not since its null in the first table. I just need it to appear in the report if one value is null or is missing from the other. Thanks in advance

You have two joins! Thus, if there is no record in sqlSumrepMTC15th you need to replace the second join with LEFT JOIN. If it is possible, that there is no join record in sqlSumrepMTC15th AND tblEmployeesInfo , you need to replace both joins with LEFT JOIN.

Furthermore, you can replace NULL by

SELECT CASE
         WHEN attribute IS NULL
              THEN 0
              ELSE attribute 
         END AS resultColumName,
       nextAttribute
FROM ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM