I'm new to SQL so this is probably why there is an issue with my query but my problem is that I run the below query and end up with many duplicates on my table.
Both tables have a list of items with the same stock numbers which is why I'm joining the stock number. The issue is that I am getting duplication in my list, same stock number but one row has turnover and days on hire, the other has the rest of the information from the second select minus turnover and days on hire.
Any help would be appreciated!
(SELECT NULL AS 'ITEM_ID',
stock_items.stock_number AS 'STOCK NUMBER',
SUM(stock_assign.turnover) AS 'TURNOVER',
SUM(stock_assign.days_on_hire) AS 'DAYS ON HIRE',
NULL AS ' CAT',
NULL AS 'ITEM NAME',
NULL AS 'DEPOT LOC',
NULL AS 'DISPOSAL DATE',
NULL AS 'DISPOSAL TYPE',
NULL AS 'DATE CREATED',
NULL AS 'CREATED BY',
NULL AS 'COST PRICE'
FROM stock_assign
left join stock_items
ON stock_assign.stock_number = stock_items.stock_number
left join depots
ON depot_id = depots.dpt_id
GROUP BY stock_items.stock_number,
depots.dpt_name)
UNION
(SELECT stock_items.item_id AS 'ITEM_ID',
stock_items.stock_number AS 'STOCK NUMBER',
NULL AS 'TURNOVER',
NULL AS 'DAYS ON HIRE',
stock_items.equipment_class AS ' CAT',
stock_items.name AS 'ITEM NAME',
depots.dpt_name AS 'DEPOT LOC',
stock_items.disposal_date AS 'DISPOSAL DATE',
stock_items.disposal_type AS 'DISPOSAL TYPE',
stock_items.date_available AS 'DATE CREATED',
users.usr_name AS 'CREATED BY',
fixed_assets.cost_amount AS 'COST PRICE'
FROM stock_items
left join users
ON users.usr_id = stock_items.userid_created
left join depots
ON stock_items.depot_id_located_at = depots.dpt_id
left join fixed_assets
ON stock_items.item_id = fixed_assets.stock_item_id)
ORDER BY 'STOCK NUMBER' ASC