postgresql

In Postgresql how to order by date while keeping custom date format

…衆ロ難τιáo~ 提交于 2021-02-10 08:14:07
问题 The issue is that using to_char will turn order by date into order by ascii. Example: SELECT foo, bar FROM baz ORDER BY foo; I would like to format foo using to_char, but doing this will affect the order: SELECT to_char(foo,'dd/MM/yyyy') as foo, bar FROM baz ORDER BY foo; Because foo now is of type text. There is a way to correctly do this? Or only in the code? 回答1: You can use a different alias for the formatted column: SELECT to_char(foo,'dd/MM/yyyy') as formatted_foo, bar FROM baz ORDER BY

In Postgresql how to order by date while keeping custom date format

我怕爱的太早我们不能终老 提交于 2021-02-10 08:13:15
问题 The issue is that using to_char will turn order by date into order by ascii. Example: SELECT foo, bar FROM baz ORDER BY foo; I would like to format foo using to_char, but doing this will affect the order: SELECT to_char(foo,'dd/MM/yyyy') as foo, bar FROM baz ORDER BY foo; Because foo now is of type text. There is a way to correctly do this? Or only in the code? 回答1: You can use a different alias for the formatted column: SELECT to_char(foo,'dd/MM/yyyy') as formatted_foo, bar FROM baz ORDER BY

In Postgresql how to order by date while keeping custom date format

▼魔方 西西 提交于 2021-02-10 08:13:01
问题 The issue is that using to_char will turn order by date into order by ascii. Example: SELECT foo, bar FROM baz ORDER BY foo; I would like to format foo using to_char, but doing this will affect the order: SELECT to_char(foo,'dd/MM/yyyy') as foo, bar FROM baz ORDER BY foo; Because foo now is of type text. There is a way to correctly do this? Or only in the code? 回答1: You can use a different alias for the formatted column: SELECT to_char(foo,'dd/MM/yyyy') as formatted_foo, bar FROM baz ORDER BY

CSS快速入门-前端布局1(抽屉)

孤街醉人 提交于 2021-02-10 07:28:33
一、效果图 前面对CSS基础知识有了一定的了解,是时候开始实战了!以下我对抽屉(https://dig.chouti.com/)主页进行模拟布局。 官方网站效果图: 模拟网站图: 二、实现步骤 1、整体布局(header、body、footer) 抽屉的首页主要分为三块:头部、网页内容、底部内容。 2、header实现 header由logo、内容菜单、登录菜单、搜索框四部分组成。 代码架构为: CSS代码: body{ margin:0px; background-color:#ededed; } ul{ list-style:none; margin:0px; } ul li{ float:left; } div.pg-header { font-size: 14px; height:44px; background-color:#2459a2; top: 0; left: 0; z-index: 1000; width: 100%; position: fixed; } .w { width:960px; margin:0 auto; } a { text-decoration:none; } .pg-header .logo{ float:left; margin-top:10px; } .pg-header .menu { float:left; line-height

PostgreSQL function returning a data cube

混江龙づ霸主 提交于 2021-02-10 06:31:28
问题 First off, the iceberg-cube query is defined as in Let's say I have a relation item,location,year,supplier,unit_sales , and I would like to write a plpgsql functions as a wrapper around the query in the image, to specify the parameter N , like so: create or replace function iceberg_query( percentage integer ) returns cube /* Code here */ as $$ declare numrows int; begin select count(*) into numrows from sales; select item, location, year, count(*) from sales group by cube(item,location,year)

PostgreSQL function returning a data cube

回眸只為那壹抹淺笑 提交于 2021-02-10 06:31:28
问题 First off, the iceberg-cube query is defined as in Let's say I have a relation item,location,year,supplier,unit_sales , and I would like to write a plpgsql functions as a wrapper around the query in the image, to specify the parameter N , like so: create or replace function iceberg_query( percentage integer ) returns cube /* Code here */ as $$ declare numrows int; begin select count(*) into numrows from sales; select item, location, year, count(*) from sales group by cube(item,location,year)

Postgres doesn't use index for slow function

一世执手 提交于 2021-02-10 06:25:13
问题 In my database design, a lot of functions are used. And many of them are very slow. So, I decided that it could be a wise idea, to create indexes on some of them in order to make execution a little bit faster. However, I don't succeed in persuading PostgreSQL (9.6) to actually use my index. Consider this table "user" id integer | name jsonb 1 | {"last_names": ["Tester"], "first_names": ["Teddy","Eddy"]} 2 | {"last_names": ["Miller"], "first_names": ["Lisa","Emma"]} Often, I need the name as

Postgres doesn't use index for slow function

一笑奈何 提交于 2021-02-10 06:24:30
问题 In my database design, a lot of functions are used. And many of them are very slow. So, I decided that it could be a wise idea, to create indexes on some of them in order to make execution a little bit faster. However, I don't succeed in persuading PostgreSQL (9.6) to actually use my index. Consider this table "user" id integer | name jsonb 1 | {"last_names": ["Tester"], "first_names": ["Teddy","Eddy"]} 2 | {"last_names": ["Miller"], "first_names": ["Lisa","Emma"]} Often, I need the name as

Count on join of big tables with conditions is slow

孤街浪徒 提交于 2021-02-10 06:11:54
问题 This query had reasonable times when the table was small. I'm trying to identify what's the bottleneck, but I'm not sure how to analyze the EXPLAIN results. SELECT COUNT(*) FROM performance_analyses INNER JOIN total_sales ON total_sales.id = performance_analyses.total_sales_id WHERE (size > 0) AND total_sales.customer_id IN ( SELECT customers.id FROM customers WHERE customers.active = 't' AND customers.visible = 't' AND customers.organization_id = 3 ) AND total_sales.product_category_id IN (

Postgres is ignoring a timestamp index, why?

蓝咒 提交于 2021-02-10 05:46:33
问题 I have the following tables: users (id, network_id) networks (id) private_messages (id, sender_id, receiver_id, created_at) I have indexes on users.network_id, and all 3 columns in private messages however the query is skipping the indexes and taking a very long time to run. Any ideas what is wrong in the query that is causing the index to be skipped? EXPLAIN ANALYZE SELECT COUNT(*) FROM "networks" WHERE ( networks.created_at BETWEEN ((timestamp '2013-01-01')) AND (( (timestamp '2013-01-31')