0

I have a problem with a mysql query and hope someone to help me.

I have the following tables:

CREATE TABLE USER
(
ID VARCHAR(80) NOT NULL, 
REGISTRATION_DATE DATE NOT NULL,
..... 
PRIMARY KEY(ID)
);

CREATE TABLE INVOICES
(
USER_ID VARCHAR(80) NOT NULL,
INVOICE_NUMBER VARCHAR(50) NOT NULL,
CREATION_DATE DATE NOT NULL,
........
PRIMARY KEY(USER_ID, INVOICE_NUMBER),
FOREIGN KEY(USER_ID) REFERENCES USER(ID)
);

I need following functionality: I choose start and end date (period of time) e.g. 01.02.2013-31.05.2013 as parameters for the query and want to see:

  1. Total count of users for each month from the chosen period (! Not the count per month, but the the total count of users up to (and including) the month.).
  2. Count of users who created at least one invoice in this month, again for each month.
  3. Count of invoices for each month (here the count per month).

Example: Period: 01.02.2013-31.05.2013 Resultset:

Month | Year | Total count of users | Count of users who created at least one invoice in this month | Count of invoices in this month
Febr  |  2013  |  20  |  12  |  45
March |  2013  |  20  |  4   |  22
April |  2013  |  23  |  20  |  30
May   |  2013  |  28  |  27  |  69

Is it possible to write such query? I have tried this:

SELECT YEAR, MONTH, @VAR := @VAR + TOTS.COUNT AS USERS_TOTAL
FROM (
    SELECT
        YEAR(REGISTRATION_DATE) AS YEAR,
        MONTH(REGISTRATION_DATE) AS MONTH,
        COUNT(ID) AS COUNT
    FROM USER
WHERE REGISTRATION_DATE <= '2013-05-31'
    GROUP BY YEAR, MONTH
) AS TOTS, (SELECT @VAR := 0) AS INC

I get the total number of users but only if there are some new registered users. I mean, if total count of users in Ferb. and in March is 20, in April 21, I get Febr. - 20, no information for March and April 21, I need information for each month from the choosen period.

With this query I get the count of users who created at least one invoice but again if the count is the same as the count in the previous months - no information:

SELECT YEAR(CREATION_DATE) as YEAR, MONTH(CREATION_DATE) as MONTH,
 COUNT(DISTINCT CUSTOMER_ID) 
FROM INVOICES
WHERE CREATION_DATE >= '2013-02-01' AND CREATION_DATE <='2013-05-31'
GROUP BY YEAR, MONTH

With this query I get the count of invoices for each month in the choosen period:

SELECT YEAR(CREATION_DATE) as YEAR, MONTH(CREATION_DATE) as MONTH, COUNT(INVOICE_NUMBER) 
FROM INVOICES
WHERE CREATION_DATE >= '2013-02-01' AND CREATION_DATE <='2013-05-31'
GROUP BY YEAR, MONTH

I want to make this work in a single query and as I said, want to have information for each month, even if the information is the same as in the previous month. How could I achieve the result I want, any ideas?

1 Answer 1

0

generating each of them separately is very simple. But if you want to put them in a single query this could be messy. it might be better to get them done in separate steps. But here is my attempt

SELECT 
       extract(MONTH FROM i.date) AS MONTH, 
       extract(YEAR FROM i.date) AS YEAR, count(i.*) AS Count_Invoices, 
       count(user.*) as count_users, 
      CASE (WHERE i.* IS NOT NULL COUNT u.id END) AS active_users_this_month

FROM 
      invoices I RIGHT JOIN users u

GROUP BY 
      Year, Month
3
  • Thank you! But I'm afraid, it doesn't work. Instead of "date", it should be "creation_date" and instead of "I" it should be "i". After correcting this, it doesn't work again. The error I'm getting is: "Error in SQL syntax near ') AS Count_Invoices, count(user.) as count_users, CASE (WHE' at line 3" May 29, 2013 at 15:21
  • kk lemme think again ... do you have some sample data i can play with? May 29, 2013 at 17:17
  • You can test with some test data, that's not so important. May 30, 2013 at 5:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.