I have several records I'd like to get a kind of boolean result from.
Here is an example of table to request:
Code:
+------------+--------+
| date | status |
+------------+--------+
| 2005-10-01 | 1 |
| 2005-10-01 | 1 |
| 2005-10-02 | 3 |
| 2005-10-02 | 0 |
| 2005-10-03 | 0 |
| 2005-10-03 | 1 |
| 2005-10-03 | 2 |
| 2005-10-04 | 1 |
| 2005-10-06 | 3 |
+------------+--------+
I would like to get the following values matching the following conditions for each distinct date:
(- condition -> wanted value)
- if status is strictly full 1 -> 1
- if status is strictly full 2 -> 2
- if status is at least one 3 -> 3
- if status is at least one 1 and at least one 2 -> 3
- else -> 0
In our example we will get this result
Code:
+------------+--------+
| date | value |
+------------+--------+
| 2005-10-01 | 1 |
| 2005-10-02 | 3 |
| 2005-10-03 | 3 |
| 2005-10-04 | 1 |
| 2005-10-06 | 3 |
+------------+--------+
I would start like that
Code:
SELECT
tbl.date AS "Date",
CASE
WHEN AND(tbl.status) = 1 THEN 1
WHEN AND(tbl.status) = 2 THEN 2
WHEN OR(tbl.status) = 3 THEN 3
WHEN (AND(tbl.status) = 1 OR AND(tbl.status) = 2) THEN 3
ELSE 0
END
AS "Value"
FROM
tbl
GROUP BY
tbl.date
AND() and OR() are functions of my imagination, I don't know if they exist such function and I'm not sure to be on the right direction.
I hope you understand my problem I don't know how explain another way