【问题标题】:Unsure as to how to join two working queries together不确定如何将两个工作查询连接在一起
【发布时间】:2013-06-10 22:14:43
【问题描述】:

我有两个运行良好的查询。我想将这两个查询合并在一起,但我不知道该怎么做。

我有一个结构如下的 SQL Server 表:

Table name : calendar. 

列:

Calendar Date (smalldatetime)
Working Day (bit)

日历日期包含所有日期,格式为 yyyy-mm-dd。工作日表示我有工作,如果是 1,如果是周末或节假日,则标记为 0:)。

我要检索的示例:

CalendarDate   NumWorkingDaysInMonthSoFar   NumWorkingDaysInThisMonthTotal
------------------------------------
2013-06-01    0 (--Due to this being marked a 0 as above (Saturday))      22
2013-06-02    0 (--Due to this being marked a 0 as above (Sunday) -- All 3 of these dates marked as 0)                22
2013-06-03    1 (--Due to this being marked a 0 as above (Bank Holiday))          22
2013-06-04    1             22
2013-06-05    2             22
2013-06-06    3             22

我有两个查询分别做这两件事,但我正在努力弄清楚如何将两者结合起来以产生上述结果集。

这是返回 NumberWorkingDaysInThisMonthTotal 的查询(示例中为 22,每个月应该是不同的值):

SELECT 
SUM(WorkingDay) As NumWorkingDaysInThisMonthTotal
FROM [calendar]
GROUP BY YEAR(CalendarDate), MONTH(CalendarDate) 

并返回该月到目前为止的每一天的工作日数(每个工作日加一个,每个月初重置):

select c.[CalendarDate],
       (select sum(cast(WorkingDay as int))
        from calendar c2
        where year(c.[CalendarDate]) = year(c2.[CalendarDate]) and
              month(c.[CalendarDate]) = month(c2.[CalendarDate]) and
              c2.[CalendarDate] <= c.[CalendarDate]
       ) as NumWorkingDaysInMonthSoFar
from calendar c

如何将这些组合起来以生成上述结果集?我真的很感谢您提供一些有关您如何弄清楚如何做到这一点的信息-在这种特定情况下都是如此;并且在您获得 SQL 背景的地方,以便我可以提高自己。非常感谢。

【问题讨论】:

    标签: sql sql-server database sql-server-2005 select


    【解决方案1】:

    这可能不是最有效的方法,但您可以将您的第一个查询作为第二个查询的子查询插入(它也未经测试,因此完全有可能这将为您带来丝绸的价格上周三廷巴克图的豆腐,而不是你想要的):

    select c.[CalendarDate],
       (SELECT SUM(C3.WorkingDay)
        FROM Calendar C3
        WHERE month(C3.CalendarDate) = month(c.CalendarDate)
          AND year(C3.CalendarDate) = year(c.CalendarDate)
       ) AS NumWorkingDaysInThisMonthTotal,
       (select sum(cast(WorkingDay as int))
        from calendar c2
        where year(c.[CalendarDate]) = year(c2.[CalendarDate]) and
              month(c.[CalendarDate]) = month(c2.[CalendarDate]) and
              c2.[CalendarDate] <= c.[CalendarDate]
       ) as NumWorkingDaysInMonthSoFar
    from calendar c
    

    它甚至可能对您的目的来说已经足够快了。

    正如您要求的一些思维过程解释,当我开始考虑我想组合的两个查询时,就会出现这种解决方案,就好像它们都被定义为视图一样,或者两者都有恰好兼容的数据集,但在不同的桌子上。那么我将如何将它们结合在一起?

    我确实倾向于最终使用纯子查询而不是使用视图来做不同的事情,但这样思考有助于我弄清楚子查询实际上应该是什么。我喜欢考虑获取一些数据并将它们组合在一起,因为它可以帮助我弄清楚我实际上在做什么。

    诚然,我可能会再研究一段时间,并想出一些方法将其统一为更有效的解决方案。第一次尝试通常不是最有效的,显然在很多情况下查询速度很重要,但首先获得正确答案对我来说是最重要的。

    【讨论】:

      【解决方案2】:

      您应该能够使用窗口分析函数,如下所示:

      select c.[CalendarDate],
             SUM(WorkingDay) over (partition by YEAR(CalendarDate), MONTH(CalendarDate))
                As NumWorkingDaysInThisMonthTotal,
             (select sum(cast(WorkingDay as int))
              from calendar c2
              where year(c.[CalendarDate]) = year(c2.[CalendarDate]) and
                    month(c.[CalendarDate]) = month(c2.[CalendarDate]) and
                    c2.[CalendarDate] <= c.[CalendarDate]
             ) as NumWorkingDaysInMonthSoFar
      from calendar c
      

      【讨论】:

        猜你喜欢
        • 2017-06-13
        • 1970-01-01
        • 2017-02-24
        • 1970-01-01
        • 1970-01-01
        • 2015-10-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多