Reputation: 21
I'm trying to get the **ID_RE**
with the highest columns in **Date_Enter,Date_Process,Date_Shipment**
For example, here have a three rows.
- **1,2012-02-01,2016-02-01,2015-02-01,Send for MX4343**
- **2,2013-02-01,2008-04-01,2009-02-01,It's For trade**
- **3,2018-02-01,2019-05-01,2007-02-01,Like a environment**
This is the highest:
3,2018-02-01,2019-05-01,2007-02-01,Like a environment
I've been trying with this:
SELECT
MAX(Date_Enter),
MAX(Date_Process),
MAX(Date_Shipment),
Description
FROM Reports
But, not works
CREATE TABLE [dbo].[Reports](
[ID_RE] [int] IDENTITY(1,1) NOT NULL,
[Date_Enter] [datetime] NOT NULL,
[Date_Process] [datetime] NOT NULL,
[Date_Shipment] [varchar](20),
[Description] [varchar](20)
CONSTRAINT [PK_Reports] PRIMARY KEY CLUSTERED
(
[ID_RE] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
END
GO
Here we have some information
insert into Reports(Date_Enter,Date_Process,Date_Shipment,Description) values('2012-02-01 02:25:23','2016-02-01 08:45:23','2015-02-01 05:35:23','Send for MX4343')
insert into Reports(Date_Enter,Date_Process,Date_Shipment,Description) values('2013-02-01 01:52:23','2008-04-01 12:45:23','209-02-01 07:35:43','It is For trade')
insert into Reports(Date_Enter,Date_Process,Date_Shipment,Description) values('2018-02-01 04:34:33','2019-05-01 04:35:44','2007-02-01 09:35:09','Like a environment')
Upvotes: 1
Views: 89
Reputation: 17943
Problem:
Query you have written is not correct. I suggest you to read GROUP BY
in SQL
Note: You have following two issues in your question data.
1- Date_Shipment is not correct date as 209 is not a valid year
2- Data type of [Date_Shipment] is VARCHAR
it should be DATETIME
,
insert into Reports(Date_Enter,Date_Process,Date_Shipment,Description) values('2013-02-01 01:52:23','2008-04-01 12:45:23','209-02-01 07:35:43','It is For trade')
Solution :
If you want to find the MAX
based on all 3 columns and decide based on that, you can write like following query.
SELECT
ID_RE,
(SELECT MAX(MaxDate)
FROM (VALUES (Date_Enter),(Date_Process),(Date_Shipment)) AS FindMax(MaxDate))
AS MaxDate
FROM [dbo].[Reports]
if you want to find MAX
based on 1 date column, you can try like following. Following query will return multiple records if Date_Enter
is same for 2 or more records.
SELECT * FROM [dbo].[Reports]
WHERE Date_Enter =
(
SELECT MAX(Date_Enter) FROM [dbo].[Reports]
)
If you simply want to get 1 records based on Date_Enter you can try like following.
SELECT TOP 1 * FROM [dbo].[Reports]
ORDER BY Date_Enter DESC
Upvotes: 0
Reputation: 14669
You can apply order by
as per your requirement as below. If you wants that your data fetch the record which Date_Enter
is max then apply order by on that column. If you wants to get record by multiple order condition
then you can also apply order on multiple columns.
Order by Only Date_Enter:
SELECT TOP 1
ID_RE
Date_Enter,
Date_Process,
Date_Shipment,
Description
FROM Reports
ORDER BY Date_Enter DESC
Order by with Date_Enter,Date_Process and Date_Shipment:
SELECT TOP 1
ID_RE
Date_Enter,
Date_Process,
Date_Shipment,
Description
FROM Reports
ORDER BY Date_Enter DESC, Date_Process DESC, Date_Shipment DESC
Upvotes: 1