1

I have written a mysql query that is getting data from two different tables stock_list and selected_items using union.From stock_list I am getting SUM(qty),SUM(weight).From selected_items I am getting COUNT( barcode ) and SUM( weight ) both based on a specific date.And the last query in the union returns the total SUM(qty),SUM(weight) from stock_list. This is the query I am using.

SELECT SUM( qty ) AS StockSum, SUM( weight ) AS Stockweight
FROM  `stock_list` 
WHERE DATE LIKE  '08-Jan-2016'
UNION SELECT COUNT( barcode ) AS BilledItems, SUM( weight ) AS Billedweight
FROM  `selected_items` 
WHERE DATE LIKE  '08-Jan-2016'
UNION SELECT SUM( qty ) AS TotalStock, SUM( weight ) AS TotalWeight
FROM  `stock_list`;

The problem is everything gets displayed in two columns StockSum and Stockweight.I would like to have 6 separate columns plus I would like to add an extra column for date at the beginning.

This is my current output.

enter image description here

This is my desired result that I would like to achieve.enter image description here

This is my selected_items table. enter image description here

This is my stock_list tableenter image description here

Dan Guzman
  • 43,250
  • 3
  • 46
  • 71
  • 2
    Union unites sets of rows, so yes, you get the new data appended to the rows. To get data added to the columns, use JOINing. I can't give you a full answer without a bit more info: - where is the date from? - how do you match the stock_list and selected_items? - total items is not the sum of all the qty column (if it was, it would remain the same), how is it filtered? – boisvert Jan 19 '16 at 12:41
  • 1
    @boisvert I have added my database table images can you please check.Both tables contains date but when I use it I am not getting correct values. This is the query I used prev to this one SELECT a.date,SUM( a.qty ),SUM( a.weight ),COUNT( b.barcode ),SUM( b.weight ) FROM stock_list a, selected_items b WHERE a.date = b.date;Thank you –  Jan 19 '16 at 13:07
  • Thanks for the edit @DanGuzman –  Jan 20 '16 at 05:24

1 Answers1

1

This is how to get the first 4 columns I think:

SELECT date,
       SUM( qty ) AS StockSum,
       SUM( stockW ) AS Stockweight,
       COUNT( barcode ) AS BilledItems,
       SUM( billedW ) AS Billedweight
FROM ( SELECT
          date,
          qty, weight AS stockW,
          null AS barcode, null AS billedW
       FROM stock_list
       UNION
       SELECT
          date,
          null AS qty, null AS stockW,
          barcode, weight AS billedW
       FROM selected_items
     )
GROUP BY date
ORDER BY date

For the final two columns, I think you want a running total of the stock up that date, for each date, and to program that, I'd use a PHP process rather than SQL.

boisvert
  • 3,679
  • 2
  • 27
  • 53
  • I get an error #1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'outer join selected_items ON stock_list.date = selected_items.date GROUP BY COAL' at line 7 –  Jan 20 '16 at 05:30
  • I used join instead of full outer join and I get wrong output date 08-Jan-2016 StockSum 216 Stockweight 424.32000000000005 BilledItems24 Billedweight 504 –  Jan 20 '16 at 06:39
  • I should think... Full outer joins don't work in mysql, you need a union of left and right outer joins, see http://stackoverflow.com/questions/4796872/full-outer-join-in-mysql - I'll edit my answer – boisvert Jan 20 '16 at 20:27
  • correction done, the updated solution is also simpler to follow through. If you're confused how it works, or need a modified version, I suggest you try the subquery separately, then the whole thing. – boisvert Jan 20 '16 at 20:50