SQL聚合和查询中显示的其他字段
我有一个查询,其中需要DateTime字段的SQL聚合和查询中显示的其他字段,sql,sql-server,aggregate,Sql,Sql Server,Aggregate,我有一个查询,其中需要DateTime字段的MIN,然后需要同一行中相应字段的值 现在,我有类似的东西,但是如果不将它也放在聚合子句中,我就不能得到Price字段,这不是我想要的 从MyData中选择MIN([Registration Time])、Price,其中[Product Series]=“XXXXX” 我需要Registration Time字段的MIN,然后我只需要该行对应的Price字段,但是如何显示 我还需要我的WHERE子句,如图所示 我肯定我忽略了一些非常明显的事情。使用S
MIN
,然后需要同一行中相应字段的值
现在,我有类似的东西,但是如果不将它也放在聚合子句中,我就不能得到Price
字段,这不是我想要的
从MyData中选择MIN([Registration Time])、Price,其中[Product Series]=“XXXXX”
我需要Registration Time
字段的MIN
,然后我只需要该行对应的Price
字段,但是如何显示
我还需要我的WHERE
子句,如图所示
我肯定我忽略了一些非常明显的事情。使用SQL Server 2008如果您只需要一条带有
[Registration Time],Price
的记录,就可以这么简单:
select top 1 [Registration Time], Price
from MyData
where [Product Series] = 'XXXXX'
order by [Registration Time]
如果您希望所有[产品系列]
的[注册时间]
和相应的价格
,则有几种方法,例如,使用row_number()函数:
with cte as (
select
[Registration Time], Price,
row_number() over(partition by [Product Series] order by [Registration Time]) as rn
from MyData
)
select
[Registration Time], Price, [Product Series]
where rn = 1
如果您只需要一条带有
[注册时间]、价格的记录,就可以这么简单:
select top 1 [Registration Time], Price
from MyData
where [Product Series] = 'XXXXX'
order by [Registration Time]
如果您希望所有[产品系列]
的[注册时间]
和相应的价格
,则有几种方法,例如,使用row_number()函数:
with cte as (
select
[Registration Time], Price,
row_number() over(partition by [Product Series] order by [Registration Time]) as rn
from MyData
)
select
[Registration Time], Price, [Product Series]
where rn = 1