Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/81.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
正在查找SQL Server函数以标识最小/最大行并从这些行提取信息_Sql_Postgresql_Subquery_Pivot_Greatest N Per Group - Fatal编程技术网

正在查找SQL Server函数以标识最小/最大行并从这些行提取信息

正在查找SQL Server函数以标识最小/最大行并从这些行提取信息,sql,postgresql,subquery,pivot,greatest-n-per-group,Sql,Postgresql,Subquery,Pivot,Greatest N Per Group,想象一下这种格式的数据: Customer, Object, Price [William, Orange, 100p William, Apple, 80p William, Banana 60p Casper, Cola, 150p Casper, Beer, 120p Casper, Peanuts, 200p] 我感兴趣的是为每个买家提取最昂贵和最便宜的购买,以及实际的项目 输出应该是这样的 Customer, MostExpItem, MostExpCost, LeastExpIte

想象一下这种格式的数据:

Customer, Object, Price
[William, Orange, 100p
William, Apple, 80p
William, Banana 60p
Casper, Cola, 150p
Casper, Beer, 120p
Casper, Peanuts, 200p]
我感兴趣的是为每个买家提取最昂贵和最便宜的购买,以及实际的项目

输出应该是这样的

Customer, MostExpItem, MostExpCost, LeastExpItem, LeastExpCost
William, Orange, 100p, Banana, 60p
Casper, Peanuts, 200p, Beer, 120p

有什么建议吗?我可以使用group by轻松提取最小/最大值。此问题是否需要子查询?

您可以使用窗口函数:

select
    customer,
    max(object) filter(where price = most_exp_price) most_exp_item,
    most_exp_price,
    max(object) filter(where price = less_exp_price) less_exp_item,
    less_exp_price
from (
    select 
        t.*, 
        max(price) over(partition by customer) most_exp_price,
        min(price) over(partition by customer) less_exp_price
    from mytable t
) t
where price in (most_exp_price, less_exp_price)
group by customer

子查询计算每个客户的最高和最低价格。外部查询过滤最高和最低价格,按客户汇总并显示相应的项目。

您可以使用窗口功能:

select
    customer,
    max(object) filter(where price = most_exp_price) most_exp_item,
    most_exp_price,
    max(object) filter(where price = less_exp_price) less_exp_item,
    less_exp_price
from (
    select 
        t.*, 
        max(price) over(partition by customer) most_exp_price,
        min(price) over(partition by customer) less_exp_price
    from mytable t
) t
where price in (most_exp_price, less_exp_price)
group by customer

子查询计算每个客户的最高和最低价格。外部查询筛选最高价和最低价,按客户汇总并显示相应的项目。

在Postgres中,如果希望避免子查询,可以使用数组:

select customer,
       min(price),
       (array_agg(order by price))[1],
       max(price),
       (array_agg(order by price desc))[1]
from t
group by customer;

在Postgres中,如果希望避免子查询,可以使用数组:

select customer,
       min(price),
       (array_agg(order by price))[1],
       max(price),
       (array_agg(order by price desc))[1]
from t
group by customer;

您将如何处理重复数据?对于我正在处理的数据,重复数据不会是一个问题!我想我可以从示例中看出这一点并不明显。您将如何处理重复的数据?对于我正在处理的数据来说,重复不会是一个问题!我想我可以从示例中看出这一点并不明显。非常感谢!您认为这是更快/更慢还是与Gordon Linoff发布的回复相同。我是SQL的新手,我有庞大的数据库要处理,所以任何洞察都非常感谢。非常感谢!您认为这是更快/更慢还是与Gordon Linoff发布的回复相同。我是SQL的新手,我有庞大的数据库要处理,所以任何见解都值得赞赏。