查询重叠日期范围

时间:2012-06-14 07:15:11

标签: sql

我创建了一个报告,其中用户选择了ID。在该ID下有一个截止期,在该ID中,它被分成一个或多个具有日期范围的记录。 例如:

   ID: MMA1512 - 3/1-3/15
        compose of
            -- > 'P2306.2012.15.03.A.STAFF' - 2/24-3/8
            -- > 'P2306.2012.08.03.A.DAILY' - 3/1-3/15

  Tables:
     Work Allocation 
       id
   startdate

     Work Allocation Details
        workallocationid
        employee no
        costcode
        subcostcode
        hrswork

问题是3 / 1-3 / 8的数据重叠,但我不能排除重叠日期,因为'P2306.2012.08.03.A.DAILY'需要它。这是我的查询

输出错误

 SELECT wa.companycode,wa.sitecode,wa_details.employeeno,    
        wa_details.costcode,wa_details.subcostcode,  
   SUM(wa_details.hrswork) hrswork,ttl_work_hrs  
FROM WorkAllocation wa  
INNER JOIN WorkAllocation_details wa_details  ON wa.id = wa_details.workallocationid  
INNER JOIN              
(  SELECT employee_id,ttl_work_hrs FROM [dbo].[Get_Total_Work_Hours]  ('mma1512','P2306.2012.15.03.l.STAFF,P2306.2012.08.03.l.DAILY','p2306','l'))                
 view_ttl_hours 
 ON 
wa_details.employeeno=view_ttl_hours.employee_id  
 WHERE 
companycode='l'
 AND 
sitecode='p2306' 
 AND 
startdate BETWEEN '2012-02-24' AND '2012-03-15'
 GROUP BY wa.companycode,wa.sitecode,       wa_details.employeeno,wa_details.costcode,wa_details.subcostcode,ttl_work_hrs   

     Company Site   EmployeeNo CostCode SubCostCode HrsWork Total Hours Work
      LCPI   P2306  PH080323    8321    169        160.50   162.5
      LCPI   P2306  PH080323    10184   2648       14.50    162.5

正确输出

SELECT wa.companycode,wa.sitecode,wa_details.employeeno,    
        wa_details.costcode,wa_details.subcostcode,  
   SUM(wa_details.hrswork) hrswork,ttl_work_hrs  
FROM WorkAllocation wa  
INNER JOIN WorkAllocation_details wa_details  ON wa.id = wa_details.workallocationid  
INNER JOIN              
(  SELECT employee_id,ttl_work_hrs FROM [dbo].[Get_Total_Work_Hours]  ('mma1512','P2306.2012.15.03.l.STAFF,P2306.2012.08.03.l.DAILY','p2306','l'))                
 view_ttl_hours 
 ON 
wa_details.employeeno=view_ttl_hours.employee_id  
 WHERE 
companycode='l'
 AND 
sitecode='p2306' 
 AND 
startdate BETWEEN '2012-02-24' AND '2012-03-08'
 GROUP BY wa.companycode,wa.sitecode,       wa_details.employeeno,wa_details.costcode,wa_details.subcostcode,ttl_work_hrs

   Company Site   EmployeeNo CostCode SubCostCode HrsWork Total Hours Work
      LCPI    P2306  PH080323   8321    169    148.00   162.5
      LCPI    P2306  PH080323   10184   2648       14.50    162.5

查询之间的差异是 startdate BETWEEN'2012-02-24'和'2012-03-15'

0 个答案:

没有答案