如何更改查询以从SQL Server提供最近15周的数据而不是过去15天

时间:2015-08-06 14:57:22

标签: sql-server sql-server-2008 sql-server-2005 sql-server-2012

以下查询提供过去15天内每天来自数据库的用户的播放时间。如果没有玩游戏,它会增加0。现在我想获得每周播放时间的数据,如果整周没有播放游戏,则为0。所以我希望查询能够提供最近15周的数据。

以下是每日查询。

<appender name="LOG-FILE"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>\log\log.txt</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
    <fileNamePattern>\log\old\log.%d{yyyyMMdd}_%i.txt</fileNamePattern>
    <maxHistory>2</maxHistory>
    <timeBasedFileNamingAndTriggeringPolicy
        class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
        <maxFileSize>100KB</maxFileSize>
    </timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
<layout class="ch.qos.logback.classic.PatternLayout">
    <Pattern>%d{yyyy.MM.dd HH:mm:ss} > [%thread] %-5level - %msg%n</Pattern>
</layout>

1 个答案:

答案 0 :(得分:0)

每天将DATEADD更改为一周。因此,有两个变化:

dateadd(week, @LastXDays, l_update)

dateadd(week, (@LastXDays + 1), @MaxDate)

在这种情况下,我还会将@LastXDays变量重命名为@LastXWeeks

CREATE PROCEDURE [dbo].[spGetPlayingTimeOfthepeoplesPerDay] @email NVARCHAR(50)
AS
BEGIN
    SET NOCOUNT ON;

    DECLARE @MinDate DATE
        ,@MaxDate DATE
        ,@LastXDays INT

    SELECT @LastXWeeks = - 15

    SELECT @MaxDate = peoples.l_update
    FROM peoples
    WHERE peoples.email = @email

    DECLARE @test TABLE (
        quantity VARCHAR(100)
        ,DATE DATE
        ,TimePerDay DECIMAL(5, 2)
        );

    WITH CTE
    AS (
        SELECT peoples.email
            ,peoples.l_update
            ,act.quantity
            ,act.starttime
            ,act.endtime
            ,act.duration AS [Totaltime]
        FROM peoples
        INNER JOIN MPeoples ON peoples.Id = MPeoples.parent_id
        INNER JOIN slines ON MPeoples.id = slines.movesuser_id
        INNER JOIN seg ON slines.id = seg.sline_id
        INNER JOIN act ON seg.id = act.seg_id
        WHERE act.quantity = 'playing'
            AND (peoples.email = @email)
        GROUP BY peoples.email
            ,act.quantity
            ,act.duration
            ,act.starttime
            ,act.endtime
            ,peoples.l_update
        )
    INSERT INTO @test (
        quantity
        ,DATE
        ,TimePerDay
        )
    SELECT quantity
        ,Cast(starttime AS DATE) AS DATE
        ,SUM(datediff(second, starttime, endtime)) / 60.0 AS TimePerDay
    FROM cte WITH (NOLOCK)
    WHERE starttime >= dateadd(week, @LastXWeeks, l_update)
    GROUP BY quantity
        ,cast(starttime AS DATE)

    SELECT @MaxDate = @MaxDate
        ,@MinDate = dateadd(week, (@LastXWeeks + 1), @MaxDate);

    WITH AllDates
    AS (
        SELECT @MinDate AS xDate

        UNION ALL

        SELECT Dateadd(Day, 7, xDate)
        FROM AllDates AS ad
        WHERE ad.xDate < @MaxDate
        )
    SELECT 'playing' AS quantity
        ,ad.xDate
        ,Isnull(t.TimePerDay, 0) AS TimePerDay
    FROM AllDates AS ad WITH (NOLOCK)
    LEFT JOIN @test AS t ON ad.xDate = t.DATE
END

另外,建议:如果您不了解其用途,请不要使用查询提示(NOLOCK)。在这种情况下,使用NOLOCK可能会对您的结果产生灾难性影响。

在决定是否继续使用NOLOCK之前,您应阅读以下几篇文章。

Understanding the SQL Server NOLOCK hint

Bad habits : Putting NOLOCK everywhere