简体   繁体   English

SQL选择组查询

[英]SQL select group query

Below is my Table 下面是我的表

Table1 表格1

+--------+----------+---------+  
| amount | make     | product |  
+--------+----------+---------+  
|    100 | Nokia    | Mobiles |   
|    300 | Samesung | Mobiles |   
|    700 | Micromax | Mobiles |   
|   1000 | Karbonn  | Mobiles |   
|    500 | Lava     | Mobiles |   
|    100 | Floyer   | Gift    |   
|    500 | Arichies | Gift    |   
|    300 | Feeling  | Gift    |   
+--------+----------+---------+  

Now I want to display the two highest amount for each product... 现在我想为每种产品显示两个最高金额...

So I want to build single SQL query which gives me result as below.. 所以我想构建单个SQL查询,它给出了如下结果。

+--------+----------+---------+  
| amount | make     | product |  
+--------+----------+---------+  
|   1000 | Karbonn  | Mobiles |   
|    700 | Micromax | Mobiles |   
|    500 | Arichies | Gift    |   
|    300 | Feeling  | Gift    |   
+--------+----------+---------+  

Kindly help me to build such query.. 请帮我建立这样的查询..

You can use this solution to retrieve the " group-wise maximum " based on the amount : 您可以使用此解决方案根据amount检索“ 分组最大值 ”:

SELECT a.*
FROM Table1 a
INNER JOIN Table1 b ON a.product = b.product AND a.amount <= b.amount
GROUP BY a.amount, a.product
HAVING COUNT(*) <= 2

Simply change the 2 to however many of the top rows you want to retrieve per product. 只需将2更改为每个产品要检索的顶部行数。

If you wanted to retrieve the lowest two rows per product, you can simply change the <= sign in the INNER JOIN to a >= . 如果要检索每个产品的最低两行,只需将INNER JOIN<=符号更改为>=

You can fiddle around with this solution here: SQL-Fiddle Demo 你可以在这里找到解决方案: SQL-Fiddle Demo

select product, make, amount, rnk
from (
  select l.product, l.make, l.amount, count(*) as rnk
  from table1 as l
  left join table1 as r
  on (r.product = l.product and l.amount <= r.amount) 
  group by l.product, l.make
) a
where rnk <= 2

see the ideea and other examples here: http://www.xaprb.com/blog/2005/09/27/simulating-the-sql-row_number-function/ 请参阅这里的ideea和其他示例: http ://www.xaprb.com/blog/2005/09/27/simulating-the-sql-row_number-function/

and sql fiddle based on zane bien test data. 和基于zane bien测试数据的sql小提琴

SELECT a.*
FROM Table1 a
INNER JOIN Table1 b ON a.product = b.product AND a.amount <= b.amount
GROUP BY a.amount, a.product
HAVING COUNT(*) <= 2
ORDER BY a.amount desc

Please refer to http://sqlfiddle.com/#!2/9ba82/1 请参阅http://sqlfiddle.com/#!2/9ba82/1

select top 2 amount, make, product from table1 
where product='Mobiles'
order by amount desc
union
select top 2 amount, make, product from table1 
where product='Gift'
order by amount desc

You can do it in two ways: 1) Add Row Index column that will reflect the order and then select all rows with Row <= 2 您可以通过两种方式执行此操作:1)添加将反映订单的行索引列,然后选择Row <= 2的所有行

SELECT amount, make,product
FROM 
(SELECT  ROW_NUMBER() OVER (PARTITION BY [product] ORDER BY [amount] DESC) AS [RowID],*
FROM [dbo].[Table1]) RESULT
WHERE RowID <= 2

2) You can also join the table to itself 2)您也可以将表连接到自己

SELECT a1.* FROM Table1 AS a1
  LEFT JOIN Table1 AS a2 
    ON a1.product = a2.product AND a1.amount<= a2.amount
GROUP BY a1.product
HAVING COUNT(*) <= 2;

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

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