T-SQL migration of working Access SQL Query with an issue on writing the CASE replacement for IIF

StackOverflow https://stackoverflow.com/questions/4972952

Question

I have two tables BMReports_FPN_Curves and BMReports_BOA_Curves each consisting of a Name, Datetime, Period and Value, for example:

BM_UNIT_NAME   RunDate               Period  FPN (or BOA)
T_DRAXX-1      2010-12-01 00:03:00   1       497

With the RunDate field incrementing by one minute (this c.1440 records per day) and the Periods being 1-48. In BMReports_FPN_Curves I have a complete data set for each time period and BMReports_BOA_Curves contains the values that will replace these base values.

There are usually duplicate BOA values and a nested IIF statement in the Access SQL statement contained a rule to pick one of either the FPN, the max BOA value or Min BOA value for any point in time. The rule stated:

1) If there is no BOA value, use the FPN value
2) If there is a BOA value and it is less than the FPN, find and use the Min BOA value
3) If there is a BOA value and it is greater than the FPN, find and use the Max BOA value

The Access SQL query works perfectly and is as follows:

SELECT 
dbo_BMReports_FPN_Curves.BM_Unit_Name, 
dbo_BMReports_FPN_Curves.RunDate, 
dbo_BMReports_FPN_Curves.Period, 
dbo_BMReports_FPN_Curves.PN_Level, 

IIf(IIf(Min([dbo_BMReports_BOA_Curves]![PN_Level]) <[dbo_BMReports_FPN_Curves]![PN_Level],Min([dbo_BMReports_BOA_Curves]! [PN_Level]),Max([dbo_BMReports_BOA_Curves]![PN_Level])) Is Null, [dbo_BMReports_FPN_Curves]![PN_Level],
IIf(Min([dbo_BMReports_BOA_Curves]![PN_Level])<[dbo_BMReports_FPN_Curves]! [PN_Level],Min([dbo_BMReports_BOA_Curves]! [PN_Level]),Max([dbo_BMReports_BOA_Curves]![PN_Level]))) AS BOA

FROM dbo_BMReports_FPN_Curves LEFT JOIN dbo_BMReports_BOA_Curves ON  (dbo_BMReports_FPN_Curves.RunDate = dbo_BMReports_BOA_Curves.RunDate) AND  (dbo_BMReports_FPN_Curves.BM_Unit_Name = dbo_BMReports_BOA_Curves.BM_Unit_Name)

GROUP BY dbo_BMReports_FPN_Curves.BM_Unit_Name, dbo_BMReports_FPN_Curves.RunDate, dbo_BMReports_FPN_Curves.Period, dbo_BMReports_FPN_Curves.PN_Level

HAVING (((dbo_BMReports_FPN_Curves.BM_Unit_Name)='T_DRAXX-1'));

I've re-written the bulk of the query in T-SQL (querying the same SQL Server data source) and have the LEFT JOIN, GROUP BY and HAVING elements all working but I am getting stuck on the CASE WHEN replacement for the IFF and would really appreciate a hand if some has a few moments spare.

The SQL Query as it currently stands:

SELECT 
BMReports_FPN_Curves.BM_Unit_Name, 
BMReports_FPN_Curves.RunDate, 
BMReports_FPN_Curves.Period,
AVG(BMReports_FPN_Curves.PN_Level) AS FPN,

    CASE
      WHEN BMReports_BOA_Curves.PN_Level IS NULL THEN AVG(BMReports_FPN_Curves.PN_Level)
      WHEN MIN(BMReports_BOA_Curves.PN_Level) IS <  AVG(BMReports_FPN_Curves.PN_Level) THEN MIN(BMReports_BOA_Curves.PN_Level)
      ELSE MAX(BMReports_BOA_Curves.PN_Level)
    END AS BOA

FROM BMReports_FPN_Curves 
  LEFT JOIN BMReports_BOA_Curves ON BMReports_FPN_Curves.BM_Unit_Name = BMReports_BOA_Curves.BM_Unit_Name
  AND  BMReports_FPN_Curves.RunDate = BMReports_BOA_Curves.RunDate

GROUP BY BMReports_FPN_Curves.BM_Unit_Name, BMReports_FPN_Curves.RunDate, BMReports_FPN_Curves.Period
HAVING BMReports_FPN_Curves.BM_Unit_Name = 'T_DRAXX-1'
ORDER BY BMReports_FPN_Curves.BM_Unit_Name, BMReports_FPN_Curves.RunDate, BMReports_FPN_Curves.Period
Was it helpful?

Solution

SELECT  fc.BM_Unit_Name
        , fc.RunDate
        , fc.Period
        , CASE 
          WHEN AVG(bc.PN_Level) IS NULL THEN AVG(fc.PN_Level)             -- No BOA Value, use the FPN Value
          WHEN MIN(bc.PN_Level) < AVG(fc.PN_Level) THEN MIN(bc.PN_Level) -- BOA Value is less than the FPN, use the BOA Value
          ELSE MAX(bc.PN_Level)                                          -- BOA Value is greater than the FPN, use the BOA Value
          END 
