0

编辑:大家好,

我想到了

select LI.ProjectId, count(*) as numLineItems
into #temp
from dbo.SQLLineItems as LI
group by LI.ProjectId
having COUNT(*) < =200

select numLineItems, count(*), SUM(PT.GrandTotalSell) from
#temp
inner join dbo.SQLProjectTotals as PT
on PT.ProjectId = #temp.ProjectId
group by numLineItems;

谢谢您的帮助。


我有两个包含项目信息的表。表 1 包含 LineItems,每个 LineItems 都有一个对该表唯一的 GUID 和 ProjectID 的 GUID。每个 ProjectID 都有重复项,因为一个 ProjectID 可以有多个 LineItem。

表 2 通过项目 ID 链接到表 1,并且 GUID 在该表中是唯一的。表 2 还包含每个项目的价格。

我想按 LineItem 的数量细分项目,看看有多少项目是 1、2、3、4... 项目项目。

我写了一个有效的查询:

use FieldedProjects;

select LI.ProjectId, count(*) as numLineItems
into #temp
from dbo.SQLLineItems as LI
group by LI.ProjectId
having COUNT(*) < =15

select numLineItems, count(*) from
#temp
group by numLineItems;

但现在我想修改它以显示总价格。所以 1 个项目的项目总价格是 100,2 个项目的项目是 300,...

我在想类似的东西

use FieldedProjects;

select LI.ProjectId, count(*) as numLineItems, SUM(PT.GrandTotalSell) as projectTotal
into #temp
from dbo.SQLLineItems as LI
inner join dbo.SQLProjectTotals as PT
on LI.ProjectID = PT.ProjectID
group by LI.ProjectId
having COUNT(*) < =15

select numLineItems, count(*), projectTotal from
#temp
group by numLineItems;

但它给了我一个错误,因为 projectTotal 不包含在聚合函数中。它应该是

select numLineItems, count(*), SUM(projectTotal) from
4

1 回答 1

0
select *
from dbo.SQLProjectTotals as PT
cross apply
(
    select count(*)
    from dbo.SQLLineItems LI
    where LI.ProjectID = PT.ProjectID
) items(Count)
where items.Count <= 15
于 2013-09-25T14:49:31.210 回答