0

I have three DM_EVENT.event_code's:

'DRL'
'CMP'
'WOV'

I am needing to use a date parameter to select the latest desired reporting date (in this example: '4/28/15') to pull the SUM(DM_DAILYCOST.cost_per_item) as of that date. 'CMP' and 'WOV' are fine using the date selected.

However, for code 'DRL', I am needing to pull the SUM(DM_DAILYCOST.cost_per_item) for DATEADD(d,-1,'4/28/15').

I think I'm making it harder than it needs to be, but I am stuck as to a solution. Any insight is greatly appreciated!

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 )

1 Answers1

0

All you need is right conditions in WHERE statement. You should select only 'CMP' and 'WOV' events at date '4/28/15' and '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 ...

And remove your HAVING statement from the query.

denied
  • 598
  • 3
  • 9