FROM    dbo.BMReports_FPN_Curves fc
        LEFT JOIN dbo.BMReports_BOA_Curves bc ON fc.RunDate = bc.RunDate        
                                                 AND fc.BM_Unit_Name = bc.BM_Unit_Name
WHERE   fc.BM_Unit_Name ='T_DRAXX-1'
GROUP BY
        fc.BM_Unit_Name
        , fc.RunDate
        , fc.Period

OTHER TIPS

You're probably better off using a CTE to do all the aggregate calculations and then doing your case statement off that

WITH cte 
     AS (SELECT bmreports_fpn_curves.bm_unit_name, 
                bmreports_fpn_curves.rundate, 
                bmreports_fpn_curves.period, 
                AVG(bmreports_fpn_curves.pn_level) AS fpn, 
                AVG(bmreports_fpn_curves.pn_level) AS boa, 
                MIN(bmreports_boa_curves.pn_level) minboa, 
                MAX(bmreports_fpn_curves.pn_level) maxfpn 
         FROM   bmreports_fpn_curves 
                LEFT JOIN bmreports_boa_curves 
                  ON bmreports_fpn_curves.bm_unit_name = 
                     bmreports_boa_curves.bm_unit_name 
                     AND bmreports_fpn_curves.rundate = 
                         bmreports_boa_curves.rundate 
         GROUP  BY bmreports_fpn_curves.bm_unit_name, 
                   bmreports_fpn_curves.rundate, 
                   bmreports_fpn_curves.period 
         HAVING bmreports_fpn_curves.bm_unit_name = 'T_DRAXX-1') 
SELECT bm_unit_name, 
       rundate, 
       period ,
       CASE 
              WHEN BOA IS NULL THEN FPN 
              WHEN BOA < FPN THEN MinBoa
              WEHN BOA > FPN THEN MaxBoa
              ELSE -- BOA = FPN THEN WHAT?
       END as BOA
FROM   cte 

For DB's that don't support CTEs you can also use a select inside the from (inline view). Incidentally Access supports this.

SELECT bm_unit_name, 
       rundate, 
       period ,
       CASE 
              WHEN BOA IS NULL THEN FPN 
              WHEN BOA < FPN THEN MinBoa
              WEHN BOA > FPN THEN MaxBoa
              ELSE -- BOA = FPN THEN WHAT?
       END as BOA
  FROM (
       SELECT bmreports_fpn_curves.bm_unit_name, 
                bmreports_fpn_curves.rundate, 
                bmreports_fpn_curves.period, 
                AVG(bmreports_fpn_curves.pn_level) AS fpn, 
                AVG(bmreports_fpn_curves.pn_level) AS boa, 
                MIN(bmreports_boa_curves.pn_level) minboa, 
                MAX(bmreports_fpn_curves.pn_level) maxfpn 
         FROM   bmreports_fpn_curves 
                LEFT JOIN bmreports_boa_curves 
                  ON bmreports_fpn_curves.bm_unit_name = 
                     bmreports_boa_curves.bm_unit_name 
                     AND bmreports_fpn_curves.rundate = 
                         bmreports_boa_curves.rundate 
         GROUP  BY bmreports_fpn_curves.bm_unit_name, 
                   bmreports_fpn_curves.rundate, 
                   bmreports_fpn_curves.period 
         HAVING bmreports_fpn_curves.bm_unit_name = 'T_DRAXX-1')   ) t

Have you tried a more literal translation of the IIF to CASE? For example, your IIF chain looks something like this:

IIf
(
    IIf
    (
        Min([dbo_BMReports_BOA_Curves]![PN_Level]) < [dbo_BMReports_FPN_Curves]![PN_Level], 
        Min([dbo_BMReports_BOA_Curves]![PN_Level]),
        Max([dbo_BMReports_BOA_Curves]![PN_Level])
    ) Is Null,
    [dbo_BMReports_FPN_Curves]![PN_Level],
    IIf
    (
        Min([dbo_BMReports_BOA_Curves]![PN_Level]) < [dbo_BMReports_FPN_Curves]![PN_Level],
        Min([dbo_BMReports_BOA_Curves]![PN_Level]),
        Max([dbo_BMReports_BOA_Curves]![PN_Level])
    )
) AS BOA

So a literal translation would be something like this:

(
case
    when 
        (
        case
            when Min(BMReports_BOA_Curves.PN_Level) < BMReports_FPN_Curves.PN_Level then
                Min(BMReports_BOA_Curves.PN_Level)
            else
                Max(BMReports_BOA_Curves.PN_Level)
        end
        ) is null then
        BMReports_FPN_Curves.PN_Level
    else
        (
            case
                when Min(BMReports_BOA_Curves.PN_Level) < BMReports_FPN_Curves.PN_Level then
                    Min(BMReports_BOA_Curves.PN_Level)
                else
                    Max(BMReports_BOA_Curves.PN_Level)
            end
        )
end
) as BOA

I don't have access to your full schema nor data so I cannot test the translation, but I believe it is syntactically correct.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top