sql query doesn't give me all the months - mysql

I have a "customers" table with many columns. I need to use only two of them. date(the date that the customer was registerd), referrer( how did the customer find us: google or facebook or friends). I need for each month to show how many customers added by each referrer. for example: 2016-8 4 customers by facebook, 10 customers by friends and 13 customers by google). of course it should give me for each month the result in the example.
I tried this query - but it doesn't give me all the months only 2 of the months and one of them is being shown twice. can someone please review my query?
SELECT referrer,date, COUNT(`date`)
FROM `customers`
GROUP BY `referrer`;

Try this:
SELECT referrer,date, COUNT(date)
FROM customers
GROUP BY Year(date), MONTH(date), referrer;
If you want to select the output for a certain year you can try this:
SELECT referrer,date, COUNT(date)
FROM customers
WHERE record_date.YEAR = 2016
GROUP BY Year(date), MONTH(date), referrer;

Related

related to query using SQL

In oracle sql, how to get the count of newly added customers only for the month of april and may and make sure they werent there in the previous months
SELECT CUSTOMER ID , COUNT(*)
FROM TABLE
WHERE DATE BETWEEN '1-APR-2018' AND '31-MAY-2018' AND ...
If we give max (date) and min(date), we can compare the greater date to check if this customer is new , correct?
expected output is month count
april ---
may ---
should show the exact count how many new customers joined in these two months
One approach is to use aggregation:
select customer_id, min(date) as min_date
from t
group by customer_id
having min(date) >= date '2018-04-01 and
min(date) < date '2018-06-01';
This gets the list of customers (which your query seems to be doing). To get the count, just use count(*) and make this a subquery.

Select from 2 tables only the new entries in table 2

I got 2 tables, Customers and Payment. I'm trying to select only the new customers that have payments in the specified month and year, and no previous payments in another month.
table Customer
id - name
table Payment
id - id_customer - month - year - amount
SELECT * FROM customer, payment
WHERE Customer.id = Payment.id_customer
AND month = '$month'
AND year = '$year'
That gets me all the payments in a specific month and year, but I don't know how to exclude all the customers that had other previous payments.
Thank you for your time.
I don't think that you could achieve this without a third table. What you can do is create a third table with all the ids that you have selected in query and update it every time you run a select query.
Then the below query might work:
SELECT * FROM customer c, payment p WHERE c.id = p.id_customer
AND month = '$month'AND year = '$year'AND p.id NOT IN (SELECT id FROM
third_table)
Hope it answers your question.
To get the first date of payment, use GROUP BY. But, you will have to convert the value to something like a date first:
SELECT p.id_customer, MIN(CONCAT_WS, '-', p.year, p.month)) as first_yyyymm
FROM payment p
GROUP BY p.id_customer;
You should store the payment date as a date.

MYSQL - SUM of a column based on common value in other column

I'm stuck on crafting a MySQL query to solve a problem. I'm trying to iterate through a list of "sales" where I'm trying to sort the Customer IDs listed by their total accumulated spend.
|Customer ID| Purchase price|
10 |1000
10 |1010
20 |2111
42 |9954
10 |9871
42 |6121
How would I iterate through the table where I sum up purchase price where the customer ID is the same?
Expecting a result like:
Customer ID|Purchase Total
10 |11881
20 |2111
42 |16075
I got to: select Customer ID, sum(PurchasePrice) as PurchaseTotal from sales where CustomerID=(select distinct(CustomerID) from sales) order by PurchaseTotal asc;
But it's not working because it doesn't iterate through the CustomerIDs, it just wants the single result value...
You need to GROUP BY your customer id:
SELECT CustomerID, SUM(PurchasePrice) AS PurchaseTotal
FROM sales
GROUP BY CustomerID;
Select CustomerID, sum(PurchasePrice) as PurchaseTotal FROM sales GROUP BY CustomerID ORDER BY PurchaseTotal ASC;
Just by having a little Google search, I managed to find a page doing exactly what you're doing (I think). I have tailored the query below to fit your circumstance.
SELECT CustomerID, SUM(PurchasePrice) AS PurchaseTotal
FROM sales
GROUP BY CustomerID
ORDER BY PurchaseTotal ASC
Link to Page with Tutorial on SQL Groups

join results of two mysql queries on the same table

I have the intuition that I'm missing something simple, so please excuse me if it's a stupid question but I haven't been able to find an answer here.
I'm treating a database with usage behaviors. We have one row per user, with date and time spent (plus other non-relevant info).
I'd like to output a histogram of the number of visits per day, and number of visits that lasted more than a certain time ; ideally I'd like to have that in one query.
For now I have these two queries:
SELECT DATE(date), COUNT(date) AS Number_of_users FROM users GROUP BY DATE(date)
SELECT DATE(date), COUNT(date) AS Number_of_stayers FROM users WHERE timespent>5 GROUP BY DATE(date)
How can I combine them to obtain a result in the form of:
date users stayers
2014-01-01 21 5
2014-01-02 13 0
etc.
Thanks in advance for any help!
You can try using IF, like this:
SELECT DATE(date),
COUNT(date) AS Number_of_users,
SUM(IF(timespent>5,1,0)) AS Number_of_stayers
FROM users
GROUP BY DATE(date)
This should work, or at least show the basic idea of using JOINs:
SELECT DATE(a.date),
COUNT(a.date) AS Number_of_users,
COUNT(b.date) AS Number_of_stayers
FROM users a
LEFT JOIN users b ON (a.date = b.date AND b.timespent>5)

query for succesive customer orders

How do I write a query to display the cust_id and cust_name_last for each customer who had orders in two successive months in the current year. (successive meaning they follow each other 'may, june')
for example: customer 3 has orders in May and June of this year.
select cust_id, cust_name_last
from customer
where date_sub (order_date, interval 1 month)
and date_sub (order_date, interval 2 months)
"I just want to know how to find customers with orders in consecutive months in a year"
Could you try this?
SELECT DISTINCT month1.cust_id, month1.cust_name_last
FROM customer month1 INNER JOIN customer month2
ON month1.cust_id = month2.cust_id
AND YEAR(month2.order_date) = YEAR(month1.order_date)
AND MONTH(month2.order_date) - MONTH(month1.order_date) = 1;
If you want to find consecutive orders including another years (e.g 2013-12 => 2014-01), need to check overflows something like as follows
SELECT DISTINCT month1.cust_id, month1.cust_name_last
FROM customer month1 INNER JOIN customer month2
ON month1.cust_id = month2.cust_id
AND (YEAR(month2.order_date) - YEAR(month1.order_date)) * 12 + (MONTH(month2.order_date) - MONTH(month1.order_date)) = 1;
If preceding SQL does not work for you, We are highly appreciated when you post your schema and sample data on sqlFiddle http://www.sqlfiddle.com/.