1
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],
       [Adres],
       [Numer],
       [Mail],
       [Powód]
FROM   (SELECT Notes AS [Adres]
        FROM   CaseActionHistory
        WHERE  CaseActionDefinitionId = 68
        UNION ALL
        SELECT Info AS [Numer]
        FROM   CaseActionHistory
        WHERE  CaseActionDefinitionId IN (54, 55, 56, 58,
                                          59, 60, 61, 62, 63)
        UNION ALL
        SELECT Notes AS [Mail]
        FROM   CaseActionHistory
        WHERE  CaseActionDefinitionId = 66
        UNION ALL
        SELECT Description AS [Powód]
        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.CaseDetaislId
       INNER JOIN CaseActionHistory AS CA
               ON C.CaseDetailsId = CA.CaseDetailId
WHERE  C.ClientId = '11'
GROUP  BY C.ClientCaseNumber

我有这样的询问。作为返回显示无效列“CaseDetailId、CaseDetailsId、Mail、Numer、Powód”的错误。


http://oi39.tinypic.com/2vwy44n.jpg

结果或多或少应该是这样的。

ClientCaseNumber 取自表 CaseDetails 所有总和都是添加到 CaseActionHistory 表的代码总和。Notes/Info 位于 CaseActionHistory 表中 描述位于 CaseActionDefinition 表中。

表之间有这样的联系:

CaseDetails.CaseDetailId = CaseActionHistory.CaseDetailsId

CaseActionHistory.CaseActionDefinitionId = CaseActionDefinition.DefinitionId

4

2 回答 2

2

UNION子句不能那样工作。

这个查询:

select Notes as [Adres] from CaseActionHistory where ...
UNION ALL
select Info as [Numer] from CaseActionHistory where ...
UNION ALL
select Notes as [Mail] from CaseActionHistory where ...
UNION ALL
select Description as [Powód] from CaseActionDefinition join CaseActionHistory ...

不会填充 4 列的表。相反,它将是一个包含一列的表,所有值一个接一个。列的名称将取自第一个SELECT,即

  • 如果第一个查询返回值 1 和 2,
  • 第二个查询返回值 3 和 4
  • 第三个查询返回值 5 和 6
  • 第四个查询返回值 7 和 8

你不会得到:

Adres  | Numer | Mail | Powód
------------------------------
    1  |     3 |    5 |     7
    2  |     4 |    6 |     8

但你会得到:

Adres  
-------
    1  
    2  
    3
    4
    5
    6
    7
    8
于 2013-09-02T09:55:55.823 回答
0

您是否没有错过这些字段中的别名“x”:

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]
,x.[Adres]
,x.[Numer]
,x.[Mail]
,x.[Powód]

FROM
(select Notes as [Adres] from CaseActionHistory where CaseActionDefinitionId = 68
UNION ALL
select Info as [Numer] from CaseActionHistory where CaseActionDefinitionId in (54,55,56,58,59,60,61,62,63)
UNION ALL
select Notes as [Mail] from CaseActionHistory where CaseActionDefinitionId = 66
UNION ALL
select Description as [Powód] 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.CaseDetaislId
inner join CaseActionHistory as CA on C.CaseDetailsId = CA.CaseDetailId

where C.ClientId = '11'

GROUP by C.ClientCaseNumber
于 2013-09-02T09:57:59.707 回答