简体   繁体   中英

What am I missing here? Join Statement/ Group by?

I currently am new to SQL and I was wondering what the primary difference is between the 2 queries as to why one gives me more records and duplicates and as to why the other gives me the correct output that I am looking for.

Currently this is the desired output I want with this query.

    SELECT
          a.EMPLOYEE, 
          m.FIRST_NAME, 
          m.LAST_NAME, 
          m.PROCESS_LEVEL as PL_1

    FROM 
          dbo.vw_PAEMPPOS a 
                           join dbo.COMPLETE_EMPLOYEE_MASTER m on m.EMPLOYEE = a.EMPLOYEE 
                           join dbo.HR_EMPUSERFIELDS s on s.EMPLOYEE = m.EMPLOYEE 

    WHERE END_DATE = '2099-12-31 00:00:00.000' and 
          EMP_STATUS NOT IN ('1A', 'RT', 'SZ', 'T1', 'XD', 'XV', 'ZZ') 

    GROUP BY 
          a.EMPLOYEE, 
          m.LAST_NAME, 
          m.FIRST_NAME, 
          m.PROCESS_LEVEL

I then add another another process level to my select and group by and I get more records then before? What am I missing here?

      SELECT 
              a.EMPLOYEE, 
              m.FIRST_NAME, 
              m.LAST_NAME, 
              m.PROCESS_LEVEL as PL_1,
              a.PROCESS_LEVEL

      FROM
              dbo.vw_PAEMPPOS a 
                               join dbo.COMPLETE_EMPLOYEE_MASTER m on m.EMPLOYEE = a.EMPLOYEE 
                               join dbo.HR_EMPUSERFIELDS s on s.EMPLOYEE = m.EMPLOYEE 

      WHERE END_DATE = '2099-12-31 00:00:00.000' and 
            EMP_STATUS NOT IN ('1A', 'RT', 'SZ', 'T1', 'XD', 'XV', 'ZZ') 

    GROUP BY 
            a.EMPLOYEE, 
            m.LAST_NAME, 
            m.FIRST_NAME, 
            m.PROCESS_LEVEL, 
            a.PROCESS_LEVEL 

The answer to this question will depend on vw_PAEMPPOS. You may have more than one record per employee in that view. To check, you can run the following:

SELECT *
FROM vw_PAEMPPOS
WHERE Employee IN (
  SELECT Employee
  FROM vw_PAEMPPOS
  GROUP BY Employee
  HAVING COUNT(*) > 1
)

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