【问题标题】:How to check dates condition from one table to another in SQL如何在 SQL 中检查从一个表到另一个表的日期条件
【发布时间】:2017-07-22 18:58:16
【问题描述】:

我们可以使用哪种方式来检查和比较一张表与另一张表中的日期。

表格:公司

+--------+---------+-----------+-----------+-------------+
| 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 计数。

【问题讨论】:

  • 请说明您应该如何得出结果。
  • 您的问题有点含糊,但我认为您正在寻找 between 关键字msdn.microsoft.com/en-us/library/ms187922.aspx
  • @vkp 我试图从带有条件的 inc 表中获取结果,inc_dates 介于 2016 年 1 月 1 日至 2016 年 12 月 31 日之间,并根据与 start 和 end 签订合同的 item_id 计算 inc_id日期
  • @mjhouseman :很抱歉解释我的问题。基本上,我试图在 2016 年 1 月 1 日至 2016 年 12 月 31 日期间按每个客户的合同获取 inc_id 计数组。日期应从合同表的开始和结束日期检查
  • @Salman 您当前的查询是什么样的?如果我们知道您目前的位置,我们就更容易将您推向正确的方向

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


【解决方案1】:

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

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

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

【讨论】:

    【解决方案2】:
    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');
    

    【讨论】:

    • 感谢您的快速解决方案,似乎是这个合适的解决方案
    • :再次感谢,如果我有一些带有项目的 inc_id 并且在合同表中没有相关信息,在这种情况下,我需要在合同文件中保留 NULL 或空白。我怎样才能做到这一点?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-23
    • 1970-01-01
    • 2013-04-22
    • 1970-01-01
    • 2021-02-24
    相关资源
    最近更新 更多