首页 > 解决方案 > 如何使用 sql 以特定格式排列日期

问题描述

![最小最大日期]

1 我有一个案例,我需要找到日期 1 的最小值、日期 2 和日期结束的最大值。

预期结果是如果日期 1 日期 2 在连续行中具有相等的值 a 那么日期 1 的最小值、日期 2 的最小值、建设性中断的日期结束的最大值。

如果需要任何澄清,请告诉我。.

我使用的代码:

select  distinct  DAY, county ,
                        case when (Date_2> Date_1 )then (region)  ) then  (region)
                        when (Date_2= Date_1 ) then min (region) Over (Partition By DAY, county )

else region end as region,

                         case when (Date_2> Date_1 ) then  handoff_date
                          when (Date_2= Date_1 ) then  min (handoff_date)  Over (Partition By DAY,county )
                         end as date2,

case when (Date_2= Date_1 )then min(Date_1) Over (Partition By DAY,county ) 

 when (Date_2> Date_1 )then (Date_1)  end as date1


, case when (Date_2= Date_1 )then max(Date_end) Over (Partition By DAY,county ) 


when (Date_2> Date_1 )then (Date_end)    end  as dateEnd 
        from table group by 1,2,3,4,5               

标签: sqlamazon-redshift

解决方案


推荐阅读