简体   繁体   English

SQL Server 子查询

[英]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.我想合并 2 个 SQL 查询,第一个查询我想拉回最近一个月的员工记录,第二个查询拉回过去 3 个月的记录。

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.有一种疯狂的方法,员工可以每个月更换经理,但最近几个月的经理需要查看他们员工的前 3 个月记录,即使该员工在这几个月有不同的经理。

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这是过去 3 个月的选择

/*********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].我将加入 [EMPLOYEE_ID] 上的 2 个选择。 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.上面的WHERE子句匹配您的第二个查询,我们使用TOP 1 WITH TIES方法来获取表中具有最大REPORT_DT值的记录。

You can use also EXISTS :您也可以使用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.我以为您想获取最近一个月的 [EMPLOYEE_ID] 列表。 And get their 3 month work history.并获得他们 3 个月的工作历史。 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.我使用以下查询创建了一个视图,然后将其与 3 个月的日期提示(这用于 Business Objects 报告)结合起来,就成功了。

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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