0

我有两个查询不同表并返回相似字段的 SQL 查询:

User - Role - Category - Points

User - Role - Category - Target

我的查询如下所示:

select
    acs.UserID,
    acs.RoleID,
    acs.CategoryID,
    sum(acs.Points)
from acs

select
    tb.UserID,
    tb.RoleID,
    tb.CategoryID,
    sum(
        (
            (DATEDIFF(dd, (case when @start >= tb.StartDate then @start else tb.StartDate end), (case when @end <= tb.EndDate then @end else tb.EndDate end)) + 1) 
        ) * tb.dailyMed
    ) as Target
from tb

我想结束的是这样的:

User - Role - Category - Points - Target

每个单独的查询运行时间不到 1 秒,但是当我尝试使用内部连接将它们组合起来时,运行时间超过 3 分钟。

我希望有一种更有效的方法来做到这一点,但我似乎找不到。

*编辑我的内部连接看起来像这样

select
    acs.UserID,
    acs.RoleID,
    acs.CategoryID,
    sum(acs.Points),
    t.Target
from
    dbo.ActualCacheSale acs
        inner join
            (select
                tb.UserID,
                tb.RoleID,
                tb.CategoryID,
                sum(
                    (
                        (DATEDIFF(dd, (case when @start >= tb.StartDate then @start else tb.StartDate end), (case when @end <= tb.EndDate then @end else tb.EndDate end)) + 1)
                    ) * tb.dailyMed
                ) as Target
            from
                dbo.TargetBucket tb
            ) t on
                t.UserID = acs.UserID and
                t.RoleID = acs.RoleID and
                t.CategoryID = acs.CategoryID
4

3 回答 3

6

尝试

select
aa.UserId,
aa.Roleid,
aa.CategoryId
Sum(aa.Points) as Points,
Sum(aa.Target) as Target
from
(
select
    acs.UserID,
    acs.RoleID,
    acs.CategoryID,
    sum(acs.Points) as Points
    null as target
from acs

union all

select
    tb.UserID,
    tb.RoleID,
    tb.CategoryID,
    0 as points
    sum(
        (
        (DATEDIFF(dd, (case when @start >= tb.StartDate then @start else tb.StartDatee end),     
    (case when @end <= tb.EndDate then @end else tb.EndDate end)) + 1) 
    ) * tb.dailyMed
    ) as Target
from tb) as aa
group by
aa.UserId,
aa.Roleid,
aa.CategoryId
于 2013-01-24T19:19:37.717 回答
0

也许我遗漏了一些东西,但我不确定你如何在不分组为聚合的情况下得到一个总和。无论如何,这是我的看法 - 希望我没有错过任何东西:

SELECT  P.UserID ,
        P.RoleID ,
        P.CategoryID ,
        P.Points ,
        T.Target
FROM    ( SELECT    acs.UserID ,
                    acs.RoleID ,
                    acs.CategoryID ,
                    SUM(acs.Points) Points
          FROM      acs
          GROUP BY  acs.UserID ,
                    acs.RoleID ,
                    acs.CategoryID
        ) P
        CROSS APPLY ( SELECT    tb.UserID ,
                                tb.RoleID ,
                                tb.CategoryID ,
                                SUM(( (DATEDIFF(dd, ( CASE WHEN @start >= tb.StartDate THEN @start
                                                           ELSE tb.StartDate
                                                      END ), ( CASE WHEN @end <= tb.EndDate THEN @end
                                                                    ELSE tb.EndDate
                                                               END )) + 1) ) * tb.dailyMed) AS Target
                      FROM      TB
                      WHERE     TB.UserID = P.UserID
                                AND TB.RoleID = P.RoleID
                                AND TB.CategoryID = P.CategoryID
                      GROUP BY  tb.UserID ,
                                tb.RoleID ,
                                tb.CategoryID
                    ) T
于 2013-01-24T19:20:59.257 回答
-2

您可以发布您用于内部联接的查询吗?通常,如果连接列上有有用的索引,则查询应该执行良好。

如果没有,一个不错的选择是创建两个临时表,其中 1 个用于存储每个查询的结果。然后在连接列的临时表上创建索引。使用临时表运行最终查询。

于 2013-01-24T19:12:51.717 回答