• <tfoot id='5tKSd'></tfoot>

    1. <legend id='5tKSd'><style id='5tKSd'><dir id='5tKSd'><q id='5tKSd'></q></dir></style></legend>

      <i id='5tKSd'><tr id='5tKSd'><dt id='5tKSd'><q id='5tKSd'><span id='5tKSd'><b id='5tKSd'><form id='5tKSd'><ins id='5tKSd'></ins><ul id='5tKSd'></ul><sub id='5tKSd'></sub></form><legend id='5tKSd'></legend><bdo id='5tKSd'><pre id='5tKSd'><center id='5tKSd'></center></pre></bdo></b><th id='5tKSd'></th></span></q></dt></tr></i><div id='5tKSd'><tfoot id='5tKSd'></tfoot><dl id='5tKSd'><fieldset id='5tKSd'></fieldset></dl></div>

      <small id='5tKSd'></small><noframes id='5tKSd'>

      • <bdo id='5tKSd'></bdo><ul id='5tKSd'></ul>
      1. 如何生成字段等于“Dayofweek"、“weekofyear"的日期表?等;行等于从 2010-0

        时间:2024-04-15

        • <i id='PLO7c'><tr id='PLO7c'><dt id='PLO7c'><q id='PLO7c'><span id='PLO7c'><b id='PLO7c'><form id='PLO7c'><ins id='PLO7c'></ins><ul id='PLO7c'></ul><sub id='PLO7c'></sub></form><legend id='PLO7c'></legend><bdo id='PLO7c'><pre id='PLO7c'><center id='PLO7c'></center></pre></bdo></b><th id='PLO7c'></th></span></q></dt></tr></i><div id='PLO7c'><tfoot id='PLO7c'></tfoot><dl id='PLO7c'><fieldset id='PLO7c'></fieldset></dl></div>
          <tfoot id='PLO7c'></tfoot>

            <tbody id='PLO7c'></tbody>
          <legend id='PLO7c'><style id='PLO7c'><dir id='PLO7c'><q id='PLO7c'></q></dir></style></legend>

            <small id='PLO7c'></small><noframes id='PLO7c'>

                • <bdo id='PLO7c'></bdo><ul id='PLO7c'></ul>
                  本文介绍了如何生成字段等于“Dayofweek"、“weekofyear"的日期表?等;行等于从 2010-01-01 到 current_date 的日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  如何生成包含Dayofweek"、weekofyear"等字段的日期表;行等于从 2010-01-01 到 current_date 的日期如下图:

                   Dayofweek Dayofmonth Dayofyear Weekofmonth Weekofyear 假期2010-01-01 6 1 1 1 1 是2010-01-02 7 2 2 1 1 否2010-01-03 1 3 3 1 1 否.....2019-03-31 1 31 90 6 14 N

                  <块引用>

                  附言DayofWeek = 一周中的第几天,Sun = 1,Sat = 7Dayofmonth = 该月的第几天

                   Holiday是一个标志,用于区分记录是否为公众假期

                  所以我需要做的第一步可能是创建从 2010-01-01 到 current_date 的记录,我想知道 hive 和 mssql 中的 while 循环会做什么?然后我准备好了专栏最后将它们结合起来.

                  我试过了

                  "声明@startdate日期声明@enddate 日期设置@startdate = '2010-01-01'设置@end_date = current_date而@start_date <=end_date开始DATEADD(DAY,1,@startdate)结尾"声明@startdate 日期声明@enddate 日期设置@startdate = '2010-01-01'设置@end_date = current_date而@start_date <=end_date开始DATEADD(DAY,1,@startdate)结尾Dayofweek Dayofmonth Dayofyear Weekofmonth Weekofyear 假期2010-01-01 6 1 1 1 1 是2010-01-02 7 2 2 1 1 否2010-01-03 1 3 3 1 1 否.....2019-03-31 1 31 90 6 14 N

                  解决方案

                  Hive 解决方案:

                  set hivevar:start_date=2010-01-01;--替换为您的开始日期设置 hivevar:end_date=current_date;--替换为您的结束日期与 date_range 作为(--此查询生成日期范围选择 date_add ('${hivevar:start_date}',s.i) 作为 dt从(选择posexplode(split(space(datediff(${hivevar:end_date},'${hivevar:end_date},'${hivevar:start_date}')),''))作为(i,x))s),假期为(选择堆栈(5,--添加更多'01-01', '新年','01-21', '马丁路德金纪念日','02-18', '总统日','05-27', '阵亡将士纪念日','07-04', '独立日') as ( mtdt,holiday_name))选择 d.dt 作为日期,date_format(current_date,'u') 作为星期几,day(dt) 作为 dayofmonth,date_format(current_date,'D') 作为 dayofyear,date_format(current_date,'W') 作为 weekofmonth,weekofyear(dt) 作为 weekofyear,如果 h.mtdt 不为空,则 'Y' 否则 'N' 以假期结束,h.holiday_name从 date_range d在 substr(d.dt,6)= h.mtdt 上左加入假期 h;

                  添加更多假期.

                  How can i generate a Date table with fields like "Dayofweek", "weekofyear" etc ;and rows equals to date from 2010-01-01 till current_date like below:

                               Dayofweek   Dayofmonth  Dayofyear Weekofmonth Weekofyear Holiday
                  2010-01-01       6           1            1          1          1        Y
                  2010-01-02       7           2            2          1          1        N
                  2010-01-03       1           3            3          1          1        N
                  .....
                  2019-03-31       1           31           90         6         14        N
                  

                  P.S. DayofWeek = day num of the week, Sun = 1, Sat = 7 Dayofmonth = day num of the month

                   Holiday is a flag to distinguish whether the records is a public holiday
                  

                  So first step i need to do may be create records from 2010-01-01 till current_date, i wonder while loop in hive and mssql will do? Then I have the column ready Finally combine them.

                  I have tried

                  "Declare @startdate date
                  Declare @enddate date
                  
                  set @startdate = '2010-01-01'
                  set @end_date = current_date
                  
                  while @ start_date <=end_date
                  BEGIN
                      DATEADD(DAY,1,@startdate)
                  END
                  
                  "Declare @startdate date
                  Declare @enddate date
                  
                  set @startdate = '2010-01-01'
                  set @end_date = current_date
                  
                  while @ start_date <=end_date
                  BEGIN
                      DATEADD(DAY,1,@startdate)
                  END
                  
                               Dayofweek   Dayofmonth  Dayofyear Weekofmonth Weekofyear Holiday
                  2010-01-01       6           1            1          1          1        Y
                  2010-01-02       7           2            2          1          1        N
                  2010-01-03       1           3            3          1          1        N
                  .....
                  2019-03-31       1           31           90         6         14        N
                  

                  解决方案

                  Solution for Hive:

                  set hivevar:start_date=2010-01-01; --replace with your start_date
                  set hivevar:end_date=current_date; --replace with your end_date
                  
                  with date_range as 
                  (--this query generates date range
                  select date_add ('${hivevar:start_date}',s.i) as dt 
                    from ( select posexplode(split(space(datediff(${hivevar:end_date},'${hivevar:start_date}')),' ')) as (i,x) ) s
                  ),
                  
                  holiday as (
                  select stack(5, --add more
                               '01-01', 'New Year',
                               '01-21', 'Martin Luther King Day',
                               '02-18', 'Presidents Day',
                               '05-27', 'Memorial Day',
                               '07-04', 'Independence Day'
                          ) as ( mtdt,holiday_name)
                  ) 
                  
                  select d.dt                                 as date,
                         date_format(current_date,'u')        as dayofweek,
                         day(dt)                              as dayofmonth,
                         date_format(current_date,'D')        as dayofyear,
                         date_format(current_date,'W')        as weekofmonth,
                         weekofyear(dt)                       as weekofyear, 
                         case when h.mtdt is not null then 'Y' else 'N' end as Holiday,
                         h.holiday_name
                    from date_range d 
                         left join holiday h on substr(d.dt,6)= h.mtdt
                  ;
                  

                  Add more holidays.

                  这篇关于如何生成字段等于“Dayofweek"、“weekofyear"的日期表?等;行等于从 2010-01-01 到 current_date 的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:将 SQL Server 表导出到多个部件文件 下一篇:为每个重复多次选择具有最新日期的sql中的行

                  相关文章

                    <bdo id='YsKsx'></bdo><ul id='YsKsx'></ul>
                  <legend id='YsKsx'><style id='YsKsx'><dir id='YsKsx'><q id='YsKsx'></q></dir></style></legend>
                • <tfoot id='YsKsx'></tfoot>
                  1. <small id='YsKsx'></small><noframes id='YsKsx'>

                  2. <i id='YsKsx'><tr id='YsKsx'><dt id='YsKsx'><q id='YsKsx'><span id='YsKsx'><b id='YsKsx'><form id='YsKsx'><ins id='YsKsx'></ins><ul id='YsKsx'></ul><sub id='YsKsx'></sub></form><legend id='YsKsx'></legend><bdo id='YsKsx'><pre id='YsKsx'><center id='YsKsx'></center></pre></bdo></b><th id='YsKsx'></th></span></q></dt></tr></i><div id='YsKsx'><tfoot id='YsKsx'></tfoot><dl id='YsKsx'><fieldset id='YsKsx'></fieldset></dl></div>