こんにちは、SQLServer2008 を使用しています。
以下は私のStoredProcedureです
DECLARE @tmp TABLE
(
Id INT IDENTITY(1, 1) ,
BrokerId INT ,
RankId INT ,
BrokerName VARCHAR(50) ,
RankName VARCHAR(50) ,
BrokerCode VARCHAR(50) ,
IntroducerCode VARCHAR(50) ,
CscName VARCHAR(50) ,
SIP DECIMAL(18, 2) ,
Fresh DECIMAL(18, 2) ,
FY DECIMAL(18, 2) ,
SY DECIMAL(18, 2) ,
TY DECIMAL(18, 2)
)
DECLARE @brokerTable TABLE
(
BrokerCode VARCHAR(50) ,
BrokerId INT
)
INSERT INTO @brokerTable
( BrokerCode, BrokerId )
EXEC GetBrokerIdByCode @BrokerCode
DECLARE @Business TABLE ( BusinessId INT )
DECLARE @BrokerId1 INT
DECLARE @BrokerCode1 VARCHAR(50)
DECLARE c1 CURSOR READ_ONLY
FOR
SELECT BrokerId ,BrokerCode
FROM @brokerTable
OPEN c1
FETCH NEXT FROM c1
INTO @BrokerId1,@BrokerCode1
WHILE @@FETCH_STATUS = 0
BEGIN
INSERT INTO @Business
( BusinessId
)
( SELECT mb.BusinessId
FROM dbo.MemberBusiness AS mb
WHERE mb.BrokerId = @BrokerId1
)
DECLARE @BusinessID2 INT
DECLARE c3 CURSOR READ_ONLY
FOR
SELECT BusinessId
FROM @Business
OPEN c3
FETCH NEXT FROM c3
INTO @BusinessID2
WHILE @@FETCH_STATUS = 0
BEGIN
INSERT INTO @tmp
( BrokerId ,
RankId ,
BrokerName ,
RankName ,
BrokerCode ,
IntroducerCode ,
CscName ,
SIP ,
Fresh ,
FY ,
SY ,
TY
--ProBusiness ,
--CscId
)
( SELECT @BrokerId1 ,
( SELECT RankId
FROM dbo.BrokerMaster AS bm
WHERE bm.BrokerId = @BrokerId1
) ,
( SELECT bm.Name
FROM dbo.BrokerMaster AS bm
WHERE bm.BrokerId = @BrokerId1
) ,
( SELECT rm.RankName
FROM dbo.RankMaster AS rm
WHERE RankId = ( SELECT
RankId
FROM
dbo.BrokerMaster
AS bm
WHERE
bm.BrokerId = @BrokerId1
)
) ,
@BrokerCode1 ,
( SELECT bm2.BrokerCode
FROM dbo.BrokerMaster AS bm2
WHERE bm2.BrokerId = ( SELECT
bm3.IntroducerId
FROM
dbo.BrokerMaster
AS bm3
WHERE
bm3.BrokerId = @BrokerId1
)
) ,
'All CSC' ,
ISNULL(( SELECT SUM(mb.SelfAmount)
+ SUM(mb.UnitAmount)
FROM dbo.MemberBusiness AS mb
INNER JOIN dbo.PlanMaster
AS pm ON mb.PlanId = pm.PlanId
INNER JOIN dbo.PlanTypeMaster
AS ptm ON pm.PlanTypeId = ptm.PlanTypeId
INNER JOIN dbo.Installment
AS i2 ON mb.InstallmentId = i2.InstallmentId
WHERE ptm.IsSingleInstallment = 'true'
AND mb.BrokerId = @BrokerId1
AND i2.CscId = i.CscId
AND ( i2.PaymentDate >= @StartDate
AND i2.PaymentDate <= @EndDate
)
), 0) ,
ISNULL(( SELECT SUM(mb.SelfAmount)
+ SUM(mb.UnitAmount)
FROM dbo.MemberBusiness AS mb
INNER JOIN dbo.PlanMaster
AS pm ON mb.PlanId = pm.PlanId
INNER JOIN dbo.PlanTypeMaster
AS ptm ON pm.PlanTypeId = ptm.PlanTypeId
INNER JOIN dbo.Installment
AS i2 ON mb.InstallmentId = i2.InstallmentId
WHERE ptm.IsSingleInstallment = 'false'
AND mb.BrokerId = @BrokerId1
AND i2.InstallmentNo = 1
AND i2.CscId = i.CscId
AND ( i2.PaymentDate >= @StartDate
AND i2.PaymentDate <= @EndDate
)
), 0) ,
( CASE WHEN ( m.Mode = 'MLY'
AND ( i.InstallmentNo >= 2
AND i.InstallmentNo <= 12
)
)
THEN ( SELECT SUM(mb.SelfAmount)
+ SUM(mb.UnitAmount)
FROM dbo.MemberBusiness
AS mb
INNER JOIN dbo.PlanMaster
AS pm ON mb.PlanId = pm.PlanId
INNER JOIN dbo.PlanTypeMaster
AS ptm ON pm.PlanTypeId = ptm.PlanTypeId
INNER JOIN dbo.Installment
AS i2 ON mb.InstallmentId = i2.InstallmentId
WHERE ptm.IsSingleInstallment = 'false'
AND mb.BrokerId = @BrokerId1
AND i2.InstallmentNo >= 2
AND i2.InstallmentNo <= 12
AND i2.CscId = i.CscId
AND ( i2.PaymentDate >= @StartDate
AND i2.PaymentDate <= @EndDate
)
)
WHEN ( m.Mode = 'QLY'
AND ( i.InstallmentNo >= 2
AND i.InstallmentNo <= 4
)
)
THEN ( SELECT SUM(mb.SelfAmount)
+ SUM(mb.UnitAmount)
FROM dbo.MemberBusiness
AS mb
INNER JOIN dbo.PlanMaster
AS pm ON mb.PlanId = pm.PlanId
INNER JOIN dbo.PlanTypeMaster
AS ptm ON pm.PlanTypeId = ptm.PlanTypeId
INNER JOIN dbo.Installment
AS i2 ON mb.InstallmentId = i2.InstallmentId
WHERE ptm.IsSingleInstallment = 'false'
AND mb.BrokerId = @BrokerId1
AND i2.InstallmentNo >= 2
AND i2.InstallmentNo <= 4
AND i2.CscId = i.CscId
AND ( i2.PaymentDate >= @StartDate
AND i2.PaymentDate <= @EndDate
)
)
WHEN ( m.Mode = 'HLY'
AND ( i.InstallmentNo = 2 )
)
THEN ( SELECT SUM(mb.SelfAmount)
+ SUM(mb.UnitAmount)
FROM dbo.MemberBusiness
AS mb
INNER JOIN dbo.PlanMaster
AS pm ON mb.PlanId = pm.PlanId
INNER JOIN dbo.PlanTypeMaster
AS ptm ON pm.PlanTypeId = ptm.PlanTypeId
INNER JOIN dbo.Installment
AS i2 ON mb.InstallmentId = i2.InstallmentId
WHERE ptm.IsSingleInstallment = 'false'
AND mb.BrokerId = @BrokerId1
AND i2.InstallmentNo = 2
AND i2.IsReceived = 'true'
AND i2.CscId = i.CscId
AND ( i2.PaymentDate >= @StartDate
AND i2.PaymentDate <= @EndDate
)
)
END ) ,
( CASE WHEN ( m.Mode = 'MLY'
AND ( i.InstallmentNo >= 13
AND i.InstallmentNo <= 24
)
)
THEN ( SELECT SUM(mb.SelfAmount)
+ SUM(mb.UnitAmount)
FROM dbo.MemberBusiness
AS mb
INNER JOIN dbo.PlanMaster
AS pm ON mb.PlanId = pm.PlanId
INNER JOIN dbo.PlanTypeMaster
AS ptm ON pm.PlanTypeId = ptm.PlanTypeId
INNER JOIN dbo.Installment
AS i2 ON mb.InstallmentId = i2.InstallmentId
WHERE ptm.IsSingleInstallment = 'false'
AND mb.BrokerId = @BrokerId1
AND i2.InstallmentNo >= 13
AND i2.InstallmentNo <= 24
AND i2.CscId = i.CscId
AND ( i2.PaymentDate >= @StartDate
AND i2.PaymentDate <= @EndDate
)
)
WHEN ( m.Mode = 'QLY'
AND ( i.InstallmentNo >= 5
AND i.InstallmentNo <= 8
)
)
THEN ( SELECT SUM(mb.SelfAmount)
+ SUM(mb.UnitAmount)
FROM dbo.MemberBusiness
AS mb
INNER JOIN dbo.PlanMaster
AS pm ON mb.PlanId = pm.PlanId
INNER JOIN dbo.PlanTypeMaster
AS ptm ON pm.PlanTypeId = ptm.PlanTypeId
INNER JOIN dbo.Installment
AS i2 ON mb.InstallmentId = i2.InstallmentId
WHERE ptm.IsSingleInstallment = 'false'
AND mb.BrokerId = @BrokerId1
AND i2.InstallmentNo >= 5
AND i2.InstallmentNo <= 8
AND i2.CscId = i.CscId
AND ( i2.PaymentDate >= @StartDate
AND i2.PaymentDate <= @EndDate
)
)
WHEN ( m.Mode = 'HLY'
AND ( i.InstallmentNo >= 3
AND i.InstallmentNo <= 4
)
)
THEN ( SELECT SUM(mb.SelfAmount)
+ SUM(mb.UnitAmount)
FROM dbo.MemberBusiness
AS mb
INNER JOIN dbo.PlanMaster
AS pm ON mb.PlanId = pm.PlanId
INNER JOIN dbo.PlanTypeMaster
AS ptm ON pm.PlanTypeId = ptm.PlanTypeId
INNER JOIN dbo.Installment
AS i2 ON mb.InstallmentId = i2.InstallmentId
WHERE ptm.IsSingleInstallment = 'false'
AND mb.BrokerId = @BrokerId1
AND i2.InstallmentNo >= 3
AND i2.InstallmentNo <= 4
AND i2.CscId = i.CscId
AND i2.IsReceived = 1
AND ( i2.PaymentDate >= @StartDate
AND i2.PaymentDate <= @EndDate
)
)
WHEN ( m.Mode = 'YLY'
AND ( i.InstallmentNo = 2 )
)
THEN ( SELECT SUM(mb.SelfAmount)
+ SUM(mb.UnitAmount)
FROM dbo.MemberBusiness
AS mb
INNER JOIN dbo.PlanMaster
AS pm ON mb.PlanId = pm.PlanId
INNER JOIN dbo.PlanTypeMaster
AS ptm ON pm.PlanTypeId = ptm.PlanTypeId
INNER JOIN dbo.Installment
AS i2 ON mb.InstallmentId = i2.InstallmentId
WHERE ptm.IsSingleInstallment = 'false'
AND mb.BrokerId = @BrokerId1
AND i2.InstallmentNo = 2
AND i2.CscId = i.CscId
AND ( i2.PaymentDate >= @StartDate
AND i2.PaymentDate <= @EndDate
)
)
END ) ,
( CASE WHEN ( m.Mode = 'MLY'
AND ( i.InstallmentNo >= 25 )
)
THEN ( SELECT SUM(mb.SelfAmount)
+ SUM(mb.UnitAmount)
FROM dbo.MemberBusiness
AS mb
INNER JOIN dbo.PlanMaster
AS pm ON mb.PlanId = pm.PlanId
INNER JOIN dbo.PlanTypeMaster
AS ptm ON pm.PlanTypeId = ptm.PlanTypeId
INNER JOIN dbo.Installment
AS i2 ON mb.InstallmentId = i2.InstallmentId
WHERE ptm.IsSingleInstallment = 'false'
AND mb.BrokerId = @BrokerId1
AND i2.InstallmentNo >= 25
AND i2.CscId = i.CscId
AND ( i2.PaymentDate >= @StartDate
AND i2.PaymentDate <= @EndDate
)
)
WHEN ( m.Mode = 'QLY'
AND ( i.InstallmentNo >= 9 )
)
THEN ( SELECT SUM(mb.SelfAmount)
+ SUM(mb.UnitAmount)
FROM dbo.MemberBusiness
AS mb
INNER JOIN dbo.PlanMaster
AS pm ON mb.PlanId = pm.PlanId
INNER JOIN dbo.PlanTypeMaster
AS ptm ON pm.PlanTypeId = ptm.PlanTypeId
INNER JOIN dbo.Installment
AS i2 ON mb.InstallmentId = i2.InstallmentId
WHERE ptm.IsSingleInstallment = 'false'
AND mb.BrokerId = @BrokerId1
AND i2.InstallmentNo >= 9
AND i2.CscId = i.CscId
AND ( i2.PaymentDate >= @StartDate
AND i2.PaymentDate <= @EndDate
)
)
WHEN ( m.Mode = 'HLY'
AND ( i.InstallmentNo >= 5 )
)
THEN ( SELECT SUM(mb.SelfAmount)
+ SUM(mb.UnitAmount)
FROM dbo.MemberBusiness
AS mb
INNER JOIN dbo.PlanMaster
AS pm ON mb.PlanId = pm.PlanId
INNER JOIN dbo.PlanTypeMaster
AS ptm ON pm.PlanTypeId = ptm.PlanTypeId
INNER JOIN dbo.Installment
AS i2 ON mb.InstallmentId = i2.InstallmentId
WHERE ptm.IsSingleInstallment = 'false'
AND mb.BrokerId = @BrokerId1
AND i2.InstallmentNo >= 5
AND i2.CscId = i.CscId
AND ( i2.PaymentDate >= @StartDate
AND i2.PaymentDate <= @EndDate
)
)
WHEN ( m.Mode = 'YLY'
AND ( i.InstallmentNo >= 3 )
)
THEN ( SELECT SUM(mb.SelfAmount)
+ SUM(mb.UnitAmount)
FROM dbo.MemberBusiness
AS mb
INNER JOIN dbo.PlanMaster
AS pm ON mb.PlanId = pm.PlanId
INNER JOIN dbo.PlanTypeMaster
AS ptm ON pm.PlanTypeId = ptm.PlanTypeId
INNER JOIN dbo.Installment
AS i2 ON mb.InstallmentId = i2.InstallmentId
WHERE ptm.IsSingleInstallment = 'false'
AND mb.BrokerId = @BrokerId1
AND i2.InstallmentNo >= 3
AND i2.CscId = i.CscId
AND ( i2.PaymentDate >= @StartDate
AND i2.PaymentDate <= @EndDate
)
)
END )
FROM dbo.MemberBusiness AS mb
INNER JOIN dbo.Member AS m ON mb.MemberId = m.MemberId
INNER JOIN dbo.Installment AS i ON mb.InstallmentId = i.InstallmentId
INNER JOIN dbo.PlanMaster AS pm ON mb.PlanId = pm.PlanId
INNER JOIN dbo.PlanTypeMaster AS ptm ON pm.PlanTypeId = ptm.PlanTypeId
WHERE mb.BusinessId = @BusinessID2
GROUP BY i.InstallmentNo ,
m.Mode ,
i.CscId
)
FETCH NEXT FROM c3
INTO @BusinessID2
END
CLOSE c3
DEALLOCATE c3
DELETE FROM @Business
FETCH NEXT FROM c1
INTO @BrokerId1,@BrokerCode1
END
CLOSE c1
DEALLOCATE c1
SELECT t.BrokerId ,
t.FY ,
t.SY ,
t.TY
FROM @tmp AS t
出力は次のとおりです。
--------------------------------------------
BrokerId | FY | SY | TY |
--------------------------------------------
27 | 500 | NULL | NULL |
--------------------------------------------
27 | 500 | 200 | NULL |
--------------------------------------------
27 | 500 | NULL | 100 |
--------------------------------------------
しかし、次のような1レコードで出力したい:
--------------------------------------------
BrokerId | FY | SY | TY |
--------------------------------------------
27 | 500 | 200 | 100 |
--------------------------------------------
どうすればそれを行うことができますか..ありがとう.