HomeLinuxPostgreSQL Group by Day (Date)

PostgreSQL Group by Day (Date)


PostgreSQL permits you to group by day. This feature is helpful if you need to group the desk parts based mostly on the day when you may have a date column. The GROUP BY clause works with the PostgreSQL choose assertion.

This information presents totally different examples on how you can group by date on Postgres. With these examples, you’ll perceive how the group by day (date) works. Let’s start!

The right way to Group by Day (Date) on PostgreSQL

The very first thing is to know the syntax that’s required to group by day. The syntax that you simply use depends upon the date operate that you simply need to execute. For example, use the next syntax utilizing the DATE_TRUNC operate:

SELECT DATE_TRUNC(‘day’, date_column) FROM table_name GROUP BY DATE_TRUNC(‘day’, date_column);

The DATE_TRUNC operate permits you to convert the date within the date_column per day. Let’s use the next desk for our examples:

Instance 1: Group by Day from the Desk

If you need to group the weather on a per-day foundation, you may choose the day utilizing the date_trunc operate. You then should group by day, as illustrated within the following command:

SELECT DATE_TRUNC(‘day’, order_date) FROM date_details GROUP BY DATE_TRUNC(‘day’, order_date);

The command eliminates the duplicate dates within the desk. You’ll be able to observe that solely six rows are returned as a substitute of seven rows within the desk.

The earlier instance solely chosen one column. Nonetheless, you may embrace totally different columns out of your desk for readability. Any column that you choose should be specified within the GROUP BY clause. It’s also possible to add what title to make use of for the group by output of the column that you choose.

For example, we specify to show our date column as “dates”.

Instance 2: Use the Group by Day (Date) with Combination Capabilities

The GROUP BY clause might be utilized with totally different mixture features for a extra particular output. On this instance, we use the COUNT() to calculate the variety of cases the place we’ve the identical dates when grouping the rows per day foundation.

Be aware how and the place we apply the combination operate. It comes instantly after choosing the goal column which we need to use with the GROUP BY clause.

The output confirms that we’ve two related dates based mostly on the depend column.

Instance 3: Utilizing the TO_CHAR Perform

When grouping the dates per day, you can too get the actual day of the week of every date in your desk utilizing the TO_CHAR operate.

The next command returns the times of the week that are related to every date within the particular column:

The TO_CHAR operate takes the date column as its first argument. Reversing the association raises an error like the next:

Instance 4: Utilizing the Group by Day with Different Clauses

Combining different clauses if you need higher output when grouping the weather per day foundation is feasible. This instance makes use of the DATE_TRUNC operate with the ORDER BY clause to order the date in descending order:

Suppose you need to group by day to search out the urgency of the weather in your desk. This instance is a wonderful illustration of reaching that.

Instance 5: Utilizing the DATE_PART with the Group by Day

When grouping the weather per day, it’s attainable to extract the actual month date from the date column. When you may have a date worth, the DATE_PART operate permits you to extract the month’s date as illustrated within the following instance.

You could nonetheless specify the “day” as an argument when utilizing the operate to make sure that the grouping is on a per-day foundation.

You’ll be able to mix the DATE_PART operate with the TO_CHAR operate to extract the date of the month and the day of the week. Right here’s a traditional instance on how the command for that appears like:

Conclusion

You’ll be able to group the dates per day utilizing the DATE_TRUNC and the DATE_PART features. This submit offered numerous examples to information you on how you can group by day when working with PostgreSQL. Hopefully, you gained a basis and may scale your expertise in working with the PostgreSQL group by day (date).

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments