PostgreSQL:在GROUPBY之后使用LEAST()命令实现第一个事务
我正在使用magento表,如下所示:PostgreSQL:在GROUPBY之后使用LEAST()命令实现第一个事务,sql,postgresql,greatest-n-per-group,Sql,Postgresql,Greatest N Per Group,我正在使用magento表,如下所示: +-----------+--------------+------------+--------------+----------+-------------+ | date | email | product_id | product_type | order_id | qty_ordered | +-----------+--------------+------------+--------------+----------
+-----------+--------------+------------+--------------+----------+-------------+
| date | email | product_id | product_type | order_id | qty_ordered |
+-----------+--------------+------------+--------------+----------+-------------+
| 2017/2/15 | x@y.com | 18W1 | custom | 12 | 1 |
+-----------+--------------+------------+--------------+----------+-------------+
| 2017/2/15 | x@y.com | 18W2 | simple | 17 | 3 |
+-----------+--------------+------------+--------------+----------+-------------+
| 2017/2/20 | z@abc.com | 22Y34 | simple | 119 | 1 |
+-----------+--------------+------------+--------------+----------+-------------+
| 2017/2/20 | z@abc.com | 22Y35 | custom | 31 | 2 |
+-----------+--------------+------------+--------------+----------+-------------+
+-----------+--------------+------------+--------------+----------+-------------+
| date | email | product_id | product_type | order_id | qty_ordered |
+-----------+--------------+------------+--------------+----------+-------------+
| 2017/2/15 | x@y.com | 18W1 | custom | 17 | 1 |
+-----------+--------------+------------+--------------+----------+-------------+
| 2017/2/15 | z@abc.com | 18W2 | simple | 31 | 3 |
+-----------+--------------+------------+--------------+----------+-------------+
我想通过电子邮件分组来创建一个新视图,然后只获取order_id最少的行
因此,从上面执行此操作后,我的最终表格应如下所示:
+-----------+--------------+------------+--------------+----------+-------------+
| date | email | product_id | product_type | order_id | qty_ordered |
+-----------+--------------+------------+--------------+----------+-------------+
| 2017/2/15 | x@y.com | 18W1 | custom | 12 | 1 |
+-----------+--------------+------------+--------------+----------+-------------+
| 2017/2/15 | x@y.com | 18W2 | simple | 17 | 3 |
+-----------+--------------+------------+--------------+----------+-------------+
| 2017/2/20 | z@abc.com | 22Y34 | simple | 119 | 1 |
+-----------+--------------+------------+--------------+----------+-------------+
| 2017/2/20 | z@abc.com | 22Y35 | custom | 31 | 2 |
+-----------+--------------+------------+--------------+----------+-------------+
+-----------+--------------+------------+--------------+----------+-------------+
| date | email | product_id | product_type | order_id | qty_ordered |
+-----------+--------------+------------+--------------+----------+-------------+
| 2017/2/15 | x@y.com | 18W1 | custom | 17 | 1 |
+-----------+--------------+------------+--------------+----------+-------------+
| 2017/2/15 | z@abc.com | 18W2 | simple | 31 | 3 |
+-----------+--------------+------------+--------------+----------+-------------+
我正在尝试使用以下查询(但不起作用):
我真的很想得到你的帮助,谢谢 我想你需要在上有明显的
:
select distinct on (email) t.*
from t
order by email, order_id;
distinct on
是Postgres的扩展。根据order by
子句,它为括号中的所有键组合获取一条记录。在这种情况下,它是每电子邮件一行,第一行是最小的订单id
(因为订单人
)。select
中的键也必须是order by
中的第一个键。最后一个表示例没有意义。这里面有错误吗?