SQL:如何获取min数量?

时间:2014-10-19 09:40:46

标签: sql sql-server

我遇到了这个问题。当我试图总结国家产品的最低数量时它并没有起作用。 我有2张桌子 PRODUCT:

ID|NAME |NaID|Qty
-------------------
01|Fruit|JP  |50
02|MEAT |AUS |10
03|MANGA|JP  |80
04|BOOK |AUS |8

NATION:

NaID |NAME 
-------------------
AUS  |Australia 
JP   |Japan      

我希望我的结果如下:

ID|NAME |Name|minQty
-------------------
01|Fruit|JP  |50     
04|BOOK |AUS |8

我用过:

select p.id,p.name, p.NaID,n.name,min(P.Qty)as minQty
from Product p,Nation n
where p.NaID=n.NaID
group by p.id,p.name, p.NaID,n.name,p.Qty

我得到了这个(T_T):

ID|NAME |NaID|minQty
-------------------
01|Fruit|JP  |50
02|MEAT |AUS |10
03|MANGA|JP  |80
04|BOOK |AUS |8

拜托,能不能帮帮我吗?我想我现在对SQL不好。

3 个答案:

答案 0 :(得分:0)

在Oracle中,您可以使用多种技术。您可以使用子查询和分析函数,但最有效的方法是使用聚合函数MINFIRST

你的桌子:

SQL> create table nation (naid,name)
  2  as
  3  select 'AUS', 'Australia' from dual union all
  4  select 'JP', 'Japan' from dual
  5  /

Table created.

SQL> create table product (id,name,naid,qty)
  2  as
  3  select '01', 'Fruit', 'JP', 50 from dual union all
  4  select '02', 'MEAT', 'AUS', 10 from dual union all
  5  select '03', 'MANGA', 'JP', 80 from dual union all
  6  select '04', 'BOOK', 'AUS', 8 from dual
  7  /

Table created.

查询:

SQL> select max(p.id) keep (dense_rank first order by p.qty) id
  2       , max(p.name) keep (dense_rank first order by p.qty) name
  3       , p.naid "NaID"
  4       , n.name "Nation"
  5       , min(p.qty) "minQty"
  6    from product p
  7         inner join nation n on (p.naid = n.naid)
  8   group by p.naid
  9       , n.name
 10  /

ID NAME  NaID Nation        minQty
-- ----- ---- --------- ----------
01 Fruit JP   Japan             50
04 BOOK  AUS  Australia          8

2 rows selected.

由于您不使用Oracle,查询效率较低,但可能适用于所有RDBMS:

SQL> select p.id
  2       , p.name
  3       , p.naid
  4       , n.name
  5       , p.qty
  6    from product p
  7         inner join nation n on (p.naid = n.naid)
  8   where ( p.naid, p.qty )
  9         in
 10         ( select p2.naid
 11                , min(p2.qty)
 12             from product p2
 13            group by p2.naid
 14         )
 15  /

ID NAME  NAID NAME             QTY
-- ----- ---- --------- ----------
01 Fruit JP   Japan             50
04 BOOK  AUS  Australia          8

2 rows selected.

请注意,如果您有多个行,每个国家/地区的最小数量相同,则会返回所有这些行,而不是像上一个" Oracle" -query中那样只返回一行。

答案 1 :(得分:0)

SQL Server 2005支持窗口函数,因此您可以执行以下操作:

select id,
       name, 
       NaID,
       name, 
       qty
from  (
  select p.id,
         p.name, 
         p.NaID,
         n.name,
         min(P.Qty) over (partition by n.naid) as min_qty, 
         p.qty
  from Product p
     join Nation n on p.NaID=n.NaID
) t
where qty = min_qty;

如果有多个国家/地区具有相同的最低价值,您将获得每个国家/地区。如果您不想这样,则需要使用row_number()

select id,
       name, 
       NaID,
       name, 
       qty
from  (
  select p.id,
         p.name, 
         p.NaID,
         n.name,
         row_number() over (partition by n.naid order by p.qty) as rn, 
         p.qty
  from Product p
     join Nation n on p.NaID = n.NaID
) t
where rn = 1;

由于您的示例输出仅包含NaID但不包含国家名称,因此您不需要productnation之间的联接。


(没有名为“SQL 2005”的DBMS产品。SQL只是查询语言的一种(标准)。您所说的DBMS产品称为Microsoft SQL 服务器 2005.或者只是SQL Server 2005)

答案 2 :(得分:0)

with cte as (
   select *,
      row_number() over (partition by Nation order by qty) as [rn]
   from product
)
select * from cte where [rn] = 1