在查询中出现了无效使用分组函数的错误。

huangapple go评论57阅读模式
英文:

getting invalid use of group function error in query

问题

我试图根据class_id和month_id将student_attendance_daily中的数据转换,但出现错误invalid use of group function。由于我使用的是MariaDB 10.4,无法使用JSON_ARRAYAGG,但它在10.5中可用。我在AWS RDS上使用它,无法选择升级,所以我尝试这样做。

这是我的查询:

INSERT INTO attendance_student (class_id, month_id, attendance_details, created_at, updated_at)
SELECT 
  class_id, 
  DATE_FORMAT(attendance_date, '%Y%m') AS month_id, 
  CONCAT('[', 
    GROUP_CONCAT(
      CONCAT(
        '{"att_date":"', DATE_FORMAT(attendance_date, '%d-%m-%Y'), '","att_status":"', attendance_status, '","att_punch":',
        IFNULL(
          CONCAT('[', 
            GROUP_CONCAT(
              CONCAT(
                '{"did":"', bio_punch_details, '","pat":"', IFNULL(DATE_FORMAT(created_at, '%d-%m-%Y %H:%i:%s')), '","ss":"', IFNULL(bio_punch_details, ''), '"}'
              )
              ORDER BY created_at ASC
              SEPARATOR ','
            ), 
          ']'),
          '[]'
        ),
        '}'
      )
      ORDER BY attendance_date ASC
      SEPARATOR ','
    ) 
  , ']') AS attendance_details, 
  NOW() AS created_at, 
  NOW() AS updated_at
FROM student_attendance_daily
GROUP BY class_id, month_id;

我想插入这样的数据:

(1, 136, 1, '[{"att_date":"12-01-2023","att_status":0,"att_punch":[{"did":"1","pat":"12-01-2023 18:35:33","ss":"ss_std_20230112183534_667757.jpg"}]},{"att_date":"20-01-2023","att_status":1,"att_punch":[{"did":"2","pat":"20-01-2023 00:29:48","ss":"ss_std_20230120002949_672689.jpg"}]}]', '2023-01-12 18:35:34', '2023-01-20 00:29:49')

这是attendance_student表的模式:

CREATE TABLE `attendance_student` (
  `id` int(11) NOT NULL,
  `class_id` int(11) NOT NULL,
  `month_id` int(11) NOT NULL,
  `attendance_details` mediumtext COLLATE utf8_unicode_ci NOT NULL,
  `created_at` datetime DEFAULT NULL,
  `updated_at` datetime DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

这是student_attendance_daily表的模式:

CREATE TABLE `student_attendance_daily` (
  `id` int(11) NOT NULL,
  `class_id` int(11) NOT NULL,
  `attendance_date` date NOT NULL,
  `attendance_status` tinyint(1) NOT NULL,
  `late_status` tinyint(1) DEFAULT NULL,
  `attendance_remarks` varchar(100) COLLATE utf8_unicode_ci DEFAULT NULL,
  `bio_punch_details` text COLLATE utf8_unicode_ci,
  `created_at` datetime NOT NULL,
  `created_by` int(11) NOT NULL,
  `updated_at` datetime NOT NULL,
  `updated_by` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
英文:

I am trying to convert the data from student_attendance_daily on the basis of class_id and month id but I am getting error invalid use of group function
I cannot use JSON_ARRAYAGG because i am using MariaDB 10.4 but it is available in 10.5 i am using it on AWS RDS and do not have choice to upgrade so i am trying like this.

here is my query

INSERT INTO attendance_student (class_id, month_id, attendance_details, created_at, updated_at)
SELECT 
  class_id, 
  DATE_FORMAT(attendance_date, '%Y%m') AS month_id, 
  CONCAT('[', 
    GROUP_CONCAT(
      CONCAT(
        '{"att_date":"', DATE_FORMAT(attendance_date, '%d-%m-%Y'), '",',
        '"att_status":"', attendance_status, '",',
        '"att_punch":',
        IFNULL(
          CONCAT('[', 
            GROUP_CONCAT(
              CONCAT(
                '{"did":"', bio_punch_details, '",',
                '"pat":"', IFNULL(DATE_FORMAT(created_at, '%d-%m-%Y %H:%i:%s'), ''), '",',
                '"ss":"', IFNULL(bio_punch_details, ''), '"}'
              )
              ORDER BY created_at ASC
              SEPARATOR ','
            ), 
          ']'),
          '[]'
        ),
        '}'
      )
      ORDER BY attendance_date ASC
      SEPARATOR ','
    ) 
  , ']') AS attendance_details, 
  NOW() AS created_at, 
  NOW() AS updated_at
FROM student_attendance_daily
GROUP BY class_id, month_id;

I want to insert data like this

(1, 136, 1, '[{\"att_date\":\"12-01-2023\",\"att_status\":0,\"att_punch\":[{\"did\":\"1\",\"pat\":\"12-01-2023 18:35:33\",\"ss\":\"ss_std_20230112183534_667757.jpg\"}]},{\"att_date\":\"20-01-2023\",\"att_status\":1,\"att_punch\":[{\"did\":\"2\",\"pat\":\"20-01-2023 00:29:48\",\"ss\":\"ss_std_20230120002949_672689.jpg\"}]}]', '2023-01-12 18:35:34', '2023-01-20 00:29:49')

it is attendance_student schema

CREATE TABLE `attendance_student` (
  `id` int(11) NOT NULL,
  `class_id` int(11) NOT NULL,
  `month_id` int(11) NOT NULL,
  `attendance_details` mediumtext COLLATE utf8_unicode_ci NOT NULL,
  `created_at` datetime DEFAULT NULL,
  `updated_at` datetime DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

--

and it is student_attendance_daily table schema

CREATE TABLE `student_attendance_daily` (
  `id` int(11) NOT NULL,
  `class_id` int(11) NOT NULL,
  `attendance_date` date NOT NULL,
  `attendance_status` tinyint(1) NOT NULL,
  `late_status` tinyint(1) DEFAULT NULL,
  `attendance_remarks` varchar(100) COLLATE utf8_unicode_ci DEFAULT NULL,
  `bio_punch_details` text COLLATE utf8_unicode_ci,
  `created_at` datetime NOT NULL,
  `created_by` int(11) NOT NULL,
  `updated_at` datetime NOT NULL,
  `updated_by` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

答案1

得分: 0

你不能在同一查询中将聚合函数嵌套在另一个聚合函数内。

您必须使用子查询来生成内部聚合,然后在外部查询中执行外部聚合。如果您有更多层次的嵌套 JSON,您将需要进一步的子查询。

如果有一天您可以升级并使用JSON_ARRAYAGG()函数,这也适用于它。

英文:

You can't put an aggregate function inside an aggregate function in the same query.

You have to use a subquery to generate the inner aggregate, then do the outer aggregate in the outer query. If you have further levels of nesting JSON, you would need further levels of subqueries.

This would apply to JSON_ARRAYAGG() as well if you are someday able to upgrade and use that function.

huangapple
  • 本文由 发表于 2023年2月24日 00:09:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/75547394.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定