两组中的值由select语句合二为一

时间:2011-09-21 15:32:43

标签: sql-server tsql group-by

我有一个用于记录方法调用的表。它有LogId,MethodId,DateTime列。

我需要编写一个select语句,该语句计算特定时间段内特定方法ID的所有日志,并显示特定方法在不同时间段内的日志数。

第一点很简单:

select
    l.[MethodId],
    count(l.[LogId]) as [Count]
from
    [Log] l (nolock)
where
    l.[DateTime] between @from and @to
    and l.[MethodId] in @methodIds
group by
    l.[MethodId]

但是现在我需要该表中的第二列,Previous,如果它在一个单独的语句中,它将如下所示:

select
    l.[MethodId],
    count(l.[LogId]) as [Previous]
from
    [Log] l (nolock)
where
    l.[DateTime] between @before and @from 
    and l.[MethodId] in @methodIds
group by
    l.[MethodId]

并非所有方法都会有两个时间段的日志,因此如果连接在这些情况下在count / previous列中插入0而不是它们为null,那将会很好。如果某个方法在任何时段都没有任何日志,那就没问题。

我希望在一个表格中看到MethodId, Count, Previous。我该如何实现这一目标?

3 个答案:

答案 0 :(得分:2)

类似的东西:

select 
    l.[MethodId], 
    sum(case when datetime between @from and @to then 1 else 0 end) as count,
    sum(case when datetime between @before and @from then 1 else 0 end) as previous
from 
    [Log] l
where 
    l.[DateTime] between @before and @to 
    and l.[MethodId] in @methodIds 
group by 
    l.[MethodId] 

where中的BETWEEN子句不会影响输出,但如果你有一个datetime索引,它可能会影响性能。如果这个表变大,你可能应该有这样的索引。

答案 1 :(得分:1)

试试这个:

select
    l.[MethodId],
    count(isnull(l.[LogId],0)) as [Previous]
from
    [Log] l (nolock)
where
    l.[DateTime] between @before and @from 
    and l.[MethodId] in @methodIds
group by
    l.[MethodId]

答案 2 :(得分:1)

尝试使用完全外连接:

DECLARE @from1 DATETIME, 
    @from2 DATETIME,
    @to1 DATETIME,
    @to2 DATETIME;

SELECT @from1 = '2011-01-01T00:00:00.000', 
    @to1 = '2011-01-31T23:59:59.997',
    @from2 = '2011-02-01T00:00:00.000',
    @to2 = '2011-02-28T23:59:59.997';


SELECT ISNULL(a.MethodID, b.MethodID) MethodID
    ,ISNULL(a.[Count]) CountA
    ,ISNULL(b.[Count]) CountB
FROM
(
select
    l.[MethodId],
    count(l.[LogId]) as [Count]
from
    [Log] l (nolock)
where
    l.[DateTime] between @from1 and @to1
    and l.[MethodId] in @methodIds
group by
    l.[MethodId]
) a 
FULL OUTER JOIN
(
select
    l.[MethodId],
    count(l.[LogId]) as [Previous]
from
    [Log] l (nolock)
where
    l.[DateTime] between @from2 and @to2 
    and l.[MethodId] in @methodIds
group by
    l.[MethodId]
) b
ON a.MethodID = b.MethodID
相关问题