简体   繁体   English

针对YTD,MTD,WTD总计的SQL查询

[英]SQL Query for YTD, MTD, WTD totals

I would like this query to be able to automagically know today's date & time as well as the first of the year (or month) (or week)... 我希望这个查询能够自动地知道今天的日期和时间以及一年(或一个月)(或一周)的第一天......

SELECT TicketID
FROM   Ticket
WHERE     (Ticket.DtCheckOut > '1/1/2011 12:00:00 AM') 
      AND (Ticket.DtCheckOut < '8/27/2011 12:00:00 AM')

I know it will use GETDATE() in some form, but you don't want to see what I've come up with, I promise! 我知道它会以某种形式使用GETDATE() ,但你不想看到我想出的东西,我保证!

Here is what I was reading on GETDATE() MDSN: GETDATE(Transact-SQL) 这是我在GETDATE() MDSN上阅读的内容:GETDATE(Transact-SQL)

I looked around here and Google - and didn't find anything 'clean' - so any input would be awesome! 我环顾四周和谷歌 - 并没有找到任何'干净' - 所以任何输入都会很棒!

DECLARE @now DATETIME
SET @now = GETDATE()

SELECT
    DATEADD(yy, DATEDIFF(yy, 0, @now), 0) AS FirstDayOfYear,
    DATEADD(mm, DATEDIFF(mm, 0, @now), 0) AS FirstDayOfMonth,
    DATEADD(DAY, -DATEDIFF(dd, @@DATEFIRST - 1, @now) % 7, @now) AS FirstDayOfWeek

@@DATEFIRST is SQL Server's first day of the week, which defaults to Sunday if you are using US English. @@DATEFIRST是SQL Server的一周中的第一天,如果您使用的是美国英语,则默认为星期日。

For the first day of the week it can be a bit tricky, depending on your actual requirements (whether you want to obey the user's datefirst setting or not, use Sunday regardless of the setting, etc.), see this question: Get first day of week in SQL Server . 对于一周的第一天,它可能有点棘手,取决于您的实际要求(无论您是否要遵守用户的datefirst设置,无论设置如何都使用星期日等),请参阅此问题: 获取第一天在SQL Server中的一周 Here is one way to do it: 这是一种方法:

DECLARE
   @today DATE = CURRENT_TIMESTAMP,
   @y DATE,
   @m DATE,
   @w DATE;

SELECT
   @y = DATEADD(YEAR, DATEDIFF(YEAR, 0, @today), 0),
   @m = DATEADD(MONTH, DATEDIFF(MONTH, 0, @today), 0),
   @w = DATEADD(DAY, 1-DATEPART(WEEKDAY, @today), @today);

SELECT
   [First day of year]  = @y,
   [First day of month] = @m,
   [First day of week]  = @w;

Whichever one you are after, you can use in the query, eg for YTD you would use: 无论你使用哪一个,你都可以在查询中使用,例如你可以使用的YTD:

SELECT TicketCount = COUNT(TicketID)
    FROM   dbo.Ticket
    WHERE  DtCheckOut >= @y;

Don't really think you need the < portion of the query if you're trying to get a count up to right now. 如果你现在正试图计算,那么你真的认为你不需要查询的<部分。 How many tickets will have been checked out tomorrow if I'm running the query today? 如果我今天运行查询,明天会检出多少张票? If you want to protect yourself against that you can use: 如果你想保护自己免受攻击,你可以使用:

SELECT COUNT(TicketID)
    FROM   dbo.Ticket
    WHERE  DtCheckOut >= @y
    AND    DtCheckOut < DATEADD(DAY, 1, @now);

You could make it a little more dynamic and pass in a parameter that says 'YTD', 'MTD' or 'WTD', eg 您可以使它更具动态性,并传入一个“YTD”,“MTD”或“WTD”参数,例如

CREATE PROCEDURE dbo.CountTickets
    @Range CHAR(3) = 'YTD' 
AS 
BEGIN
    SET NOCOUNT ON;

    -- you may want to handle invalid ranges, e.g.
    IF @Range NOT IN ('YTD', 'MTD', 'WTD')
    BEGIN
        RAISERROR('Please enter a valid range.', 11, 1);
        RETURN;
    END

    DECLARE
       @today DATE = CURRENT_TIMESTAMP,
       @start DATE;

    SELECT
       @start = CASE @range
          WHEN 'YTD' THEN DATEADD(YEAR,  DATEDIFF(YEAR,  0, @today), 0)
          WHEN 'MTD' THEN DATEADD(MONTH, DATEDIFF(MONTH, 0, @today), 0)
          WHEN 'WTD' THEN DATEADD(DAY, 1-DATEPART(WEEKDAY, @today), @today)
    END;

    SELECT 
        Range       = @range,
        TicketCount = COUNT(TicketID)
    FROM dbo.Ticket
    WHERE dtCheckOUt >= @start; 
END 
GO

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM