Is the code for converting rows to columns and columns to rows not usable in TiDB?

Note:
This topic has been translated from a Chinese forum by GPT and might contain errors.

Original topic: 行转列,列转行的代码是不是在tidb里面用不了?

| username: Steve阿辉

Is the code for converting rows to columns and columns to rows not usable in TiDB?

| username: tidb菜鸟一只 | Original post link

The PIVOT function for row-to-column transformation is only available in Oracle and PostgreSQL, right? MySQL doesn’t support it either.

| username: Steve阿辉 | Original post link

It seems so. When I input this function in Navicat, it is not recognized.

| username: tidb菜鸟一只 | Original post link

It is recommended to use aggregate functions and case functions as a replacement:

SELECT
  username,
  SUM(CASE WHEN subject = 'Chinese' THEN score ELSE 0 END) AS "Chinese",
  SUM(CASE WHEN subject = 'Math' THEN score ELSE 0 END) AS "Math",
  SUM(CASE WHEN subject = 'English' THEN score ELSE 0 END) AS "English",
  SUM(CASE WHEN subject = 'Biology' THEN score ELSE 0 END) AS "Biology"
FROM scores
GROUP BY username;
| username: Kongdom | Original post link

Yes, it should not be supported.

| username: Steve阿辉 | Original post link

Thank you, I learned something. Much appreciated.

| username: system | Original post link

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.