Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/eclipse/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Mysql 分组和总计_Mysql_Grouping - Fatal编程技术网

Mysql 分组和总计

Mysql 分组和总计,mysql,grouping,Mysql,Grouping,好的。我有一张表,我需要我的结果像这样 Month Active_Total SIHO_Total Active_Paid SIHO_Paid 2004-02 317 315 2620.19 1503.56 但是我有一个问题…前面提到的表是分组的。这是一个医疗保健类型的innoDB数据库,具有多个不同的模式和表。在我试图隔离SIHO成员(这是我真正关心的)的过程中,我隔离了不属于SIHO的成员,并创建了一个只包含我关心的成员的表。此表如下所示: S

好的。我有一张表,我需要我的结果像这样

Month  Active_Total  SIHO_Total Active_Paid SIHO_Paid
2004-02   317            315       2620.19   1503.56
但是我有一个问题…前面提到的表是分组的。这是一个医疗保健类型的innoDB数据库,具有多个不同的模式和表。在我试图隔离SIHO成员(这是我真正关心的)的过程中,我隔离了不属于SIHO的成员,并创建了一个只包含我关心的成员的表。此表如下所示:

SCC PHID SID Care_Program Month Total_Paid
B11  abc  01   null      2004-02  120.76
B11  bcd  00   null      2004-02  98.40
  Month SIHO_Total SIHO_Paid
2004-02   2           219.16 or whatever
SCC是静态的,PHID和SID一起构成一个唯一的键,Care_程序是不相关的。它只是用于我的测试,月份-所有内容都需要按月分组,因此对于所有具有“支付总额”值的记录,它们应按如下方式汇总和显示:

SCC PHID SID Care_Program Month Total_Paid
B11  abc  01   null      2004-02  120.76
B11  bcd  00   null      2004-02  98.40
  Month SIHO_Total SIHO_Paid
2004-02   2           219.16 or whatever
我基本上需要第二张桌子看起来像这样

Month  Active_Total  SIHO_Total Active_Paid SIHO_Paid
2004-02   317            315       2620.19   1503.56
我如何将这些PHID分组,以获得该成员当月的计数


如果你需要更多的信息,请告诉我。提前谢谢

所以我的第一步是汇总这些表。为了得到我的最终结果表,我把问题分为两部分。第一部分是ActiveMembersPerMonth,它反映了:

month    total
2004-02    5
为此,我创建了一个例程来处理分组,我的存储过程如下所示:

DELIMITER $$

CREATE PROCEDURE `ActiveMembersPerMonth`(sStartMonth VARCHAR(10), sEndMonth VARCHAR(10))
BEGIN
DECLARE dStartMonth DATE DEFAULT DB.ConvertYearMonthDate(sStartMonth,False);
DECLARE dEndMonth DATE DEFAULT DB.ConvertYearMonthDate(sEndMonth,True);
DECLARE curMonthStart DATE;
DECLARE curMonthEnd DATE;
    DECLARE curMonthStr CHAR(7);


TRUNCATE DB.ActiveMembersPerMonth_;

SET curMonthStart = dStartMonth;
WHILE curMonthStart<=dEndMonth DO
    SET curMonthEnd = LAST_DAY(curMonthStart);
SET curMonthStr = date_format(curMonthStart,'%Y-%m');

    INSERT INTO DB.ActiveMembersPerMonth
        SELECT eh.SCC, eh.PHID, eh.SID, eh.CID, eh.CGID, curMonthStr,count(*),sum(IF(m.membership='Employee',1,0)),
        sum(IF(m.membership<>'Employee',1,0)),null 
  from DB.table_SIHO eh
  join DB.Membership m on eh.SCC=m.SCC and eh.PHID=m.PHID
        and eh.SID=m.SID
  WHERE eh.Enroll_Date<=curMonthStart AND (eh.Disenroll_Date>curMonthEnd OR eh.Disenroll_Date IS NULL)
  GROUP BY SCC, CID, CGID, curMonthStr;

    SET curMonthStart = curMonthStart + INTERVAL 1 MONTH;
END WHILE;
END;

call ActiveMembersPerMonth_SIHO('2006-01-01', '2012-05-31')
现在,我有了另一个表聚合,它包含月份和支付的总金额,所以我将把它们与从存储过程中创建的月份和活动总金额结合起来

CREATE TABLE Rx_Paid_
SELECT CGID, date_format(DOS, '%Y-%m') as Month, SUM(Amt_Paid) as Rx_Paid FROM    DB.Prescriptions
WHERE concat(Policy_HoldeR_ID, Suffix_ID) not in 
('M000560838','M000029518','M00002699','M00002769') and DOS between '2006-01-01'   and '2012-05-31'
group by CGID, Month; 
服务也是一样-

CREATE TABLE Services_Paid;
SELECT CGID, date_format(Serv_Beg_Date, '%Y-%m') as Month, SUM(Amt_Paid) as    Services_Paid FROM DB.Services
WHERE concat(PHID, SID) not in    ('M000560838','M000029518','M00002699','M00002769') and Beg_Date between '2006-01- 01' and '2012-05-31'
group by CGID, Month; 
基本上,我在团队中通过以下方式实现了我想要的结果。我在第一个提到的表格中支付了总金额。我所要做的就是按月份分组和一个标识符(ClientGroupID)(组级标识符)

我的意思是它显然是一个总和(total_paid),我更关心的是计数(如果(PHID==true??)(逻辑而不是语法),其中total_paid为null?IDK我希望它是准确的。