0

我有一个需要转换的包含数百万行的文件。我无法获得想要的结果,所以我想听从专家的意见;

select
[ID],
min ([date]) as 'Payment1',
min ([date]) as 'Payment2', 
where 'Payment2' > 'Payment1'
min ([date]) as 'Payment3', 
where 'Payment3' > 'Payment2'
min ([date]) as 'Payment4', 
where 'Payment4' > 'Payment3'
from [FP&A].[dbo].[PaymentSchedules]
group by [ID]
order by [ID]
4

2 回答 2

0

所以一个朋友的朋友知道我在做什么,并想出了这段代码,它分两步完成了这个技巧。我发布它以防将来对其他人有所帮助;

Select [DBO].[ID], 
[DBO].[Date], 
count(*) as 'alias1'
into ##Temp
from [DBO] left outer join [DBO] as 'alias2'
on [DBO].[ID] = [alias2].[id]
And [DBO].[Date] >= [alias2].[Date]
Group BY [dbo].[id], 
     [dbo].[Date]
order by [dbo].[ID], [dbo].[alias1]


Select ##Temp.id, 
MIN(case when [alias1] = 1 then Date Else NULL END) as Payment1,
MIN(case when [alias1] = 2 then Date Else NULL END) as Payment2,
MIN(case when [alias1] = 3 then Date Else NULL END) as Payment3,
MIN(case when [alias1] = 4 then Date Else NULL END) as Payment4,
MIN(case when [alias1] = 5 then Date Else NULL END) as Payment5,
MIN(case when [alias1] = 6 then Date Else NULL END) as Payment6,
MIN(case when [alias1] = 7 then Date Else NULL END) as Payment7,
MIN(case when [alias1] = 8 then Date Else NULL END) as Payment8,
MIN(case when [alias1] = 9 then Date Else NULL END) as Payment9
from ##
group by ##Temp.id
order by ##Temp.id
于 2013-07-26T19:18:17.613 回答
0

看起来您想选择四个最低的不同日期作为您的四次付款。我将介绍一种易于理解的方法来实现这一点:

  1. 查找付款1
  2. 再次加入表以查找 payment2
  3. 再次加入表以查找付款3
  4. ...

最终结果在这里:

with step1 as (
  select id, min(date) as payment1
  from ps
  group by id
), step2 as (
  select step1.*, min(date) as payment2
  from step1
  join ps on step1.id = ps.id
  where step1.payment1 < ps.date
  group by step1.id, payment1
), step3 as (
  select step2.*, min(date) as payment3
  from step2
  join ps on step2.id = ps.id
  where step2.payment2 < ps.date
  group by step2.id, payment1, payment2
), step4 as (
  select step3.*, min(date) as payment4
  from step3
  join ps on step3.id = ps.id
  where step3.payment3 < ps.date
  group by step3.id, payment1, payment2, payment3
)
select * from step4

这是一个展示它的小提琴:http://sqlfiddle.com/#!3/66576/8

现在这产生了 4 个连接,因此性能不是最好的。如果您在性能方面遇到问题,那么我会考虑对日期进行排名,选择四个最低的(不包括重复项),然后旋转表格。

于 2013-07-26T00:19:47.367 回答