Quantcast
Channel: Microsoft SQL Server
Viewing all articles
Browse latest Browse all 4871

SQL - How to Return Multiple Fields from SubQuery using MIN(date) and GROUP BY

$
0
0

I am appealing to the T-SQL Spice gurus: my head-banging has created a big-enough hole in my cube wall already...

I run T-SQL on SQL Server 2014. I'm trying to return multiple fields from a subquery that uses MIN function on a date field. I've not been able to find the answer on the GoogleWebs.

The ClientVisit table contains ClientVisitID, ClientID, and VisitDate (among many, many other fields, but these are the pertinent ones for this query). I want to return the ClientVisitID value for the earliest VisitDate for each ClientID.

So the data in the table looks like this:

ClientID

ClientVisitID

VisitDate

111

5109

03/01/2018

111

5105

02/01/2018

111

5101

01/01/2018

222

5201

03/02/2018

222

5205

02/02/2018

222

5209

01/02/2018

333

5301

01/03/2018

333

5302

03/01/2018

I...


Viewing all articles
Browse latest Browse all 4871

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>