SQL Server 2008:SELECT min(date)

时间:2022-09-26 15:23:31

I'm trying to do a simple select statement but I'm having a hard time doing it.

我正在尝试做一个简单的选择语句,但我很难做到这一点。

I have a table with these columns:

我有一个包含这些列的表:

companyID, CompanyDept, DateAdded

I'm trying to do a select statement like this...

我正在尝试做这样的选择声明......

Select CompanyID, CompanyDept 
where min(dateAdded)

So there are multiple dates for DateAdded - I'm trying to select the CompanyDept and COmpanyID by the earliest DateAdded.

所以DateAdded有多个日期 - 我试图通过最早的DateAdded选择CompanyDept和COmpanyID。

EDIT: Select CompanyID, CompanyDEpt where dateAdded=MIN so because there might be three or more dates such as 10/1/2015, 11/12/2015, 1/4/2016 -(rows of data i mean) I'm trying to select this date looking at the earliest possible date (10/1/2016)

编辑:选择CompanyID,CompanyDEpt其中dateAdded = MIN所以因为可能有三个或更多日期,如10/1 / 2015,11 / 12 / 2015,1 / 4/2016 - (我的意思是数据行)我正在尝试选择此日期查看最早可能的日期(10/1/2016)

3 个解决方案

#1


2  

You can try just getting the first record using TOP 1 and ordering by date.

您可以尝试使用TOP 1获取第一条记录并按日期排序。

SELECT TOP 1
    CompanyID, CompanyDept
FROM 
    Table
ORDER BY 
    dateAdded

#2


1  

Not totally clear what you want but I think is what you are looking for.

不完全清楚你想要什么,但我认为你正在寻找。

with sortedResults as
(
    Select CompanyID
        , CompanyDept 
        , ROW_NUMBER() over (partition by CompanyID, CompanyDept order by dateAdded desc) as RowNum 
    from YourTable
)

select CompanyID
    , CompanyDept
from sortedResults
where RowNum = 1

#3


1  

SELECT CompanyID, CompanyDept 
FROM TableName 
order by dateadd ASC 

I assume you want all record , not? you want the 1 record which is old or new for old do ASC and for new do DESC

我假设你想要所有记录,不是吗?你想要旧的或旧的1条记录做ASC和新的DESC

select top (1)* from CompanyID, CompanyDept 
FROM TableName 
order by dateadd ASC 

if not above read below: Q.how do you know that earlier date? is there another column name to compare date with.?

如果不在上面,请阅读以下内容:Q。您知道更早的日期吗?是否有另一个列名称来比较日期。?

if yes,

如是,

where dateadd <= that column
order by dateadd ASC 

#1


2  

You can try just getting the first record using TOP 1 and ordering by date.

您可以尝试使用TOP 1获取第一条记录并按日期排序。

SELECT TOP 1
    CompanyID, CompanyDept
FROM 
    Table
ORDER BY 
    dateAdded

#2


1  

Not totally clear what you want but I think is what you are looking for.

不完全清楚你想要什么,但我认为你正在寻找。

with sortedResults as
(
    Select CompanyID
        , CompanyDept 
        , ROW_NUMBER() over (partition by CompanyID, CompanyDept order by dateAdded desc) as RowNum 
    from YourTable
)

select CompanyID
    , CompanyDept
from sortedResults
where RowNum = 1

#3


1  

SELECT CompanyID, CompanyDept 
FROM TableName 
order by dateadd ASC 

I assume you want all record , not? you want the 1 record which is old or new for old do ASC and for new do DESC

我假设你想要所有记录,不是吗?你想要旧的或旧的1条记录做ASC和新的DESC

select top (1)* from CompanyID, CompanyDept 
FROM TableName 
order by dateadd ASC 

if not above read below: Q.how do you know that earlier date? is there another column name to compare date with.?

如果不在上面,请阅读以下内容:Q。您知道更早的日期吗?是否有另一个列名称来比较日期。?

if yes,

如是,

where dateadd <= that column
order by dateadd ASC