T-SQL为MIN和MAX返回不同的行值

时间:2018-05-26 18:54:50

标签: sql-server tsql max min

我有一些看起来像这样的数据:

LA Code Local_Authority         Region          Spend per pupil
----------------------------------------------------------------
831     Derby                   East Midlands   4370
830     Derbyshire              East Midlands   4600
822     Bedford                 East of England 4694
873     Cambridgeshire          East of England 4455
301     Barking & Dagenham      London          5377
302     Barnet                  London          4965
330     Birmingham              West Midlands   5483
331     Coventry                West Midlands   4970

我想创建一个汇总表,显示一个地区,一个地方当局的最低支出和最高支出,如下所示:

Region            Min_LA_Name    Min_Amount   Max_LA_Name        Max_Amount
---------------------------------------------------------------------------
East Midlands     Derby          4370         Derbyshire         4600
East of England   Cambridgeshire 4455         Bedford            4694
London            Barnet         4965         Barking & Dagenham 5377
West Midlands     Coventry       4970         Birmingham         4970

我已经得到以下代码:

SELECT 
    Region,
    (SELECT Local_Authority 
     FROM LASpendPerPupil_df 
     GROUP BY REGION 
     HAVING MIN("Spend per pupil")) AS Min_LA_Name, 
    MIN("Spend per pupil") AS Min_Amount,
    (SELECT Local_Authority 
     FROM LASpendPerPupil_df 
     GROUP BY REGION 
     HAVING MAX("Spend per pupil")) AS Max_LA_Name,
    MAX("Spend per pupil") AS Max_Amount
FROM
    LASpendPerPupil_df
GROUP BY 
    REGION;

我得到最小值和最大值的正确输出,但是Local Authority name列只是重复第一个区域的最小和最大名称,所以这样:

Region            Min_LA_Name    Min_Amount   Max_LA_Name       Max_Amount
---------------------------------------------------------------------------
East Midlands     Derby          4370         Derbyshire         4600
East of England   Derby          4455         Derbyshire         4694
London            Derby          4965         Derbyshire         5377
West Midlands     Derby          4970         Derbyshire         4970

任何人都可以帮我解决这个问题吗?我是一个Stack Overflow新手,对SQL来说是新手,如果我没有正确发布,我会道歉。

2 个答案:

答案 0 :(得分:0)

试试这个:

select a.region,b.la_name as min_la_name,a.min_amount,c.la_name as max_la_name,a.max_amount
from
(select region,min([SPEND PER PUPIL])  as min_amount, max([SPEND PER PUPIL]) as max_amount
from your_table
group by region ) a
left join
your_table b
on a.region = b.region and a.min_amount = b.[SPEND PER PUPIL]
left join
your_table c
on a.region = c.region and a.max_amount = c.[SPEND PER PUPIL];

如果有任何澄清,请告诉我

答案 1 :(得分:-1)

使用示例数据/脚本编辑实际上是正确的。

IF OBJECT_ID('tempdb..#Tmp') IS NOT NULL
    DROP TABLE #Tmp 

CREATE TABLE #Tmp (
    [LA Code] int
    , Local_Authority VARCHAR(50)
    , Region VARCHAR(50)
    , [Spend per pupil] INT);   

INSERT INTO #Tmp VALUES
(831,'Derby','East Midlands',4370),
(830,'Derbyshire','East Midlands', 4600),
(822,'Bedford','East of England', 4694),
(873,'Cambridgeshire','East of England', 4455),
(301,'Barking & Dagenham','London',5377),
(302,'Barnet','London',4965),
(330,'Birmingham','West Midlands',5483),
(331,'Coventry','West Midlands',4970)

SELECT
    x.Region
    , (SELECT Local_Authority 
        FROM #Tmp WHERE Region=x.Region AND [Spend per pupil]=MaxSpend) AS MaxAuth
    , MaxSpend
    , (SELECT Local_Authority 
        FROM #Tmp WHERE Region=x.Region AND [Spend per pupil]=MinSpend) AS MinAuth
    , MinSpend 
FROM
    (SELECT Region, MAX([Spend per pupil]) AS MaxSpend 
    FROM #Tmp GROUP BY Region) x
LEFT OUTER JOIN
    (SELECT Region, MIN([Spend per pupil]) AS MinSpend 
    FROM #Tmp GROUP BY Region) y on x.Region = y.region

enter image description here