首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何根据用户输入的日期获取输出变化?

如何根据用户输入的日期获取输出变化?
EN

Stack Overflow用户
提问于 2015-10-06 13:59:05
回答 1查看 308关注 0票数 1

我有3个输入表- day_level

代码语言:javascript
运行
复制
   Dim_type                       Id                            day_date                    month                 year
    1                              1                            2015-01-05               January                2015
    1                              2                            2015-01-06               January                2015
    1                              3                            2015-01-07               January                2015
    1                              4                            2015-01-08               January                2015        
    1                              5                            2015-01-09               January                2015
    1                              6                            2015-01-10               January                2015
    1                              7                            2015-01-11               January                2015
    1                              8                            2015-01-12               January                2015
    1                              9                            2015-01-13               January                2015
    1                              10                           2015-01-14               January                2015
    1                              11                           2015-01-15               January                2015
    1                              12                           2015-01-16               January                2015
    1                              13                           2015-01-17               January                2015
    1                              14                           2015-01-18               January                2015
    1                              15                           2015-01-19               January                2015
    1                              16                           2015-01-20               January                2015

这显示了每周的基础数据。week_level

代码语言:javascript
运行
复制
Dim_type                         Id                          week_number                   month                    year
    2                            101                             week1                          January                  2015
    2                            102                             week2                          January                  2015
    2                            103                             week3                          January                  2015
    2                            104                             week4                          January                  2015         
    2                            105                             week1                         February                 2015

这显示了每月的基础数据。

month_level

代码语言:javascript
运行
复制
Dim_type                          Id                                      month                  year
    3                              1001                                  January                 2015
    3                              1002                                  January                 2015
    3                              1003                                  January                 2015
    3                              1004                                  January                 2015         
    3                              1005                                 February                2015

我有一个3表,其中有数据根据日水平,周水平和月水平。有一个Dim_type列,它告诉我们哪些数据来自哪个表,例如

代码语言:javascript
运行
复制
dim_type=1 is for day level 

dim_type=2 is for week level

dim_type=3 is for month level

在这里我不能写一个函数/过程,它可以根据用户给出的输入日期来决定显示哪个数据-

这里我给你一些例子,假设日期是由用户start date- 2015-01-01 and end date- 2015-01-31输入的。现在这里需要整个一月份的数据,所以数据将来自month表。第二,就像start date-2015-01-05 and end date- 2015-01-06。现在我们两边都没有完整的月份,所以这里我们必须考虑周数据。因此,这里的输出将如下所示-

代码语言:javascript
运行
复制
id                                                                       value                     
102                                                          week2 ( January)  
103                                                          week3 (    ,,       )
104                                                          week4  (    ,,       )
105                                                          week5  (Febuaray)

这里考虑了整个星期,因为周六和周日是非工作日。

第三个类似于开始日期- 2015-01-05和结束日期- 2015-01-20,因此它将类似于

代码语言:javascript
运行
复制
 id                                                                   value                     
102                                                          week2 ( January)  
103                                                          week3 (    ,,       )
 14                                                           day level data for 18 January
 15                                                           day level data for 19  January
 12                                                           day level data for 20  January

每个表id都有唯一的id,该id包含数据,该数据将根据日期过滤器在输出中表示。如何编写过滤代码是我需要帮助的部分!

因此,我不能创建一个存储过程/函数来判断是否有整月或按周的数据,或者应该将其输出为日级别。有谁可以帮我?谢谢

EN

回答 1

Stack Overflow用户

发布于 2015-10-06 18:48:09

这将完成您所要求的操作。

有一些注意事项:该函数将根据开始日期获取一个月中的天数。

您应该将日期存储在数据库的月份表中,这样就不会一次又一次地重新创建临时表。您最好聚合数据,这样就完全不需要使用这个函数了。

