简体   繁体   中英

Why complex DB2 "VALUE" Function is used here?

While searching through some older view in our system. I came across some value function use that while I understand what it is doing I cannot for the life of me figure why it would be made unnecessarily complex. This is from a View creation DDL.

SELECT 
...
SUBSTR(
  MAX(
    CHAR(
      VALUE(TABLEO.TIMESTAMPFIELD, TIMESTAMP(CURRENT_DATE, CURRENT_TIME))
    )
    ||
    TABLEP.VARCHARFIELD
  ),
  27
)
...
FROM TABLEA
INNER JOIN TABLEB ON ...
INNER JOIN TABLEC ON ...
LEFT JOIN TABLED ON ...
LEFT JOIN TABLEE ON ...
LEFT JOIN TABLEF ON ...
LEFT JOIN TABLEG ON ...
LEFT JOIN TABLEH ON ...
LEFT JOIN TABLEI ON ...
LEFT JOIN TABLEJ ON ...
LEFT JOIN TABLEK ON ...
LEFT JOIN TABLEL ON ...
LEFT JOIN TABLEM ON ...
LEFT JOIN TABLEN ON ...
LEFT JOIN TABLEO ON ...
LEFT JOIN TABLEP ON ...
LEFT JOIN TABLEQ ON ...
WHERE .
GROUP BY ...

I get that VALUE works like COALESCE. The SUBSTR will leave only the VARCHAR (or NULL). But if the left join has no results it would be null anyway.

As you can guess this view is a very inefficient running bit of SQL with all the joins

I am a SAP developer working on a BODS ETL where the data would be used in a BOBJ WEBI report. Our project DBAs are a bit too young to understand the reasoning behind it either. Figure there are brighter minds out here somewhere that might have some insight.

EDIT:

   LEFT JOIN TABLEO 
          ON TABLEO.ID_NOTE = TABLEN.ID_NOTE
             AND TABLEO.ID_CASE = TABLEC.ID_CASE
             AND TABLEO.ID_PRSN = TABLEA.ID_PRSN
             AND TABLEO.CD_FTOF_CNTC = 'C'
   LEFT JOIN TABLEP
          ON TABLEP.ID_WORKER_ROLE = TABLEN.ID_CR

The values in these two joins do not directly relate to each other. Only indirectly through the other join results.

As presented, this query will use the value of TABLEO.TIMESTAMPFIELD if present. If the left join does not produce any matching row for TABLEO then the first parameter will be null, and the VALUE() function will use the second parameter -- the date/time of today.

In short, this expression will never be null, since in the absence of TABLEO.TIMESTAMPFIELD it will default to "now".

In any case that's a hell of a join. You need to make sure you have the appropriate indexes to make it reasonable fast.

Hard to tell, what is unnecessary in this query, as you have posted an obfuscated version...

But I think the reason behind the complex max/value is to select that tablep.varcharfield for the very one newest row in data group (according to TABLEO.TIMESTAMPFIELD), if all of them are present, or if not all are present (NOT NULL), then for the newest lying in future; but if some are not present and not a single one lies in future, then at least the simple maximum tablep.varcharfield is used.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM