I have a display table that churns out daily result in the form of mysql. The daily result does not include products that are not making any sales today. How do i read from the table and determine to list all products even if a particular product is not making a sale that particular day but exists on the table on another day.
I researched and understand that using EXIST
on the WHERE
clause works. But i tried but to no avail. I think I just have to include a grouped sub query inside a query but where should i put it. Below is an example of my code
SELECT
transaction.transactionservicetype AS Services,
COUNT(transaction.transactionid) AS Count,
IFNULL (SUM(transaction.transactionamount),'0') AS Amount,
IFNULL (SUM(statement.statementdebit),'0') AS NetCost,
IFNULL((SUM(transaction.transactionamount) - SUM(statement.statementdebit)),'0') as TotalEarning
FROM transaction
RIGHT JOIN statement ON transaction.transactionid = statement.transactionid
WHERE transaction.transactiondate = '2019-04-03' AND transaction.transactionstatus = 'SUCCESS'
GROUP BY `transaction`.transactionservicetype ASC
Instead of displaying the table such as this:
Services Count Amount Netcost Total Earning
Chicken 4 5.30 5.14 -
Beef 3 3.30 3.13 -
I want the result to include products not found on the same day but determine another type of products had existed on the table thus displaying the result as such:
Services Count Amount Netcost Total Earning
Chicken 4 5.30 5.14 -
Beef 3 3.30 3.13 -
Venison 0 0 0 -
Fowl 0 0 0 -
Update:
I did not get the correct outcome using anti join and the other results. Scanning thru and using Ultimater's code as an example, this is as close to what i'd like to get from the result using sql query:
(
SELECT
transaction.transactionservicetype AS Services,
COUNT(transaction.transactionid) AS Count,
IFNULL (SUM(transaction.transactionamount),'0') AS Amount,
IFNULL (SUM(statement.statementdebit),'0') AS NetCost,
IFNULL((SUM(transaction.transactionamount) - SUM(statement.statementdebit)),'0') as TotalEarning
FROM
transaction RIGHT JOIN statement
ON
transaction.transactionid = statement.transactionid
WHERE
transaction.transactiondate = '2019-04-03' AND transaction.transactionstatus = 'SUCCESS'
GROUP BY
`transaction`.transactionservicetype ASC
)
UNION
(
SELECT
transactionservicetype AS Services,
'0' AS Count,
'0' AS Amount,
'0' AS NetCost,
'0' AS TotalEarning
FROM
transaction
GROUP BY
transactionservicetype ASC
)
The above sql query got me a bit closer to what I wanted from my outcome. And this is the result:
Services Count Amount Netcost Total Earning
Chicken 4 5.30 5.14 -
Beef 3 3.30 3.13 -
Chicken 0 0 0 -
Beef 0 0 0 -
Venison 0 0 0 -
Fowl 0 0 0 -
I just have to remove the duplicated rows (Chicken, Beef)
how do i fix it using sql query?