pingcap / tidb

TiDB is an open-source, cloud-native, distributed, MySQL-Compatible database for elastic scale and real-time analytics. Try AI-powered Chat2Query free at : https://www.pingcap.com/tidb-serverless/
https://pingcap.com
Apache License 2.0
36.55k stars 5.75k forks source link

Percentile Function in Tidb #46985

Open adishwarsonapally1 opened 9 months ago

adishwarsonapally1 commented 9 months ago

Bug Report

perncentile on a column is not working SELECT PERCENTILE_CONT(0.75) WITHIN GROUP (ORDER BY column_name) OVER () AS Q3 FROM YourTable; This is the query which is supported in both postgresql and mysql but it is throwing an error in TIDB

2. What did you expect to see? (Required)

result

3. What did you see instead (Required)

You have an error in your SQL syntax; check the manual that corresponds to your TiDB version for the right syntax to use line 2 column 41 near "(ORDER BY _org_unit_id) OVER () AS Q1

4. What is your TiDB version? (Required)

7.3.0

adishwarsonapally1 commented 9 months ago

Is there any alternative query to find percentile?

adishwarsonapally1 commented 9 months ago

Can I know the status of this feature?

dveeden commented 7 months ago

Looks like this would increase compatibility with Oracle Database, PostgreSQL, MariaDB, SQL Server, Snowflake, etc. I haven't checked but that probably means this is in the ISO/IEC SQL Standards?

dveeden commented 7 months ago

If someone wants to implement this then https://databaseblog.myname.nl/2023/08/tutorial-add-qrcode-function-to-tidb.html could be a start to learn how to implement functions in TiDB.

akoshchiy commented 7 months ago

@dveeden Hi! Can I try it?

dveeden commented 7 months ago

@dveeden Hi! Can I try it?

You want to try and implement PERCENTILE_CONT()? Yes, please try. Feel free to ask any questions you might have either here on TiDB Community Slack.

dveeden commented 6 months ago

This seems to help with compatibility with AWS Redshift, PostgreSQL, MariaDB, DB2, Vertica, Databricks, Oracle, SingleStore (memsql), MS SQL Server and more. However there seems to be significant differences in these implementations. However I haven't really seen conflicting implementations.

dveeden commented 6 months ago

https://modern-sql.com/slides/SQLinThe21stCentury-2019-10-17.pdf seems related.

harsh-ande commented 1 month ago

Hi @dveeden, can I work on this?

dveeden commented 1 month ago

@harsh-vardhan-ande there is a draft PR ( #49895 ) from @akoshchiy that looks like it wasn't updated for a while. So I think it would be ok for you to work on this.