0
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 或有时甚至更多。只运行查询 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,默认为 NULL。设置代码为删除后为真;恢复代码后为假。


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
4

2 回答 2

1

尝试改变

CA.IsDeleted <> 'True'

(CA.IsDeleted <> 'True' OR CA.IsDeleted IS NULL)
于 2013-10-01T06:46:10.607 回答
0

试试这个

(isnull(CA.IsDeleted,'')='' or CA.IsDeleted <> 'True')
于 2013-10-01T07:00:09.553 回答