简体   繁体   English

如何根据条件过滤行

[英]How to filter rows based on criteria

Consider this toy example where I have a very simple SQL table containing some partnumbers, prices and currency. 考虑这个玩具示例,其中我有一个非常简单的SQL表,其中包含一些零件号,价格和货币。 I want to find the lowest price for each item. 我想找到每个项目的最低价格。

Hers is table PRICELIST 她的桌子是PRICELIST

PartNumber     Price               Currency
1              19                  USD
1              10                  CAD
1              18                  GBP
2              15                  USD
2              14                  CAD
2              8                   GBP
3              5                   USD
3              1                   CAD
3              11                  GBP

I want to show the lowest price with the currency. 我想显示货币的最低价格。 This is the output I want: 这是我想要的输出:

PartNumber     Price               Currency
1              10                  CAD
2              8                   GBP
3              1                   CAD

if I say select partnumber, min(price) from pricelist group by partnumber 如果我说select partnumber, min(price) from pricelist group by partnumber

the query will execute, but if I specify the currency: 查询将执行,但是如果我指定货币:

select partnumber, min(price),currency from pricelist group by partnumber

Then I get an error saying: 然后我得到一个错误说:

An expression starting with "CURRENCY" specified in a SELECT clause, HAVING clause, or ORDER BY clause is not specified in the GROUP BY clause or it is in a SELECT clause, HAVING clause, or ORDER BY clause with a column function and no GROUP BY clause is specified.. 在SELECT子句,HAVING子句或ORDER BY子句中指定的以“ CURRENCY”开头的表达式未在GROUP BY子句中指定,或者在SELECT子句,HAVING子句或ORDER BY子句中且具有列函数且没有GROUP指定了BY子句。

I want to display the currency value for the row that has the lowest price. 我想显示价格最低的行的货币值。 What should I do? 我该怎么办?

database is DB2. 数据库是DB2。

By the way, this is a highly simplified example, in my actual query I have left joins to create larger sets, if that matters 顺便说一下,这是一个高度简化的示例,在我的实际查询中,我离开了联接以创建更大的集合,如果那很重要的话

You have to join back to the original table so as to get the rest of the fields: 您必须重新加入原始表才能获取其余字段:

select t1.partnumber, t1.price, t1.Currency
from pricelist as t1
join (
   select partnumber, min(price) as price
   from pricelist 
   group by partnumber
) as t2 on t1.partnumber = t2.partnumber and t1.price = t2.price

Alternatively you can use ROW_NUMBER : 或者,您可以使用ROW_NUMBER

select partnumber, price, Currency
from (
  select partnumber, price, Currency,
         row_number() over (partition by partnumber 
                            order by price) as rn
  from pricelist ) as t
where t.rn = 1

Note: The first method may select more than record per partnumber (in case of ties), whereas the second method always selects one record per partnumber . 注意:第一种方法可能为每个partnumber选择多条记录(如果是partnumber ),而第二种方法总是为每个partnumber选择一个记录。

Just use row_number() : 只需使用row_number()

select pl.*
from (select pl.*,
             row_number() over (partition by partnumber order by price) as seqnum
      from pricelist
     ) pl
where seqnum = 1;

If there are ties for the lowest price, this chooses an arbitrary one. 如果存在最低价格的关系,则选择任意关系。 For all of them, use rank() or dense_rank() instead of row_number() . 对于所有这些,请使用rank()dense_rank()而不是row_number()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM