SELECT Row Values WHERE MAX() 是 GROUP BY 查询中的列值

SELECT Row Values WHERE MAX() is Column Value In GROUP BY Query(SELECT Row Values WHERE MAX() 是 GROUP BY 查询中的列值)
本文介绍了SELECT Row Values WHERE MAX() 是 GROUP BY 查询中的列值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我该如何选择?我可以创建用户定义的聚合函数吗

How can I select like this? Can I create a User defined Aggregate Function

SELECT Max(A),(SELECT TOP 1 FROM TheGroup Where B=Max(A)) FROM MyTable

SELECT Max(A),(SELECT TOP 1 FROM TheGroup Where B=Max(A)) FROM MyTable

其中 MyTable 如下所示

where MyTable as Shown Below

    A  B  C
--------------
    1  2  S
    3  4  S
    4  5  T
    6  7  T

我想要这样的查询

SELECT MAX(A),(B Where A=Max(A)),C FROM MYTable GROUP BY C

我期待的结果如下

 MAX(A)  Condition    C
-----------------------
   3        4         S
   6        7         T

推荐答案

尝试以下查询:

SELECT TABLE1.A , TABLE2.B , TABLE1.C
FROM 
(
    SELECT MAX(A) AS A,C 
    FROM MYTable 
    GROUP BY C
) AS TABLE1 
INNER JOIN
(
SELECT *
FROM MYTable
) AS TABLE2 ON TABLE1.A = TABLE2.A

SQLFIDDLE

您可以通过简单的连接查询来完成.join 查询总是比 In query 运行得更快.联接查询仅在执行查询时运行一次.我们可以使用 IN 查询存档相同的结果.

you can do it by simple join query . join query always run faster then In query . Join query run only one time at the time of execution of the query . we can archive same result by using IN query .

这篇关于SELECT Row Values WHERE MAX() 是 GROUP BY 查询中的列值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Execute complex raw SQL query in EF6(在EF6中执行复杂的原始SQL查询)
Hibernate reactive No Vert.x context active in aws rds(AWS RDS中的休眠反应性非Vert.x上下文处于活动状态)
Bulk insert with mysql2 and NodeJs throws 500(使用mysql2和NodeJS的大容量插入抛出500)
Flask + PyMySQL giving error no attribute #39;settimeout#39;(FlASK+PyMySQL给出错误,没有属性#39;setTimeout#39;)
auto_increment column for a group of rows?(一组行的AUTO_INCREMENT列?)
Sort by ID DESC(按ID代码排序)