Getting date list in a range in PostgreSQL

后端 未结 8 492
悲&欢浪女
悲&欢浪女 2020-11-28 07:59

I\'d like to get the list of days between the two dates (including them) in a PostgreSQL database. For example, if I had:

  • start date: 29 june
8条回答
  •  余生分开走
    2020-11-28 08:17

    For things like this its generally handy to have a dates table in the system.

    Just like a numbers table they can be very useful and quicker to use than generating the dates on the fly, especially when you scale up to large data sets.

    Such a date table from 1900 to 2100 will be very small, so there isn't much over head in storage.

    Edit: Dunno why this is getting voted down, it will probably be the best for performance. Plus it has so many other advantages. Want to link orders to a an quarters performance numbers? Its a simple link between the tables. (Order.OrderDate -> Dates.Date -> Dates.Quarter -> PerformanceTotal.Quarter) etc. Its the same for dealing with working days, like the last working day of a month, or the first Tuesday of the previous month. Like a numbers table, I'd strongly recommend them!

提交回复
热议问题