SQL Server - Select if one of all columns are unique
up vote
0
down vote
favorite
I want to select if one row where multiple columns are the same. For example:
col1 col2 col3 col4
a b 1 2
b b 1 2
a c 1 2
b b 1 3
a c 2 1
Condition: Select only if values of columns (col1, col2, col3) are different from other rows and value of col4 is max of rows which are the same.
For example expected Output is:
a b 1 2
b b 1 3
a c 1 2
a c 2 1
sql sql-server tsql sql-server-2012
|
show 2 more comments
up vote
0
down vote
favorite
I want to select if one row where multiple columns are the same. For example:
col1 col2 col3 col4
a b 1 2
b b 1 2
a c 1 2
b b 1 3
a c 2 1
Condition: Select only if values of columns (col1, col2, col3) are different from other rows and value of col4 is max of rows which are the same.
For example expected Output is:
a b 1 2
b b 1 3
a c 1 2
a c 2 1
sql sql-server tsql sql-server-2012
2
SQL queries become increasingly possible when you actually try to write them. That being said, have you tried answering this question yet yourself?
– Tim Biegeleisen
Nov 10 at 14:22
you have two answers because it is not clear what you mean when you say col4 is max. max of what? col3 and col4? other matching rows? something else?
– Hogan
Nov 10 at 15:11
@TimBiegeleisen I've tried with distinct and it got no point
– Hung Nguyen
Nov 10 at 15:21
@Hogan I couldn't get your point, guys. value for col4 is integer so if there is multiple same rows, It will return row have max value for col4
– Hung Nguyen
Nov 10 at 15:24
@HungNguyen it would be nice if you accept the most appropriate answer
– Salman A
Nov 10 at 15:59
|
show 2 more comments
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I want to select if one row where multiple columns are the same. For example:
col1 col2 col3 col4
a b 1 2
b b 1 2
a c 1 2
b b 1 3
a c 2 1
Condition: Select only if values of columns (col1, col2, col3) are different from other rows and value of col4 is max of rows which are the same.
For example expected Output is:
a b 1 2
b b 1 3
a c 1 2
a c 2 1
sql sql-server tsql sql-server-2012
I want to select if one row where multiple columns are the same. For example:
col1 col2 col3 col4
a b 1 2
b b 1 2
a c 1 2
b b 1 3
a c 2 1
Condition: Select only if values of columns (col1, col2, col3) are different from other rows and value of col4 is max of rows which are the same.
For example expected Output is:
a b 1 2
b b 1 3
a c 1 2
a c 2 1
sql sql-server tsql sql-server-2012
sql sql-server tsql sql-server-2012
edited Nov 11 at 10:42
William Robertson
7,98232133
7,98232133
asked Nov 10 at 14:20
Hung Nguyen
135
135
2
SQL queries become increasingly possible when you actually try to write them. That being said, have you tried answering this question yet yourself?
– Tim Biegeleisen
Nov 10 at 14:22
you have two answers because it is not clear what you mean when you say col4 is max. max of what? col3 and col4? other matching rows? something else?
– Hogan
Nov 10 at 15:11
@TimBiegeleisen I've tried with distinct and it got no point
– Hung Nguyen
Nov 10 at 15:21
@Hogan I couldn't get your point, guys. value for col4 is integer so if there is multiple same rows, It will return row have max value for col4
– Hung Nguyen
Nov 10 at 15:24
@HungNguyen it would be nice if you accept the most appropriate answer
– Salman A
Nov 10 at 15:59
|
show 2 more comments
2
SQL queries become increasingly possible when you actually try to write them. That being said, have you tried answering this question yet yourself?
– Tim Biegeleisen
Nov 10 at 14:22
you have two answers because it is not clear what you mean when you say col4 is max. max of what? col3 and col4? other matching rows? something else?
– Hogan
Nov 10 at 15:11
@TimBiegeleisen I've tried with distinct and it got no point
– Hung Nguyen
Nov 10 at 15:21
@Hogan I couldn't get your point, guys. value for col4 is integer so if there is multiple same rows, It will return row have max value for col4
– Hung Nguyen
Nov 10 at 15:24
@HungNguyen it would be nice if you accept the most appropriate answer
– Salman A
Nov 10 at 15:59
2
2
SQL queries become increasingly possible when you actually try to write them. That being said, have you tried answering this question yet yourself?
– Tim Biegeleisen
Nov 10 at 14:22
SQL queries become increasingly possible when you actually try to write them. That being said, have you tried answering this question yet yourself?
– Tim Biegeleisen
Nov 10 at 14:22
you have two answers because it is not clear what you mean when you say col4 is max. max of what? col3 and col4? other matching rows? something else?
– Hogan
Nov 10 at 15:11
you have two answers because it is not clear what you mean when you say col4 is max. max of what? col3 and col4? other matching rows? something else?
– Hogan
Nov 10 at 15:11
@TimBiegeleisen I've tried with distinct and it got no point
– Hung Nguyen
Nov 10 at 15:21
@TimBiegeleisen I've tried with distinct and it got no point
– Hung Nguyen
Nov 10 at 15:21
@Hogan I couldn't get your point, guys. value for col4 is integer so if there is multiple same rows, It will return row have max value for col4
– Hung Nguyen
Nov 10 at 15:24
@Hogan I couldn't get your point, guys. value for col4 is integer so if there is multiple same rows, It will return row have max value for col4
– Hung Nguyen
Nov 10 at 15:24
@HungNguyen it would be nice if you accept the most appropriate answer
– Salman A
Nov 10 at 15:59
@HungNguyen it would be nice if you accept the most appropriate answer
– Salman A
Nov 10 at 15:59
|
show 2 more comments
2 Answers
2
active
oldest
votes
up vote
1
down vote
accepted
Mandatory NOT EXISTS
solution... your condition written as a not exist query:
DECLARE @t TABLE (col1 varchar(100), col2 varchar(100), col3 int, col4 int);
INSERT INTO @t VALUES
('a', 'b', 1, 2),
('a', 'c', 1, 2),
('a', 'c', 2, 1),
('b', 'b', 1, 2),
('b', 'b', 1, 3);
SELECT *
FROM @t AS t
WHERE NOT EXISTS (
SELECT 1
FROM @t AS dup
WHERE dup.col1 = t.col1
AND dup.col2 = t.col2
AND dup.col3 = t.col3
AND dup.col4 > t.col4 -- outer row has smaller col4
)
Demo on DB Fiddle
What amazing! That's working now. Tkssss
– Hung Nguyen
Nov 10 at 15:37
add a comment |
up vote
4
down vote
Yes possible, just use group by
with max
aggregation as
with tab(col1,col2,col3,col4) as
(
select 'a','b',1,2 union all
select 'b','b',1,2 union all
select 'a','c',1,2 union all
select 'b','b',1,3 union all
select 'a','c',2,1
)
select col1, col2, col3, max(col4) as col4
from tab
group by col1, col2, col3;
col1 col2 col3 col4
a b 1 2
a c 1 2
a c 2 1
b b 1 3
Rextester Demo
While yours is the basic, simplest, and best approach, I just need to remind you withROW_NUMBER()
approach as well. It'll be nicer to have an alternative approaches as well (just for references).
– iSR5
Nov 10 at 14:41
@iSR5 yes, that works for some situations mostlyrow_number
used with a subquery. Thanks.
– Barbaros Özhan
Nov 10 at 14:47
@iSR5 -- row number is used when the other columns (col1, col2, col3 in this case) are not the same. eg return values of person with max salary in the same dept number. row_number() makes no sense here.
– Hogan
Nov 10 at 16:37
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
Mandatory NOT EXISTS
solution... your condition written as a not exist query:
DECLARE @t TABLE (col1 varchar(100), col2 varchar(100), col3 int, col4 int);
INSERT INTO @t VALUES
('a', 'b', 1, 2),
('a', 'c', 1, 2),
('a', 'c', 2, 1),
('b', 'b', 1, 2),
('b', 'b', 1, 3);
SELECT *
FROM @t AS t
WHERE NOT EXISTS (
SELECT 1
FROM @t AS dup
WHERE dup.col1 = t.col1
AND dup.col2 = t.col2
AND dup.col3 = t.col3
AND dup.col4 > t.col4 -- outer row has smaller col4
)
Demo on DB Fiddle
What amazing! That's working now. Tkssss
– Hung Nguyen
Nov 10 at 15:37
add a comment |
up vote
1
down vote
accepted
Mandatory NOT EXISTS
solution... your condition written as a not exist query:
DECLARE @t TABLE (col1 varchar(100), col2 varchar(100), col3 int, col4 int);
INSERT INTO @t VALUES
('a', 'b', 1, 2),
('a', 'c', 1, 2),
('a', 'c', 2, 1),
('b', 'b', 1, 2),
('b', 'b', 1, 3);
SELECT *
FROM @t AS t
WHERE NOT EXISTS (
SELECT 1
FROM @t AS dup
WHERE dup.col1 = t.col1
AND dup.col2 = t.col2
AND dup.col3 = t.col3
AND dup.col4 > t.col4 -- outer row has smaller col4
)
Demo on DB Fiddle
What amazing! That's working now. Tkssss
– Hung Nguyen
Nov 10 at 15:37
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
Mandatory NOT EXISTS
solution... your condition written as a not exist query:
DECLARE @t TABLE (col1 varchar(100), col2 varchar(100), col3 int, col4 int);
INSERT INTO @t VALUES
('a', 'b', 1, 2),
('a', 'c', 1, 2),
('a', 'c', 2, 1),
('b', 'b', 1, 2),
('b', 'b', 1, 3);
SELECT *
FROM @t AS t
WHERE NOT EXISTS (
SELECT 1
FROM @t AS dup
WHERE dup.col1 = t.col1
AND dup.col2 = t.col2
AND dup.col3 = t.col3
AND dup.col4 > t.col4 -- outer row has smaller col4
)
Demo on DB Fiddle
Mandatory NOT EXISTS
solution... your condition written as a not exist query:
DECLARE @t TABLE (col1 varchar(100), col2 varchar(100), col3 int, col4 int);
INSERT INTO @t VALUES
('a', 'b', 1, 2),
('a', 'c', 1, 2),
('a', 'c', 2, 1),
('b', 'b', 1, 2),
('b', 'b', 1, 3);
SELECT *
FROM @t AS t
WHERE NOT EXISTS (
SELECT 1
FROM @t AS dup
WHERE dup.col1 = t.col1
AND dup.col2 = t.col2
AND dup.col3 = t.col3
AND dup.col4 > t.col4 -- outer row has smaller col4
)
Demo on DB Fiddle
edited Nov 10 at 15:34
answered Nov 10 at 15:00
Salman A
173k66332419
173k66332419
What amazing! That's working now. Tkssss
– Hung Nguyen
Nov 10 at 15:37
add a comment |
What amazing! That's working now. Tkssss
– Hung Nguyen
Nov 10 at 15:37
What amazing! That's working now. Tkssss
– Hung Nguyen
Nov 10 at 15:37
What amazing! That's working now. Tkssss
– Hung Nguyen
Nov 10 at 15:37
add a comment |
up vote
4
down vote
Yes possible, just use group by
with max
aggregation as
with tab(col1,col2,col3,col4) as
(
select 'a','b',1,2 union all
select 'b','b',1,2 union all
select 'a','c',1,2 union all
select 'b','b',1,3 union all
select 'a','c',2,1
)
select col1, col2, col3, max(col4) as col4
from tab
group by col1, col2, col3;
col1 col2 col3 col4
a b 1 2
a c 1 2
a c 2 1
b b 1 3
Rextester Demo
While yours is the basic, simplest, and best approach, I just need to remind you withROW_NUMBER()
approach as well. It'll be nicer to have an alternative approaches as well (just for references).
– iSR5
Nov 10 at 14:41
@iSR5 yes, that works for some situations mostlyrow_number
used with a subquery. Thanks.
– Barbaros Özhan
Nov 10 at 14:47
@iSR5 -- row number is used when the other columns (col1, col2, col3 in this case) are not the same. eg return values of person with max salary in the same dept number. row_number() makes no sense here.
– Hogan
Nov 10 at 16:37
add a comment |
up vote
4
down vote
Yes possible, just use group by
with max
aggregation as
with tab(col1,col2,col3,col4) as
(
select 'a','b',1,2 union all
select 'b','b',1,2 union all
select 'a','c',1,2 union all
select 'b','b',1,3 union all
select 'a','c',2,1
)
select col1, col2, col3, max(col4) as col4
from tab
group by col1, col2, col3;
col1 col2 col3 col4
a b 1 2
a c 1 2
a c 2 1
b b 1 3
Rextester Demo
While yours is the basic, simplest, and best approach, I just need to remind you withROW_NUMBER()
approach as well. It'll be nicer to have an alternative approaches as well (just for references).
– iSR5
Nov 10 at 14:41
@iSR5 yes, that works for some situations mostlyrow_number
used with a subquery. Thanks.
– Barbaros Özhan
Nov 10 at 14:47
@iSR5 -- row number is used when the other columns (col1, col2, col3 in this case) are not the same. eg return values of person with max salary in the same dept number. row_number() makes no sense here.
– Hogan
Nov 10 at 16:37
add a comment |
up vote
4
down vote
up vote
4
down vote
Yes possible, just use group by
with max
aggregation as
with tab(col1,col2,col3,col4) as
(
select 'a','b',1,2 union all
select 'b','b',1,2 union all
select 'a','c',1,2 union all
select 'b','b',1,3 union all
select 'a','c',2,1
)
select col1, col2, col3, max(col4) as col4
from tab
group by col1, col2, col3;
col1 col2 col3 col4
a b 1 2
a c 1 2
a c 2 1
b b 1 3
Rextester Demo
Yes possible, just use group by
with max
aggregation as
with tab(col1,col2,col3,col4) as
(
select 'a','b',1,2 union all
select 'b','b',1,2 union all
select 'a','c',1,2 union all
select 'b','b',1,3 union all
select 'a','c',2,1
)
select col1, col2, col3, max(col4) as col4
from tab
group by col1, col2, col3;
col1 col2 col3 col4
a b 1 2
a c 1 2
a c 2 1
b b 1 3
Rextester Demo
edited Nov 10 at 14:37
answered Nov 10 at 14:24
Barbaros Özhan
11.6k71530
11.6k71530
While yours is the basic, simplest, and best approach, I just need to remind you withROW_NUMBER()
approach as well. It'll be nicer to have an alternative approaches as well (just for references).
– iSR5
Nov 10 at 14:41
@iSR5 yes, that works for some situations mostlyrow_number
used with a subquery. Thanks.
– Barbaros Özhan
Nov 10 at 14:47
@iSR5 -- row number is used when the other columns (col1, col2, col3 in this case) are not the same. eg return values of person with max salary in the same dept number. row_number() makes no sense here.
– Hogan
Nov 10 at 16:37
add a comment |
While yours is the basic, simplest, and best approach, I just need to remind you withROW_NUMBER()
approach as well. It'll be nicer to have an alternative approaches as well (just for references).
– iSR5
Nov 10 at 14:41
@iSR5 yes, that works for some situations mostlyrow_number
used with a subquery. Thanks.
– Barbaros Özhan
Nov 10 at 14:47
@iSR5 -- row number is used when the other columns (col1, col2, col3 in this case) are not the same. eg return values of person with max salary in the same dept number. row_number() makes no sense here.
– Hogan
Nov 10 at 16:37
While yours is the basic, simplest, and best approach, I just need to remind you with
ROW_NUMBER()
approach as well. It'll be nicer to have an alternative approaches as well (just for references).– iSR5
Nov 10 at 14:41
While yours is the basic, simplest, and best approach, I just need to remind you with
ROW_NUMBER()
approach as well. It'll be nicer to have an alternative approaches as well (just for references).– iSR5
Nov 10 at 14:41
@iSR5 yes, that works for some situations mostly
row_number
used with a subquery. Thanks.– Barbaros Özhan
Nov 10 at 14:47
@iSR5 yes, that works for some situations mostly
row_number
used with a subquery. Thanks.– Barbaros Özhan
Nov 10 at 14:47
@iSR5 -- row number is used when the other columns (col1, col2, col3 in this case) are not the same. eg return values of person with max salary in the same dept number. row_number() makes no sense here.
– Hogan
Nov 10 at 16:37
@iSR5 -- row number is used when the other columns (col1, col2, col3 in this case) are not the same. eg return values of person with max salary in the same dept number. row_number() makes no sense here.
– Hogan
Nov 10 at 16:37
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53239862%2fsql-server-select-if-one-of-all-columns-are-unique%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
SQL queries become increasingly possible when you actually try to write them. That being said, have you tried answering this question yet yourself?
– Tim Biegeleisen
Nov 10 at 14:22
you have two answers because it is not clear what you mean when you say col4 is max. max of what? col3 and col4? other matching rows? something else?
– Hogan
Nov 10 at 15:11
@TimBiegeleisen I've tried with distinct and it got no point
– Hung Nguyen
Nov 10 at 15:21
@Hogan I couldn't get your point, guys. value for col4 is integer so if there is multiple same rows, It will return row have max value for col4
– Hung Nguyen
Nov 10 at 15:24
@HungNguyen it would be nice if you accept the most appropriate answer
– Salman A
Nov 10 at 15:59