[thelist] Complex SQL query
Jay Blanchard
jay.blanchard at niicommunications.com
Wed May 29 14:23:00 CDT 2002
I have the following table;
+----+--------------+------------+--------+
| id | wtn | dueDate | ageAmt |
+----+--------------+------------+--------+
| 1 | 210-212-3871 | 2001-12-01 | 0.51 |
| 2 | 210-212-3871 | 2001-12-01 | 0.49 |
| 3 | 210-212-3871 | 2001-12-01 | 0.50 |
| 4 | 210-212-3871 | 2002-01-01 | 1.32 |
| 5 | 210-212-3871 | 2002-01-01 | 0.02 |
| 6 | 210-212-3871 | 2002-01-01 | 3.54 |
| 7 | 210-212-3871 | 2002-02-01 | 39.95 |
| 8 | 210-212-3871 | 2002-02-01 | 0.03 |
| 9 | 210-212-3871 | 2002-03-01 | 2.72 |
| 10 | 210-212-3871 | 2002-03-01 | 0.51 |
+----+--------------+------------+--------+
where I would like to to this;
select wtn,
sum(if(dueDate between 2001-12-01 and 2001-12-30, ageAmt, 0)) AS "+150",
sum(if(dueDate between 2002-01-01 and 2001-01-30, ageAmt, 0)) AS "+120",
sum(if(dueDate between 2002-02-01 and 2001-02-28, ageAmt, 0)) AS "+90",
sum(if(dueDate between 2002-03-01 and 2001-03-30, ageAmt, 0)) AS "+60",
sum(if(dueDate between 2002-04-01 and 2001-04-30, ageAmt, 0)) AS "+30"
from tblAcctRec
group by wtn;
it returns;
+--------------+------+------+------+------+------+
| wtn | +150 | +120 | +90 | +60 | +30 |
+--------------+------+------+------+------+------+
| 210-212-3871 | 0.00 | 0.00 | 0.00 | 0.00 | 0.00 |
+--------------+------+------+------+------+------+
which is not right. Can anyone see what I'm doing wrong here?
Thanks!
Jay
More information about the thelist
mailing list