I have the following payments
table:
ID |Jan |Feb |Mar |Apr |May |Jun
1 | |PAID |PAID | |PAID |
2 | |PAID | | | |PAID
3 |PAID | |PAID |PAID |PAID |PAID
4 |PAID |PAID | |PAID |PAID |
5 |PAID | |PAID | | |
6 | | | | | |PAID
I would like to check for each ID how many months are NOT paid.
For example, column wise, I can do the following:
SELECT ID FROM payments WHERE Jan IS NULL OR Jan = ''
I can also count the occurrences of Null string or PAID string in each column as follow:
SELECT Jan COUNT(*) FROM payments GROUP BY MyTable;
However, I want to to do the same but base on rows not columns. And with a little tweak. I want to count, for instance, the occurrences of empty string in a row taking into account the beginning of the year until the current month. For example, if this month is February, then for ID 1, it should count 1. Then if the current-month is April, it should count 2. If it June, it should count 3, something like:
SELECT occurrences of '' WHERE ID = 1 AND current-month = Apr
Is it possible ?! if so, how ?
P.S.: I am implementing these selection statements on Qt for a c++ application. I am displaying the results of the select statements on QTableView
using QSqlQueryModel
.
Aucun commentaire:
Enregistrer un commentaire