그래서 첫 번째 단계는 이러한 테이블을 집계하는 것이 었습니다. 내 최종 결과 테이블을 달성하기 위해 문제를 두 부분으로 나누었습니다. 이를 위해
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')
문제의 다음 부분에 있었다 첫 번째 부분은 반영 ActiveMembersPerMonth이다
Active_Total Month
5 2006-02
지금은 달을 가진 다른 테이블의 집계하고 그래서 나는 total_paid을 가지고있는 ... 내가 지금 무슨와 일치하는 total_Paid과 관련된 달을 얻을 단지 서비스에 대한 .... 내 저장 프로 시저에서 만들어진 개월 active_Total 이러한를 가입
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;
를 기본적으로 내가 내 원하는 결과를 얻을 내 그룹별로. 처음 언급 한 표에 total_paid가 있습니다. 내가해야 할 일을했을 모두는 월별로 그룹 식별자 (ClientGroupID) (그룹 레벨 식별자)
CGID Month Active Service_Paid Rx_Paid
128 2006-01 314 2620.19 0.00
128 2006-02 330 4880.46 0.00
128 2006-03 344 29312.00 0.00
128 2006-04 359 15177.99 0.00
128 2006-05 386 774.58 0.00
128 2006-06 421 5966.52 0.00
는 내가합니다 (COUNT에 대한 자세한 걱정 IF (PHID == 사실은 분명 합 (total_paid)을 의미했다 ???) (논리가 아닌 구문) 여기서 Total_Paid는 null입니까? IDK 정확해야합니다. – Hituptony