简体   繁体   English

sql最小值来自一列,另一列唯一的第三列

[英]sql minimum from one column, unique for another column group by a third

I have this query where I'm selecting from three tables 我有这个查询,我从三个表中选择

select 
    min(t.ReminderDt) as 'rem dt',  
    m.Group_Id, m.AccountNumber 
from 
    ACE_AccsLevelTran t, ACE_AccsLevelMaster m 
where 
    t.MasterAccNumber = m.AccountNumber 
group by  
    m.Group_Id, m.AccountNumber;

This results in: 这导致:

rem dt | Group_Id| AccountNumber
--------------------------------    
2/8/2013 | 3 | 4216985
2/22/2013 | 4 | 4274863
2/7/2013 | 3 | 4366383
2/28/2013 | 4 | 7151712

How do I get the rows for only the minimum dates for 3 and 4 like the result - 我如何获得3和4的最小日期行,如结果 -

2/7/2013 | 3 | 4366383
2/22/2013 | 4 | 4274863

Just remove the account_number from the group by and surround it with min() or max() on the select line: 只需从组中删除account_number,并在select行上使用min()max()将其包围:

select min(t.ReminderDt) as 'rem dt',  m.Group_Id, min(m.AccountNumber)
from ACE_AccsLevelTran t,     ACE_AccsLevelMaster m 
where t.MasterAccNumber=m.AccountNumber
group by  m.Group_Id

That returns an arbitrary account number. 返回任意帐号。 To get the row with the minimum value, the best way is to use row_number() : 要获得具有最小值的 ,最好的方法是使用row_number()

select *
from (select t.ReminderDt) as 'rem dt',  m.Group_Id, m.AccountNumber,
            row_number() over (partition by group_id order by reminderdt desc) as seqnum
      from ACE_AccsLevelTran t join ACE_AccsLevelMaster m 
           on t.MasterAccNumber=m.AccountNumber
     ) t
where seqnum = 1

Also, you should learn ANSI standard JOIN syntax, as used in this query. 此外,您应该学习此标准中使用的ANSI标准JOIN语法。

If your accountNumber is Unique too you can do it in this way: 如果您的accountNumber也是唯一的,您可以这样做:

Select m.Group_Id ,X.MinReminderDT,m.AccountNumber 
from ACE_AccsLevelMaster m join(
select min(t.ReminderDt) as MinReminderDT,t.MasterAccNumber
from ACE_AccsLevelTran t
Group By t.MasterAccNumber) X on X.MasterAccNumber=m.AccountNumber

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

相关问题 SQL按一列分组,按另一列排序并转发第三列 - SQL group by one column, sort by another and transponse a third 通过另一列SQL中的唯一条目对列进行分组 - Group column by unique entries in another column SQL 如何在T / SQL中按一列分组并检索具有另一列最小值的行? - How do you group by one column and retrieve a row with the minimum value of another column in T/SQL? SQL 将一列的值按另一列分组 - SQL group values for one column by another column SQL从一个表列中选择一个最小值,然后将结果插入一个SQL语句的另一个表列中 - Sql Select a minimum value from a table column and insert the results in another table column in one SQL statement SQL对另一列中的每个唯一值求和一个值 - SQL sum one value for each unique value from another column 按一列分组并从另一列中查找最小值 - Grouping by one column and finding minimum from another SQL查询以选择另一列的最小一列最大值 - SQl query to select minimum of one column maximum of another column SQL Server 2008-在一列上进行透视,另一列进行分组,保持对第三列的引用 - SQL Server 2008 - Pivot on one column, group by another, maintain reference to third SQL按一列排序,然后按另一列分组 - SQL order by one column then group by another
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM