I've a table in which write every subscription sold of my magazine, like this:
USERID | DATE |
31 | 2011-09-22 |
54 | 2011-09-22 |
59 | 2011-09-23 |
11 | 2011-10-02 |
88 | 2011-10-05 |
31 | 2011-10-06 |
17 | 2011-10-12 |
54 | 2011-10-15 |
31 | 2011-11-05 |
54 | 2011-11-12 |
Now, for statistical purpose, i need to having an outcome in which i see, for every single month, how many subscriptions i've sold and how many users have already bought once this.
For instance, if we look the datas on top, i should have an outcome likie this:
DATE | SOLD | RENEWAL
09 | 3 | 0
10 | 5 | 1
11 | 2 | 2
I can to group the subscriptions sold monthly, but i can't add the "renewal" info.
SELECT COUNT( * ) AS sold, MONTH(date) FROM table_sold WHERE date >= CURDATE() - INTERVAL 13 MONTH GROUP BY YEAR( date ) , MONTH( date ) ORDER BY date ASC LIMIT 1,15
In this way i only have an outcome like this:
DATE | SOLD
09 | 3
10 | 5
11 | 2
I've tried several option, with subquery, union and so on, but without successful.
There is to consider that the table has 70.000 entries and a query with a hard scan could be a problem.
In your opinion is there a way in mysql or i've to take the idea to make it in php?
Here is an idea. Look at the first date for each user. Then count renewals in every month that is not the first date:
select year(date), month(date), count(*) as sold,
sum(case when date <> firstdate then 1 else 0 end) as renewals
from subscriptions s join
(select userid, min(date) as firstdate
from subscriptions s
group by userid
) su
on s.userid = su.userid
group by year(date), month(date)
order by year(date), month(date);
Related
I'm trying to get Sale Data of every month of particular year, but I'm having a problem building a query for it.
Here is What I've tried
SELECT COUNT(`id`) AS `total_order`
FROM orders
WHERE date BETWEEN ? AND ?
GROUP BY `total_order`
HERE is How my table look like
----------------------------------------
| id | item_name | amount | time |
| 21 | item_1 | 10 | 1506675630 |
| 22 | item_2 | 30 | 1506675630 |
| 23 | item_3 | 70 | 1506675630 |
| 24 | item_4 | 100 | 1506675630 |
----------------------------------------
Now here is what i want from the query
1 - Total Sales amount made from the beginning of the year till today.
2 - Sales made Today
3 - Sales made in Last Month
4 - Sales Made in Last 3 month
5 - Sales Made in Last 6 Month
6 - Total Number of Sales made in every month of this particular year
for e.g -
January - 20
Feb -100
March - 200 & so on.
How can i achieve this complex query?
SELECT `id` AS `Order_Number`, item_name, SUM(Amount)
FROM orders
WHERE time >= '01/01/17'
GROUP BY date
That would give you your first result. Try the others and let me know what you get
This answers your first question
SELECT
DATE_FORMAT(FROM_UNIXTIME(time), '%Y-%m') as interval,
COUNT(*) AS sales_made,
SUM(amount) AS sales_amount
FROM orders
WHERE
time BETWEEN UNIX_TIMESTAMP('2017-01-01') AND UNIX_TIMESTAMP('2018-01-01')
GROUP BY 1
ORDER BY 1;
Here is what i think would work:
for the first 5 queries try something like this.
SELECT SUM(amount)
FROM orders
WHERE DATE_FORMAT(FROM_UNIXTIME(`orders.time`), '%Y-%m-%d') BETWEEN ? AND ?
And for the last one you'll need :
SELECT DATE_FORMAT(FROM_UNIXTIME(`orders.time`), '%Y-%m-%d') AS 'date', SUM(amount)
FROM orders
WHERE date between ? AND ?
GROUP BY DATE_FORMAT(date, '%Y%m')
You can try without the FROM_UNIXTIME if it doesnt work.
Assuming we have a table like below
id | name | userid | score | date |
------------------------------------------------------------
1 | john | 1 | 44 | 2013-03-2
2 | mary | 2 | 59 | 2013-03-5
3 | john | 1 | 38 | 2013-03-8
4 | elvis | 3 | 19 | 2013-03-10
5 | john | 1 | 100 | 2013-03-11
I want to select four winner of last week.week start Sunday to Sunday.
also i want to select one winner of a month.
winner have highest score.
i use this code for weekly winner
SELECT winner.id,winner.score FROM winner WHERE winner.date >= curdate() - INTERVAL DAYOFWEEK(curdate())+6 DAY
AND winner.date < curdate() - INTERVAL DAYOFWEEK(curdate())-1 DAY ORDER BY winner.score DESC
LIMIT 4
Also monthly winner
SELECT winner.id,winner.score FROM winner WHERE MONTH(CURDATE())= MONTH(winner.date) ORDER BY winner.score DESC
LIMIT 1
both are give wrong results
Try this
select id ,score
from winner
where DATE_FORMAT(date, '%Y-%m-%d') between date_sub(DATE_FORMAT(now(), '%Y-%m-%d'),INTERVAL 1 WEEK) and DATE_FORMAT(now(), '%Y-%m-%d')
ORDER BY score DESC
LIMIT 4
-------------------
SELECT id,score FROM winner
WHERE MONTH(CURDATE())= MONTH(date)
ORDER BY score DESC
LIMIT 1
Make sure that your example data will not work because they are 2013 !!
DEMO HERE
So I have this order table
| order_id | sales | order_date(timestamp) |
2 100 2013-10-30 04:07:01
3 300 2013-10-31 04:30:02
4 400 2014-01-10 08:07:01
and here is my query, $m1 and $m2 are from order_date
SELECT `order_id`, MONTH(`order_date`) AS month,
COUNT( `order_id`) AS orders_made,
SUM(`total`) AS sales, YEAR(`order_date`) AS YEAR
FROM `orders`
WHERE `order_date` BETWEEN ('$m1') AND ('$m2')
GROUP BY MONTH(`order_date`)
And this is what I got
| month | year | sales | orders_made |
10 2013 100 1
11 2014 400 1
My query does not return all the data that I wanted, what should be the query to display something like this
| month | year | sales | orders_made |
10 2013 400 2
11 2014 400 1
Need your help guys
SELECT YEAR(order_date),
MONTH(order_date),
COUNT(order_id) AS orders_made,
SUM(sales) AS sales
FROM orders
WHERE order_date BETWEEN ('$m1') AND ('$m2')
GROUP BY YEAR(order_date), MONTH(order_date)
I have a database with the following format:
myTable
productgroupID | productID | views | date
1 | 10 | 25 | 2013-05-23
4 | 105 | 15 | 2013-05-23
7 | 60 | 65 | 2013-05-23
7 | 60 | 55 | 2013-05-22
7 | 60 | 45 | 2013-05-21
Now I want to sum all views of a product in the moth May.
Result should be:
productgroupID | productID | viewed | month
7 | 60 | 165 | 2013-05-01
1 | 10 | 25 | 2013-05-01
4 | 105 | 15 | 2013-05-01
I tried the query below, but this gives me all views of a specific productgroupID. But I need the sum of the unique productgroupID & productID.
SELECT COUNT( views ) AS viewed, productgroupID FROM product_stats_daily GROUP BY productgroupID
If you want the views totaled, then you can use the sum() aggregate function and then you can group by the month and year for the date:
select productGroupId,
productId,
sum(views) viewed,
month(date) Month,
year(date) Year
from myTable
group by productGroupId, productId, month(date), year(date);
See SQL Fiddle with Demo
You could also use Date_Format to get the date in the format that you want:
select productGroupId,
productId,
sum(views) viewed,
DATE_FORMAT(date, '%Y-%m-01') date
from myTable
group by productGroupId, productId, DATE_FORMAT(date, '%Y-%m-01')
See SQL Fiddle with Demo
COUNT will count number of rows while SUM will sum up value of retrieved rows.
So your query becomes:
SELECT SUM( views ) AS viewed, productgroupID
FROM product_stats_daily
GROUP BY productgroupID
There's probably a more elegant way to force the date to the beginning of the month, but this should work:
SELECT
ProductGroupID,
ProductID,
SUM(views) AS viewed,
DATE_FORMAT(date, '%Y-%m-01') AS Month
FROM myTable
GROUP BY
ProductGroupID,
ProductID,
DATE_FORMAT(date, '%Y-%m-01')
I am using the following query,
SELECT
b.sales_id,b.category_id,a.bus_title,b.sales_title,b.sale_starts,b.sale_ends
FROM tbl_sales b INNER JOIN tbl_business a
ON a.bus_id=b.bus_id
WHERE b.active=1
AND CURDATE( ) < DATE_ADD(b.sale_ends, INTERVAL 14 DAY )
AND b.category_id='16' OR b.category_id
IN (SELECT cat_id FROM tb_category WHERE parent_id=16)
ORDER BY (b.sale_ends=CURDATE()) DESC,(b.sale_ends>CURDATE()) DESC,b.sale_ends ASC
and getting the result as shows below,
sales_id | category_id |bus_title | sales_title|sale_starts|sale_ends
----------|-------------|------------|------------|-----------|----------
36 | 17 | my bus | my sale |2012-04-03 |2012-05-03
35 | 19 | my bus 1 | my sale 1 |2012-04-03 |2012-05-03
42 | 16 | my bus 12 | my sale 12 |2012-04-05 |2012-05-05
10 | 17 | my bus 123| my sale 123|2011-12-15 |2011-12-18
I need to omit(not delete) sales after X amount of days past the End Date (here after 14 days). But the MySQL query i had written above returns the wrong results and displays the records with End Date 2011-12-18. How can i write the query in an effective way.
Need help. Thanks in advance.
Add this to your query :
AND sale_ends < DATE_SUB(NOW(), INTERVAL 14 day)