sql - Find records using max date but exclude time - Stack Overflow

I need a query to pull records from a SQL Server table based on the max date in the InsertDTS column an

I need a query to pull records from a SQL Server table based on the max date in the InsertDTS column and the date should not be included in the output.

The InsertDTS column is defined as Datetime. I need it to pull everything with the max date, but ignore the time, since records can be loaded throughout the day. I have the query below but it is pulling only records with the most recent date and time.

SELECT
    [Payer],
    [File],
    [Data_Rows],
    [Amt_Billed],
    [Amt_Paid] 
FROM
    [Customer].[dbo].[Billing] 
WHERE
    InsertDTS = (SELECT MAX(InsertDTS) 
                 FROM [Customer].[dbo].[Billing])

I tried using a CAST in the WHERE clause, but could not get it to work.

I need a query to pull records from a SQL Server table based on the max date in the InsertDTS column and the date should not be included in the output.

The InsertDTS column is defined as Datetime. I need it to pull everything with the max date, but ignore the time, since records can be loaded throughout the day. I have the query below but it is pulling only records with the most recent date and time.

SELECT
    [Payer],
    [File],
    [Data_Rows],
    [Amt_Billed],
    [Amt_Paid] 
FROM
    [Customer].[dbo].[Billing] 
WHERE
    InsertDTS = (SELECT MAX(InsertDTS) 
                 FROM [Customer].[dbo].[Billing])

I tried using a CAST in the WHERE clause, but could not get it to work.

Share Improve this question edited Mar 6 at 5:18 marc_s 757k184 gold badges1.4k silver badges1.5k bronze badges asked Mar 6 at 0:12 DonDon 2241 silver badge7 bronze badges 3
  • Please tag the RDBMS you are using. – Dale K Commented Mar 6 at 0:12
  • Please also provide a minimal reproducible example with sample data and desired results. – Dale K Commented Mar 6 at 0:12
  • Yeah. I think that did it. I was trying to CAST. Gracias! – Don Commented Mar 6 at 0:26
Add a comment  | 

2 Answers 2

Reset to default 4

From a performance perspective its best not to run functions against columns in your where clause because it makes the query unsargable i.e. unable to use indexes. Therefore its best to use a date window rather than a specific date e.g.

select
  [Payer]
  , [File]
  , [Data_Rows]
  , [Amt_Billed]
  , [Amt_Paid] 
from [Customer].[dbo].[Billing] 
where InsertDTS >= (
  select convert(date, max(InsertDTS))
  from [Customer].[dbo].[Billing]
)

and it works equally well with cast e.g.

select
  [Payer]
  , [File]
  , [Data_Rows]
  , [Amt_Billed]
  , [Amt_Paid] 
from [Customer].[dbo].[Billing] 
where InsertDTS >= (
  select cast(max(InsertDTS) as date)
  from [Customer].[dbo].[Billing]
)

Convert the DATETIME to DATE to ignore the time of day.

select [Payer]
      ,[File]
      ,[Data_Rows]
      ,[Amt_Billed]
      ,[Amt_Paid] 
      from [Customer].[dbo].[Billing] 
      where CONVERT(DATE, InsertDTS) = (select CONVERT(DATE, MAX(InsertDTS)) from [Customer].[dbo].[Billing])

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745001606a4605527.html

相关推荐

  • sql - Find records using max date but exclude time - Stack Overflow

    I need a query to pull records from a SQL Server table based on the max date in the InsertDTS column an

    16小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信