I have three columns in a table Id, State, City. Each state contains dozens of cities. all I want to do is select unique states and get their Id.
SELECT DISTINCT(State) FROM LocationTable;
This works perfectly, but I need to get Id too. I tried:
SELECT DISTINCT(State), Id FROM LocationTable
And
SELECT State, Id FROM LocationTable GROUP BY State, Id
But it still doesn’t return unique states.. Basically I need to get this MySQL query running on MSSQL:
SELECT UNIQUE(State), Id FROM LocationTable;
Thanks!
What do you expect it to do when it finds 2 states that match, but have different IDs? It would return the state twice, once per ID.
I suspect your data is something like this:
When you say you want the ID for the state, you are going to have to make a choice about which ID would would want, max, min, etc. There is no single ID for the state.
Response to comment : It could be something like…
But I think the validity of the ID is really in question here, what are you going to do with the ID, it will be for 1 row only, not all rows for that state. (Cast to now deal with the fact its a uniqueidentifier field)