SQL - 提取具有最新日期和最低值的唯一名称
SQL - pull unique name with the lastest date and lowest value
如何获得具有最新日期和最低值的唯一名称。
命名日期值
布拉德 1/2/10 1.1
布拉德 1/2/10 2.3
鲍勃 1/6/10 1.0
布拉德 2/4/09 13.2
这个查询似乎不起作用
SELECT distinct
A.[ViralLoadMemberID]
,B.LastName
,B.FirstName
,A.[Date]
,A.[vaule]
FROM [t].[dbo].[tblViralLoad] A
left join [dbo].[tblEnrollees] B on A.ViralLoadMemberID = B.MemberID
where
A.Date =
(
select MAX(Date)
from dbo.tblViralLoad
where ViralLoadMemberID = A.ViralLoadMemberID
and
( Date >= '07/01/2014'
and Date <= '12/3/2014' ) )
想法是使用 order by
并只获取一行。如果您想要最新日期的最低值,标准 SQL 将是:
select t.*
from table t
order by desc desc, value asc
fetch first 1 row only;
对于旧版本的 SQL 服务器,您可以省略最后一行并执行 select top 1 * . . .
。对于 MySQL,最后一行将是 limit 1
。
有趣的 rank()
declare @t as table (name varchar(50),dte date,val decimal(18,10));
insert into @t(name,dte,val) values
('Dave','1/1/2015',1.0),
('Dave','1/3/2015',1.2),
('Dave','1/4/2015',1.5),
('Dave','1/10/2015',1.3),
('Dave','1/15/2015',1.2),
('Steve','1/11/2015',1.6),
('Steve','1/12/2015',1.1),
('Steve','1/15/2015',1.2),
('Bill','1/21/2015',1.9),
('Ted','1/1/2015',1.8),
('Ted','1/10/2015',1.0),
('Ted','1/12/2015',1.7)
-- This will show the lowest prices by each person
select name,dte,val from (select name,dte,val, rank() over (partition by name order by val) as r from @t) as data where r = 1
-- This will be users lowest price and the last day they sublitted a prices regurdless if it is the lowest
select name,max(dte) as [last Date] ,min(val) as [Lowest Value] from @t group by name
-- Who had the lowest price last regurdless if they have raised there price later.
select top(1) name,dte [last lowest quote],val from (select name,dte,val, rank() over (order by val) as r from @t) as data where r = 1 order by dte desc
-- what is the lowest price cueently quoted reguarless who quoted it
select top(1) name,dte [best active quote],val from (select name,dte,val, rank() over (partition by name order by dte desc) as r from @t) as data where r = 1 order by val
如何获得具有最新日期和最低值的唯一名称。
命名日期值
布拉德 1/2/10 1.1
布拉德 1/2/10 2.3
鲍勃 1/6/10 1.0
布拉德 2/4/09 13.2
这个查询似乎不起作用
SELECT distinct
A.[ViralLoadMemberID]
,B.LastName
,B.FirstName
,A.[Date]
,A.[vaule]
FROM [t].[dbo].[tblViralLoad] A
left join [dbo].[tblEnrollees] B on A.ViralLoadMemberID = B.MemberID
where
A.Date =
(
select MAX(Date)
from dbo.tblViralLoad
where ViralLoadMemberID = A.ViralLoadMemberID
and
( Date >= '07/01/2014'
and Date <= '12/3/2014' ) )
想法是使用 order by
并只获取一行。如果您想要最新日期的最低值,标准 SQL 将是:
select t.*
from table t
order by desc desc, value asc
fetch first 1 row only;
对于旧版本的 SQL 服务器,您可以省略最后一行并执行 select top 1 * . . .
。对于 MySQL,最后一行将是 limit 1
。
有趣的 rank()
declare @t as table (name varchar(50),dte date,val decimal(18,10));
insert into @t(name,dte,val) values
('Dave','1/1/2015',1.0),
('Dave','1/3/2015',1.2),
('Dave','1/4/2015',1.5),
('Dave','1/10/2015',1.3),
('Dave','1/15/2015',1.2),
('Steve','1/11/2015',1.6),
('Steve','1/12/2015',1.1),
('Steve','1/15/2015',1.2),
('Bill','1/21/2015',1.9),
('Ted','1/1/2015',1.8),
('Ted','1/10/2015',1.0),
('Ted','1/12/2015',1.7)
-- This will show the lowest prices by each person
select name,dte,val from (select name,dte,val, rank() over (partition by name order by val) as r from @t) as data where r = 1
-- This will be users lowest price and the last day they sublitted a prices regurdless if it is the lowest
select name,max(dte) as [last Date] ,min(val) as [Lowest Value] from @t group by name
-- Who had the lowest price last regurdless if they have raised there price later.
select top(1) name,dte [last lowest quote],val from (select name,dte,val, rank() over (order by val) as r from @t) as data where r = 1 order by dte desc
-- what is the lowest price cueently quoted reguarless who quoted it
select top(1) name,dte [best active quote],val from (select name,dte,val, rank() over (partition by name order by dte desc) as r from @t) as data where r = 1 order by val