本文介绍了如何从没有主id的同一列获取最小值和最大值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! CREATE TABLE dim.SaleTable ([年] INTEGER,国家 VARCHAR ( 50 ),省 VARCHAR ( 50 ), Sales INTEGER ); INSERT INTO dim.SaleTable([year],Country,Province,销售) VALUES ( 2008 ,' USA',' NJ', 10000 ),( 2008 , ' INDIA',' DELHI', 5000 ),( 2008 ,' USA',' WA ', 40000 ),( 2008 ,' CHINA',' Beijing', 10000 ),( 2009 ,' CHINA',' 上海', 30000 ),( 2009 ,' UK',' 伦敦', 10000 ), ( 2009 ,' INDIA',' TamilNadu', 20000 ),( 2009 ,' USA' ,' NJ', 30000 ),( 2010 ,' UK',' London', 60000 ),( 2011 ,' CHINA',' Beijing', 70000 ),( 2011 ,' INDIA',' Delhi', 80000 ),( 2011 ,' FRANCE',' Mende', 50000 ),( 2011 ,' USA',' NY', 60000 )< pre lang = SQL> 这是表格,我想从此表中获得最大销售额和最小销售额的最小和最大销售额。我可以获得最大和最小销售额,但最低价格状态和最大销售状态我没有得到。 SELECT Rank1,MAX(销售) AS MAXSales,MIN(销售) AS MinSales,Country FROM ( SELECT 国家,省,销售,RANK( ) OVER ( ORDER BY 国家/地区) AS Rank1 FROM dim.SaleTable GROUP BY 国家,省,销售)d GRoup BY Rank1,Country 国家MaxSales MaxSaleprovince MinSales MinSaleprovince ======= ======== =============== ========= ============== = CHINA 70000 beiging 10000 beijing 印度80000德里20000 tamilnadu 解决方案 这就是我的想法with: WITH cte1 AS ( SELECT s.Country,s.Province,s.Sales ,ROW_NUMBER() OVER ( PARTITION BY s.Country ORDER BY s.Sales) AS RMX ,ROW_NUMBER() OVER ( PARTITION BY s.Country ORDER BY s.Sales DESC ) AS RM N 来自 #SaleTable s ) SELECT c1.Country , c1.Province AS ProvinceMin,c1.Sales AS SalesMin, c2 .Province AS ProvinceMax,c2.Sales AS SalesMax 来自 cte1 c1 INNER JOIN cte1 c2 ON c1.Country = c2.Country AND c2.RMX = 1 WHERE c1.RMN = 1 ; CREATE TABLE dim.SaleTable([Year] INTEGER,Country VARCHAR(50),Province VARCHAR(50),Sales INTEGER);INSERT INTO dim.SaleTable([year],Country,Province,Sales)VALUES (2008,'USA','NJ',10000),(2008,'INDIA','DELHI',5000),(2008,'USA','WA',40000),(2008,'CHINA','Beijing',10000),(2009,'CHINA','Shanghai',30000),(2009,'UK','London',10000),(2009,'INDIA','TamilNadu',20000),(2009,'USA','NJ',30000),(2010,'UK','London',60000),(2011,'CHINA','Beijing',70000),(2011,'INDIA','Delhi',80000),(2011,'FRANCE','Mende',50000),(2011,'USA','NY',60000)<pre lang="SQL">This is the table and I want to get min and max sales with max sales province and min sales province from this table. I can get max and min sales but for min value of state and max sales of state I am not getting.SELECT Rank1,MAX(Sales) AS MAXSales,MIN(Sales) AS MinSales,CountryFROM (SELECT Country, Province,sales,RANK() OVER (ORDER BY Country) AS Rank1 FROM dim.SaleTable GROUP BY Country,Province,Sales)dGRoup BY Rank1,CountryCountry MaxSales MaxSaleprovince MinSales MinSaleprovince ======= ======== =============== ========= =============== CHINA 70000 beiging 10000 beijing India 80000 delhi 20000 tamilnadu 解决方案 Here is what I came up with:WITH cte1 AS ( SELECT s.Country, s.Province, s.Sales , ROW_NUMBER() OVER(PARTITION BY s.Country ORDER BY s.Sales) AS RMX , ROW_NUMBER() OVER(PARTITION BY s.Country ORDER BY s.Sales DESC) AS RMN from #SaleTable s)SELECT c1.Country, c1.Province AS ProvinceMin, c1.Sales AS SalesMin, c2.Province AS ProvinceMax, c2.Sales AS SalesMaxfrom cte1 c1INNER JOIN cte1 c2 ON c1.Country = c2.Country AND c2.RMX = 1WHERE c1.RMN = 1; 这篇关于如何从没有主id的同一列获取最小值和最大值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-18 18:49