Click here to Skip to main content
15,881,172 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have below [Location] table in MSSQL.
Location	Value
loc1      	value1    
loc1      	value2    
loc2      	value1    
loc2      	value2    
loc3      	value2    
loc3      	value3    
loc4      	value3    
loc4      	value4    

I want distinct value of Value like below o/p:
Location	Value
loc1      	value1    
loc1      	value2    
loc3      	value3    
loc4      	value4 


What I have tried:

SQL
select Location,value from Location
group by Location,value
order by location
Posted
Updated 24-Sep-18 2:38am
v4
Comments
ZurdoDev 24-Sep-18 8:51am    
What you put in the "What I have tried" section will give you the requested output, so what are you asking?
jaideepsinh 24-Sep-18 9:07am    
It gives me all records i want only distinct values for location.
ZurdoDev 24-Sep-18 9:11am    
But what does that mean? Distinct locations? Distinct values per location?

Because the sql you have will give you each value for each location. The sql you have will give you the output you showed you want.
[no name] 24-Sep-18 14:32pm    
What I read in all the comments you like to have distinct value only for Location. So far so good, now explain he logic what you like to see for Value. Is it maybe min(Value) ... or what else?

create table #Location (
	Location varchar(10),
	Value	varchar(20)
	)


insert into #Location select 'loc1','value1'    
insert into #Location select 'loc1','value2'    
insert into #Location select 'loc2','value1'    
insert into #Location select 'loc2','value2'    
insert into #Location select 'loc3','value2'    
insert into #Location select 'loc3','value3'    
insert into #Location select 'loc4','value3'    
insert into #Location select 'loc4','value4'


select Min(Location) as Location, Value from #Location group by Value


Results:

Location	Value
loc1	value1
loc1	value2
loc3	value3
loc4	value4



Based on comments below of additional fields including an ID field, here is an expanded example/solution:

create table #Location (
	ID int IDENTITY(1,1),
	Location varchar(10),
	Value	varchar(20),
	FieldA datetime 
	)


insert into #Location(Location, Value, FieldA) select 'loc1','value1',GETDATE()    
insert into #Location(Location, Value, FieldA) select 'loc1','value2',GETDATE()    
insert into #Location(Location, Value, FieldA) select 'loc2','value1',GETDATE()    
insert into #Location(Location, Value, FieldA) select 'loc2','value2',GETDATE()    
insert into #Location(Location, Value, FieldA) select 'loc3','value2',GETDATE()    
insert into #Location(Location, Value, FieldA) select 'loc3','value3',GETDATE()    
insert into #Location(Location, Value, FieldA) select 'loc4','value3',GETDATE()    
insert into #Location(Location, Value, FieldA) select 'loc4','value4',GETDATE()


Select ID, LocSumm.Location, LocSumm.Value, FieldA from #Location L
	inner join (
select Min(Location) as Location, Value from #Location group by Value) as LocSumm
on L.Location = LocSumm.Location and
	L.Value = LocSumm.Value
order by LocSumm.Location, LocSumm.Value


And the new result set based on this query:

ID	Location	Value	FieldA
1	loc1	value1	2018-09-24 09:48:45.700
2	loc1	value2	2018-09-24 09:48:45.700
6	loc3	value3	2018-09-24 09:48:45.700
8	loc4	value4	2018-09-24 09:48:45.703
 
Share this answer
 
v3
Comments
jaideepsinh 24-Sep-18 9:17am    
This will work when i have only this two column in select but there are many other columns so this will not give me proper o/p.
littleGreenDude 24-Sep-18 9:27am    
You should be able to take the select provided and join it to get the desired results. Put parenthesis around this select and add an "AS locSumm" and then inner join to your main select. Something like this:

Select Location, locSumm.Value, fieldA, fieldB, fieldC from Location L
inner join (
select Min(Location) as Location, Value from #Location group by Value
) AS locSumm
on locSumm.Location = L.Location and
locSumm.Value = L.Value
order by L.Location, L.Value
jaideepsinh 24-Sep-18 9:39am    
I have another column ID and when i add it in select its not working.
select Min(Location) as Location, Value,ID from Location group by Value,ID
littleGreenDude 24-Sep-18 10:07am    
I added an expanded example above, please let me know if this works for you?
jaideepsinh 24-Sep-18 11:12am    
that is working but as i told you i have many tables and columns so i need to think proceed with ROW_NUMBER().
 
Share this answer
 
Comments
jaideepsinh 24-Sep-18 9:07am    
I want only distinct values for location. so only distinct not works.
The problem is twofold, and the first one is trivial:
Location	Value
loc1      	value1    
loc1      	value2    
loc3      	value3    
loc4      	value4    
presumably should be
Location	Value
loc1      	value1    
loc2      	value2    
loc3      	value3    
loc4      	value4    
If not, then it makes no sense at all.
Then you have the second, rather bigger problem.
When you use GROUP BY Location, ValueYou are not telling SQL to group by anything useful - you are saying that the values both columns must be the same to be a single group: so "loc1, value1" and "loc1, value2" are in separate groups.

What you have to do to group them, is to specify one column "Location" as the GROUP BY, and then use an aggregate function to select the remaining data (MIN, MAX, SUM, AVERAGE ...) This may help: SQL GROUP By and the "Column 'name' is invalid in the select list because..." error[^]

What you want to do is a little complicated, because it's not at all obvious why you get the results you want: given that loc1 and loc2 both have the same Values, but select a different Value in the output makes it extremely difficult to work out exactly what you are trying to do ...
 
Share this answer
 
Comments
jaideepsinh 25-Sep-18 2:50am    
No, location should not be in order it should be like below if loc1 and loc2 record not exists:
Location Value
loc3 value1
loc3 value2
loc4 value3
loc4 value4
OriginalGriff 25-Sep-18 3:02am    
Just randomly posting tiny fragments of data without any explanation of what you expect to get or why doesn't help anyone.
jaideepsinh 25-Sep-18 3:30am    
That is not random post. As i explain i want first Location wise Distinct sequence value. And that is sql table so you might know data can not inserted manually and it can be anything. I just post sample data for people who are trying to help me.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900