Column value needs to be broken into two columns: “rangefrom” and “rangeto.”

huangapple go评论121阅读模式
英文:

i have requirement in which column value need to break into two column rangefrom and rangeto

问题

我有一个需求,需要将msn列拆分成rangefrom和rangeto表格

num    msn    date

1ab    5       25/2/2019
1ab    7       25/2/2019
1ab    8       25/2/2019
1ac    8       25/4/2019
1ad    9       25/5/2019
1ad    10      25/5/2019
1ad    11      25/5/2019

输出应该如下,如果msn不连续,则相同的值将作为rangefrom和rangeto,如果连续,则根据日期和num,最小值将作为rangefrom,最大值将作为rangeto

num   rangefrom  rangeto   date
1ab     5         5        25/2/2019
1ab     7         8        25/2/2019
1ad     9         11       25/4/2019
英文:

I have requirement in which msn column need to break into rangefrom and rangeto
table

num    msn    date

1ab    5       25/2/2019
1ab    7       25/2/2019
1ab    8       25/2/2019
1ac    8       25/4/2019
1ad    9       25/5/2019
1ad    10       25/5/2019
1ad    11       25/5/2019

output should like this if msn is not continuous then same value goes to rangefrom and rangeto if continuous then min value will go to rangefrom and max value will go rangeto based on date and num

num   rangefrom  rangeto   date
1ab     5         5        25/2/2019
1ab     7         8        25/2/2019
1ad     9         11        25/4/2019

答案1

得分: 1

以下是代码的翻译部分:

SELECT NUM, MIN(MSN), MAX(MSN), DTE
FROM (SELECT NUM, TRIM(MSN) MSN, DTE, TRIM(MSN)-ROW_NUMBER() OVER(ORDER BY DTE, TRIM(MSN)) SEQNUM
      FROM T)
GROUP BY NUM, SEQNUM, DTE
ORDER BY NUM;

这里 是演示1。
这里 是演示2。

英文:

You may try below query -

SELECT NUM, MIN(MSN), MAX(MSN), DTE
FROM (SELECT NUM, TRIM(MSN) MSN, DTE, TRIM(MSN)-ROW_NUMBER() OVER(ORDER BY DTE, TRIM(MSN)) SEQNUM
      FROM T)
GROUP BY NUM, SEQNUM, DTE
ORDER BY NUM;

Here is the demo1.
Here is the demo2.

答案2

得分: 1

使用LAG()LEAD()窗口函数以及一个CTE

with cte as (
 select t.*,
    case when "msn" - lag("msn") over (partition by "num", "date" order by "msn") = 1 then 0 else 1 end isfrom,
    case when lead("msn") over (partition by "num", "date" order by "msn") - "msn" = 1 then 0 else 1 end isto
  from tablename t 
)
select c1."num", c1."msn" rangefrom, min(c2."msn") rangeto, c1."date"
from cte c1 inner join cte c2
on c2."num" = c1."num" and c2."date" = c1."date" 
and (
  (c1.isfrom = 1 and c2.isto = 1 and c1."msn" < c2."msn")
  or
  (c1.isfrom = 1 and c1.isto = 1 and c1."msn" = c2."msn")
) 
group by c1."num", c1."msn", c1."date"
order by c1."num", c1."msn", c1."date"

查看演示

结果:

> num | RANGEFROM | RANGETO | date     
> :-- | --------: | ------: | :--------
> 1ab |         5 |       5 | 25/2/2019
> 1ab |         7 |       8 | 25/2/2019
> 1ac |         8 |       8 | 25/4/2019
> 1ad |         9 |      11 | 25/5/2019
英文:

With LAG() and LEAD() window functions and a CTE:

with cte as (
 select t.*,
    case when &quot;msn&quot; - lag(&quot;msn&quot;) over (partition by &quot;num&quot;, &quot;date&quot; order by &quot;msn&quot;) = 1 then 0 else 1 end isfrom,
    case when lead(&quot;msn&quot;) over (partition by &quot;num&quot;, &quot;date&quot; order by &quot;msn&quot;) - &quot;msn&quot; = 1 then 0 else 1 end isto
  from tablename t 
)
select c1.&quot;num&quot;, c1.&quot;msn&quot; rangefrom, min(c2.&quot;msn&quot;) rangeto, c1.&quot;date&quot;
from cte c1 inner join cte c2
on c2.&quot;num&quot; = c1.&quot;num&quot; and c2.&quot;date&quot; = c1.&quot;date&quot; 
and (
  (c1.isfrom = 1 and c2.isto = 1 and c1.&quot;msn&quot; &lt; c2.&quot;msn&quot;)
  or
  (c1.isfrom = 1 and c1.isto = 1 and c1.&quot;msn&quot; = c2.&quot;msn&quot;)
) 
group by c1.&quot;num&quot;, c1.&quot;msn&quot;, c1.&quot;date&quot;
order by c1.&quot;num&quot;, c1.&quot;msn&quot;, c1.&quot;date&quot;

See the demo.<br/>
Results:

&gt; num | RANGEFROM | RANGETO | date     
&gt; :-- | --------: | ------: | :--------
&gt; 1ab |         5 |       5 | 25/2/2019
&gt; 1ab |         7 |       8 | 25/2/2019
&gt; 1ac |         8 |       8 | 25/4/2019
&gt; 1ad |         9 |      11 | 25/5/2019

huangapple
  • 本文由 发表于 2020年1月6日 22:47:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/59614101.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定