0

在不使用参数的情况下,我无法找到一种方法来执行下面的查询。数据库有超过 2000 个可能的参数值,所以我想要输出一个表,其中列出一列中的所有参数,另一列中列出查询的输出。以下是现在有效的简化版本:

SELECT [enter name] AS ExclName, 
       Sum(revenue) AS MaxRev 
FROM   tbltasks 
WHERE  tbltasks.task IN (SELECT DISTINCT tblabilities.task 
                         FROM   tblabilities 
                         WHERE  tblabilities.name <> [enter name]); 

    tbltasks                tblpeople       tblabilities            
    task    revenue         name            name    task                
    A       10              Bob             Bob     A    
    B       9               Tom             Tom     A    
    C       8               Jack            Jack    A    
    D       7               Mary            Tom     B    
    E       6               Diane           Jack    B    
    F       5               Alice           Mary    B    
    G       4               Sam             Jack    C    
    H       3                               Mary    C    
    I       2                               Diane   C    
                                            Mary    D    
                                            Diane   D    
                                            Alice   D    
                                            Diane   E    
                                            Alice   E    
                                            Sam     E    
                                            Bob     F    
                                            Tom     G    
                                            Alice   H   

现在我运行查询,在出现提示时手动输入“Bob”之类的名称,并获得单行输出,例如:

ExclName   MaxRev
Bob         47

但我想要的是完整的表格:

ExclName   MaxRev
Bob         47
Tom         48
Jack        52
Mary        52
Diane       52
Alice       49
Sam         52

或者换句话说,我想知道排除指定成员的团队可以实现的最大收入是多少。真正的应用程序更复杂,因为涉及到其他分组(例如,如果排除 Jack,也必须排除 Mary),但我认为如果我解决了上面的简单问题,我可以处理剩下的。

我一直认为必须有一种方法可以使用 tblPeople 中的名称,并通过单个查询将它们链接到 MaxRev 计算,但我无法弄清楚。我可以做一些代码来遍历所有参数,但我希望有一个更直接的解决方案。欣赏任何人的意见。

4

1 回答 1

0

您可以简单地为所有人扩展您的查询:

select
   p.name as ExclName, 
   sum(t.revenue) as MaxRev 
from tblpeople as p
    cross join tbltasks as t
where
    t.task in
    (
       select l.task 
       from tblabilities as l
       where l.name <> p.name
    )
group by p.name

sql fiddle demo

更新。仍然不知道您使用哪个 RDBMS,这是 SQL Server 解决方案,可以将下面的解决方案拆分为临时表、变量和查询以提高性能,但无论如何它应该比第一个更快(如果您的索引上有适当的索引桌子):

with cte as (
    select l.[name], sum(t.[revenue]) as [revenue]
    from tblabilities as l
        inner join tbltasks as t on t.[task] = l.[task]
    where
        not exists
        (
            select * 
            from tblabilities as tl
            where tl.[task] = l.[task] and tl.[name] <> l.[name]
        )
    group by l.[name]
)
select
    p.[name] as ExclName,
    t.revenue_total - isnull(c.[revenue], 0) as MaxRev
from tblpeople as p
    left outer join cte as c on c.[name] = p.[name]
    outer apply (
        select sum(tt.[revenue]) as revenue_total
        from tbltasks as tt
        where
            tt.[task] in (select tl.[task] from tblabilities as tl)
    ) as t
order by p.[name]

sql fiddle demo

于 2013-08-31T19:09:30.760 回答