I am trying to select the Max value of each group based on the PrimaryMobile. The idea is that for each group I want to select its most recently entered value (based on its personID not on the DateCreated)
My data look likes
PersonID PrimaryMobile FirstName LastName City DateCreated
1 34455666 CAD Null Pu 01-01-2014
2 34455666 ABC AND Null 02-01-2015
3 34455666 BFG Null Tu Null
4 34567 New ABC Null 01-01-2014
5 34567 Null Null Ta 02-01-2014
Result that I want
PersonID PrimaryMobile FirstName LastName City DateCreated
3 34455666 BFG AND Tu 02-01-2015
5 34567 New ABC Ta 02-01-2014
Please, let me know I can achieve this
This honestly feels like a mess but it is the best answer I can come up with. What I am doing below is breaking each column into a unique table. I am then performing the grouping function on the table, finding the most recent Non Null value for each column and rejoining the columns based on the PrimaryMobile. Also as a note since you did not give the table name I am going to assume the table name is "Person"
SQL
Note though that this solution will remove a number if any one of the columns has no value assocuated with its group. ie if you removed NEW from the FirstName column of your dataset you wuold not get the second number in your result set.
It appears you're trying to get the "best" value out of each of your columns. "Best" being defined as not null and/or max value (even for personid - which doesn't appear to be your primary key). Also, it appears to you might want "ABC" instead of "ANB" for the Lastname value of 34567.
One way to do this is with subqueries for each of the select parameters. I've named your table t1:
One way you can achieve your desired results is with a sub-select for every column (except PrimaryMobile):