I have a mysql table with sales information: Seller, product, sales date.
How do I write a query to generate a table that shows total products shown by each seller, per month, and in total?
This is the query I use to create a table with SellerName and total sales in July:
select SellerName, count(*) as july
from onlineDATA
where ReportMonth = 201207
GROUP BY SellerName
ORDER BY july DESC
LIMIT 0,30
How do I modify this to get additional sales totals for each month?
I am looking for output like this:
SellerName | Jun | Jul | Aug | YTD
John Doe | 30 | 25 | 30 | 85
Bonus question - how would I code this in PHP when the number of months would be a user input - anywhere from 1 to 12?
Thanks
I think you will have to define the columns separately, like this:
SELECT
SellerName,
SUM(IF(ReportMonth = 201206, 1.0)) AS Jun,
SUM(IF(ReportMonth = 201207, 1.0)) AS Jul,
SUM(IF(ReportMonth = 201208, 1.0)) AS Aug,
COUNT(*) AS YTD
FROM onlineDATA
GROUP BY SellerName
ORDER BY SellerName DESC LIMIT 30;
Related
I'm trying to create a query to select records that fit current year month range. Here is my database table:
id name date_start date_end
======================================
1 John 2016-05-20 2018-01-01
2 Peter 2017-02-02 2017-05-10
3 Mike 2015-02-02 2017-02-06
and my query:
SELECT * FROM users
WHERE YEAR(date_start) = 2017
AND MONTH(date_start) = 01
OR YEAR(date_end) = 2017
AND MONTH(date_end) = 01
So basically I need to select all users where current year/month in this case 2017/01 is within start/end column range.
In above query, only John and Mike records should be selected.
I think you just want to find out which users have the date 2017-01-01 within their ranges, defined by the date_start and date_end. If so, then the following query should work:
SELECT *
FROM users
WHERE date_start <= '2017-01-01' AND date_end >= '2017-01-01'
Demo here:
SQLFiddle
I need to make a query that selects from two different tables. Basically, I only want to select the rows from the dates table that have no pending orders in the orders table.
For example, the dates table has the values of July 1, July 2 and July 3. July 2 has orders with the status = PEN in the orders table so the table will only show July 1 and July 3.
Query 1 for dates:
$sql = "SELECT * FROM dates WHERE DATEDIFF(CURDATE(), date) >= 30 AND `30day`='No'";
I have yet to build a query for the orders table as I am sure this needs to be integrated into one query together, and I am not sure what to do.
I know you can do two SELECT queries in one, and I am aware of how to do this, but I am unsure of how to cause the second SELECT query to be affected by the first SELECT query.
dates database has columns id date closed 30day 60day
orders database has columns id date order status
I need this query to flag any orders with the statuses PEN BO FBO.
Thank you in advance!
Sample Data:
dates table:
Date - 30-day Value
July 1 - No
July 2 - No
July 3 - No
orders table:
Date - Orders - Status
July 1 - 7123456 - PEN
July 1 - 7123457 - SHI
July 1 - 7123487 - SHI
July 2 - 7256789 - SHI
July 2 - 7256790 - SHI
July 2 - 7256791 - SHI
July 3 - 7215368 - SHI
July 3 - 7125369 - SHI
July 3 - 7659876 - BO
July 4 - 7569235 - FBO
July 4 - 7986585 - FBO
Expected Result:
Date
July 2
July 3
Omitted Dates:
Date - Reason
July 1 - because there is an open order
July 4 - because there is an open order
I don't want an omitted table - just wanted to show what wouldn't show up.
could be this what you are looking for (i don't know your schema so for the join i have used a column named key
$sql = "SELECT *
FROM dates
LEFT JOIN orders on (dates.date = orders.date and orders.status not in ('PEN','BO', 'FBO'))
WHERE DATEDIFF(CURDATE(), dates.date) >= 30
AND `30day`='No'";
otherwise if you need that status is not PEN, BO and FBO then you can
..
$sql = "SELECT *
FROM dates
LEFT JOIN orders ON dates.date = orders.date
WHERE DATEDIFF(CURDATE(), dates.date) >= 30
AND `30day`='No'
AND orders.status NOT IN ('PEN','BO', 'FBO' ) ";
..
$sql = "SELECT *
FROM dates
INNER JOIN orders ON dates.date = orders.date
WHERE DATEDIFF(CURDATE(), dates.date) >= 30
AND `30day`='No'
AND orders.status IN ('PEN','BO', 'FBO' ) ";
based on the sample provided this should return the rows you need
$sql = " SELECT *
FROM dates
WHERE DATEDIFF(CURDATE(), date) >= 30
AND `30day`='No'
AND date not in (
select date
from orders
where order.status IN ('PEN','BO', 'FBO' )
) ";
I have a visit table with user_id and visited_at columns. I would like to find how many are new and repeat customer in this month.
user_id visited_at
--------------------------------------------------------------------------
1750 2015-04-06 10:39:20
1870 2015-04-05 15:48:11
1990 2015-04-04 12:38:35
1920 2015-04-03 10:18:21
1080 2015-04-01 10:18:21
1750 2015-01-28 12:38:59
1920 2015-01-19 17:20:20
1920 2015-01-17 15:10:10
1080 2015-01-13 20:18:41
1920 2014-04-04 10:31:15
1750 2013-10-04 10:39:20
In January 2015, user 1750 and 1920 visited the same place so total repeated customers are 2. In April 2015, user 1750, 1920 and 1080 visited the same place so total repeated customers are 3. The output should be something like this
Month New Repeat
----------------------------------------------
October 2013 1 0
April 2014 1 0
January 2015 1 2
April 2015 2 3
One approach is to get the date of the first visit for each user using a subquery. Then join in this information, and use count(distinct) to count the number of users:
select year(v.visited_at) as yyyy, month(visited_at) as mm,
count(distinct user_id) as num_users,
count(case when v.visited_at = vv.minva then user_id end) as num_new_users
from visits v join
(select user_id, min(visited_at) as minva
from visits t
group by user_id
) vv
on v.user_id = vv.user_id
group by year(v.visited_at), month(visited_at)
order by year(v.visited_at), month(visited_at);
I note that this gives the total and new users; the repeats are the difference.
Something like this will work:
SELECT * FROM table_name WHERE updated_at >= CAST('2014-02-01' AS DATE) AND updated_at <= CAST('2014-02-28' AS DATE);
SELECT
MONTH(created_at) as _Month,
YEAR(created_at) as _Year,
COUNT(*) as New
FROM
yourtable
GROUP BY 0,1
You can also filter results using where clause.
I am trying to fetch daily customer flow from table 'tbl_transaction' using the following query:
SELECT DAY(date_time) AS date, SUM(members) AS customers FROM tbl_transaction WHERE MONTH(date_time)='.$mon.' GROUP BY "date_time"
But it is generating this dataset which is not grouped according to date:
date | customers
11 3
12 2
13 1
14 2
14 3
15 7
16 4
17 3
17 2
17 7
17 2
18 5
18 5
18 4
19 2
How to show the dates and no of customers as on particular date.
Your query should look like
SELECT DAY(date_time) AS date,
SUM(members) AS customers
FROM tbl_transaction
WHERE MONTH(date_time)='03' GROUP BY DATE(date_time)
Since date_time is in Y-m-d H:i:s so doing Group by date_time will return all the rows.
Also if you do Group By date which is obtained as DAY(date_time) AS date yield wrong result.
DAY('2014-01-01') = 1
DAY('2014-02-01') = 1
So it will sum up the result for days irrespective of actual date.
I have a table which keeps track of data from a game, here is an example of the table:
id | player_name | date | score | kills
1 | test1 | 2013-01-01 00:00:00 | 10000 | 200
2 | test1 | 2013-01-01 00:01:00 | 12000 | 300
I have a leaderboards for players, it ranks people who gain the most score/kills, etc. in a certain time period. At the moment I have only got it so that it ranks players in the previous 24 hours. I am doing this by selecting the first and last records in a specified time period and then subtracting them to get the difference.
This is my current query:
SELECT date, score FROM datapoints WHERE player_name = :player AND date = (SELECT MIN(date) FROM datapoints WHERE player_name = :player AND date > DATE_SUB(CURDATE(), INTERVAL 24 HOUR))
UNION ALL
SELECT date, score FROM datapoints WHERE player_name = :player AND date = (SELECT MAX(date) FROM datapoints WHERE player_name = :player AND date > DATE_SUB(CURDATE(), INTERVAL 24 HOUR))
After subtracting I use the PHP arsort() function to order them and then display them on the page.
However, I want to add another feature. I want to be able to see the on which day was the users best day for score/kills.
I have been thinking of how I could possibly do it and one was was using the above query but having a loop for each day and taking out the best day, however this probably isn't very efficient and I was wondering, if there was a better way of doing this?
Here is how you would get the score changes and kills that occur on any given calendar day:
select date(date) as thedate, max(score) - min(score) as DayScore,
max(kills) - min(kills) as DayKills
from datapoints dp
where player_name = :player
group by date(date);
To get the top day for scores, for instance, you would add an order by and limit clause like this:
order by DayScore desc
limit 1;