如何在使用计数和 group_contat 后找到总数

How to find the total after using count and group_contat

所以我做了一个显示总数的 table,但总数不正确。

SQL Fiddle: http://sqlfiddle.com/#!9/de76b6e/12

预期输出:

ClientName ClientPhone Address Services Total
Chan Tai Man 12345678 82 Kennedy Street, Wan Chai Carpets(2),Sofas(1) 1120
Wong Kai tung 28473465 24 Kennedy Road, Wan Chai Mattresses(1) 100
Chan Tai Man 63827482 26 Queen Road East, Wan Chai Carpets(1) 500

实际输出:

ClientName ClientPhone Address Services Total
Chan Tai Man 12345678 82 Kennedy Street, Wan Chai Carpets(2),Sofas(1) 620
Wong Kai tung 28473465 24 Kennedy Road, Wan Chai Mattresses(1) 100
Chan Tai Man 63827482 26 Queen Road East, Wan Chai Carpets(1) 500

我的数据:


INSERT INTO MAIN VALUES( 
    null,'Carpets', '2021-03-09', '1');  
INSERT INTO MAIN VALUES( 
    null,'Sofas', '2021-03-09', '1');
INSERT INTO MAIN VALUES( 
    null,'Mattresses', '2021-03-10', '2');
INSERT INTO MAIN VALUES( 
    null,'Carpets', '2021-03-11', '3');
INSERT INTO MAIN VALUES( 
    null,'Carpets', '2021-03-11', '1');

  
 
/*****  CREATE SERVICE  **************************************************/
DROP TABLE IF EXISTS SERVICE;
CREATE TABLE SERVICE(
  ServiceName                       VarChar(20)   NOT NULL,
  CPS                               Numeric(20)   NOT NULL,
  CONSTRAINT          SERVICE_PK            PRIMARY KEY(ServiceName)
    );

/*****   SERVICE DATA   ***********************************************************/

INSERT INTO SERVICE VALUES('Carpets', 500);
INSERT INTO SERVICE VALUES('Sofas', 120);
INSERT INTO SERVICE VALUES('Mattresses', 100);
INSERT INTO SERVICE VALUES('Curtains', 80);



/*****   CREATE CLIENT   ***********************************************************/
DROP TABLE IF EXISTS CLIENT;
CREATE TABLE CLIENT (
    ClientID                            Int      auto_increment  NOT NULL,
    ClientName                      Char (50)           NOT NULL,
    ClientPhone                              Int (8)                    NOT NULL,
    Address                              VarChar(100)               NULL,
    CONSTRAINT        CLIENT_PK             PRIMARY KEY(ClientID)
    );

/*****   CLIENT DATA   *************************************************************/

INSERT INTO CLIENT VALUES(
    null,'Chan Tai Man', '12345678', '82 Kennedy Street, Wan Chai');
INSERT INTO CLIENT VALUES(
    null,'Wong Kai tung', '28473465', '24 Kennedy Road, Wan Chai');
INSERT INTO CLIENT VALUES(
    null,'Chan Tai Man', '63827482', '26 Queen Road East, Wan Chai');

/***********************************************************************************/

我的代码:

SELECT CLIENT.ClientName, CLIENT.ClientPhone, CLIENT.Address, 
GROUP_CONCAT(MAIN.Num) AS Services,
SUM(SERVICE.CPS) AS Total
FROM (
  SELECT DOS, InvoiceID, ClientID, ServiceName, 
  CONCAT(ServiceName, '(', count(ServiceName), ')') AS Num
  From MAIN
  GROUP BY ClientID, ServiceName
  ) MAIN
LEFT JOIN CLIENT ON MAIN.ClientID = CLIENT.ClientID 
LEFT JOIN SERVICE ON MAIN.ServiceName = SERVICE.ServiceName
GROUP BY CLIENT.ClientID
ORDER BY MAIN.DOS;

问题是您在对值进行汇总后才计算 SUM。您应该在子查询中对服务 table 进行求和和 JOIN,例如:

SELECT CLIENT.ClientName, CLIENT.ClientPhone, CLIENT.Address, 
       GROUP_CONCAT(MAIN.Num) AS Services,
       sum(main.Total) as total
FROM (
  SELECT DOS, InvoiceID, ClientID, main.ServiceName, 
         CONCAT(main.ServiceName, '(', count(main.ServiceName), ')') AS Num,
         SUM(SERVICE.CPS) AS Total
  From MAIN
  LEFT JOIN SERVICE ON MAIN.ServiceName = SERVICE.ServiceName
  GROUP BY ClientID, ServiceName
  ) MAIN
LEFT JOIN CLIENT ON MAIN.ClientID = CLIENT.ClientID 
GROUP BY CLIENT.ClientID
ORDER BY MAIN.DOS;

除此之外,我不会使用与 table 名称相同的名称作为别名