Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
134 views
in Technique[技术] by (71.8m points)

How to change the query to give last 15 weeks of data instead of last 15 days from the SQL Server

The following query gives playing time of the users from the database on daily basis for the last 15 days. It adds 0 if no game is played. Now I want to get the data of playing time on weekly basis and 0 if no game is played in the whole week. So I want the query to give the last 15 weeks of data.

Here is the daily query.

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

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

    SELECT @LastXDays = - 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(day, @LastXDays, l_update)
    GROUP BY quantity
        ,cast(starttime AS DATE)

    SELECT @MaxDate = @MaxDate
        ,@MinDate = dateadd(day, (@LastXDays + 1), @MaxDate);

    WITH AllDates
    AS (
        SELECT @MinDate AS xDate

        UNION ALL

        SELECT Dateadd(Day, 1, 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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Change the DATEADD from day to week. Hence, two changes:

dateadd(week, @LastXDays, l_update)

and

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

In this case, I would also rename the @LastXDays variable to @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

Also, a piece of advice: don't use query hints (NOLOCK) if you do not understand their use. In this case, using NOLOCK can have disastrous effects on your results.

Here are a few articles which you should read before deciding if you are going to keep using NOLOCK or not.

Understanding the SQL Server NOLOCK hint

Bad habits : Putting NOLOCK everywhere


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...