0

查询显示如下结果:

我需要将结果显示在一行而不是几行中。

询问:

SELECT C.ClientCaseNumber,
            C.SetId,
           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],
           [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
    GROUP  BY C.ClientCaseNumber, [Adres], [Numer], [Mail], [Info1], [Powód], [Info2], [Info3], C.SetId

我想我需要以某种方式摆脱 GROUP BY 中的一些东西,但是我真的不知道如何更改查询本身以使事情正常工作。

4

1 回答 1

0

我认为您应该声明表并将其插入:SetId 和所有 SUM(...) 并按 SetId 分组。然后用其他数据更新表。

样本:

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
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.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
        X.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
于 2013-09-30T08:47:01.497 回答