I have the following three queries:
SELECT NAME,
ABS(sum(UNITS)) as WK1
FROM products
inner join stockdiary on stockdiary.PRODUCT = products.ID
where SUB_CATEGORY='LB40'
and DATENEW BETWEEN DATE_SUB( CURDATE( ) ,INTERVAL 7 DAY ) AND CURDATE( )
and REASON = (-1)
group by NAME
order by CATEGORY;
SELECT NAME,
ABS(sum(UNITS)) as WK2
FROM products
inner join stockdiary on stockdiary.PRODUCT = products.ID
where SUB_CATEGORY='LB40'
and DATENEW between curdate() - interval 14 day and curdate() - interval 8 day
and REASON = (-1)
group by NAME
order by CATEGORY;
SELECT NAME,
ABS(sum(UNITS)) as wk3
FROM products
inner join stockdiary on stockdiary.PRODUCT = products.ID
where SUB_CATEGORY='LB40'
and DATENEW between curdate() - interval 21 day and curdate() - interval 15 day
and REASON = (-1)
group by NAME
order by CATEGORY;
Sample output from one of the queries:
NAME | WK2 |
---|---|
Longbeach 40/160 Original Ctn | 2 |
Longbeach 40/160 Menthol Fresh Ctn | 1 |
Longbeach 40 Fine Flavour Pkt | 9 |
Longbeach 40 Rich Flavour Pkt | 9 |
Longbeach 40 Menthol Fresh Pkt | 5 |
Longbeach 40 Menthol Pkt | 6 |
Longbeach 40 Select Pkt | 5 |
How can I join the queries to get the following results?
NAME | WK1 | WK2 | WK3 |
---|---|---|---|
Longbeach 40/160 Original Ctn | 2 | 3 | 4 |
... | ... | ... | ... |
The products
table contains the following columns: ID
, NAME
, SUB_CATEGORY
.
The stockckdiary
table contains the following columns: PRODUCT
, DATENEW
, UNITS
.