将两个表合并为一个具有不同where子句的表

时间:2014-04-25 07:01:11

标签: sql sql-server-2008

我有两个Sql查询如下:

第一次查询

SELECT TC.TCName, 
sum(BS.BLDOS) as BLDOS,
sum(BS.CollectedAmount) as CollectedAmount, 
(0.35*sum(BS.BLDOS)) as TargetAmount
FROM [Customer] C 
INNER JOIN [dbo].[BillingStatus] BS ON BS.CustomerID = C.CustomerID 
INNER JOIN [dbo].[TCDetails] TC ON TC.CustomerID = BS.CustomerID
WHERE TC.TCName in (Select distinct(TCName) 
                    FROM [dbo].[TCDetails] )
GROUP BY TCName 
order by [TCName] ASC

我得到了一个结果:

   ||TCName ||  BLDOS  || CollectedAmount || TargetAmount||
    | Aarti  |  81234.2 |    1678.76       |  789065     |
    | Dev    |  181234.2|     678.76       |  2389065    | 

第二个查询是:

SELECT COUNT(*) as count 
FROM [dbo].[TCDetails] 
where TCName in (Select distinct(TCName) FROM [dbo].[TCDetails] ) 
AND CallDate is NOT NULL 
GROUP BY TCName

我正在获取:

  ||count||
     231
     123

我想像这样打印

   ||TCName ||  BLDOS  || CollectedAmount || TargetAmount|| count ||
    | Aarti  |  81234.2 |    1678.76       |  789065     |   231   |
    | Dev    |  181234.2|     678.76       |  2389065    |   123   |

我很困惑,因为两个查询都有不同的wheres子句

5 个答案:

答案 0 :(得分:1)

试试这个

SELECT  * FROM 
(
SELECT TC.TCName, sum(BS.BLDOS) as BLDOS, sum(BS.CollectedAmount) as CollectedAmount, (0.35*sum(BS.BLDOS)) as TargetAmount
FROM [Customer] C INNER JOIN
     [dbo].[BillingStatus] BS ON BS.CustomerID = C.CustomerID INNER JOIN
     [dbo].[TCDetails] TC ON TC.CustomerID = BS.CustomerID
WHERE TC.TCName in (Select distinct(TCName) FROM [dbo].[TCDetails] )
GROUP BY TCName order by [TCName] ASC
) S JOIN
(
SELECT TCName,COUNT(*) as count FROM [dbo].[TCDetails] 
where TCName in (Select distinct(TCName) FROM [dbo].[TCDetails] ) 
AND CallDate is NOT NULL GROUP BY TCName
) T ON S.TCName=T.TCName;

OP:

+--------------------------------------------------------------+
|TCName  |  BLDOS   | CollectedAmount  | TargetAmount| count   |
+--------------------------------------------------------------+
| Aarti  |  81234.2 |    1678.76       |  789065     |   231   |
| Dev    |  181234.2|     678.76       |  2389065    |   123   |
+--------------------------------------------------------------+

答案 1 :(得分:1)

在两个查询中,您都使用TCDetails并按TCName分组。您不必仅执行此操作两次以计算非空呼叫日期。 COUNT(CallDate)为您做到了这一点。我还删除了没有为查询添加anthing的IN子句。所以查询只是:

SELECT 
  TC.TCName, 
  sum(BS.BLDOS) as BLDOS, 
  sum(BS.CollectedAmount) as CollectedAmount, 
  0.35 * sum(BS.BLDOS) as TargetAmount,
  count(TC.CallDate) as cnt
FROM [Customer] C 
INNER JOIN [dbo].[BillingStatus] BS ON BS.CustomerID = C.CustomerID 
INNER JOIN [dbo].[TCDetails] TC ON TC.CustomerID = BS.CustomerID
GROUP BY TC.TCName 
ORDER BY TC.TCName ASC;

答案 2 :(得分:0)

SELECT TC.TCName, sum(BS.BLDOS) as BLDOS, sum(BS.CollectedAmount) as CollectedAmount, (0.35*sum(BS.BLDOS)) as TargetAmount ,
            (SELECT COUNT(*) as count FROM [dbo].[TCDetails] TC2
                 where TCName in (Select distinct(TCName) FROM [dbo].[TCDetails] ) 
                  AND CallDate is NOT NULL AND TC2.TCName = TC.TCName GROUP BY TCName) AS Count
 FROM [Customer] C 
 INNER JOIN [dbo].[BillingStatus] BS
          ON BS.CustomerID = C.CustomerID 
   INNER JOIN [dbo].[TCDetails] TC 
          ON TC.CustomerID = BS.CustomerID
     WHERE TC.TCName in (Select distinct(TCName) FROM [dbo].[TCDetails] )
     GROUP BY TCName order by [TCName] ASC

答案 3 :(得分:0)

尝试以下:

SELECT  S.*,T.count FROM 
(
SELECT TC.TCName, sum(BS.BLDOS) as BLDOS, sum(BS.CollectedAmount) as CollectedAmount, (0.35*sum(BS.BLDOS)) as TargetAmount
FROM [Customer] C JOIN
     [dbo].[BillingStatus] BS ON BS.CustomerID = C.CustomerID JOIN
     [dbo].[TCDetails] TC ON TC.CustomerID = BS.CustomerID
WHERE TC.TCName in (Select distinct(TCName) FROM [dbo].[TCDetails] )
GROUP BY TCName order by [TCName] ASC
) S join 
(
SELECT TCName,COUNT(*) as count FROM [dbo].[TCDetails] 
where TCName in (Select distinct(TCName) FROM [dbo].[TCDetails] ) 
AND CallDate is NOT NULL GROUP BY TCName
) T on S.TCName=T.TCName;

答案 4 :(得分:0)

尝试:

SELECT TC.TCName, 
       sum(BS.BLDOS) as BLDOS,
       sum(BS.CollectedAmount) as CollectedAmount, 
       (0.35*sum(BS.BLDOS)) as TargetAmount,
       Count(CallDate) as count
FROM [Customer] C 
INNER JOIN [dbo].[BillingStatus] BS ON BS.CustomerID = C.CustomerID 
INNER JOIN [dbo].[TCDetails] TC ON TC.CustomerID = BS.CustomerID
WHERE TC.TCName in (Select distinct(TCName) 
                    FROM [dbo].[TCDetails] )
                    GROUP BY TCName 
order by [TCName] ASC

Check Demo here.