将两个查询连接成一个

时间:2013-09-26 11:01:12

标签: tsql reporting-services

我有两个问题:

SELECT
count (C.CaseDetailsId) as [Z telefonem]
,U.FirstName + ' ' + U.LastName as [Windykator]
from CaseDetails as C
join DebtorDetails as D on C.CaseDetailsId = D.CaseDetailsId
join Users as U on C.UserId = U.UserId
where D.DebtorDetailsId in 
(SELECT DebtorDetailsId from DebtorPhone 
where (IsValid = 'True') or (IsDefault = 'True')) 
and C.CaseStatusId <> 2 and C.CaseStatusId <> 6 group by U.FirstName,U.LastName

ë

SELECT
count (CaseDetailsId) as [Beztel]
,U.FirstName + ' ' + U.LastName as [Windykator]
from CaseDetails as C
join Users as U on C.UserId = U.UserId
where
C.CaseStatusId <> 2 and C.CaseStatusId <> 6 group by U.FirstName,U.LastName

我需要将这两个的结果分组到一个表中,所以我想最好连接它们但我不知道该怎么做。

查询必须在两种情况下计算行数 1)总体而言 2)在另一个表中存在指定的行

一般来说 - &gt;我需要计算每个雇员(用户)有无电话号码(DebtorPhones)的案件数量(CaseDetailsId)

2 个答案:

答案 0 :(得分:0)

下面的查询将使用union从两个查询中为您提供一个记录集。您可以在select上面添加insert语句以插入到您需要的表中。

SELECT Count (C.casedetailsid)        AS [Z telefonem], 
               U.firstname + ' ' + U.lastname AS [Windykator] 
        FROM   casedetails AS C 
               JOIN debtordetails AS D 
                 ON C.casedetailsid = D.casedetailsid 
               JOIN users AS U 
                 ON C.userid = U.userid 
        WHERE  D.debtordetailsid IN (SELECT debtordetailsid 
                                     FROM   debtorphone 
                                     WHERE  ( isvalid = 'True' ) 
                                             OR ( isdefault = 'True' )) 
               AND C.casestatusid <> 2 
               AND C.casestatusid <> 6 
        GROUP  BY U.firstname, 
                  U.lastname  
    Union
    SELECT Count (casedetailsid)          AS [Z telefonem], 
           U.firstname + ' ' + U.lastname AS [Windykator] 
    FROM   casedetails AS C 
           JOIN users AS U 
             ON C.userid = U.userid 
    WHERE  C.casestatusid <> 2 
           AND C.casestatusid <> 6 
    GROUP  BY U.firstname, 
              U.lastname  

答案 1 :(得分:0)

@Pratik Kaje不是真的。它仅显示2列 - [Z tel]和[Windykator]

我测试了一下并以此类查询结束:

SELECT
[Windykator]
,[Beztel]
,[Z telefonem]
from
(SELECT
count (C.CaseDetailsId) as [Z telefonem]
,NULL as [Beztel]
,NULL as [Windykator]
from CaseDetails as C
join DebtorDetails as D on C.CaseDetailsId = D.CaseDetailsId
join Users as U on C.UserId = U.UserId
where D.DebtorDetailsId in 
(SELECT DebtorDetailsId from DebtorPhone 
where (IsValid = 'True') or (IsDefault = 'True')) 
and C.CaseStatusId <> 2 and C.CaseStatusId <> 6
group by U.FirstName, U.LastName
UNION ALL
SELECT
NULL
,count (CaseDetailsId) as [Beztel]
,NULL
from CaseDetails as C
join Users as U on C.UserId = U.UserId
where
C.CaseStatusId <> 2 and C.CaseStatusId <> 6
group by U.FirstName, U.LastName
UNION ALL
SELECT
NULL
,NULL
,U.FirstName + ' ' + U.LastName
from CaseDetails as C
join Users as U on C.UserId = U.UserId
where
C.CaseStatusId <> 2 and C.CaseStatusId <> 6
group by U.FirstName, U.LastName
) as x

几乎完美但结果没有分组。所以用

显示结果
NULLS NULLS [Bez tel]
NULLS [Z tel] NULLS
[Windykator] NULLS NULLS