SQL SUM和Count在单个查询中

时间:2017-01-10 05:38:44

标签: sql sql-server

我创建了两个查询,它们都返回我想要的结果,但是我需要一个查询中的单个结果集,而我无法弄清楚如何合并这两个。 我计划使用查询的输出作为insert语句的输入,以在另一个表中创建新记录。

无论我尝试什么,最终都会将TotalExpense乘以手数。

我正在使用SQL Server 2008 R2。

非常感谢任何建议/解决方案。

SELECT
    sPortfolioName
    ,Count(lot.lLotID) as TotalLots

FROM [Owners]

inner join Portfolio on Portfolio.lUserID = Owners.lUserID
inner Join tblExpense on tblExpense.lOwnersID = Owners.lOwnersID
inner Join Lot on Lot.lOwnersID = Owners.lOwnersID

where bManaged = 'Y' 
AND tblExpense.lExpenseCodeID = '5' 
Group By sPortfolioName 
Order By sPortfolioName


SELECT 
      Portfolio.sPortfolioName
      ,sum (tblExpense.mRate) as TotalExpense
  FROM [Owners]
  inner join Portfolio on Portfolio.lUserID = Owners.lUserID
  inner Join tblExpense on tblExpense.lOwnersID = Owners.lOwnersID
  where bManaged = 'Y' 
  AND tblExpense.lExpenseCodeID = '5' 
  Group By sPortfolioName

当前结果

sPortfolioName  TotalLots
Black           836
Blue            1107
Brown           22

sPortfolioName  TotalExpense
Black           393.90
Blue            17.83
Brown           511.00

期望的结果

sPortfolioName  TotalLots   TotalExpense
Black           836         393.90
Blue            1107        17.83
Brown           22          511.00

3 个答案:

答案 0 :(得分:2)

两个查询与GROUP BY子句中使用的相同列几乎相同,因此您可以在具有聚合函数COUNT()SUM()

的单个查询中将它们组合在一起
SELECT
    sPortfolioName
    ,Count(lot.lLotID) as TotalLots
    ,sum (tblExpense.mRate) as TotalExpense
FROM [Owners]

inner join Portfolio on Portfolio.lUserID = Owners.lUserID
inner Join tblExpense on tblExpense.lOwnersID = Owners.lOwnersID
inner Join Lot on Lot.lOwnersID = Owners.lOwnersID

where bManaged = 'Y' 
AND tblExpense.lExpenseCodeID = '5' 
Group By sPortfolioName 
Order By sPortfolioName

修改

尝试此操作并检查其是否返回正确的所需结果

SELECT t1.sPortfolioName, t1.TotalLots, t2.TotalExpense
FROM
(
SELECT
    sPortfolioName
    ,Count(lot.lLotID) as TotalLots

FROM [Owners]

inner join Portfolio on Portfolio.lUserID = Owners.lUserID
inner Join tblExpense on tblExpense.lOwnersID = Owners.lOwnersID
inner Join Lot on Lot.lOwnersID = Owners.lOwnersID

where bManaged = 'Y' 
AND tblExpense.lExpenseCodeID = '5' 
Group By sPortfolioName
) As t1

INNER JOIN

(
  SELECT 
      Portfolio.sPortfolioName
      ,sum (tblExpense.mRate) as TotalExpense
  FROM [Owners]
  inner join Portfolio on Portfolio.lUserID = Owners.lUserID
  inner Join tblExpense on tblExpense.lOwnersID = Owners.lOwnersID
  where bManaged = 'Y' 
  AND tblExpense.lExpenseCodeID = '5' 
  Group By sPortfolioName
) As t2

ON t1.sPortfolioName = t2.sPortfolioName
ORDER BY t1.sPortfolioName

答案 1 :(得分:1)

即使已经有一个已接受的答案,我只想指出可以简化查询。像这样:

SELECT
    sPortfolioName
    ,Lot.TotalLots
    ,sum (tblExpense.mRate) as TotalExpense
FROM [Owners]
inner join Portfolio on Portfolio.lUserID = Owners.lUserID
inner Join tblExpense on tblExpense.lOwnersID = Owners.lOwnersID
inner Join (select lOwnersID, count(*) as TotalLots from Lot group by lOwnersID) Lot on Lot.lOwnersID = Owners.lOwnersID
where bManaged = 'Y' 
AND tblExpense.lExpenseCodeID = '5' 
Group By sPortfolioName 
Order By sPortfolioName

答案 2 :(得分:0)

检查这个。

        SELECT
            Portfolio.sPortfolioName
            ,Count(lot.lLotID) as TotalLots
            ,sum (tblExpense.mRate) as TotalExpense

        FROM [Owners]

        inner join Portfolio on Portfolio.lUserID = Owners.lUserID
        inner Join tblExpense on tblExpense.lOwnersID = Owners.lOwnersID
        inner Join Lot on Lot.lOwnersID = Owners.lOwnersID

        where bManaged = 'Y' 
        AND tblExpense.lExpenseCodeID = '5' 
        Group By sPortfolioName ,sPortfolioName
        Order By sPortfolioName