I can't seem to find any information on this. I didn't think there was a limit but I can't get my code to work. It ignores one of the unions. Here is my code:
Can anyone see why it would be ignoring the second union. I've been staring at this for too long. There must be something I'm missing.
Any help would be appreciated.
Thanks
BJ
Code:
select store.short_name as "Store",
store.store_id,
store.sort_id,
category.description as "Description",
sum(sale_item.selling_price) as sales,
sum(sale_item.cost) as costsales,
sum(sale_item.selling_price - sale_item.cost) as gp,
CASE WHEN(sum(sale_item.selling_price)) <> 0
THEN(((sum(sale_item.selling_price) - sum(sale_item.cost)))/ sum(sale_item.selling_price))*100
ELSE '0'
END AS gppercent
FROM store
join sale_item on store.store_id = sale_item.store_id
join category on sale_item.category_number = category.category_number AND sale_item.store_id = category.store_id
join sale on store.store_id = sale.store_id and
sale.transaction_number = sale_item.transaction_number and sale.date = sale_item.date
where store.store_id = 71 and
sale_item.date between '2006-07-01' and '2006-07-31' and
sale_item.refund_qty = 0 and
sale_item.selling_price <> 0 and
sale.is_voided = 'N' and
category.category_number not in('079998','329998','359998','369998')
group by 1,2,3,4
UNION ALL
select store.short_name as "Store",
store.store_id,
store.sort_id,
category.description as "Description",
sum(sale_item.price) as sales,
sum(sale_item.cost) as costsales,
sum(sale_item.price - sale_item.cost) as gp,
CASE WHEN(sum(sale_item.price)) <> 0
THEN(((sum(sale_item.price) - sum(sale_item.cost)))/ sum(sale_item.price))*100
ELSE '0'
END AS gppercent
FROM store
join sale_item on store.store_id = sale_item.store_id
join category on sale_item.category_number = category.category_number AND sale_item.store_id = category.store_id
join sale on store.store_id = sale.store_id and
sale.transaction_number = sale_item.transaction_number and sale.date = sale_item.date
where store.store_id = 71 and
sale_item.date between '2006-07-01' and '2006-07-31' and
sale_item.selling_price = 0 and
sale_item.refund_qty = 0 and
sale.is_voided = 'N' and
category.category_number not in('079998','329998','359998','369998')
group by 1,2,3,4
UNION
select store.short_name as "Store",
store.store_id,
store.sort_id,
category.description as "Description",
sum((sale_item.selling_price + sale.adjust1)*sale_item.qty_sold) as sales,
sum(sale_item.cost) as costsales,
sum(((sale_item.selling_price + sale.adjust1)*sale_item.qty_sold) - sale_item.cost) as gp,
CASE WHEN(sum(sale_item.selling_price + sale.adjust1)) <> 0
THEN((sum((sale_item.selling_price + sale.adjust1)*sale_item.qty_sold) - sum(sale_item.cost))/ sum((sale_item.selling_price + sale.adjust1)*sale_item.qty_sold))*100
ELSE '0'
END AS gppercent
FROM store
join sale_item on store.store_id = sale_item.store_id
join category on sale_item.category_number = category.category_number AND sale_item.store_id = category.store_id
join sale on store.store_id = sale.store_id and
sale.transaction_number = sale_item.transaction_number and sale.date = sale_item.date
where store.store_id = 71 and
sale_item.date between '2006-07-01' and '2006-07-31' and
sale_item.refund_qty = 0 and
sale.is_voided = 'N' and
category.category_number in('079998','329998','359998','369998')
group by 1,2,3,4
order by 3,6 desc
Can anyone see why it would be ignoring the second union. I've been staring at this for too long. There must be something I'm missing.
Any help would be appreciated.
Thanks
BJ