我有这样的日期
CustId Frequency Date
------ --------- -----
C1 2 20-Jan-2015
C2 3 22-Feb-2015
我必须得到如下所示的输出
C1 20-Jan-2015
C1 20-Jan-2015
C2 22-Feb-2015
C2 22-Feb-2015
C2 22-Feb-2015
任何人都可以帮我解决这个问题。
先谢谢
答案 0 :(得分:2)
你需要一个数字表。有多种方法可以生成一个。这是一个明确的方法:
with numbers(n) as (
select 1 from dual union all select 2 from dual union all select 3 from dual
)
select d.CustId, d.date + n.n - 1 as date
from data d join
numbers n
on d.frequency <= n.n;
如果你想生成一堆数字,一个典型的方法是:
with numbers(n) as (
select level as n
from dual
connect by level <= 365
)
答案 1 :(得分:2)
您可以使用相关的分层查询来生成额外的行(无需创建额外的表格,并确保您的频率不超过这些数字的范围匹配):
SELECT CustID,
"Date"
FROM CustomerFrequencies c,
TABLE(
CAST(
MULTISET(
SELECT LEVEL
FROM DUAL
CONNECT BY LEVEL <= c.Frequency
)
AS SYS.ODCINUMBERLIST
)
)
答案 2 :(得分:0)
你可以有一个数字表并加入它
select t1.CustId,t1.Date+t2.number as date
from table t1 inner join numbers t2
on t2.number<=t1.frequency