Domanda

I am having an absolute brain lock on this query and was wondering if anyone could assist me.

I have daily data in 1 ms access table in the form: clients (unique) | date | openingbal | closingbal | I have a SQL to get the no. of daily client balances that closed higher than they opened (increase)

SELECT Dates, Count(closingbal) AS Increases FROM tablename 
WHERE closingbal >[ openingbal] GROUP BY Dates

This works ok.

I also have one for client balances that closed lower than they opened (decrease):

SELECT Dates, Count(closingbal) AS Decreases FROM tablename 
WHERE closingbal <[ openingbal]GROUP BY Dates 

This works ok too. I am totally stuck on how to combine these two queries into one query in the form:

Date | Increases | Decreases |
2013-08-22 | 1748 | 743 |
2013-08-23 | 1145 | 112 |
2013-08-24 | 1640 | 912 |

Any assistance with this query is highly appreciated.

È stato utile?

Soluzione

How 'bout using Union

Select Dates, Sum(Increases), Sum(Decreases)
from
{
    SELECT Dates, Count(closingbal) AS Increases, 0 as Decreases FROM tablename 
    WHERE closingbal >[ openingbal] GROUP BY Dates

    Union All

    SELECT Dates, 0 as Increase, Count(closingbal) AS Decreases FROM tablename 
    WHERE closingbal <[ openingbal]GROUP BY Dates 
}
GroupBy Dates
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top