I use the following query to group work hours and costs for customers from three tables: one for customers, one for work hours and one for costs:
SELECT a.*,
COALESCE(b.totalCount, 0) AS CountWork,
COALESCE(b.totalAmount, 0) AS WorkTotal,
COALESCE(c.totalCount, 0) AS CountExpense,
COALESCE(c.totalAmount, 0) AS ExpenseTotal
FROM clients A
LEFT JOIN
(
SELECT Client,
COUNT(*) totalCount,
SUM(Amount) totalAmount
FROM work_times
WHERE DATE BETWEEN '2013-01-01' AND '2013-02-01'
GROUP BY Client
) b ON a.Client = b.Client
LEFT JOIN
(
SELECT Client,
COUNT(*) totalCount,
SUM(Amount) totalAmount
FROM expenses
WHERE DATE BETWEEN '2013-01-01' AND '2013-02-01'
GROUP BY Client
) c ON a.Client = c.Client
WHERE b.Client IS NOT NULL OR
c.Client IS NOT NULL
You can see the query running in the script here .
I am trying to modify this query so that for each client there is a row for each month, sorted by month, and then to the client. I am trying to do this with the following modified request:
SELECT a.*,
COALESCE(b.totalCount, 0) AS CountWork,
COALESCE(b.totalAmount, 0) AS WorkTotal,
COALESCE(c.totalCount, 0) AS CountExpense,
COALESCE(c.totalAmount, 0) AS ExpenseTotal
FROM clients A
LEFT JOIN
(
SELECT Client,
COUNT(*) totalCount,
SUM(Amount) totalAmount,
SUBSTR(Date, 1, 7) as Month
FROM work_times
GROUP BY Month,Client
ORDER BY Month
) b ON a.Client = b.Client
LEFT JOIN
(
SELECT Client,
COUNT(*) totalCount,
SUM(Amount) totalAmount,
SUBSTR(Date, 1, 7) as Month
FROM expenses
GROUP BY Month,Client
ORDER BY Month,Client
) c ON a.Client = c.Client
WHERE b.Client IS NOT NULL OR
c.Client IS NOT NULL
You can see the modified request in action here .
However, it does not work. For client B, only one row is returned, although in January 2013 there are working hours and expenses in February 2013 (therefore there should be 2 rows), and it seems that the rows are ordered by the client, and not a month. Can someone suggest how to change the query to get the desired result, which for example on the second fiddle will be:โโโโโโโโโโฆโโโโโโโโโโโโฆโโโโโโโโโโโโฆโโโโโโโโโโโโโโโฆโโโโโโโโโโโโโโโ
โ CLIENT โ COUNTWORK โ WORKTOTAL โ COUNTEXPENSE โ EXPENSETOTAL โ
โ โโโโโโโโโฌโโโโโโโโโโโโฌโโโโโโโโโโโโฌโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโฃ
โ A โ 1 โ 10 โ 1 โ 10 โ
โ B โ 1 โ 20 โ 0 โ 0 โ
โ A โ 1 โ 15 โ 0 โ 0 โ
โ B โ 0 โ 0 โ 1 โ 10 โ
โ C โ 1 โ 10 โ 0 โ 0 โ
โโโโโโโโโโฉโโโโโโโโโโโโฉโโโโโโโโโโโโฉโโโโโโโโโโโโโโโฉโโโโโโโโโโโโโโโ