SQL Server 2005商业智能使用2个数据集一个报告

时间:2012-02-02 14:51:24

标签: sql-server-2005

只需要一些SQL Server 2005和Visual Studio 2005的帮助/建议。

基本上我有2个数据集,其中一个导致好的呼叫总数,另一个导致不良呼叫的数量。

我想做一份报告,显示好坏的比率。

对于呼叫失败,actstatus有“105”

SELECT Country, count(status) AS FailedCalls
FROM termactivity(nolock) 
INNER JOIN termconfig(NOLOCK) ON cfgterminalID = actterminalID
WHERE actstatus IN (105)
GROUP BY country
ORDER BY country

对于好的电话,actstatus的单元格中有“5”

SELECT Country, count(status) AS FailedCalls
FROM termactivity(nolock) 
INNER JOIN termconfig(NOLOCK) ON cfgterminalID = actterminalID
WHERE actstatus IN (5)
GROUP BY country
ORDER BY country

现在,这两个都会产生包含国家名称和数字的表格,如下所示

 Country    fails
    AT  240
    BE  3
    CH  1
    CZ  23
    DE  5733
    ES  336
    FR  2
    IE  1066
    IT  7085
    NL  260
    NZ  89
    PT  790

    Country CallSuccess
    AT  5662
    BE  480
    CH  370
    CZ  1124
    DE  19412
    ES  7740
    FR  1976
    IE  26616
    IT  32764
    NL  4046
    NZ  114
    PT  6567

如何将这些添加到一个布局中,并最终有一个如下所示的表:

Country CallSuccess  fails
AT  5662                 240
BE  480                  10
CH  370                  22
CZ  1124                 112
DE  19412                888
ES  7740                 etc..
FR  1976
IE  26616
IT  32764
NL  4046
NZ  114
PT  6567

2 个答案:

答案 0 :(得分:1)

COUNT忽略NULL,CASE为隐含的ELSE条件gies NULL

SELECT
   Country, 
   count(CASE WHEN actstatus = 105 THEN status END) AS FailedCalls,
   count(CASE WHEN actstatus = 5 THEN status END) AS GoodCalls
FROM
   termactivity(nolock) 
    INNER JOIN
   termconfig(NOLOCK) ON cfgterminalID = actterminalID
where actstatus IN (105, 5)
GROUP BY country
ORDER BY country

答案 1 :(得分:1)

我会更改你的sql以返回这样的数据集。

Select Country,
        Sum(Case When actstatus = 5 Then 1 Else 0 End) As FailedCalls,
        Sum(Case When actstatus = 105 Then 1 Else 0 End) As SucceededCalls
From  termactivity(nolock)  
    INNER JOIN termconfig(NOLOCK) ON cfgterminalID = actterminalID 
Where  actstatus IN (5, 105)
Group By Country
order By Country