0

我有三个DM_EVENT.event_code

'DRL'
'CMP'
'WOV'

我需要使用日期参数来选择所需的最新报告日期(在此示例'4/28/15'中:)以提取该SUM(DM_DAILYCOST.cost_per_item)日期的截止日期。'CMP'并且'WOV'可以使用所选日期。

但是,对于 code 'DRL',我需要拉SUM(DM_DAILYCOST.cost_per_item)for DATEADD(d,-1,'4/28/15')

我想我让它变得比它需要的更难,但我被困在一个解决方案上。非常感谢任何见解!

SELECT cd_well.completion_well_id, 
       cd_well.spud_date, 
       cd_well.well_common_name, 
       dm_afe.afe_no, 
       dm_dailycost.activity_code, 
       dm_dailycost.activity_subcode, 
       dm_dailycost.cost_code, 
       Sum(dm_dailycost.cost_per_item) AS Expr1, 
       dm_dailycost.cost_subcode, 
       dm_event.date_ops_end, 
       dm_event.event_code 
FROM   dm_dailycost 
       INNER JOIN dm_afe 
               ON dm_dailycost.afe_id = dm_afe.afe_id 
       INNER JOIN dm_afe_event_link 
               ON dm_afe.afe_id = dm_afe_event_link.afe_id 
       INNER JOIN dm_event 
               ON dm_afe_event_link.well_id = dm_event.well_id 
                  AND dm_afe_event_link.event_id = dm_event.event_id 
                  AND dm_dailycost.well_id = dm_event.well_id 
                  AND dm_dailycost.event_id = dm_event.event_id 
       INNER JOIN cd_well 
               ON dm_event.well_id = cd_well.well_id 
       INNER JOIN cd_site 
               ON cd_well.site_id = cd_site.site_id 
       INNER JOIN dm_report_journal 
               ON dm_dailycost.afe_id = dm_report_journal.afe_id 
WHERE  ( dm_report_journal.date_report BETWEEN '4/1/15' AND '4/28/15' ) 
        OR ( dm_report_journal.date_report IS NULL ) 
GROUP  BY dm_dailycost.activity_subcode, 
          dm_dailycost.cost_code, 
          dm_event.date_ops_end, 
          cd_well.spud_date, 
          cd_well.completion_well_id, 
          dm_event.event_code, 
          dm_afe.afe_no, 
          dm_dailycost.cost_subcode, 
          dm_dailycost.activity_code, 
          cd_well.well_common_name, 
          dm_report_journal.date_report 
HAVING ( Sum(dm_dailycost.cost_per_item) IS NOT NULL )
4

1 回答 1

0

WHERE您所需要的只是陈述中的正确条件。您应该只选择'CMP'and 'WOV'events at date'4/28/15''DRL'events at date DATEADD(d,-1,'4/28/15')

SELECT
 ...,
 SUM(dm_dailycost.cost_per_item) as Expr1,
 ...
FROM ...
INNER JOIN ...
...
WHERE
      dm_event.event_code in ('CMP', 'WOV') 
  AND dm_report_journal.date_report = '4/28/15'
  OR 
      dm_event.event_code = 'DRL'
  AND dm_report_journal.date_report = DATEADD(d,-1,'4/28/15')
GROUP BY ...

并从查询中删除您的 HAVING 语句。

于 2015-04-28T19:05:51.537 回答