I have a Table “Postings”
CREATE TABLE POSTINGS(
Account_FK INT,
Department_FK INT,
Project_FK INT,
Company_FK INT,
Year INT,
Month INT,
Amount float,
Handled BIT
)
Im trying to make a select statement that will select the sum of Amounts for each company each month.
Like this:
SELECT Company_FK, Year, Month, Sum(Amount)
FROM Postings
GROUP BY Company_FK, Year, Month
But i will only need the rows that have not been handled. E.g. the rows with Handled = 0
SELECT Company_FK, Year, Month, Sum(Amount)
FROM Postings
WHERE Handled = 0
GROUP BY Company_FK, Year, Month
Now this query will sum up only the rows with Handled = 0 for the Company each year and month.
But i will also need the sums to include all the other rows for the company. I mean, if one row in the company is not handled. I will need to return the company sum of all company-rows.
So if Company_FK = 1 has three postings. All of which have handled = 1. Then this company could be ignored. But if Company_FK = 2 has three postings. And one of them has handled = 0, then i would need to return the sum of all three rows.
Do you understand what i mean?
Any suggestions?
Try this: