我想运行此查询:
SELECT DISTINCT ON (address_id) purchases.address_id, purchases.*
FROM purchases
WHERE purchases.product_id = 1
ORDER BY purchases.purchased_at DESC
Run Code Online (Sandbox Code Playgroud)
但我得到这个错误:
PG ::错误:错误:SELECT DISTINCT ON表达式必须与初始ORDER BY表达式匹配
添加address_id
为第一个ORDER BY
表达式会使错误无效,但我真的不想添加排序address_id
.是否可以不通过订购address_id
?
与 - PostgreSQL DISTINCT ON有关,使用不同的ORDER BY
我有桌子购买(product_id,purchase_at,address_id)
样本数据:
| id | product_id | purchased_at | address_id |
| 1 | 2 | 20 Mar 2012 21:01 | 1 |
| 2 | 2 | 20 Mar 2012 21:33 | 1 |
| 3 | 2 | 20 Mar 2012 21:39 | 2 |
| 4 | 2 | 20 Mar 2012 21:48 | 2 |
Run Code Online (Sandbox Code Playgroud)
我期望的结果是每个address_id的最近购买的产品(完整行),并且结果必须由downloaded_at字段以后代顺序排序:
| id | product_id | purchased_at | address_id |
| 4 | 2 | …
Run Code Online (Sandbox Code Playgroud) 我有一个rails应用程序:
user has_many :projects
user has_many :tasks, :through => :projects
project has_many :tasks
每项任务都有一个里程碑日期.
要显示我正在使用的下一个里程碑日期的项目详细信息表:
@projects = current_user.tasks.joins(:project).select("distinct on (projects.id) projects.*, tasks.*").reorder("projects.id, tasks.milestone ASC")
Run Code Online (Sandbox Code Playgroud)
这很好用.
我现在希望能够对表列进行排序.
根据Postgres DISTINCT ON
不可排序,你必须将它包装在另一个select语句中,即SELECT * FROM (SELECT DISTINCT ON....) ORDER BY column_3
我确实认为订购的列可以根据需要在SQL中使用,即(按项目名称DESC排序):
@projects = current_user.tasks.joins(:project).select("distinct on (projects.name) projects.*, tasks.*").reorder("projects.name DESC, tasks.milestone ASC")
Run Code Online (Sandbox Code Playgroud)
哪个有效,但我也希望能够通过里程碑进行排序,但这样做不起作用.
有人能告诉我如何转换我的rails查询,以便可以通过任何列进行排序吗?
UPDATE
我想我的问题很简单我怎么在周围包裹一个ActiveRecord的查询SELECT
和ORDER BY
?
我想我已经成功实现了它:
inner_query = current_user.tasks.select("distinct on (projects.id) projects.*, tasks.*").reorder("projects.id, tasks.milestone ASC").to_sql
@projects = Task.paginate_by_sql("select * from (#{inner_query}) as user_projects order by user_projects.name", …
Run Code Online (Sandbox Code Playgroud) 在PostgreSQL中,我想一次获取每个用户并按日期排序.
这是我的查询:
SELECT id, useridx, isread, message, date
FROM messages
WHERE isread = 1
GROUP BY useridx
ORDER BY date DESC
Run Code Online (Sandbox Code Playgroud)
这是一个示例数据:
------------------------------------------------------
+ id | useridx | isread | messsage | date +
------------------------------------------------------
1 | 1 | 0 | Hello | 2012-01-01
2 | 2 | 1 | Hi | 2012-01-02
3 | 3 | 1 | Test | 2012-01-03
4 | 3 | 0 | My Msg | 2012-01-04
5 | 4 | 1 | …
Run Code Online (Sandbox Code Playgroud) 对于以下查询,我只需要选择shape_type值最小的第一条记录(范围从1到10).如果你有任何关于如何轻松做到这一点的知识是postgresql,请帮忙.谢谢你的时间.
select g.geo_id, gs.shape_type
from schema.geo g
join schema.geo_shape gs on (g.geo_id=gs.geo_id)
order by gs.shape_type asc;
Run Code Online (Sandbox Code Playgroud) 我刚刚遇到一个SQL查询,特别是针对Postgres数据库,它使用名为"distinct"的函数.即:
select distinct(pattern) as pattern, style, ... etc ...
from styleview
where ... etc ...
Run Code Online (Sandbox Code Playgroud)
请注意,这不是SELECT上的普通DISTINCT限定符 - 至少它不是DISTINCT限定符的常规语法,请注意括号.它显然使用DISTINCT作为函数,或者这可能是一些特殊的语法.
知道这意味着什么吗?
如果我写的话,我试着用它玩一点
select distinct(foo)
from bar
Run Code Online (Sandbox Code Playgroud)
我得到了相同的结果
select distinct foo
from bar
Run Code Online (Sandbox Code Playgroud)
当我将它与同一个选择中的其他字段组合时,我不清楚它到底在做什么.
我在Postgres文档中找不到任何内容.
谢谢你的帮助!
是否可以选择DISTINCT ON
一些单独的、独立的列集的行?
假设我想要所有符合以下条件的行:
(name, birth)
(name, height)
因此,在下表中,标有红色叉号的行不会是不同的(带有失败子句的指示):
name birth height
--------------------------
William 1976 1.82
James 1981 1.68
Mike 1976 1.68
Tom 1967 1.79
William 1976 1.74 ? (name, birth)
William 1981 1.82 ? (name, height)
Tom 1978 1.92
Mike 1963 1.68 ? (name, height)
Tom 1971 1.86
James 1981 1.77 ? (name, birth)
Tom 1971 1.89 ? (name, birth)
Run Code Online (Sandbox Code Playgroud)
在上面的示例中,如果DISTINCT ON
子句刚刚为DISTINCT ON (name, birth, height)
,则所有行都将被视为不同的。
试过了,没有用:
SELECT DISTINCT ON (name, birth) …
我了解DISTINCT
工作原理,但不了解DISTINCT ON (expression)
。
以以下屏幕截图为例:
(a % 2)
零件如何影响一切?是否表示如果a % 2
计算结果为true,然后将其返回,然后继续对所有其他元组执行此操作,但仅在返回值不同时才返回?
我有一个Message
ActiveRecord模型,我想选择所有具有不同的消息conversation_partner_id
.我目前正在将该DISTINCT ON
子句指定为字符串,但我想知道它是否可以作为Arel
表达式提供,以及是否会有好处.我的发言如下:
Message.select('DISTINCT ON ("messages"."conversation_partner_id") messages.*').from(t)
Run Code Online (Sandbox Code Playgroud)
where t
是messages
表的并集,并指定为有点复杂的Arel
表达式.我把它留下来,因为它独立于SELECT
问题并且工作得很好.
下面的代码并不能正常工作:
Message.select([
Arel::Nodes::DistinctOn.new(Message.arel_table[:conversation_partner_id]),
Message.arel_table[Arel.star]
]).from(t)
Run Code Online (Sandbox Code Playgroud)
它导致SQL格式错误,因为它在DISTINCT ON ()
子句后放置逗号:
SELECT DISTINCT ON ("messages"."conversation_partner_id"), "messages".* ...
^
Run Code Online (Sandbox Code Playgroud) 我有一个带有数据的TempTable:
------------------------------------
| KEY_1 | KEY 2 | NAME | VALUE |
------------------------------------
| 1 | 0001 | NAME 2 | VALUE 1 |
| 1 | 0002 | NAME 1 | VALUE 3 |
| 1 | 0003 | NAME 3 | VALUE 2 |
| 2 | 0001 | NAME 1 | VALUE 2 |
| 2 | 0001 | NAME 2 | VALUE 1 |
------------------------------------
Run Code Online (Sandbox Code Playgroud)
我想获得以下数据:
------------------------------------
| KEY_1 | KEY 2 | NAME | VALUE …
Run Code Online (Sandbox Code Playgroud) distinct-on ×10
postgresql ×7
sql ×7
distinct ×4
activerecord ×2
sql-order-by ×2
arel ×1
database ×1
duplicates ×1
firebird ×1
group-by ×1
mod ×1