MySQL Query GROUP BY day / month / year
GROUP BY YEAR(record_date), MONTH(record_date)
Check out the date and time functions in MySQL.
Group by day using the timestamp field
Use MySQL DATE()
function to extract the date from the timestamp:
SELECT name, count(*) FROM mytable GROUP BY DATE(mytable.modified);
The
DATE()
function extracts the date value from a date or datetime expression.
How can I group by date time column without taking time into consideration
Cast/Convert the values to a Date
type for your group by.
GROUP BY CAST(myDateTime AS DATE)
Group by date only on a Datetime column
Cast the datetime to a date, then GROUP BY using this syntax:
SELECT SUM(foo), DATE(mydate) FROM a_table GROUP BY DATE(a_table.mydate);
Or you can GROUP BY the alias as @orlandu63 suggested:
SELECT SUM(foo), DATE(mydate) DateOnly FROM a_table GROUP BY DateOnly;
Though I don't think it'll make any difference to performance, it is a little clearer.
How to GROUP BY date with a timestamp field in Postgres?
You need to truncate in the GROUP BY
as well:
SELECT created_at::date, COUNT(*)
FROM table_name
WHERE product_uuid = '586d8e21b9529d14801b91bd' AND
created_at > now() - interval '30 days'
GROUP BY created_at::date
ORDER BY created_at::date ASC;
Your version is aggregating by each date/time value but only showing the date component.
Also, I would recommend that you use current_date
rather than now()
so the first date is not truncated.
How do I group by day instead of date?
Time
is a quite complex object to group by. Assuming you want to group by the creation date, instead of the full Time
, start creating a custom method in your model to return the group criteria.
The method should return the creation date, possibly as string.
def group_by_criteria
created_at.to_date.to_s(:db)
end
Then, group by that method.
list.group_by(&:group_by_criteria).map {|k,v| [k, v.length]}.sort
Group by day and hour
You can use lubridate::ymd_hms
to convert the date
variable to date-time, group by day and hour from it and take mean
value of price
for each hour.
library(dplyr)
prices_2019 %>%
mutate(date = lubridate::ymd_hms(date),
date_hour = format(date, "%Y-%m-%d %H")) %>%
group_by(date_hour) %>%
summarize(mean_price = mean(price))
How to group data based on continuous date range?
the date range for price is 23.9 is not right because price not same for all the days in that range.
Because there are two same price
in different overlapping date ranges, so you might get only one row when you used aggregate function.
This is a gap-and-island problem, we can try to use ROW_NUMBER
window function to get the gap of overlapping date and then group by
that.
SELECT Product_Code,
min(Pricing_Date) AS Min_Date ,
max(Pricing_Date) AS Max_Date,
price
FROM (
SELECT *,
ROW_NUMBER() OVER(ORDER BY PRICING_DATE) - ROW_NUMBER() OVER(PARTITION BY PRODUCT_CODE,PRICE ORDER BY PRICING_DATE) grp
FROM PRICE_DATA
) t1
GROUP BY grp,Product_Code,price
ORDER BY min(Pricing_Date)
sqlfiddle
Explain
The gap-and-island problem is a feature
continuous(overlapping) data is that a set
(continuous range of sequence) - (values based on a certain order of conditions sequence)
yields the same grouping.
so that We can use
ROW_NUMBER() OVER(ORDER BY PRICING_DATE)
making a continuous range of values.ROW_NUMBER() OVER(PARTITION BY PRODUCT_CODE,PRICE ORDER BY PRICING_DATE)
making values based on a certain order of conditions.
Then we will get a grouping column with overlapping data as sqlfiddle
Related Topics
Set Global Default Encoding For Ruby 1.9
How to Access Method Arguments in Ruby
Why Does Adding "Sleep 1" in an After Hook Cause This Rspec/Capybara Test to Pass
Exporting an Environment Variable in Ruby
How to Change the Default Path of View Files in a Rails 3 Controller
Why Do I Get a "Permission Denied" Error While Installing a Gem
Can You Ask Ruby to Treat Warnings as Errors
Ruby Method Lookup Path For an Object
Getting Fields_For and Accepts_Nested_Attributes_For to Work With a Belongs_To Relationship
How to Read a User Uploaded File, Without Saving It to the Database
Dynamic Class Definition with a Class Name