Hard to describe what i want, but I will try:
First of all, i have 2 tables: employee and salary. I joined 2 tables by this SQL command:
"SELECT employee.id, employee.employee_name, employee.image, salary.year, salary.month, salary.day, salary.total_salary, salary.id, salary.created
FROM employee
JOIN salary ON salary.employee_id=employee.id"
to get all value i inserted, after execute, i got:
Created name year month day(working) total_salary
------------------------------------------------------------------------------
2016-12-17 20:41 A 2016 1 31 1550000
2016-12-17 21:49 A 2016 1 20 2000000
2016-12-17 22:49 A 2016 2 20 2000000
2016-12-18 22:43 B 2016 2 15 1550000
2016-12-18 23:43 B 2016 2 10 1000000
2016-12-18 23:49 B 2016 2 1 100000
After that, i want to get the newest value per month, so i add this command to the end of sql above:
GROUP BY employee.employee_name, salary.year, salary.month"
and got this:
Created name year month day(working) total_salary
------------------------------------------------------------------------------
2016-12-17 20:41 A 2016 1 31 1550000
2016-12-17 22:49 A 2016 2 20 2000000
2016-12-18 22:43 B 2016 2 15 1550000
What query will return the following result?
Created name year month day(working) total_salary
------------------------------------------------------------------------------
2016-12-17 21:49 A 2016 1 20 2000000
2016-12-17 22:49 A 2016 2 20 2000000
2016-12-18 23:49 B 2016 2 1 100000
My starting point was after your joins, I just made a table that has the data as your first result.
With this query:
SELECT created, name, year, month, day, total_salary FROM
( SELECT * FROM employees
ORDER BY created DESC
) AS sub
GROUP BY name, month, year;
I've got the results you asked for:
Created name year month day(working) total_salary
------------------------------------------------------------------------------
2016-12-17 21:49 A 2016 1 20 2000000
2016-12-17 22:49 A 2016 2 20 2000000
2016-12-18 23:49 B 2016 2 1 100000
Basically the trick was to handle the ordering before you pack them up into groups with GROUP BY
So I made a subQuery that just handles the ordering. This post helped me figure out how to do this.
Here's a Fiddle where you can play around and adjust it to your starting query.
Before you group you should sort the entries by created date the newest entries are first:
ORDER BY salary.created DESC
Change the first query (you have to remove or alias one of the id's because you will have two columns with the same name):
SELECT employee.id as employee_id, employee.employee_name, employee.image, salary.year, salary.month, salary.day, salary.total_salary, salary.id, salary.created
FROM employee JOIN salary ON salary.employee_id=employee.id
ORDER BY salary.created DESC
So the last created are shown first. Then group the results like you did:
SELECT * FROM (
SELECT employee.id as employee_id, employee.employee_name, employee.image, salary.year, salary.month, salary.day, salary.total_salary, salary.id, salary.created
FROM employee
JOIN salary ON salary.employee_id=employee.id
ORDER BY salary.created DESC
) as employee_salary
GROUP BY employee_salary.employee_name, employee_salary.year, employee_salary.month
Related
i have this query that get all regsiterd data between this two dates
SELECT date as date_month, status FROM tbl_reports WHERE status != 0 AND (date BETWEEN '2017-01-01' AND '2017-12-31') GROUP BY MONTH(date)
result
date_month
2017-04-19
2017-05-24
2017-06-26
2017-07-01
but how i can get the result even if there is no regsitered input in the other months
want result
Date_month
2017-01-01
2017-02-01
2017-03-01 - it will give this value if there is no such data in this month
2017-04-19 - i will get the input date from database
'' '' ''
2017-12-31
is it possible ? or i need some php code to manipulate this data?
thank you in advance for my answering my question.. :)
Create an extra table, named datehelper. Provide it with 2 columns: year and month. And fill those with year: 2017 until 2027 and 1 / 12 for the months.
SELECT
datehelper.year,
datehelper.month,
r.status
FROM datehelper
LEFT JOIN tbl_reports r ON MONTH(r.date) = datehelper.month
AND YEAR(r.date) = datehelper.year AND status != 0
WHERE (datehelper.year = '2017')
ORDER BY datehelper.year, datehelper.month
I see you would get duplicates on the left joined part if there are more records there in a certain month.
What status would you expect? Let's think there are records with status 0 (excluded) status = 1, and status = 2
datehelper:
year month
2017 1
2017 2
2017 3
2017 4
...
2017 11
2017 12
2018 1
...
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 table structure like this:
Id Name Rank Date
-----------------------------------
1 test 1000 2012-1-11
2 test 7000 2012-1-10
3 test2 2000 2012-1-11
4 test2 200 2012-1-10
5 test3 4000 2012-1-10
6 test4 6500 2012-1-11
Consider today date is 2012-1-11
Yesterday date is 2012-1-10
In single query i get the difference between the each user name for today and yesterday's date.
i.e test has 7000 rank on yesterday and 1000 on today. So the result is 6000
Similarly test2 has -1800.
I need the output as:
Name Difference (Orderby the difference Desc)
--------------------
test 6000
test2 -1800
If the today date or yesterday date's record is not available then we will not take this record to calculation.
Is this possible in PHP MySQL?
How about this? (not very clear what you are trying to achieve though..) Pleaes comment.
SQLFIDDLE DEMO
Code:
select b.id, b.name, (b.rank-a.rank) diff
from t1 a
left join t1 b
on b.date < a.date
and b.name = a.name
having not diff is null
;
Results:
ID NAME DIFF
2 test 6000
4 test2 -1800
Edit as per OP's comment:
Notice that I have added extra few records to your sample table for triggering out conditions.
SQLFIDDLE DEMO2
Code2:
select b.id, b.name,b.rank AS New,
b.Date new_date,
a.Rank as Old, a.date as old_date,
(b.rank-a.rank) diff
from t1 a
left join t1 b
on b.name = a.name
where b.date > a.date and b.date <= Now()
and datediff(b.date, a.date) = 1
having not diff is null and diff <> 0
order by diff desc
;
Results:
ID NAME NEW NEW_DATE OLD OLD_DATE DIFF
3 test 8000 January, 12 2012 1000 January, 11 2012 7000
4 test2 2000 January, 11 2012 200 January, 10 2012 1800
1 test 1000 January, 11 2012 7000 January, 10 2012 -6000
Literally everything you need is on this page
https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html
Can anyone post a SQL query for Calculating Total No. of Orders per Day?
Here are the Columns along with their data in my Database.
order_id order_placed_date order_total
- 1 12/30/2008 12:06:24 AM 2499.99
- 2 2/3/2009 1:57:17 AM 199.99
- 3 2/3/2009 1:58:27 AM 449.99
- 4 5/3/2009 1:58:48 AM 299.99
- 5 6/3/2009 2:00:31 AM 359.94
- 6 6/3/2009 2:01:47 AM 279.97
- 7 6/3/2009 2:02:31 AM 1359.94
- 9 7/1/2009 2:21:18 PM 5099.98
- 10 7/1/2009 2:21:36 PM 2621.97
- 11 7/2/2009 2:22:18 PM 2169.95
- 12 7/3/2009 2:23:29 PM 2249.95
- 13 7/4/2009 2:24:24 PM 5509.95
- 14 7/5/2009 12:15:17 AM 449.99
- 15 7/5/2009 12:18:08 AM 2299.99
- 16 7/5/2009 12:18:28 AM 3999.99
- 17 7/5/2009 12:18:45 AM 1939.99
- 18 7/5/2009 11:58:07 PM 39.99
- 19 7/6/2009 12:00:42 AM 1899.99
- 20 7/6/2009 12:01:00 AM 3999.99
- 21 7/7/2009 12:06:38 AM 199.99
- 22 7/7/2009 12:08:31 AM 1143.97
- 23 7/7/2009 12:09:13 AM 449.99
- 26 7/15/2009 1:30:03 PM 5469
- 27 7/15/2009 2:14:24 PM 329.97
- 28 7/15/2009 6:18:47 PM 5469
- 29 7/15/2009 10:17:36 PM 39.99
For e.g. there are 2 orders in the month of Febuary 2009
- 2 2/3/2009 1:57:17 AM 199.99
- 3 2/3/2009 1:58:27 AM 449.99
I need a sql query which would calculate and show the total amount per day. So for 3rd of Feb 2009, the total would be 699.98
I need to display Total Order Amount per day in a Chart
If it would be easier to do it with PHP, do mention it as well.
UPDATE:
I would like to clarify that I needed the Total Amount Per Day in the Present Month. I forgot to mention that in my initial question.
So i udpated Peter's query to get Total No. of Orders + Total Amount per Day in this Month.
Please, let me know if it needs any correction or is there a better and shorter way of doing it.
SELECT date(order_placed_date), COUNT(order_id) AS num_orders, SUM(order_total) AS daily_total
FROM orders
WHERE order_placed_date>=date_sub(current_date, INTERVAL 31 DAY)
GROUP BY date(order_placed_date)
MySQL's date() function will return a DATEIME or TIMESTAMP value without any hour/minute/second info - which means you reduce the accuracy to the day of the value.
So all you need to do is group by that and then add your aggregate functions to the right columns.
SELECT date(order_placed_date)
, COUNT(id) AS num_orders
, SUM(order_total) AS daily_total
FROM [Table]
GROUP BY date(order_placed_date)
SELECT date(order_placed_date)
, COUNT(id) AS num_orders
, SUM(order_total) AS daily_total
FROM orders
GROUP BY 1
Just copied Peter's answer but altered it so it will work. Plus shortcuted the group by.
A group by is your friend here. It can aggregate on grouped rows. An example query would be:
SELECT order_placed_date, SUM(order_total)
FROM orders
GROUP BY order_placed_date
Of course, in your example you'd probably want to extract just the day/month/year part using the DATE() function, and group by that.
select YEAR(datename(year,ORDER_DATE)) as YEARS,
count(ORDER_nO) as [NO(ORDER)]
from ProductOrder
group by YEAR(datename(year,ORDER_DATE))
I don't know what it is in MySQL but in MSSQL it would be this,
select
datepart(yyyy, order_placed_date),
datepart(mm, order_placed_date),
datepart(dd, order_placed_date),
sum(order_total)
from orders
group by datepart(yyyy, order_placed_date), datepart(mm, order_placed_date), datepart (dd, order_placed_date)