1

I have tables that look like this:

ProductList
+------+----------------+--------------+----------+--------+-------+
|  id  | product_number | product_type | location | status | price |
+------+----------------+--------------+----------+--------+-------+
|  1   | A-01           | A001         | Nevada   | READY  | 5000  |
|  2   | B-02           | A002         | Texas    | STORED | 6000  |
|  3   | A-01           | A002         | Utah     | READY  | 6000  |
|  4   | A-02           | B003         | Utah     | READY  | 8000  |
|  5   | B-01           | A001         | Nevada   | STORED | 5000  |
+------+----------------+--------------+----------+--------+-------+

id is the primary key.

Supervisor
+------+-------+--------------+----------+-----------+--------+
|  id  | month | product_type | location | unit_sold | income |
+------+-------+--------------+----------+-----------+--------+
|  1   | 1     | A001         | Nevada   | 5         | 25000  |
|  2   | 3     | A002         | Texas    | 6         | 36000  |
|  3   | 1     | A002         | Utah     | NULL      | NULL   |
|  4   | 4     | B003         | Utah     | 4         | 32000  |
|  5   | 2     | A001         | Nevada   | 6         | 30000  |
+------+-------+--------------+----------+-----------+--------+

I was asked that the result should be like this table (this is actually my estimation) :

SalesTarget table

Location : Nevada

+------+----------------+--------+--------+---------------+---------------+
|      |     STATUS     |      TOTAL      |   1st MONTH   |   2nd MONTH   |
| TYPE | READY | STORED |  UNIT  | PRICE  | UNIT | INCOME | UNIT | INCOME |
+------+-------+--------+--------+--------+------+--------+------+--------+
| A001 |   6   |    8   |  14    |  70000 |  5   | 25000  |  6   | 30000  |
| A002 |   7   |    4   |  11    |  66000 |  3   | 39000  |  0   |   0    |
| B001 |   3   |    6   |  19    |  95000 |  0   |   0    |  0   |   0    |
| B002 |   5   |    5   |  10    | 100000 |  4   | 40000  |  6   | 60000  |
+------+-------+--------+--------+--------+------+--------+------+--------+

The table only shows data only from specific location. STATUS READY is a cumulative from unit that has the same type and location which has READY state and so for STATUS STORED. TOTAL is total unit / price of the same type in that Location.

Unit and income in SalesTarget table can be NULL and it will be entered by the user, but they are classified by Supervisor.month (and also, it's for 12th month). I join both table (ProductList and Supervisor) by product_type.

I tried using prepared statement query command like this, but it didn't give my exact result that I want.

SELECT

pl.product_type AS TYPE,
SUM(IF(pl.status = ? , 1, 0)) AS READY,
SUM(IF(pl.status = ? , 1, 0)) AS STORED,
SUM((IF((pl.status = ? OR pl.status = ? ), 1, 0))) AS UNIT_TOTAL,
SUM((IF((pl.status = ? OR pl.status = ? ), pl.price, 0))) AS PRICE_TOTAL,
IF(sv.month=1, sv.unit_sold, 0) AS UNIT1,
IF(sv.month=1, sv.income, 0) AS INCOME1,
IF(sv.month=2, sv.unit_sold, 0) AS UNIT2,
IF(sv.month=2, sv.income, 0) AS INCOME2

FROM ProductList pl
LEFT OUTER JOIN Supervisor sv ON pl.product_type = sv.product_type
WHERE pl.location = ?
GROUP BY pl.product_type
ORDER BY TYPE DESC

How can I do this?

4

0 に答える 0