I need to fix the following SQL query:
SELECT sep.name AS company_name,
atnp.name AS employee_name,
(Case WHEN ip.id_item = 0001 THEN 'Product A'
ELSE 'Product B'
END ) AS product_name,
(Case WHEN ivcp.id_item = 0001 THEN SUM(ivcp.quantity)
ELSE NULL
END ) AS quantityA,
(Case WHEN ivcp.id_item = 0002 THEN SUM(ivcp.quantity)
ELSE NULL
END ) AS quantityB
FROM sale_item AS ivcp
INNER JOIN stock AS mep ON (mep.id_stock = ivcp.id_stock)
INNER JOIN sale AS vcp ON (vcp.id_sale = ivcp.id_sale)
INNER JOIN terminal_sale AS mvtp ON (mvtp.id_terminal_sale = vcp.id_terminal_sale)
INNER JOIN item AS ip ON (ivcp.id_item = ip.id_item)
INNER JOIN item_category AS cip ON (ip.id_category_item = cip.id_category_item)
LEFT OUTER JOIN employee AS atnp ON (atnp.id_employee = ivcp.id_employee)
INNER JOIN sis_company AS sep ON (sep.id_company = mvtp.id_company)
WHERE mvtp.id_company IN (1)
AND mvtp.date_sale BETWEEN :BeginDate AND :EndDate
AND DATE(ivcp.date_sale) BETWEEN :BeginDate AND :EndDate
AND DATE(vcp.data_rec) BETWEEN :BeginDate AND :EndDate
AND mep.date_sale BETWEEN :BeginDate AND :EndDate
AND (-1 = -1 OR ivcp.id_employee IN
(SELECT id_employee FROM team_employee WHERE id_employee = -1))
AND ip.id_category_item = 100
AND ivcp.id_item IN (0002,0001)
AND (ivcp.cancel = 'N' AND vcp.cancel = 'N')
GROUP BY sep.name,
atnp.name,
ip.id_item,
ivcp.id_item
ORDER BY sep.name
Results:
COMPANY | EMPLOYEE | PRODUCT NAME | QUANTITY A | QUANTITY B
Company A | Employee A | Product A | 1.64 | NULL
Company A | Employee A | Product B | NULL | 4.70
Company A | Employee B | Product A | 84.191 | NULL
Company A | Employee B | Product B | NULL | 91.885
But I want this:
COMPANY | EMPLOYEE | QUANTITY A | QUANTITY B
Company A | Employee A | 1.64 | 4.70
Company A | Employee B | 84.191 | 91.885
This query objective is to order some employees sales into a rank of best sellers. The rank will be ordered by the 'product A' and 'product B' sales (ivcp.quantity, id_item = 0001 and id_item = 0002).
The inner joins are used to match the sales date, item category, employees ids, stock, etc.
I'm working with a Stimulsoft based program to design the report layout, but Im not very used to it. So if it's possible to solve the problem within the query, great!