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记录(CA.IsDeleted ='True')时,值都会乘以x2,有时甚至更多,没有任何理由。只运行查询的某个部分:
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
然而,这会使所有计数和所有总和都相等。但是有趣的是:
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,默认为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
答案 0 :(得分:1)
尝试更改
CA.IsDeleted <> 'True'
到
(CA.IsDeleted <> 'True' OR CA.IsDeleted IS NULL)
答案 1 :(得分:0)
试试这个
(isnull(CA.IsDeleted,'')='' or CA.IsDeleted <> 'True')