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









share|improve this question



















  • 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














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









share|improve this question



















  • 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












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









share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 11 at 10:42









William Robertson

7,86922033




7,86922033










asked Nov 10 at 14:20









Hung Nguyen

85




85







  • 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




    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












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






share|improve this answer






















  • What amazing! That's working now. Tkssss
    – Hung Nguyen
    Nov 10 at 15:37

















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






share|improve this answer






















  • 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 -- 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










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













 

draft saved


draft discarded


















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

























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






share|improve this answer






















  • What amazing! That's working now. Tkssss
    – Hung Nguyen
    Nov 10 at 15:37














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






share|improve this answer






















  • What amazing! That's working now. Tkssss
    – Hung Nguyen
    Nov 10 at 15:37












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






share|improve this answer














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







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 10 at 15:34

























answered Nov 10 at 15:00









Salman A

170k65328413




170k65328413











  • 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




What amazing! That's working now. Tkssss
– Hung Nguyen
Nov 10 at 15:37












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






share|improve this answer






















  • 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 -- 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














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






share|improve this answer






















  • 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 -- 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












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






share|improve this answer














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







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 10 at 14:37

























answered Nov 10 at 14:24









Barbaros Özhan

10.8k71430




10.8k71430











  • 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 -- 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










  • @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















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

















 

draft saved


draft discarded















































 


draft saved


draft discarded














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





















































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







這個網誌中的熱門文章

How to read a connectionString WITH PROVIDER in .NET Core?

Node.js Script on GitHub Pages or Amazon S3

Museum of Modern and Contemporary Art of Trento and Rovereto