如何在SQL中检查从一个表到另一个表的日期条件

时间:2017-03-02 20:56:17

标签: sql sql-server sql-server-2008 join

我们可以使用哪种方式来检查和比较从一个表到另一个表的日期。

表:inc

+--------+---------+-----------+-----------+-------------+
| inc_id | cust_id |  item_id  | serv_time |  inc_date   |
+--------+---------+-----------+-----------+-------------+
|      1 | john    | HP        |        40 | 17-Apr-2015 |
|      2 | John    | HP        |        60 | 10-Jan-2016 |
|      3 | Nick    | Cisco     |       120 | 11-Jan-2016 |
|      4 | samanta | EMC       |       180 | 12-Jan-2016 |
|      5 | Kerlee  | Oracle    |        40 | 13-Jan-2016 |
|      6 | Amir    | Microsoft |       300 | 14-Jan-2016 |
|      7 | John    | HP        |       120 | 15-Jan-2016 |
|      8 | samanta | EMC       |        20 | 16-Jan-2016 |
|      9 | Kerlee  | Oracle    |        10 | 2-Feb-2017  |
+--------+---------+-----------+-----------+-------------+

表:合同:

+-----------+---------+----------+------------+
|  item_id  | con_id  |  Start   |    End     |
+-----------+---------+----------+------------+
| Dell      | DE2015  | 1/1/2015 | 12/31/2015 |
| HP        | HP2015  | 1/1/2015 | 12/31/2015 |
| Cisco     | CIS2016 | 1/1/2016 | 12/31/2016 |
| EMC       | EMC2016 | 1/1/2016 | 12/31/2016 |
| HP        | HP2016  | 1/1/2016 | 12/31/2016 |
| Oracle    | OR2016  | 1/1/2016 | 12/31/2016 |
| Microsoft | MS2016  | 1/1/2016 | 12/31/2016 |
| Microsoft | MS2017  | 1/1/2017 | 12/31/2017 |
+-----------+---------+----------+------------+

结果:

+-------+---------+---------+--------------+
| Calls | Cust_id | Con_id  | Tot_Ser_Time |
+-------+---------+---------+--------------+
|     2 | John    | HP2016  |          180 |
|     2 | samanta | EMC2016 |          200 |
|     1 | Nick    | CIS2016 |          120 |
|     1 | Amir    | MS2016  |          300 |
|     1 | Oracle  | OR2016  |           40 |
+-------+---------+---------+--------------+

我的查询:

select count(inc_id) as Calls,  inc.cust_id,  contract.con_id,
  sum(inc.serv_time) as tot_serv_time
from  inc inner join contract   on inc.item_id = contract.item_id  
where  inc.inc_date between '2016-01-01' and '2016-12-31'
group by inc.cust_id,  contract.con_id

2016年1月1日至2016年12月31日期间带有过滤器的inc表的结果 根据项目及其合同的开始和结束日期计算inc_id。

2 个答案:

答案 0 :(得分:1)

如果我正确理解了您的问题,此查询将返回所需的结果:

X:Bind

问题有点模糊,因此您可能需要对此查询进行一些调整。

答案 1 :(得分:1)

select
    Calls = count(*)
  , Cust = i.Cust_id
  , Contract = c.con_id
  , Serv_Time = sum(Serv_Time)
from inc as i
  inner join contract as c
    on i.item_id = c.item_id
    and i.inc_date >= c.[start]
    and i.inc_date <= c.[end]
where c.[start]>='20160101'
group by i.Cust_id, c.con_id 
order by i.Cust_Id, c.con_id

返回:

+-------+---------+----------+-----------+
| Calls |  Cust   | Contract | Serv_Time |
+-------+---------+----------+-----------+
|     1 | Amir    | MS2016   |       300 |
|     2 | John    | HP2016   |       180 |
|     1 | Kerlee  | OR2016   |        40 |
|     1 | Nick    | CIS2016  |       120 |
|     2 | samanta | EMC2016  |       200 |
+-------+---------+----------+-----------+

测试设置:http://rextester.com/WSYDL43321

create table inc(
    inc_id int
  , cust_id varchar(16)
  , item_id varchar(16)
  , serv_time int
  , inc_date  date
 );
insert into inc values
 (1,'john','HP', 40 ,'17-Apr-2015')
,(2,'John','HP', 60 ,'10-Jan-2016')
,(3,'Nick','Cisco', 120 ,'11-Jan-2016')
,(4,'samanta','EMC', 180 ,'12-Jan-2016')
,(5,'Kerlee','Oracle', 40 ,'13-Jan-2016')
,(6,'Amir','Microsoft', 300 ,'14-Jan-2016')
,(7,'John','HP', 120 ,'15-Jan-2016')
,(8,'samanta','EMC', 20 ,'16-Jan-2016')
,(9,'Kerlee','Oracle', 10 ,'02-Feb-2017');

create table contract (
    item_id varchar(16) 
  , con_id varchar(16)
  , [Start] date
  , [End] date
);
insert into contract values 
 ('Dell','DE2015','20150101','20151231')
,('HP','HP2015','20150101','20151231')
,('Cisco','CIS2016','20160101','20161231')
,('EMC','EMC2016','20160101','20161231')
,('HP','HP2016','20160101','20161231')
,('Oracle','OR2016','20160101','20161231')
,('Microsoft','MS2016','20160101','20161231')
,('Microsoft','MS2017','20170101','20171231');