【问题标题】:SQL Table UPDATE by row with days per monthSQL 表按行更新,每月天数
【发布时间】:2014-10-20 05:41:06
【问题描述】:

SQL Server 2005:

我正在尝试创建一个如下所示的表:

                         JAN  |   FEB    |   March .......Dec   | YTD
Total Volume: 

Days in Month:

Gallons per day AVG:

3 行左侧有描述,13 列(每个月和年初至今总计一列)。 我知道如何填充每月的总量。我每个月的天数和平均值会使用什么?我希望每月的天数显示过去一个月的完整天数,如果是当前月份,则显示当前完成的天数。

【问题讨论】:

  • SQL Server 具有可用于计算这些值的内置日期函数。你试过了吗?

标签: sql sql-server-2005 sql-update


【解决方案1】:

您正在对数据进行透视,因此您需要列中的结果。您可以通过使用直接计算来做到这一点。以下是前三个月的示例:

select 'Days In Month' as col1,
       (case when month(getdate()) < 1 then 0
             when month(getdate()) = 1 then day(getdate())
             else 31
        end) as Jan,
       (case when month(getdate()) < 2 then 0
             when month(getdate()) = 2 then day(getdate())
             when year(getdate()) % 4 = 0 then 29
             else 28
        end) as Feb,
       (case when month(getdate()) < 3 then 0
             when month(getdate()) = 3 then day(getdate())
             else 31
        end) as Mar,

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-09
    • 2018-07-27
    • 1970-01-01
    • 2023-01-23
    • 2021-10-30
    • 1970-01-01
    相关资源
    最近更新 更多