Choosing values from multiple columns based on latest value of one column I have an Azure Cosmos DB that contains columns DateTime, userName, temperature, and bloodPressure, plus many other columns. There are several distinct userName values, and each userName has temperature and bloodPressure data collected at different values of DateTime. The temperature and bloodPressure values are different for each of those 15 times.
Here is an example of the data,
userName, DateTime, bloodPressure, temperature, heartBeat, hatSize
Curly, &quot;2021-01-19 18:24:53&quot;, 121, 98, 60, 7.25
Larry, &quot;2021-01-14 18:24:53&quot;, 125, 99, 80, 7.5
Mo, &quot;2021-01-13 18:24:53&quot;, 123, 100, 70, 7.75
Curly, &quot;2021-01-18 18:24:53&quot;, 120, 97, 50, 8
Larry, &quot;2021-01-13 18:24:53&quot;, 119, 99, 75, 7.15
Larry, &quot;2021-01-15 18:24:53&quot;, 115, 98, 85, 7.37
Mo, &quot;2021-01-12 18:24:53&quot;, 110, 102, 100, 8.01
Mo, &quot;2021-01-20 18:24:53&quot;, 130, 99, 110, 6.97
Larry, &quot;2021-01-18 18:24:53&quot;, 127, 98, 72, 7.76
Curly, &quot;2021-01-17 18:24:53&quot;, 126, 97, 82, 8.0

For each user, I would like to return the DateTime of their latest measurement, and their temperature and bloodPressure from their latest DateTime:
Larry, &quot;2021-01-18 18:24:53&quot;, 127, 98
Mo, &quot;2021-01-20 18:24:53&quot;, 130, 99
Curly, &quot;2021-01-19 18:24:53&quot;, 121, 98

I tried this,
SELECT  
    c.userName,
    MAX(c.DateTime),
    c.bloodPressure, 
    c.temperature
FROM CheckupData c
GROUP BY 
    c.userName

but got an error that GROUP BY should include the other outputs of SELECT. Since bloodPressure and temperature are always changing, it seems like I shouldn't use them in GROUP BY, because I only want the lastest values. What am I doing wrong?
(If it matters, my real database is larger: 50,000 userNames measured at 50 or so DateTime values each, and 30 or so columns).
EVERY Column in a SELECT has to be in the GROUP BY or use a aggregator function
SELECT  
    c.userName,
    MAX(c.DateTime),
    c.bloodPressure, 
    c.temperature
FROM CheckupData c
GROUP BY 
    c.userName,c.bloodPressure, c.temperature

OR
SELECT  
    c.userName,
    MAX(c.DateTime),
    AVG(c.bloodPressure), 
    AVG(c.temperature)
FROM CheckupData c
GROUP BY 
    c.userName

As we don't know nothing about your data and desired result. You have to choose what values you need.
For big tables you can use


CREATE TABLE CheckupData 
    ([userName] varchar(5), [DateTime] varchar(21), [bloodPressure] int, [temperature] int, [heartBeat] int, [hatSize] int)
;
    
INSERT INTO CheckupData 
    ([userName], [DateTime], [bloodPressure], [temperature], [heartBeat], [hatSize])
VALUES
    ('Curly', '&quot;2021-01-19 18:24:53&quot;', 121, 98, 60, 7.25),
    ('Larry', '&quot;2021-01-14 18:24:53&quot;', 125, 99, 80, 7.5),
    ('Mo', '&quot;2021-01-13 18:24:53&quot;', 123, 100, 70, 7.75),
    ('Curly', '&quot;2021-01-18 18:24:53&quot;', 120, 97, 50, 8),
    ('Larry', '&quot;2021-01-13 18:24:53&quot;', 119, 99, 75, 7.15),
    ('Larry', '&quot;2021-01-15 18:24:53&quot;', 115, 98, 85, 7.37),
    ('Mo', '&quot;2021-01-12 18:24:53&quot;', 110, 102, 100, 8.01),
    ('Mo', '&quot;2021-01-20 18:24:53&quot;', 130, 99, 110, 6.97),
    ('Larry', '&quot;2021-01-18 18:24:53&quot;', 127, 98, 72, 7.76),
    ('Curly', '&quot;2021-01-17 18:24:53&quot;', 126, 97, 82, 8.0)
;



GO




SELECT
cd.[userName], cd.[DateTime], cd.[bloodPressure], cd.[temperature], cd.[heartBeat], cd.[hatSize]
FROM CheckupData cd INNER JOIN 
(SELECT MAX([DateTime]) lastdate,[userName] FROM CheckupData GROUP BY [userName]) cd1
ON cd.[userName]  = cd1.[userName] AND cd.[DateTime] = cd1.lastdate
GO


userName | DateTime              | bloodPressure | temperature | heartBeat | hatSize
:------- | :-------------------- | ------------: | ----------: | --------: | ------:
Mo       | &quot;2021-01-20 18:24:53&quot; |           130 |          99 |       110 |       6
Larry    | &quot;2021-01-18 18:24:53&quot; |           127 |          98 |        72 |       7
Curly    | &quot;2021-01-19 18:24:53&quot; |           121 |          98 |        60 |       7


db&lt;&gt;fiddle here