代码语言:javascript
运行
复制
CREATE FUNCTION dbo.ISFullMonth (@StartDate DATE, @EndDate DATE)
RETURNS VARCHAR(5)
BEGIN

    /* variables to be used */
    DECLARE @Return VARCHAR(5), @Difference INT, @DaysInMonth TINYINT;

    /* 
        table variable to store the number of days in a month
        this would be better as a fixed SQL table as it'll 
        be called a lot
     */
    DECLARE @Months TABLE 
        ([Month] TINYINT, [NoDays] TINYINT);

    /*
        month values
    */
    INSERT INTO @Months
    VALUES
        (1, 31),
        (2, 28),
        (3, 31),
        (4, 30),
        (5, 31),
        (6, 30),
        (7, 31),
        (8, 31),
        (9, 30),
        (10, 31),
        (11, 30),
        (12, 31);

    /*
        get the number of days in the month
    */
    SELECT @DaysInMonth = [NoDays] FROM @Months WHERE [Month] = MONTH(@StartDate);

    /* 
        Check if it's a leap year and alter the number of days in Febuary to 29 
        This was taken from https://www.mssqltips.com/sqlservertip/1527/sql-server-function-to-determine-a-leap-year/
    */
    IF((SELECT CASE DATEPART(mm, DATEADD(dd, 1, CAST((CAST(@StartDate AS VARCHAR(4)) + '0228') AS DATE))) 
            WHEN 2 THEN 1 
            ELSE 0 
            END) = 1) AND MONTH(@StartDate) = 2
        SET @DaysInMonth = 29;

    /*  
        Get the difference between the two dates
        add 1 to the value to include the first day in the count
    */
    SET @Difference = DATEDIFF(day, @StartDate, @EndDate)+1;

    /*
        Check how many days difference there are
    */
    If (@Difference >= @DaysInMonth)
    BEGIN
        SET @Return = 'Month';
    END
    ELSE IF (@Difference > 7)
    BEGIN
        SET @Return = 'Week';
    END
    ELSE
    BEGIN
        SET @Return = 'Day';
    END

    RETURN @Return;

END
GO

好的,这比我预期的花了更长的时间来写,但这是给你的。这现在应该可以工作,但它根本不能很好地跨越几年。

代码语言:javascript
运行
复制
CREATE PROCEDURE GetDateParts 
(
@StartDate DATE ,
@EndDate DATE
)
AS
BEGIN

    /* variables to be used */
    DECLARE @Return VARCHAR(5)
    /*  
        Get the difference between the two dates
        add 1 to the value to include the first day in the count
    */
    , @TotalNumberOfDays INT
    , @DaysInMonth TINYINT;

    /*  table variable to store the number of days in a month
        this would be better as a fixed SQL table as it'll 
        be called a lot */
    DECLARE @Months TABLE 
        ([Month] TINYINT, [NoDays] TINYINT);

    /* month values */
    INSERT INTO @Months
    VALUES
        (1, 31),
        (2, 28),
        (3, 31),
        (4, 30),
        (5, 31),
        (6, 30),
        (7, 31),
        (8, 31),
        (9, 30),
        (10, 31),
        (11, 30),
        (12, 31);

    /* Create Result table */
    DECLARE @ResultTable TABLE ([MonthNumber] TINYINT, [FullMonth] BIT, [Weeks] TINYINT, [Days] TINYINT)

    -- set the count as the mointh number
    DECLARE @Count TINYINT = MONTH(@StartDate);
    SET @TotalNumberOfDays = DATEDIFF(day, @StartDate, @EndDate)+1
    WHILE @Count <= MONTH(@EndDate)
    BEGIN

        /* get the number of days in the month */
        SELECT @DaysInMonth = [NoDays] FROM @Months WHERE [Month] = @Count;

        /* 
        Check if it's a leap year and alter the number of days in Febuary to 29 
        This was taken from https://www.mssqltips.com/sqlservertip/1527/sql-server-function-to-determine-a-leap-year/
        */
        IF((SELECT CASE DATEPART(mm, DATEADD(dd, 1, CAST((CAST(@StartDate AS VARCHAR(4)) + '0228') AS DATE))) 
                WHEN 2 THEN 1 
                ELSE 0 
                END) = 1) AND MONTH(@StartDate) = 2
            SET @DaysInMonth = 29;

        IF (@TotalNumberOfDays >= @DaysInMonth)
        BEGIN
            INSERT INTO @ResultTable ([MonthNumber], [FullMonth])
            VALUES (@Count, 1)

            SET @TotalNumberOfDays = @TotalNumberOfDays - (@DaysInMonth-DAY(@StartDate));

            SET @StartDate = DATEADD(day, (@DaysInMonth-DAY(@StartDate)+1), @StartDate);

            SET @Count = @Count + 1;
        END
        ELSE IF (@TotalNumberOfDays >= 7)
        BEGIN
            INSERT INTO @ResultTable ([MonthNumber], [Weeks])
            VALUES (@Count, CAST(@TotalNumberOfDays/7 AS INT))
            DECLARE @Remainder TINYINT = @TotalNumberOfDays%7;

            IF (@Remainder = 0)
            BEGIN
                SET @Count = @Count + 1;
            END
            ELSE
            BEGIN
                SET @TotalNumberOfDays = @Remainder;
            END
        END
        ELSE
        BEGIN
            INSERT INTO @ResultTable ([MonthNumber], [Days])
            VALUES (@Count, @TotalNumberOfDays)
            SET @Count = @Count + 1;
        END

    END;

    -- Return Results
    SELECT * FROM @ResultTable;
END
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32962729

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档