I have information in a CheckLine table that essentially breaks down the various fees and information on a particular check. For this query I am wanting to SUM the total of the CheckLine but it is instead giving a SUM for ALL CheckLines together. I know I am missing something blatantly obvious, but I keep scratching my head on why I individualize the SUMs. Here is my query:
SELECT DISTINCT
O.FileNumber
,(SELECT DISTINCT
SUM(CL.Amount)
FROM
dbo.Orders O
LEFT JOIN dbo.Checks C
ON O.OrdersID = C.OrdersID
LEFT JOIN dbo.CheckLine CL
ON C.ChecksID = CL.ChecksID
) AS 'Total'
FROM
dbo.Orders O
LEFT JOIN dbo.Checks C
ON O.OrdersID = C.OrdersID
LEFT JOIN dbo.CheckLine CL
ON C.ChecksID = CL.ChecksID
This is what it is returning:
| FileNumber | … | Total |
| 1 | | 2000 |
| 2 | | 2000 |
What it should be returning is:
| FileNumber | … | Total |
| 1 | | 700 |
| 2 | | 1300 |
Thoughts on my complete brain fart here? Thanks guys!