SQL Server Incorrect syntax near the keyword 'on'. incorrect syntax near bcolor. expecting '(' or select [closed]

xesrikrc  于 2023-06-21  发布在  其他
关注(0)|答案(1)|浏览(154)

Closed. This question is not reproducible or was caused by typos . It is not currently accepting answers.

This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.

Closed 15 hours ago.
Improve this question

SELECT
    DISTINCT on (bcolor) bcolor,
    fcolor
FROM
    distinct_dem 
ORDER BY
    bcolor,
    fcolor;

Help me to solve this issue

Incorrect syntax near the keyword 'on'.

I am working on SQL Server.

eqqqjvef

eqqqjvef1#

The query I provided appears to be using the PostgreSQL syntax for the DISTINCT ON clause. In SQL Server, the equivalent functionality can be achieved using a different approach. Here's an alternative query that achieves a similar result in SQL Server:

SELECT bcolor, fcolor
FROM (
    SELECT bcolor, fcolor,
        ROW_NUMBER() OVER (PARTITION BY bcolor ORDER BY fcolor) AS row_num
    FROM distinct_demo
) AS subquery
WHERE row_num = 1
ORDER BY bcolor, fcolor;

相关问题