vendredi 23 septembre 2016

How to SUM Only One Time Per UniqueId in SQL?

I have two tables that look roughly like this:

Table A

DocumentId (*is unique) DocumentDate
1                       2016-01-01
2                       2016-01-01
3                       2016-02-01
4                       2016-03-01

and Table B

ContractId SnapshotTimeId NetFinanced
1          20160231       300
1          20160331       300
1          20160431       300
2          20160231       450
2          20160331       450
2          20160431       450
3          20160331       500
3          20160431       500
4          20160431       150

I would like the final table to look something like this:

DocumentDate NetFinanced
2016-01-01   750
2016-02-01   500
2016-03-01   150

I have tried the following and it doesn't work:

SELECT A.DocumentDate, SUM(B.NetFinanced)
 FROM A
 JOIN B on B.ContractId=A.DocumentId
GROUP BY A.DocumentDate

Any ideas? Thanks in advance

Aucun commentaire:

Enregistrer un commentaire