mysql 将计数函数添加到现有查询

lnlaulya  于 2022-11-21  发布在  Mysql
关注(0)|答案(1)|浏览(131)

我有四张这样的table:

create table GROSS(DATE_ACT DATE, SUB_ID BIGINT, PP_ID BIGINT, CUSTOMER_TYPE VARCHAR(50), REACTIVATION int);

        insert into GROSS values(2019-11-3, 234, 5, 'Business', 1);
        insert into GROSS values(2018-9-2, 131, 8, 'Business', 0);
        insert into GROSS values(2018-11-3, 98, 3, 'Private', 1);
        
        create table TARIFF(PP_ID INT, PP_NAME VARCHAR(100), SUB_ID INT, PP_START_DATE DATE, PP_END_DATE DATE);
        insert into TARIFF values(3, 'PLAN_1', 98, 2021-5-3, 2021-6-3);
        insert into TARIFF values(5, 'Business plan 3.0', 234, 2021-5-6, 2021-6-6);
        insert into TARIFF values(8, 'Business plan 4.0', 131, 2021-5-10, 2021-6-10);
        
        create table SERVICE(SERVICE_START_DATE DATE, SERVICE_STOP_DATE DATE, SUB_ID INT, SERVICE_NAME VARCHAR(100));
        insert into SERVICE values(2021-5-7, 2021-6-7, 98, 'Unlimited Internet 2');
        insert into SERVICE values(2021-5-7, 2021-6-7, 98, 'Internet');
        insert into SERVICE values(2021-5-7, 2021-6-7, 98, 'Unlimited Internet 512');
        
        create table SUBSCRIBER(MONTH DATE, COMPANY_NAME VARCHAR(100), SUB_ID INT, CUSTOMER_TYPE VARCHAR(100), STATUS INT, PP_TYPE_ID VARCHAR(50));
        insert into SUBSCRIBER values(2022-1-6, 'A1', 98, 'Private', 1, 'Fixed');
        insert into SUBSCRIBER values(2022-1-6, 'MTS', 234, 'Business', 1, 'Fixed');
        insert into SUBSCRIBER values(2022-1-6, 'Life', 131, 'Business', 1, 'Fixed');
insert into GROSS values(2021-12-15, 228, 5, 'Business', 0);
insert into TARIFF values(5, 'Бизнес-план 3.0', 228, 2021-12-15, 2999-01-01);
insert into SERVICE values(2021-12-15, 2999-01-01, 228, 'Безлимитный Интернет 512');
insert into SUBSCRIBER values(2022-01-01,'MTS', 228, 'Business', 1, 'Voice');

我已经做了一个查询,我需要有这样的:

select GROSS.DATE_ACT, GROSS.SUB_ID, TARIFF.PP_NAME, SERVICE.SERVICE_NAME, SUBSCRIBER.COMPANY_NAME
from GROSS 
inner join TARIFF on GROSS.SUB_ID = TARIFF.SUB_ID 
inner join SERVICE on TARIFF.SUB_ID = SERVICE.SUB_ID 
inner join SUBSCRIBER on SERVICE.SUB_ID = SUBSCRIBER.SUB_ID
where MONTH(GROSS.DATE_ACT) = 12
and GROSS.CUSTOMER_TYPE = 'Business'
and TARIFF.PP_NAME regexp 'Бизнес-план.+'
and GROSS.DATE_ACT = SERVICE.SERVICE_START_DATE
and SERVICE.SERVICE_NAME = 'Безлимитный интернет 512' or 'Безлимитный интернет 1' or 'Безлимитный интернет 2'
and YEAR(SERVICE.SERVICE_STOP_DATE) = 2999;

enter image description here
我如何添加count函数,以便它返回查询中返回的表,同时我需要计算SERVICE.COMPANY_NAME与接收到的用户相同的所有用户中通常有多少活动用户(SUBSCRIBER.STATUS = 1),在本例中为MTS
我浏览了所有的互联网,但我的大脑不能帮助我解决这个问题

2lpgd968

2lpgd9681#

为count添加case语句,

select GROSS.DATE_ACT, 
       GROSS.SUB_ID, 
       TARIFF.PP_NAME, 
       SERVICE.SERVICE_NAME, 
       SUBSCRIBER.COMPANY_NAME,
       SUM(CASE WHEN(SUBSCRIBER.STATUS = 1 and SUBSCRIBER.COMPANY_NAME = 'MTS') THEN 1 ELSE 0 END) as CNT
  from GROSS 
  inner join TARIFF on GROSS.SUB_ID = TARIFF.SUB_ID 
  inner join SERVICE on TARIFF.SUB_ID = SERVICE.SUB_ID 
  inner join SUBSCRIBER on SERVICE.SUB_ID = SUBSCRIBER.SUB_ID
  where MONTH(GROSS.DATE_ACT) = 12
    and GROSS.CUSTOMER_TYPE = 'Business'
    and TARIFF.PP_NAME regexp 'Бизнес-план.+'
    and GROSS.DATE_ACT = SERVICE.SERVICE_START_DATE
    and SERVICE.SERVICE_NAME = 'Безлимитный интернет 512' or 'Безлимитный интернет 1' or 'Безлимитный интернет 2'
    and YEAR(SERVICE.SERVICE_STOP_DATE) = 2999
  group by GROSS.DATE_ACT, 
           GROSS.SUB_ID, 
           TARIFF.PP_NAME, 
           SERVICE.SERVICE_NAME, 
           SUBSCRIBER.COMPANY_NAME;

找不到您所述的有问题的SERVICE.COMPANY_NAME列。请在解决问题时进行相应编辑。

相关问题