Search code examples
mysqlsqldatabasepartition-by

How to resolve the following mysql query on partition by clause. I believe I am using the clauses correctly


query = """
        SELECT
            id,
            CAST(CAST(ts AS DATE) AS VARCHAR) AS param1,
            param2
        FROM (
            SELECT
                id,
                ts,
                param2_long_name AS param2,
                RANK() OVER (
                    PARTITION BY
                        id
                    ORDER BY
                        ts
                ) AS rank
            FROM (
                SELECT
                    DISTINCT
                    id,
                    ts,
                    param2_long_name
                FROM my_table
                WHERE
                    CAST(CAST(ts AS DATE) AS VARCHAR) > '2018-07-01'
                    AND id IN {ids}
            )
        )
        WHERE
            rank = 1
    """.format(my_table=table, ids=ids)

Getting the following error : ': (1064, "You have an error in your SQL syntax; it seems the error is around: 'VARCHAR) AS param1, param2 FROM ( SELECT id, ts, param' at line 3")'

PS: querying mysql from python


Solution

  • You have to write

      SELECT CAST(CAST("2017-08-29" AS DATE) as CHAR(12));
    

    varchar is not supported.