SELECT
C.ClientCaseNumber,
Sum(CASE
WHEN CA.CaseActionDefinitionId IN (28, 29, 30) THEN 1
ELSE 0
END) AS [Wezwania],
Sum(CASE
WHEN CA.CaseActionDefinitionId IN (14, 21) THEN 1
ELSE 0
END) AS [Kontakt],
Sum(CASE
WHEN CA.CaseActionDefinitionId = 32 THEN 1
ELSE 0
END) AS [SMS],
Sum(CASE
WHEN CA.CaseActionDefinitionId = 44 THEN 1
ELSE 0
END) AS [Zgon],
Sum(CASE
WHEN CA.CaseActionDefinitionId = 49 THEN 1
ELSE 0
END) AS [Areszt],
Sum(CASE
WHEN CA.CaseActionDefinitionId = 37 THEN 1
ELSE 0
END) AS [Odmowa],
Sum(CASE
WHEN CA.CaseActionDefinitionId = 39 THEN 1
ELSE 0
END) AS [Podważa],
Sum(CASE
WHEN CA.CaseActionDefinitionId = 99 THEN 1
ELSE 0
END) AS [Ugoda]
FROM (SELECT cast(Notes as char) AS [Adres], CaseActionDefinitionId AS [Info1], NULL AS [Numer], NULL as [Info2], NULL AS [Mail], NULL as [Info3], NULL AS [Powód], CaseDetailId
FROM CaseActionHistory
WHERE CaseActionDefinitionId = 68
UNION ALL
SELECT NULL, NULL, cast(Info as char), CaseActionDefinitionId, NULL, NULL, NULL, CaseDetailId
FROM CaseActionHistory
WHERE CaseActionDefinitionId IN (54, 55, 56, 58,
59, 60, 61, 62, 63)
UNION ALL
SELECT NULL, NULL, NULL, NULL, cast(Notes as char), CaseActionDefinitionId, NULL, CaseDetailId
FROM CaseActionHistory
WHERE CaseActionDefinitionId = 66
UNION ALL
SELECT NULL, NULL, NULL, NULL, NULL, NULL, cast(Description as char(100)), CaseDetailId
FROM CaseActionDefinition JOIN CaseActionHistory AS C ON DefinitionId = C.CaseActionDefinitionId
WHERE DefinitionId BETWEEN 70 AND 78) AS x
INNER JOIN CaseDetails AS C
ON x.CaseDetailId = C.CaseDetailsId
INNER JOIN CaseActionHistory AS CA
ON C.CaseDetailsId = CA.CaseDetailId
WHERE C.ClientId = 11
GROUP BY C.ClientCaseNumber
sum(case) に問題があります。CaseActionHistory レコードの 1 つが削除されるたびに (CA.IsDeleted ='True')、値が 2 倍または場合によってはそれ以上の理由もなく乗算されます。クエリの特定の部分だけを実行する ex:
SELECT
C.ClientCaseNumber
,Sum(CASE
WHEN CA.CaseActionDefinitionId IN (28, 29, 30) THEN 1
ELSE 0
END) AS [Wezwania]
from
CaseActionHistory as CA
join CaseDetails as C on CA.CaseDetailId = C.CaseDetailsId
where C.ClientId = '11'
group by CD.ClientCaseNumber
レコードを問題なく完全に合計します。
さらに、引数を追加する必要があります
....AS x
INNER JOIN CaseDetails AS C
ON x.CaseDetailId = C.CaseDetailsId
INNER JOIN CaseActionHistory AS CA
ON C.CaseDetailsId = CA.CaseDetailId
WHERE C.ClientId = 11 and CA.IsDeleted <> 'True'
GROUP BY C.ClientCaseNumber
ただし、これはすべてのカウントを台無しにし、すべての合計は0に等しくなります。しかし、興味深いのは次のとおりです。
AS x
INNER JOIN CaseDetails AS C
ON x.CaseDetailId = C.CaseDetailsId
INNER JOIN CaseActionHistory AS CA
ON C.CaseDetailsId = CA.CaseDetailId
WHERE C.ClientId = 11 and CA.IsDeleted = 'True'
GROUP BY C.ClientCaseNumber
完全に機能し、削除されたコードを合計します。
IsDeleted には、NULL/True/False の 3 つの値を指定できます。デフォルトは NULL です。コードを削除済みとして設定した後は True。コードを復元した後は False。
declare @table table (
ClientCaseNumber int
,SetId int
,Wezwania int
,Kontakt int
,SMS int
,Zgon int
,Areszt int
,Odmowa int
,Podważa int
,Ugoda int
,Adres nvarchar(255)
,Info1 nvarchar(255)
,Numer nvarchar(255)
,Info2 nvarchar(255)
,Mail nvarchar(255)
,Info3 nvarchar(255)
,Powód nvarchar(255))
insert into @table (
ClientCaseNumber
,Wezwania
,Kontakt
,SMS
,Zgon
,Areszt
,Odmowa
,Podważa
,Ugoda)
SELECT
C.ClientCaseNumber,
Sum(CASE
WHEN CA.CaseActionDefinitionId IN (28, 29, 30) THEN 1
ELSE 0
END) AS [Wezwania],
Sum(CASE
WHEN CA.CaseActionDefinitionId IN (14, 21) THEN 1
ELSE 0
END) AS [Kontakt],
Sum(CASE
WHEN CA.CaseActionDefinitionId = 32 THEN 1
ELSE 0
END) AS [SMS],
Sum(CASE
WHEN CA.CaseActionDefinitionId = 44 THEN 1
ELSE 0
END) AS [Zgon],
Sum(CASE
WHEN CA.CaseActionDefinitionId = 49 THEN 1
ELSE 0
END) AS [Areszt],
Sum(CASE
WHEN CA.CaseActionDefinitionId = 37 THEN 1
ELSE 0
END) AS [Odmowa],
Sum(CASE
WHEN CA.CaseActionDefinitionId = 39 THEN 1
ELSE 0
END) AS [Podważa],
Sum(CASE
WHEN CA.CaseActionDefinitionId = 99 THEN 1
ELSE 0
END) AS [Ugoda]
FROM (SELECT cast(Notes as char) AS [Adres], CaseActionDefinitionId AS [Info1], NULL AS [Numer], NULL as [Info2], NULL AS [Mail], NULL as [Info3], NULL AS [Powód], CaseDetailId
FROM CaseActionHistory
WHERE CaseActionDefinitionId = 68
UNION ALL
SELECT NULL, NULL, cast(Info as char), CaseActionDefinitionId, NULL, NULL, NULL, CaseDetailId
FROM CaseActionHistory
WHERE CaseActionDefinitionId IN (54, 55, 56, 58,
59, 60, 61, 62, 63)
UNION ALL
SELECT NULL, NULL, NULL, NULL, cast(Notes as char), CaseActionDefinitionId, NULL, CaseDetailId
FROM CaseActionHistory
WHERE CaseActionDefinitionId = 66
UNION ALL
SELECT NULL, NULL, NULL, NULL, NULL, NULL, cast(Description as char(100)), CaseDetailId
FROM CaseActionDefinition JOIN CaseActionHistory AS C ON DefinitionId = C.CaseActionDefinitionId
WHERE DefinitionId BETWEEN 70 AND 78) AS x
INNER JOIN CaseDetails AS C
ON x.CaseDetailId = C.CaseDetailsId
INNER JOIN CaseActionHistory AS CA
ON C.CaseDetailsId = CA.CaseDetailId
WHERE C.ClientId = 11 and (CA.IsDeleted <> 'True' OR CA.IsDeleted IS NULL)
GROUP BY C.ClientCaseNumber
update @table
set SetId = X.SetId
, Adres = X.Adres
,Info1 = X.Info1
,Numer = X.Numer
,Info2 = X.Info2
,Mail = X.Mail
,Info3 = X.Info3
,Powód = X.Powód
from
@table as T
join (
SELECT
C.ClientCaseNumber,
C.ClientId,
C.SetId,
[Adres],
[Info1],
[Numer],
[Info2],
[Mail],
[Info3],
[Powód]
FROM (SELECT cast(Notes as char) AS [Adres], CaseActionDefinitionId AS [Info1], NULL AS [Numer], NULL as [Info2], NULL AS [Mail], NULL as [Info3], NULL AS [Powód], CaseDetailId
FROM CaseActionHistory
WHERE CaseActionDefinitionId = 68
UNION ALL
SELECT NULL, NULL, cast(Info as char), CaseActionDefinitionId, NULL, NULL, NULL, CaseDetailId
FROM CaseActionHistory
WHERE CaseActionDefinitionId IN (54, 55, 56, 58,
59, 60, 61, 62, 63)
UNION ALL
SELECT NULL, NULL, NULL, NULL, cast(Notes as char), CaseActionDefinitionId, NULL, CaseDetailId
FROM CaseActionHistory
WHERE CaseActionDefinitionId = 66
UNION ALL
SELECT NULL, NULL, NULL, NULL, NULL, NULL, cast(Description as char(100)), CaseDetailId
FROM CaseActionDefinition JOIN CaseActionHistory AS C ON DefinitionId = C.CaseActionDefinitionId
WHERE DefinitionId BETWEEN 70 AND 78) AS x
INNER JOIN CaseDetails AS C
ON x.CaseDetailId = C.CaseDetailsId
INNER JOIN CaseActionHistory AS CA
ON C.CaseDetailsId = CA.CaseDetailId
WHERE
C.ClientId = 11
) as X on X.ClientCaseNumber = T.ClientCaseNumber
SELECT DISTINCT ClientCaseNumber,
SetId,
[Wezwania],
[Kontakt],
[SMS],
[Zgon],
[Areszt],
[Odmowa],
[Podważa],
[Ugoda],
[Adres],
[Info1],
[Numer],
[Info2],
[Mail],
[Info3],
[Powód]
FROM @table