-3

I have this query I have written in oracle that returns an error saying: subquery must have an alias any help please i need to convert it to PostgreSQL so the query will executed in order to create a pipeline in elasticserver.

  • What's unclear about the error message? Your derived table `from (...)` needs an alias in Postgres –  Jan 05 '23 at 19:50
  • I need to convert this query to PostreSQL it's because i'm using driver PostreSQL in my logstash – aymen krifa Jan 05 '23 at 20:02
  • Java::OrgPostgresqlUtil::PSQLException: ERROR: function instr(character varying, unknown, integer) does not exist Hint: No function matches the given name and argument types. You might need to add explicit type casts. – aymen krifa Jan 05 '23 at 20:19

1 Answers1

1

I guess it is quite self explanatory - why didn't you already do that?

... from
                (
                  select flowid_,nbrecords_,externalid_,upper(substr(externalid_,44,4)) fn,upper(substr(externalid_,48,1)) sfn,
                          status_,creationdate_, 
                          upper(substr(externalid_,position('/' in externalid) -1)+1))  ffn
                          from broker_flowin 
                          where INPUTDEVICE_DEVICEID_='INSTRUMENTPRICE_IN'
                          and to_javadate(creationdate_)>:sql_last_value
                ) x
                  ^
                  |
               alias

In PostreSQL, that might look like ) as x (but - as you mentioned Oracle - in Oracle subquery (or a table) can have an alias, but you can't use the as keyword there).


As of converting the whole code to PostgreSQL, first of all - I don't use that database so I'll try my best but can't promise anything. Besides, I'm not sure that code you posted actually is Oracle.

A few comments:

  • there's no TO_JAVADATE function in Oracle (at least, as far as I can tell)
  • /* ${cinstall} */ is just a comment; that's not a hint (if you meant to use it; maybe it means something in PostgreSQL)
  • instead of instr function, PostgreSQL uses position
  • PostgreSQL has decode function, but its purpose is different from the one in Oracle. Use case expression instead

Therefore, that code (converted to PostgreSQL) might look like this:

SELECT /* ${cinstall}  */     --> in Oracle, this is just a comment
    flowid_ AS pk_,
    CASE
        WHEN position('AFB' in ffn) > 0    THEN
            'AFB'
        WHEN position('CAMDIV' in ffn) > 0 THEN
            'FX'
        WHEN position('AC_I' in ffn) > 0   THEN
            'Indexes'
        WHEN position('AC_RF' in ffn) > 0  THEN
            'Fixed Income'
        WHEN position('AC_RV' in ffn) > 0  THEN
            'Variable Income'
        ELSE
            'Unknown'
    END AS priceinterfacename,
    --
    case when fn = 'AFS_' then 'AFS'
         when fn = 'CAMD' then 'FX'
         when fn = 'AC_I' then 'Indexes'
         when fn = 'AC_R' then 
            case when sfn = 'F' then 'Fixed Income'
                 else 'Variable Income'
            end
         else fn
    end as priceinterfacenameold,
    status_,
    creationdate_,
    nbrecords_,
    externalid_,
    ffn
FROM
    (
        SELECT
            flowid_,
            nbrecords_,
            externalid_,
            upper(substr(externalid_, 44, 4)) fn,
            upper(substr(externalid_, 48, 1)) sfn,
            status_,
            creationdate_,
            upper(substr(externalid_, position('/' in externalid) + 1)) ffn
        FROM
            broker_flowin
        WHERE
                inputdevice_deviceid_ = 'INSTRUMENTPRICE_IN'
            AND to_javadate(creationdate_) > :sql_last_value      --> no to_javadate in Oracle, AFAIK
    ) x
ORDER BY
    creationdate_ DESC
Littlefoot
  • 131,892
  • 15
  • 35
  • 57
  • I need to convert this query to PostreSQL it's because i'm using driver PostreSQL in my logstash – aymen krifa Jan 05 '23 at 20:01
  • 1
    Looks like a valid conversion except that postgres does not have the function `instr()` instead Postgres provides the [position()](https://www.postgresql.org/docs/9.1/functions-string.html#FUNCTIONS-STRING-SQL). There is a slight format difference. `WHEN instr(ffn, 'AFB') > 0` that becomes `WHEN position( 'AFB' in ffn) > 0`. Similar transforms for others. – Belayer Jan 07 '23 at 03:01
  • Thank you, @Belayer. What's even worse, I *said* that at the beginning of that answer and - somehow (can't explain why) - "forgot" to apply that to code I posted. Fixed now. – Littlefoot Jan 07 '23 at 11:47
  • hello @Littlefoot, i tried to execute your last code and i had the following error : [2023-01-10T14:49:01,123][ERROR][logstash.inputs.jdbc ] Java::OrgPostgresqlUtil::PSQLException: Unterminated string literal started at position 1,373 in SQL SELECT /* creta */ --> in Oracle, this is just a comment flowid_ AS pk_, – aymen krifa Jan 10 '23 at 17:50
  • Indeed, AFB lacked in closing single quote. Fixed, so - try again. – Littlefoot Jan 10 '23 at 21:19