0

我有这样两个查询:

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

e

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) 在另一个表中存在指定行的位置

所以总的来说->我需要为每个有和没有电话号码(DebtorPhones)的员工(用户)计算案件数量(CaseDetailsId)

4

2 回答 2

0

下面的查询将使用联合为您提供来自两个查询的一个记录集。您可以在 select 上方添加插入语句以插入所需的表中。

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  
于 2013-09-26T11:16:53.063 回答
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
于 2013-09-26T11:46:07.173 回答