SQL Query获取Max版本值 [英] SQL Query to get Max version Value
问题描述
我有一个如下表格
Unique_ID Person_ID产品版本数量
1 1高露洁1 8
2 1高露洁2 5
3 1高露洁3 2
4 1 Vicco 1 5
5 1 Vicco 2 2
6 1 Vicco 3 10
从这个我需要的结果表如下。结果应该是最大值(版本)
Unique_ID Person_ID产品版本数量
3 1高露洁3 2
6 1 Vicco 3 10
请帮助我实现这个
谢谢
Mohan
Hi,
I Have a table like below
Unique_ID Person_ID Product Version Count
1 1 Colgate 1 8
2 1 Colgate 2 5
3 1 Colgate 3 2
4 1 Vicco 1 5
5 1 Vicco 2 2
6 1 Vicco 3 10
From this i need result table as below . Result should be max (Version)
Unique_ID Person_ID Product Version Count
3 1 Colgate 3 2
6 1 Vicco 3 10
Please help me to achive this
Thanks
Mohan
推荐答案
你应该使用下一个SQL:
You should use the next SQL:
SELECT Unique_ID, Person_ID,Product, Version, Count FROM YourTableName
WHERE Version in (SELECT distinct MAX(Version) FROM YourTableName GROUP BY Product)
create table sample (Unique_ID bigint, Person_ID bigint, Product nvarchar(max), Version bigint, Count bigint)
insert Into sample values(1,1,'colgate',1,8)
insert Into sample values(2,1,'colgate',2,5)
insert Into sample values(3,1,'colgate',3,2)
insert Into sample values(4,1,'Vicco',1,5)
insert Into sample values(5,1,'Vicco',2,2)
insert Into sample values(6,1,'Vicco',3,10)
查询:
query:
select t2.unique_id ,t2.Person_ID,t2.product,t2.version,t2.count from (select max(version) as version,Product from sample group by product) t1
inner join (select *from sample) as t2 on t1.version=t2.Version and t1.Product=t2.Product
你几乎得到了答案。使用
SQL MAX() [ ^ ]实现此目的。
SELECT MAX(Version)as MaxVersion FROM YourTableName WHERE ProductName ='Colgate';
-KR
You almost had your answer. Use
SQL MAX()[^] to achieve this.
SELECT MAX(Version) as MaxVersion FROM YourTableName WHERE ProductName='Colgate';
-KR
这篇关于SQL Query获取Max版本值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!