gpt4 book ai didi

mysql - 有GROUP_CONCAT,有没有GROUP_SUM?

转载 作者:可可西里 更新时间:2023-11-01 07:55:43 27 4
gpt4 key购买 nike

qty 是一个整数。此 SQL 有效,但我想对 qty 值求和,而不是将它们连接起来。没有 GROUP_SUM: 叫别的什么?

SELECT sku as filterSKU, storenumber as storenumberSKU,
GROUP_CONCAT((CASE weekdayoftransaction WHEN 0 THEN qty ELSE NULL END)) AS Monday,
GROUP_CONCAT((CASE weekdayoftransaction WHEN 1 THEN qty ELSE NULL END)) AS Tuesday,
GROUP_CONCAT((CASE weekdayoftransaction WHEN 2 THEN qty ELSE NULL END)) AS Wednesday,
GROUP_CONCAT((CASE weekdayoftransaction WHEN 3 THEN qty ELSE NULL END)) AS Thursday,
GROUP_CONCAT((CASE weekdayoftransaction WHEN 4 THEN qty ELSE NULL END)) AS Friday,
GROUP_CONCAT((CASE weekdayoftransaction WHEN 5 THEN qty ELSE NULL END)) AS Saturday,
GROUP_CONCAT((CASE weekdayoftransaction WHEN 6 THEN qty ELSE NULL END)) AS Sunday
FROM `reconciled`.sale
GROUP BY sku, storenumber;

这是表格:

CREATE TABLE `sale` (
`saleID` int(11) NOT NULL AUTO_INCREMENT,
`EmployeeFirstName` varchar(45) DEFAULT NULL,
`EmployeeLastName` varchar(45) DEFAULT NULL,
`ProductDescription` varchar(45) DEFAULT NULL,
`Unit` varchar(45) DEFAULT NULL,
`SKU` varchar(45) DEFAULT NULL,
`Qty` int(11) DEFAULT NULL,
`UnitCost` decimal(10,3) DEFAULT NULL,
`UnitPrice` decimal(10,3) DEFAULT NULL,
`Manufacturer` varchar(45) DEFAULT NULL,
`EmployeeNumber` varchar(45) DEFAULT NULL COMMENT 'The employee who handled the transaction',
`LoyaltyNumber` varchar(45) DEFAULT NULL,
`StoreNumber` varchar(45) DEFAULT NULL,
`DateOfTransaction` date DEFAULT NULL,
`TimeOfTransaction` time DEFAULT NULL,
`DateOfTransactionString` varchar(45) DEFAULT NULL,
`TimeOfTransactionString` varchar(45) DEFAULT NULL,
`WeekdayOfTransaction` int(11) DEFAULT NULL,
`MonthOfTransaction` int(11) DEFAULT NULL,
`YearOfTransaction` int(11) DEFAULT NULL,
`WeekdayNameOfTransaction` varchar(45) DEFAULT NULL,
`MonthNameOfTransaction` varchar(45) DEFAULT NULL,
`TotalPrice` decimal(10,3) DEFAULT NULL,
PRIMARY KEY (`saleID`)
) ENGINE=InnoDB AUTO_INCREMENT=13 DEFAULT CHARSET=utf8;

最佳答案

简称为sum .您可以通过 group by 子句(您已经有了!)实现分组:

SELECT   sku AS filterSKU, storenumber AS storenumberSKU,
SUM(CASE weekdayoftransaction WHEN 0 THEN qty ELSE NULL END) AS Monday,
SUM(CASE weekdayoftransaction WHEN 1 THEN qty ELSE NULL END) AS Tuesday,
SUM(CASE weekdayoftransaction WHEN 2 THEN qty ELSE NULL END) AS Wednesday,
SUM(CASE weekdayoftransaction WHEN 3 THEN qty ELSE NULL END) AS Thursday,
SUM(CASE weekdayoftransaction WHEN 4 THEN qty ELSE NULL END) AS Friday,
SUM(CASE weekdayoftransaction WHEN 5 THEN qty ELSE NULL END) AS Saturday,
SUM(CASE weekdayoftransaction WHEN 6 THEN qty ELSE NULL END) AS Sunday
FROM `reconciled`.sale
GROUP BY sku, storenumber;

关于mysql - 有GROUP_CONCAT,有没有GROUP_SUM?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51126277/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com