postgresql组和排序
我需要在一些参数中使用提取的唯一字段“article”以最低价格重新排序我的查询。 我应该按其他参数排序。postgresql组和排序,sql,postgresql,group-by,Sql,Postgresql,Group By,我需要在一些参数中使用提取的唯一字段“article”以最低价格重新排序我的查询。 我应该按其他参数排序。 示例表: article price id_item name partner weight 1000000001 500 1 Cake 1 sony 100 1000000001 1000 2 Cake 2 apple 100 1000000002 500 3
示例表: article price id_item name partner weight 1000000001 500 1 Cake 1 sony 100 1000000001 1000 2 Cake 2 apple 100 1000000002 500 3 Beer 1 htc 100 1000000002 1000 4 Beer 2 htc 200 商品价格标识\商品名称合作伙伴重量 100000001500 1蛋糕1索尼100 100000000001000 2蛋糕2苹果100 1000000025003啤酒1 htc 100 100000002 1000 4啤酒2 htc 200 我需要按其他参数(名称、价格、合作伙伴和其他…)排序的结果:
姓名: 1000000025003啤酒1 htc 100 100000001500 1蛋糕1索尼100 按名称描述: 1000000001 500 1 Cake 1 sony 100 1000000002 500 3 Beer 1 htc 100 100000001500 1蛋糕1索尼100 1000000025003啤酒1 htc 100 按合作伙伴分列: 1000000002 500 3 Beer 1 htc 100 1000000001 500 1 Cake 1 sony 100 1000000025003啤酒1 htc 100 100000001500 1蛋糕1索尼100
您不必对数据进行分组并获取最小值。您将在
文章中以最低价格
获取所有记录
select distinct on (article)
article, price, id_item, name
from rurbox_mod_product.item
order by article, price
如果要对结果集进行排序,可以使用公共表表达式或子查询,然后对结果进行排序:
with cte as (
select distinct on (article)
article, price, id_item, name
from item
order by article, price
)
select *
from cte
order by name;
select *
from (
select distinct on (article)
article, price, id_item, name
from item
order by article, price
) as A
order by name desc;
您不必将数据分组并获取min。您将以最低
价格获取文章中的所有记录
select distinct on (article)
article, price, id_item, name
from rurbox_mod_product.item
order by article, price
如果要对结果集进行排序,可以使用公共表表达式或子查询,然后对结果进行排序:
with cte as (
select distinct on (article)
article, price, id_item, name
from item
order by article, price
)
select *
from cte
order by name;
select *
from (
select distinct on (article)
article, price, id_item, name
from item
order by article, price
) as A
order by name desc;
如果您需要查找“最小”值,然后使用其他参数对其进行排序,则需要首先将上述SQL返回到一个新表中(使用别名),然后在其周围进行选择,这是您的其他排序。您可以给我一个示例吗?如果您需要查找“最小”值,然后使用其他参数对其进行排序,您需要首先将上面的SQL返回到一个新表中(使用别名),然后对其进行选择,这会影响您的其他排序您能给我一个示例吗?