如何找到前一年的“某个具体日期”

时间:2015-09-29 06:16:54

标签: sql

这是我的表

+-------------+-----------+---------+-----------+--------+
| CompanyName | Companyid | Country |   Year    | Salary |
+-------------+-----------+---------+-----------+--------+
|  Company6   |    111    |  INDIA  | 30-Dec-06 | 295000 |
|  Company1   |    111    |  INDIA  | 30-Dec-07 | NULL   |
|  Company2   |    111    |  INDIA  | 28-Dec-08 | NULL   |
|  Company3   |    111    |  INDIA  | 27-Dec-09 | NULL   |
|  Company4   |    111    |  INDIA  | 02-Jan-11 | 135000 |
|  Company5   |    111    |  INDIA  | 01-Jan-12 | 188000 |
|  Company7   |    111    |  INDIA  | 29-Dec-13 | 296000 |
+-------------+-----------+---------+-----------+--------+

我想知道谁的工资为null且条件为的详细信息 上一年或明年的工资应该不为空

3 个答案:

答案 0 :(得分:0)

如果您使用的是SQL Server,请使用select dateadd(yy, -1, year) AS PreviousYear from YourtableName 功能。 例如,

DATE_ADD()

您可以找到更多信息here

对于MySQL,您可以使用if(variable.Contains("Year")) bool Hasyear = true; if(variable.Contains("Month")) bool HasMonth= true; if(variable.Contains("Day")) bool HasDay= true; 功能。

答案 1 :(得分:0)

尝试以下查询,我认为这是您的预期..有关详情,请查看此SQLFiddle

- 创建表

Create table #company (CompanyName  varchar(20), Companyid int, Country varchar(10) , yr date,  Salary int)

- 插入数据

insert into #company
VALUES 
('Company6' ,    111    ,  'INDIA'  , '30-Dec-06' , 295000 ),
('Company1'   ,    111    ,  'INDIA'  , '30-Dec-07' , NULL   ),
('Company2'   ,    111    ,  'INDIA'  , '28-Dec-08' , NULL   ),
('Company3'   ,    111    ,  'INDIA'  , '27-Dec-09' , NULL  ) ,
('Company4'   ,    111    ,  'INDIA'  , '02-Jan-11' , 135000) ,
('Company5'   ,    111    ,  'INDIA'  , '01-Jan-12' , 188000) ,
('Company7'   ,    111    ,  'INDIA'  , '29-Dec-13' , 296000) 


-- all data
select * from #company

--Resulted data
select * from #company  where (year(yr)-1) not in (
SELECT year(yr) FROM #company C1 where C1.salary is not null
)
and (year(yr)+1)  not in (
SELECT year(yr) FROM #company C1 where C1.salary is not null
)
and salary is  null

答案 2 :(得分:0)

根据您提供的最佳信息,我尽力做到以下几点:

我创建了自己的临时

CREATE TABLE #temp
  (
    CompanyName varchar(10)
    , CompanyId int
    , Country varchar(10)
    , [Year] date
    , Salary money
  )

然后插入一些值(注意:我使用公司名称作为主键)

insert into #temp values
  ('Company1',111,'INDIA','12-30-06',295000),
  ('Company1',111,'INDIA','12-30-07',NULL),
  ('Company2',111,'INDIA','12-28-08',NULL),
  ('Company2',111,'INDIA','12-27-09',NULL),
  ('Company2',111,'INDIA','12-28-10',135000),
  ('Company3',111,'INDIA','01-02-11',135000),
  ('Company4',111,'INDIA','01-01-12',188000),
  ('Company5',111,'INDIA','12-29-13',296000)

然后让它连续两次加入自己的年份 - 1年和1年

首先让我们深入了解左连接的复杂性而不使用where

  SELECT
    *
  FROM #temp AS [payroll]
  LEFT JOIN #temp AS [payrollyearminus]
  ON
     DATEPART(YEAR,[payroll].[Year]) - 1 = DATEPART(YEAR,[payrollyearminus].[Year])
  AND [payroll].CompanyName = [payrollyearminus].CompanyName
  AND [payroll].Salary IS NULL
  AND [payrollyearminus].Salary IS NOT NULL
  LEFT JOIN #temp AS [payrollyearplus]
  ON
     DATEPART(YEAR,[payroll].[Year]) + 1 = DATEPART(YEAR,[payrollyearplus].[Year])
  AND [payroll].CompanyName = [payrollyearplus].CompanyName
  AND [payroll].Salary IS NULL
  AND [payrollyearplus].Salary IS NOT NULL
  /* disabled
  WHERE
      [payrollyearminus].CompanyName IS NOT NULL
      OR [payrollyearplus].CompanyName IS NOT NULL
  */

以下是结果

enter image description here

现在,启用WHERE,您将获得那些当前年薪为IS NULL且前一年/年后工资为IS NOT NULL的公司

编辑: 通过更改以下代码修改结果

SELECT
  *
FROM #temp AS [payroll]  

进入特定的别名表

SELECT
  [payroll].*
FROM #temp AS [payroll] 

然后以文本模式运行表

enter image description here

有这样的印刷品。

enter image description here

编辑:好的,然后我改变了我的左连接,请看新的左连接逻辑。

SELECT
    [payroll].*
  FROM #temp AS [payroll]
  LEFT JOIN #temp AS [payrollyearminus]
  ON
     DATEPART(YEAR,[payroll].[Year]) - 1 = DATEPART(YEAR,[payrollyearminus].[Year])
  --AND [payroll].CompanyName = [payrollyearminus].CompanyName
  AND [payroll].Salary IS NULL
  AND [payrollyearminus].Salary IS NOT NULL
  LEFT JOIN #temp AS [payrollyearplus]
  ON
     DATEPART(YEAR,[payroll].[Year]) + 1 = DATEPART(YEAR,[payrollyearplus].[Year])
  --AND [payroll].CompanyName = [payrollyearplus].CompanyName
  AND [payroll].Salary IS NULL
  AND [payrollyearplus].Salary IS NOT NULL
  WHERE
      [payrollyearminus].[Year] IS NOT NULL
      OR [payrollyearplus].[Year] IS NOT NULL