SELECT
month(CA.PromiseDate) as [Miesiąc]
,year(CA.PromiseDate) as [Rok]
,x.[Deklaracje] as [Deklaracje]
,x.[Bpotw] as [Bpotw]
,x.[Potw] as [Potw]
,C.ClientId
,C.SetId
,K.NameShort
,U.FirstName
,U.LastName
from (
SELECT
CaseDetailId
,sum(case when CaseActionHistory.CaseActionDefinitionId = 34 then PromiseValue else 0 end) AS [Deklaracje]
,sum(case when CaseActionHistory.CaseActionDefinitionId = 35 then PromiseValue else 0 end) AS [Bpotw]
,sum(case when CaseActionHistory.CaseActionDefinitionId = 36 then PromiseValue else 0 end) AS [Potw]
from
CaseActionHistory
group by
CaseDetailId
) as x
join CaseActionHistory as CA on CA.CaseDetailId = x.CaseDetailId
join CaseDetails as C on CA.CaseDetailId = C.CaseDetailsId
join Client as K on C.ClientId = K.ClientId
join ClientProducts as CP on C.ProductId = CP.ClientProductId
join Users as U on CA.UserId = U.UserId
join CaseDetailsView as CDV on C.CaseId = CDV.CaseId
WHERE
(CA.IsDeleted IS NULL or CA.IsDeleted <> 'True')
and
(C.ClientId = @ClientId or @ClientId IS NULL)
and
(CA.CaseActionDefinitionId in (34,35,36,99))
and
(C.CaseStatusId not in (6,2))
and
(CA.PromiseDate >= CDV.EndServiceDate)
and
(U.UserId = @User or @User IS NULL)
and
(CA.PromiseDate >= @Min or @Min IS NULL)
and
(CA.PromiseDate <= @Max or @Max IS NULL)
and
CA.PromiseValue > 0
group by C.ClientId, C.SetId, K.NameShort, year(PromiseDate), month(PromiseDate) ,U.FirstName, U.LastName, x.Deklaracje, x.Bpotw, x.Potw
グループ化部分を除いて正常に動作します。年と月だけでなく、PromiseDate全体をグループ化します。次のような結果を示しています。
http://oi44.tinypic.com/141719i.jpg
ただし、それらすべてを 1 つのレコードとして表示する必要があります。