gpt4 book ai didi

Mysql 数据透视表按两列分组

转载 作者:行者123 更新时间:2023-11-30 01:02:36 25 4
gpt4 key购买 nike

我在使用 Mysql 创建数据透视表时遇到问题。

实际查询如下

SELECT DISTINCT `bills_organization`.`name`,
IF(`bills_bill`.`os_status`=1,COUNT(`bills_bill`.`os_status`),NULL) AS Unnknown,
IF(`bills_bill`.`os_status`=2,COUNT(`bills_bill`.`os_status`),NULL) AS Introduced,
IF(`bills_bill`.`os_status`=3,COUNT(`bills_bill`.`os_status`),NULL) AS Passedonechamber,
IF(`bills_bill`.`os_status`=4,COUNT(`bills_bill`.`os_status`),NULL) AS Passedbothchambers,
IF(`bills_bill`.`os_status`=5,COUNT(`bills_bill`.`os_status`),NULL) AS Enacted
FROM `bills_mybill`
INNER JOIN `auth_user` ON (`bills_mybill`.`user_id` = `auth_user`.`id`)
LEFT OUTER JOIN `bills_bill` ON (`bills_mybill`.`bill_id` = `bills_bill`.`id`)
LEFT OUTER JOIN `bills_userprofile` ON (`auth_user`.`id` = `bills_userprofile`.`user_id`)
LEFT OUTER JOIN `bills_organization` ON (`bills_userprofile`.`organization_id` = `bills_organization`.`id`)
WHERE `bills_mybill`.`favorite` = TRUE
GROUP BY `bills_organization`.`name`,`bills_bill`.`os_status`

输出如下

name   Unnknown   Introduced   Passedonechamber   Passedbothchambers   EnactedNUL    NULL       5            NULL               NULL                 NULLNULL   NULL       NULL         NULL               1                    NULLAEE    NULL       16           NULL               NULL                 NULLAEE    NULL       NULL         1                  NULL                 NULLAEE    NULL       NULL         NULL              4                     NULLAEE    NULL       NULL         NULL              NULL                  2Testing NULL      6            NULL              NULL                  NULLTesting NULL      NULL         NULL              2                     NULLTesting NULL      NULL         NULL              NULL                  6

我想要组织并且数在一行中。我不想多行。

提前致谢。

最佳答案

SELECT `bills_organization`.`name`,
SUM(`bills_bill`.`os_status`=1) AS Unnknown,
SUM(`bills_bill`.`os_status`=2) AS Introduced,
SUM(`bills_bill`.`os_status`=3) AS Passedonechamber,
SUM(`bills_bill`.`os_status`=4) AS Passedbothchambers,
SUM(`bills_bill`.`os_status`=5) AS Enacted
FROM `bills_mybill`
INNER JOIN `auth_user` ON (`bills_mybill`.`user_id` = `auth_user`.`id`)
LEFT OUTER JOIN `bills_bill` ON (`bills_mybill`.`bill_id` = `bills_bill`.`id`)
LEFT OUTER JOIN `bills_userprofile` ON (`auth_user`.`id` = `bills_userprofile`.`user_id`)
LEFT OUTER JOIN `bills_organization` ON (`bills_userprofile`.`organization_id` = `bills_organization`.`id`)
WHERE `bills_mybill`.`favorite` = TRUE
GROUP BY `bills_organization`.`name`

关于Mysql 数据透视表按两列分组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19971209/

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