简体   繁体   中英

SQL Server sub query

I want to combine 2 SQL queries, the first query i want to pull back employee records for the latest month, and the second query, pull back records for the last 3 months.

There is a method to the madness, employees can change manager each month, but the latest months manager needs to see the previous 3 month records for their employee, even if that employee had a different manager for those months.

This is the 1st select to pull in latest month

/******latest month*****************/
SELECT [REPORT_DT]
      ,[EMPLOYEE_ID]
      ,[EMPLOYEE_NAME]
      ,[LOCATION]
      ,[JOB_DESCRIPTION]
      ,[MANAGER_ID]
  FROM [EMPLOYEE]
  where [REPORT_DT]=
  (select max([REPORT_DT]) from [EMPLOYEE])

this is the select to pull in last 3months

/*********last 3 months*******************/
SELECT [REPORT_DT]
      ,[EMPLOYEE_ID]
      ,[EMPLOYEE_NAME]
      ,[LOCATION]
      ,[JOB_DESCRIPTION]
      ,[MANAGER_ID]
  FROM [EMPLOYEE]
  where [REPORT_DT]>=
  ( DATEADD(M, -3, GETDATE()))

i would be joining the 2 selects on [EMPLOYEE_ID]. Any ideas how i can combined these 2 querys? Thanks!

I would use:

SELECT TOP 1 WITH TIES
    [REPORT_DT],
    [EMPLOYEE_ID],
    [EMPLOYEE_NAME],
    [LOCATION],
    [JOB_DESCRIPTION],
    [MANAGER_ID],
FROM [EMPLOYEE]
WHERE
    [REPORT_DT] >= DATEADD(M, -3, GETDATE()
ORDER BY
    [REPORT_DT] DESC;

The above WHERE clause matches your second query, and we use a TOP 1 WITH TIES approach to obtain the records having the maximum REPORT_DT values in the table.

You can use also EXISTS :

SELECT [REPORT_DT]
      ,[EMPLOYEE_ID]
      ,[EMPLOYEE_NAME]
      ,[LOCATION]
      ,[JOB_DESCRIPTION]
      ,[MANAGER_ID]
FROM [EMPLOYEE]
WHERE [REPORT_DT] = (SELECT MAX([REPORT_DT]) FROM [EMPLOYEE]) AND
      EXISTS( SELECT 1 FROM [EMPLOYEE]
              WHERE [EMPLOYEE_ID] == t.[EMPLOYEE_ID] AND
                    [REPORT_DT] >= (DATEADD(M, -3, GETDATE())))

Regarding comment, this query should solve the problem:

SELECT [REPORT_DT]
      ,[EMPLOYEE_ID]
      ,[EMPLOYEE_NAME]
      ,[LOCATION]
      ,[JOB_DESCRIPTION]
      ,[MANAGER_ID]
FROM [EMPLOYEE]
WHERE [REPORT_DT] >= (DATEADD(M, -3, GETDATE())) AND
      EXISTS( SELECT 1 FROM [EMPLOYEE]
              WHERE [EMPLOYEE_ID] == t.[EMPLOYEE_ID] AND
                    [REPORT_DT] >= (DATEADD(M, -1, GETDATE())))

If you want to join the two tables, you would use:

select ecurr.*, e.*  -- or whatever columns you like
from employee ecurr join
     employee e
     on ecurr.employee_id = e.employee_id
where ecurr.report_dt = (select max(e2.report_dt) from employee e2) and
      e.report_dt >= dateadd(month, 3, getdate())
order by ecurr.employee_id, e.report_dt;

I will add that this result doesn't make particular sense to me. But this is the question that you specifically asked.

I thought you want to get [EMPLOYEE_ID] list for the latest month. And get their 3 month work history. According to your current select scripts (I did not understand your first select script if it gives a employee list or not) you can do like this. But it is not an effective solution. You can also add sub selection in the join section. First try this please.

SELECT [REPORT_DT]
      ,[EMPLOYEE_ID]
      ,[EMPLOYEE_NAME]
      ,[LOCATION]
      ,[JOB_DESCRIPTION]
      ,[MANAGER_ID]
  FROM [EMPLOYEE]
  where [REPORT_DT]>=(DATEADD(M, -3, GETDATE()))
AND [EMPLOYEE_ID] IN (SELECT [REPORT_DT]
      ,[EMPLOYEE_ID]
      ,[EMPLOYEE_NAME]
      ,[LOCATION]
      ,[JOB_DESCRIPTION]
      ,[MANAGER_ID]
  FROM [EMPLOYEE]
  where [REPORT_DT]=
  (select max([REPORT_DT]) from [EMPLOYEE]))

I created a View using the below query, and then combined this with a 3 month date prompt (this is being used on a Business Objects report) and that did the trick.

SELECT 
b.[MANAGER_ID],
a.[EMPLOYEE_ID],
a.[START_DT],
a.[EMPLOYEE_NAME], 
a.[LOCATION],
a.[JOB_DESCRIPTION],
a.[REPORT_DT]
FROM  [EMPLOYEE] b INNER JOIN
      [EMPLOYEE] a
     ON b.[EMPLOYEE_ID] = a.[EMPLOYEE_ID]
WHERE b.[REPORT_DT] = (SELECT MAX(c.[REPORT_DT]) 
FROM [EMPLOYEE] c)

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