如何使用Sql查询获得以下解释的结果 [英] how can i get a result as Explained below using Sql query

查看:84
本文介绍了如何使用Sql查询获得以下解释的结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的大家,

我在SQL中有这样的值表

Dear ALL,

I have table with values in sql like this

SID   ID       Name      version
1     1         A          1.0
2     1         B          1.1
3     1         C          1.2
4     2         D          1.0


在这里,我需要选择具有ID和名称的版本的最大值.

我需要基于上表的结果


Here I need to select Max values of version with ID and Name.

I need result based on above table

SID   ID     Name        version
3      1       C          1.2
4      2       D          1.0


如何选择呢?.

在此先感谢..!


How to select this?.

Thanks in Advance..!

推荐答案


我通过以下查询实现了此输出

Hi,
I acheived this output by following query

with cte(ID,version)
as
(
    select distinct ID,max(version) from Table_Name
    group by ID
)
select  SID,t.ID,Name,t.version from Table_Name t
join cte c on t.version = c.version and t.ID = c.ID


尝试一下:
Try this:
SELECT t1.*
FROM (SELECT *
        FROM YourTable) AS t1
    RIGHT JOIN (SELECT [ID],  MAX([Version]) AS [Version]
    FROM YourTable
    GROUP BY [ID]) AS t2
    ON t1.[ID] = t2.[ID] AND t1.[Version] = t2.[Version]


hiii,

这是查询

从table_1 t选择SID,ID,名称,版本,其中version =(从table_1选择MAX(version)从ID = t.ID)按SID顺序
hiii ,

here is the query

select SID,ID,name,version from table_1 t where version=(select MAX(version) from table_1 where ID=t.ID ) order by SID


这篇关于如何使用Sql查询获得以下解释的结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