我有这张桌子
Customer Date Revenue
D 20140825 25
F 20130606 53
C 20130315 70
A 20140201 86
H 20140125 18
B 20140408 74
B 20130515 84
G 20130107 0
H 20141218 86
D 20130916 78
E 20140814 38
G 20130304 36
C 20131119 94
F 20131126 48
A 20131014 20
B 20140623 50
H 20141224 60
B 20140305 19
E 20130208 46
F 20140203 55
B 20140813 0
F 20130419 87
B 20131108 28
A 20140609 82
D 20140524 90
B 20130117 19
C 20140610 74
G 20130221 58
H 20140322 85
A 20141210 3
B 20140905 42
B 20131212 4
F 20131003 87
D 20141208 93
D 20140922 36
H 20140113 62
E 20130607 45
E 20140117 8
C 20130205 47
C 20140710 9
D 20140526 95
D 20140718 98
H 20140201 15
B 20140510 49
F 20140316 19
C 20140112 99
A 20130828 31
D 20131017 81
F 20131005 5
H 20140124 16
A 20130405 17
F 20130613 73
E 20130512 97
A 20130915 94
C 20140920 80
B 20130907 53
C 20130621 22
F 20141213 23
H 20140914 21
B 20130620 5
B 20141008 12
G 20130523 87
B 20140506 79
C 20130124 13
B 20130120 91
C 20130328 62
C 20141104 29
H 20130817 85
F 20140105 58
B 20130727 34
A 20140525 20
G 20130103 87
C 20130403 99
H 20140801 24
C 20141001 20
H 20130227 82
C 20131128 43
D 20130503 47
F 20130917 52
C 20130501 21
F 20140305 13
F 20140919 67
D 20140627 16
G 20140222 92
H 20130903 40
B 20140103 98
A 20140816 39
我想从下表的另一列中的日期开始总结收入列
customer registrationdate Sum of Revenue From Registration date till today
A 20140101
B 20140120
C 20140201
D 20131222
E 20131202
F 20130102
G 20131212
H 20140209
答案 0 :(得分:3)
诀窍是客户加入另一张桌子。然后,您可以过滤掉Date在RegistrationDate之前的所有记录。剩余的记录被分组和汇总(总和)以获得每个客户的总收入。
select
r.Customer,
sum(r.Revenue) as RevenueSinceRegistrationDate
from
Revenue r
inner join OtherTable t on t.Customer = r.Customer
where
r.Date >= t.RegistrationDate
group by
r.Customer