Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
143 views
in Technique[技术] by (71.8m points)

sql server - Having trouble with a max date in SQL

So, new to writing SQL querys and reports, and I have a doozy.. for me anyway. Trying to only grab the latest auth_date including ties for that date. Because for each date there are multiple entries. This report works, but it returns all auth dates and not just the latest. For example, I have 6 entries for 11/9/2020 and 6 for 12/11/2020 and would only like the ones from 12/11/2020. Here is my code. Any help would be appreicated. TSQL

Select Clients.first_name,
  Clients.last_name,
  Clients.client_id,
  Authorizations.payer_id,
  Max(Authorizations.auth_date) As Max_auth_date,
  Authorizations.period_start,
  Authorizations.period_end,
  Clients.client_status,
  Authorizations.pending,
  Authorizations.auth_units,
  Authorizations.used_units,
  Authorizations.ext_units_used,
  Authorizations.auth_number,
  Authorizations.period_type,
  Authorizations.count_units,
  Authorizations.requested_units,
  Payer.payer_description,
  Team.team_name
From Authorizations
  Left Join Clients On Authorizations.client_id = Clients.client_id
  Inner Join Payer On Payer.payer_id = Authorizations.payer_id And
    Authorizations.payer_id = Payer.payer_id And Authorizations.payer_id =
    Payer.payer_id
  Right Join TeamClient On Clients.client_id = TeamClient.client_id
  Inner Join Team On Team.team_id = TeamClient.team_id And TeamClient.team_id =
    Team.team_id
  Inner Join EmployeeClients On Authorizations.client_id =
    EmployeeClients.client_id
Where EmployeeClients.primary_flag = 'True' And EmployeeClients.emp_id = @param1
Group By Clients.first_name,
  Clients.last_name,
  Clients.client_id,
  Authorizations.payer_id,
  Authorizations.period_start,
  Authorizations.period_end,
  Clients.client_status,
  Authorizations.pending,
  Authorizations.auth_units,
  Authorizations.used_units,
  Authorizations.ext_units_used,
  Authorizations.auth_number,
  Authorizations.period_type,
  Authorizations.count_units,
  Authorizations.requested_units,
  Payer.payer_description,
  Team.team_name
Order By Clients.last_name,
  Clients.first_name
question from:https://stackoverflow.com/questions/66055224/having-trouble-with-a-max-date-in-sql

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...