Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/79.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/image-processing/2.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 按问题分组_Sql_Greatest N Per Group - Fatal编程技术网

Sql 按问题分组

Sql 按问题分组,sql,greatest-n-per-group,Sql,Greatest N Per Group,按问题分组的SQL 我有一个SQL分组问题。我的桌子是这样的 Cust_id.  Price_id     Price. ---------------------------- 1.          556.        5000. ----------------------------- 2.          654.        600. 2.          432.         487. 2.          546.         500. -

按问题分组的SQL

我有一个SQL分组问题。我的桌子是这样的

Cust_id.  Price_id     Price. 
----------------------------  
1.          556.        5000. 
-----------------------------  
2.          654.         600. 
2.          432.         487. 
2.          546.         500. 
---------------------------  
3.          455.         200. 
3.          877.         143. 
3.          123.         879. 
现在,当我运行此查询时:

Select  cust_id,  max(price) as max, min(price) as min. 
From table. 
Group by cust_id. 
我明白了

Cust_id.      Max.        Min. 
1.           5000.       5000. 
2.            600.        487. 
3.            879.        143. 
但我真正想要的不是最高和最低价格,而是与价格相关联的价格id。
因此,结果将是

Cust_id.       Max.        Min.   
1.             556.        556.   
2.             654.        432.   
3.             123.        877.    
我不知道该怎么做。我认为上面的查询将是某种类型的子查询,但这是我得到的。使用:

   SELECT x.cust_id,
          y.price_id AS max,
          z.price_id AS min
     FROM (SELECT t.cust_id,  
                  MAX(t.price) as max, 
                  MIN(t.price) as min
             FROM TABLE t
         GROUP BY t.cust_id) x
LEFT JOIN TABLE y ON y.cust_id = x.cust_id
                 AND y.price = x.max
LEFT JOIN TABLE z ON z.cust_id = x.cust_id
                 AND z.price = x.min

问题在于,如果一个客户id有两条相同高(或低)价格的记录,您将看到重复的记录,并且需要提供处理关系的逻辑。

对于具有排名/分析功能的引擎来说,这应该可以做到:

SELECT Pmin.Cust_id, Pmax.Price_id Price_max_id, Pmin.Price_id Price_min_id FROM
(SELECT t.*, ROW_NUMBER() OVER (PARTITION BY t.Cust_id ORDER BY t.Price DESC) ix FROM @table t) Pmin
JOIN (SELECT t.*, ROW_NUMBER() OVER (PARTITION BY t.Cust_id ORDER BY t.Price ASC) ix FROM @table t) Pmax
    ON Pmin.Cust_id = Pmax.Cust_id
WHERE (Pmin.ix = 1) AND (Pmax.ix = 1)

这是大多数使用MySQL的人在GroupBy中遇到的典型问题。MySQL允许标准SQL和大多数其他品牌的数据库中不允许的查询

您需要的是整行,包括分组依据的cust_id以外的列,以便该行具有该组中的最高(或最低)价格。你不能从小组里得到这个

我真正想要的是。。。价格标识与价格关联

但是您想要哪种价格?是最高价格行中的价格,还是最低价格行中的价格?这些可能是不同的行

Cust_id.  Price_id     Price
----------------------------
2.          654          600  <-- max price, price_id 654
2.          432          487  <-- min price, price_id 432
2.          546          500

下面是一种SQL Server方法

with Data as 
(
    select 1 Cust_id, 556 Price_id,  5000  Price union ALL
    select 2,          654,          600 union ALL
    select 2,          432,          487 union ALL
    select 2,          546,          500 union ALL
    select 3,          455,           200 union ALL
    select 3,          877,           143 union ALL
    select 3,          123,           879
),
Prices as
(
    select Cust_id, MAX(Price) MaxP, MIN(Price) MinP
    from Data
    group by Cust_id
)
select Prices.Cust_id
        ,Data.Price MaxPrice
        , d2.Price MinPrice
from Prices
inner join Data  on Data.Cust_id = Prices.Cust_id and Data.Price = Prices.MaxP
inner join Data d2 on d2.Cust_id = d2.Cust_id and d2.Price = Prices.MinP

您使用的数据库引擎是什么?您使用的是哪个数据库?(SQL Server,MySQL,PostGRES,…)@dlb-不知道为什么要还原我的更改。。。但是有一个难看的问题并不能帮助你得到答案…你在问题中有切割id和客户id-选择一个!@Jonathan Leffler的可能重复项:我使用了OP提供的查询,基于它进行构建。需要调整-OP想要的是最大/最小价格ID,而不是价格。使用RANK可以返回重复项;行号将确保不会返回重复项。是的,在我看到您的评论之前已经修复了。谢谢。这也有重复的问题。
SELECT tmax.cust_id, tmax.price_id, tmax.price, tmin.price_id, tmin.price
FROM table tmax
JOIN table tmin ON tmax.cust_id = tmin.cust_id
WHERE NOT EXISTS (SELECT * FROM table t1 WHERE t1.cust_id = tmax.cust_id AND t1.price > tmax.price) 
  AND NOT EXISTS (SELECT * FROM table t2 WHERE t2.cust_id = tmin.cust_id AND t2.price > tmin.price) 
with Data as 
(
    select 1 Cust_id, 556 Price_id,  5000  Price union ALL
    select 2,          654,          600 union ALL
    select 2,          432,          487 union ALL
    select 2,          546,          500 union ALL
    select 3,          455,           200 union ALL
    select 3,          877,           143 union ALL
    select 3,          123,           879
),
Prices as
(
    select Cust_id, MAX(Price) MaxP, MIN(Price) MinP
    from Data
    group by Cust_id
)
select Prices.Cust_id
        ,Data.Price MaxPrice
        , d2.Price MinPrice
from Prices
inner join Data  on Data.Cust_id = Prices.Cust_id and Data.Price = Prices.MaxP
inner join Data d2 on d2.Cust_id = d2.Cust_id and d2.Price = Prices.MinP