Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在SQL中检查从一个表到另一个表的日期条件_Sql_Sql Server_Sql Server 2008_Join - Fatal编程技术网

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

如何在SQL中检查从一个表到另一个表的日期条件,sql,sql-server,sql-server-2008,join,Sql,Sql Server,Sql Server 2008,Join,我们可以使用哪种方法来检查和比较一个表与另一个表的日期 表格:inc +--------+---------+-----------+-----------+-------------+ | inc_id | cust_id | item_id | serv_time | inc_date | +--------+---------+-----------+-----------+-------------+ | 1 | john | HP |

我们可以使用哪种方法来检查和比较一个表与另一个表的日期

表格: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 |
+-------+---------+---------+--------------+
结果:

+-----------+---------+----------+------------+
|  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表格中的结果(带过滤器)
基于项目及其合同开始和结束日期的公司id计数。

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

select
  count(*) 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
     and inc.inc_date between contract.start and contract.end
where
  inc.inc_date between '2016-01-01' and '2016-12-31'
group by
  inc.cust_id,
  contract.con_id
这个问题有点模糊,因此您可能需要对此查询进行一些调整

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
测试设置:


请解释你应该如何得出结果。你的问题有点模糊,但我认为你在寻找中间关键字@vkp,我试图从inc table with condition中获得结果,inc_日期为2016年1月1日至2016年12月31日,inc_id根据与开始和结束签订合同的项目进行计数dates@mjhouseman:很抱歉解释我的问题。基本上,我正试图通过合同获得2016年1月1日至2016年12月31日期间每个客户的inc_id计数。日期应从合同表开始和结束处进行检查dates@Salman您当前的查询是什么样子的?如果我们知道您目前的位置,我们会更容易将您推向正确的方向感谢快速解决方案,这似乎是一个合适的解决方案:再次感谢,如果我对项目有一些inc_id,并且它在合同表中没有相关信息,在这种情况下,我需要在合同文件中保留NULL或空白。我怎样才能做到这一点?