Row wise sorting2019 Community Moderator ElectionHow can I remove duplicate rows?Best way to get identity of inserted row?How to concatenate text from multiple rows into a single text string in SQL server?Inserting multiple rows in a single SQL query?Creating table from structure of another table with no dataSQL query to divide value across rows that add to total in SQL ServerUnable to combine the rows in sql by group by clauseUpdate table if rows already exists while inserting if row does not exists from a datasetIs the sequence of SQL query output is fixed or may Change?Multiple CASE with concatenation
How is the wildcard * interpreted as a command?
Do I really need to have a scientific explanation for my premise?
Reverse string, can I make it faster?
Word for a person who has no opinion about whether god exists
Does this video of collapsing warehouse shelves show a real incident?
Doesn't allowing a user mode program to access kernel space memory and execute the IN and OUT instructions defeat the purpose of having CPU modes?
At what distance can a bugbear, holding a reach weapon, with the Polearm Master feat, get their Opportunity Attack?
Do f-stop and exposure time perfectly cancel?
What are some noteworthy "mic-drop" moments in math?
weren't playing vs didn't play
Shifting between bemols (flats) and diesis (sharps)in the key signature
Could you please stop shuffling the deck and play already?
What are actual Tesla M60 models used by AWS?
Should I tell my boss the work he did was worthless
NASA's RS-25 Engines shut down time
Does a warlock using the Darkness/Devil's Sight combo still have advantage on ranged attacks against a target outside the Darkness?
Why does Captain Marvel assume the people on this planet know this?
Reversed Sudoku
How do I express some one as a black person?
Latex does not go to next line
Why does liquid water form when we exhale on a mirror?
Conservation of Mass and Energy
Makefile strange variable substitution
PTIJ: Should I kill my computer after installing software?
Row wise sorting
2019 Community Moderator ElectionHow can I remove duplicate rows?Best way to get identity of inserted row?How to concatenate text from multiple rows into a single text string in SQL server?Inserting multiple rows in a single SQL query?Creating table from structure of another table with no dataSQL query to divide value across rows that add to total in SQL ServerUnable to combine the rows in sql by group by clauseUpdate table if rows already exists while inserting if row does not exists from a datasetIs the sequence of SQL query output is fixed or may Change?Multiple CASE with concatenation
I have a table like below,
col1 col2 col3
1 2 3
2 1 3
3 2 1
1 4 6
4 6 1
6 4 1
Here I want to sort the record row wise.
Expected output.
col1 col2 col3
1 2 3
1 2 3
1 2 3
1 4 6
1 4 6
1 4 6
I have used ASCII Values for comparison.
declare @tab table(col1 varchar(10),col2 varchar(10),col3 varchar(20))
insert into @tab
select '4','6','1' union
select '6','4','1' union
select '1','2','3' union
select '2','1','3' union
select '3','1','2' union
select '4','2','3' union
select '1','4','6' union
select '5','5','1' union
select '5','5','1' union
select 'a','2','2' union
select '2','a','2' union
select '2','2','a'
;with CTE as(
Select Case When ascii(Col1) <= ascii(Col2) And ascii(Col1) <=
ascii(Col3) Then cast(Col1 as varchar)
When ascii(Col2) <= ascii(Col1) And ascii(Col2) <=
ascii(Col3) Then cast(Col2 as varchar)
Else cast(Col3 as varchar) END as col1,
case when ( ascii(col1) >= ascii(col2) and ascii(col2) >=
ascii(col3)) or ( ascii(col3) >= ascii(col2) and
ascii(col2) >= ascii(col1)) then cast(Col2 as
varchar)
when ( ascii(col1) >= ascii(col3) and ascii(col3) >=
ascii(col2)) or ( ascii(col2) >= ascii(col3) and
ascii(col3) >= ascii(col1)) then cast(Col3 as varchar)
when ( ascii(col3) >= ascii(col1) and ascii(col1) >= ascii(col2)) or ( ascii(col2) >= ascii(col1) and ascii(col1) >= ascii(col3)) then cast(Col1 as varchar) end as col2,
Case When ascii(Col1) >= ascii(Col2) And ascii(Col1) >= ascii(Col3) Then cast(Col1 as varchar)
When ascii(Col2) >= ascii(Col1) And ascii(Col2) >= ascii(Col3) Then cast(Col2 as varchar)
Else cast(Col3 as varchar) END as col3
From @tab)
select * from CTE
Is there any shortest way to achieve this process?
sql sql-server
add a comment |
I have a table like below,
col1 col2 col3
1 2 3
2 1 3
3 2 1
1 4 6
4 6 1
6 4 1
Here I want to sort the record row wise.
Expected output.
col1 col2 col3
1 2 3
1 2 3
1 2 3
1 4 6
1 4 6
1 4 6
I have used ASCII Values for comparison.
declare @tab table(col1 varchar(10),col2 varchar(10),col3 varchar(20))
insert into @tab
select '4','6','1' union
select '6','4','1' union
select '1','2','3' union
select '2','1','3' union
select '3','1','2' union
select '4','2','3' union
select '1','4','6' union
select '5','5','1' union
select '5','5','1' union
select 'a','2','2' union
select '2','a','2' union
select '2','2','a'
;with CTE as(
Select Case When ascii(Col1) <= ascii(Col2) And ascii(Col1) <=
ascii(Col3) Then cast(Col1 as varchar)
When ascii(Col2) <= ascii(Col1) And ascii(Col2) <=
ascii(Col3) Then cast(Col2 as varchar)
Else cast(Col3 as varchar) END as col1,
case when ( ascii(col1) >= ascii(col2) and ascii(col2) >=
ascii(col3)) or ( ascii(col3) >= ascii(col2) and
ascii(col2) >= ascii(col1)) then cast(Col2 as
varchar)
when ( ascii(col1) >= ascii(col3) and ascii(col3) >=
ascii(col2)) or ( ascii(col2) >= ascii(col3) and
ascii(col3) >= ascii(col1)) then cast(Col3 as varchar)
when ( ascii(col3) >= ascii(col1) and ascii(col1) >= ascii(col2)) or ( ascii(col2) >= ascii(col1) and ascii(col1) >= ascii(col3)) then cast(Col1 as varchar) end as col2,
Case When ascii(Col1) >= ascii(Col2) And ascii(Col1) >= ascii(Col3) Then cast(Col1 as varchar)
When ascii(Col2) >= ascii(Col1) And ascii(Col2) >= ascii(Col3) Then cast(Col2 as varchar)
Else cast(Col3 as varchar) END as col3
From @tab)
select * from CTE
Is there any shortest way to achieve this process?
sql sql-server
how this is sorting i mean what is the logic behind ?
– Zaynul Abadin Tuhin
Mar 6 at 5:34
Why a in last row?
– Suraj Kumar
Mar 6 at 5:36
If row 2 started as (2, 7,3) instead of (2,1,3) like it is now, I assume the result should be (2, 3, 7), but which row should it be in final table- still row 2 or at the bottom?
– DancingFool
Mar 6 at 5:39
This shouldn't be downvoted. It is a completely legitimate question. Just because the answer is "you shouldn't do this" doesn't mean that the question isn't clear and on-topic for the site.
– pwilcox
Mar 6 at 5:57
This seems like a bad design
– Gurwinder Singh
Mar 6 at 6:24
add a comment |
I have a table like below,
col1 col2 col3
1 2 3
2 1 3
3 2 1
1 4 6
4 6 1
6 4 1
Here I want to sort the record row wise.
Expected output.
col1 col2 col3
1 2 3
1 2 3
1 2 3
1 4 6
1 4 6
1 4 6
I have used ASCII Values for comparison.
declare @tab table(col1 varchar(10),col2 varchar(10),col3 varchar(20))
insert into @tab
select '4','6','1' union
select '6','4','1' union
select '1','2','3' union
select '2','1','3' union
select '3','1','2' union
select '4','2','3' union
select '1','4','6' union
select '5','5','1' union
select '5','5','1' union
select 'a','2','2' union
select '2','a','2' union
select '2','2','a'
;with CTE as(
Select Case When ascii(Col1) <= ascii(Col2) And ascii(Col1) <=
ascii(Col3) Then cast(Col1 as varchar)
When ascii(Col2) <= ascii(Col1) And ascii(Col2) <=
ascii(Col3) Then cast(Col2 as varchar)
Else cast(Col3 as varchar) END as col1,
case when ( ascii(col1) >= ascii(col2) and ascii(col2) >=
ascii(col3)) or ( ascii(col3) >= ascii(col2) and
ascii(col2) >= ascii(col1)) then cast(Col2 as
varchar)
when ( ascii(col1) >= ascii(col3) and ascii(col3) >=
ascii(col2)) or ( ascii(col2) >= ascii(col3) and
ascii(col3) >= ascii(col1)) then cast(Col3 as varchar)
when ( ascii(col3) >= ascii(col1) and ascii(col1) >= ascii(col2)) or ( ascii(col2) >= ascii(col1) and ascii(col1) >= ascii(col3)) then cast(Col1 as varchar) end as col2,
Case When ascii(Col1) >= ascii(Col2) And ascii(Col1) >= ascii(Col3) Then cast(Col1 as varchar)
When ascii(Col2) >= ascii(Col1) And ascii(Col2) >= ascii(Col3) Then cast(Col2 as varchar)
Else cast(Col3 as varchar) END as col3
From @tab)
select * from CTE
Is there any shortest way to achieve this process?
sql sql-server
I have a table like below,
col1 col2 col3
1 2 3
2 1 3
3 2 1
1 4 6
4 6 1
6 4 1
Here I want to sort the record row wise.
Expected output.
col1 col2 col3
1 2 3
1 2 3
1 2 3
1 4 6
1 4 6
1 4 6
I have used ASCII Values for comparison.
declare @tab table(col1 varchar(10),col2 varchar(10),col3 varchar(20))
insert into @tab
select '4','6','1' union
select '6','4','1' union
select '1','2','3' union
select '2','1','3' union
select '3','1','2' union
select '4','2','3' union
select '1','4','6' union
select '5','5','1' union
select '5','5','1' union
select 'a','2','2' union
select '2','a','2' union
select '2','2','a'
;with CTE as(
Select Case When ascii(Col1) <= ascii(Col2) And ascii(Col1) <=
ascii(Col3) Then cast(Col1 as varchar)
When ascii(Col2) <= ascii(Col1) And ascii(Col2) <=
ascii(Col3) Then cast(Col2 as varchar)
Else cast(Col3 as varchar) END as col1,
case when ( ascii(col1) >= ascii(col2) and ascii(col2) >=
ascii(col3)) or ( ascii(col3) >= ascii(col2) and
ascii(col2) >= ascii(col1)) then cast(Col2 as
varchar)
when ( ascii(col1) >= ascii(col3) and ascii(col3) >=
ascii(col2)) or ( ascii(col2) >= ascii(col3) and
ascii(col3) >= ascii(col1)) then cast(Col3 as varchar)
when ( ascii(col3) >= ascii(col1) and ascii(col1) >= ascii(col2)) or ( ascii(col2) >= ascii(col1) and ascii(col1) >= ascii(col3)) then cast(Col1 as varchar) end as col2,
Case When ascii(Col1) >= ascii(Col2) And ascii(Col1) >= ascii(Col3) Then cast(Col1 as varchar)
When ascii(Col2) >= ascii(Col1) And ascii(Col2) >= ascii(Col3) Then cast(Col2 as varchar)
Else cast(Col3 as varchar) END as col3
From @tab)
select * from CTE
Is there any shortest way to achieve this process?
sql sql-server
sql sql-server
edited Mar 6 at 6:06
Mano
asked Mar 6 at 5:32
ManoMano
614113
614113
how this is sorting i mean what is the logic behind ?
– Zaynul Abadin Tuhin
Mar 6 at 5:34
Why a in last row?
– Suraj Kumar
Mar 6 at 5:36
If row 2 started as (2, 7,3) instead of (2,1,3) like it is now, I assume the result should be (2, 3, 7), but which row should it be in final table- still row 2 or at the bottom?
– DancingFool
Mar 6 at 5:39
This shouldn't be downvoted. It is a completely legitimate question. Just because the answer is "you shouldn't do this" doesn't mean that the question isn't clear and on-topic for the site.
– pwilcox
Mar 6 at 5:57
This seems like a bad design
– Gurwinder Singh
Mar 6 at 6:24
add a comment |
how this is sorting i mean what is the logic behind ?
– Zaynul Abadin Tuhin
Mar 6 at 5:34
Why a in last row?
– Suraj Kumar
Mar 6 at 5:36
If row 2 started as (2, 7,3) instead of (2,1,3) like it is now, I assume the result should be (2, 3, 7), but which row should it be in final table- still row 2 or at the bottom?
– DancingFool
Mar 6 at 5:39
This shouldn't be downvoted. It is a completely legitimate question. Just because the answer is "you shouldn't do this" doesn't mean that the question isn't clear and on-topic for the site.
– pwilcox
Mar 6 at 5:57
This seems like a bad design
– Gurwinder Singh
Mar 6 at 6:24
how this is sorting i mean what is the logic behind ?
– Zaynul Abadin Tuhin
Mar 6 at 5:34
how this is sorting i mean what is the logic behind ?
– Zaynul Abadin Tuhin
Mar 6 at 5:34
Why a in last row?
– Suraj Kumar
Mar 6 at 5:36
Why a in last row?
– Suraj Kumar
Mar 6 at 5:36
If row 2 started as (2, 7,3) instead of (2,1,3) like it is now, I assume the result should be (2, 3, 7), but which row should it be in final table- still row 2 or at the bottom?
– DancingFool
Mar 6 at 5:39
If row 2 started as (2, 7,3) instead of (2,1,3) like it is now, I assume the result should be (2, 3, 7), but which row should it be in final table- still row 2 or at the bottom?
– DancingFool
Mar 6 at 5:39
This shouldn't be downvoted. It is a completely legitimate question. Just because the answer is "you shouldn't do this" doesn't mean that the question isn't clear and on-topic for the site.
– pwilcox
Mar 6 at 5:57
This shouldn't be downvoted. It is a completely legitimate question. Just because the answer is "you shouldn't do this" doesn't mean that the question isn't clear and on-topic for the site.
– pwilcox
Mar 6 at 5:57
This seems like a bad design
– Gurwinder Singh
Mar 6 at 6:24
This seems like a bad design
– Gurwinder Singh
Mar 6 at 6:24
add a comment |
5 Answers
5
active
oldest
votes
Another way of doing it using ROW_NUMBER()
like following
SELECT (SELECT x
FROM (SELECT x,
Row_number()
OVER(
ORDER BY x) rn
FROM (VALUES(col1),
(col2),
(col3))f(x))t
WHERE rn = 1) c1,
(SELECT x
FROM (SELECT x,
Row_number()
OVER(
ORDER BY x) rn
FROM (VALUES(col1),
(col2),
(col3))f(x))t
WHERE rn = 2) c2,
(SELECT x
FROM (SELECT x,
Row_number()
OVER(
ORDER BY x) rn
FROM (VALUES(col1),
(col2),
(col3))f(x))t
WHERE rn = 3) c3
FROM @table
OR using nested CTE
like following.
;WITH cte1
AS (SELECT (SELECT Min(f)
FROM (VALUES (col1),
(col2),
(col3)) AS Fields(f)) m1,
*
FROM @table),
cte2
AS (SELECT (SELECT COALESCE(Min(f), M1) AS M2
FROM (VALUES (col1),
(col2),
(col3)) AS Fields(f)
WHERE f > m1) m2,
*
FROM cte1),
cte3
AS (SELECT m1,
m2,
(SELECT COALESCE(Min(f),m2) as m3
FROM (VALUES (col1),
(col2),
(col3)) AS Fields(f)
WHERE f > m2) m3
FROM cte2)
SELECT *
FROM cte3
Online Demo
This is return null values in some of the column.
– Mano
Mar 6 at 6:20
Please insert one row like '5','1','5'
– Mano
Mar 6 at 6:22
You can check the updated query here .. rextester.com/STKZ87454
– PSK
Mar 6 at 6:27
add a comment |
This need for row-wise sorting is usually a sign that your tables could benefit from a new structure. What are you really trying to accomplish? It can probably be better done by normalizing col1, col2, and col3 to look more vertical (this is what the 'unpivoted' CTE is forcing below, but the table should look something like that in the first place).
If you must do this, consider adding a row identifier (basically a primary key) to your table.
declare @tab table(
rowId int identity(1,1),
col1 varchar(10),
col2 varchar(10),
col3 varchar(20)
);
insert @tab values
('4','6','1'),
-- etc
Then you can avoid a bunch of case statements and more easily extend to more than just three columns with something like the following:
with
unpivoted as (
select rowId,
val,
ord = row_number() over(partition by rowId order by val)
from @tab
cross apply (values (col1), (col2), (col3)) ap (val)
)
select rowId,
col1 = [1],
col2 = [2],
col3 = [3]
from unpvioted
pivot (max(val) for ord in ([1],[2],[3])) piv
You can see it in action here.
are you a wizard?
– Manfred Wippel
Mar 6 at 7:12
When I use data type as varchar it return the error like " The type of column "col3" conflicts with the type of other columns specified in the UNPIVOT list."
– Mano
Mar 6 at 7:20
1
Aww thanks @ManfredWippel. But if you're familiar with pivots and unpivots then this isn't too magical. Unfortunately, I've worked in environments with severely non-normalized data, so I've used them a lot.
– pwilcox
Mar 6 at 15:10
@Mano, there is another way to unpivot. I changed to that in my answer. It's has the same effect except it is less limiting when converting the values for you.
– pwilcox
Mar 6 at 15:26
@pwilcox, This is working thank you.
– Mano
Mar 7 at 4:15
add a comment |
use case when like below
select case when col1>col2>col3 then col1
when col2>col3 then col2
else col3 end as col1, -- this is the condition for first column
you have to write this way for 3columns
add a comment |
If all columns are Integer type then the below query will works,
SELECT c1 = CASE
WHEN c1 <= c2 AND c1 <= c3 THEN c1
WHEN c2 <= c1 AND c2 <= c3 THEN c2
ELSE c3 END,
c2 = CASE
WHEN c1 <= c2 AND c1 <= c3 THEN
CASE WHEN c2 <= c3 THEN c2 ELSE c3 END
WHEN c2 <= c1 AND c2 <= c3 THEN
CASE WHEN c1 <= c3 THEN c1 ELSE c3 END
ELSE
CASE WHEN c1 <= c2 THEN c1 ELSE c2 END
END,
c3 = CASE
WHEN c1 >= c2 AND c1 >= c3 THEN c1
WHEN c2 >= c1 AND c2 >= c3 THEN c2
ELSE c3 END
FROM temp_x;
If any character then you can use ASCII value to cpompare.
add a comment |
The fastest way is likely to be case
expressions, but that doesn't generalize.
I would go for apply
as a good balance between performance and scalability:
select v.*
from t cross apply
(select max(case when seqnum = 1 then col end) as col1,
max(case when seqnum = 2 then col end) as col2,
max(case when seqnum = 3 then col end) as col3
from (select v.*,
row_number() over (order by col) as seqnum
from (values (t.col1), (t.col2), (t.col3)) v(col)
) v
) v;
This can also readily handle NULL
values and ties, which greatly complicate a case
approach.
add a comment |
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',
autoActivateHeartbeat: false,
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
);
);
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%2f55016188%2frow-wise-sorting%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
5 Answers
5
active
oldest
votes
5 Answers
5
active
oldest
votes
active
oldest
votes
active
oldest
votes
Another way of doing it using ROW_NUMBER()
like following
SELECT (SELECT x
FROM (SELECT x,
Row_number()
OVER(
ORDER BY x) rn
FROM (VALUES(col1),
(col2),
(col3))f(x))t
WHERE rn = 1) c1,
(SELECT x
FROM (SELECT x,
Row_number()
OVER(
ORDER BY x) rn
FROM (VALUES(col1),
(col2),
(col3))f(x))t
WHERE rn = 2) c2,
(SELECT x
FROM (SELECT x,
Row_number()
OVER(
ORDER BY x) rn
FROM (VALUES(col1),
(col2),
(col3))f(x))t
WHERE rn = 3) c3
FROM @table
OR using nested CTE
like following.
;WITH cte1
AS (SELECT (SELECT Min(f)
FROM (VALUES (col1),
(col2),
(col3)) AS Fields(f)) m1,
*
FROM @table),
cte2
AS (SELECT (SELECT COALESCE(Min(f), M1) AS M2
FROM (VALUES (col1),
(col2),
(col3)) AS Fields(f)
WHERE f > m1) m2,
*
FROM cte1),
cte3
AS (SELECT m1,
m2,
(SELECT COALESCE(Min(f),m2) as m3
FROM (VALUES (col1),
(col2),
(col3)) AS Fields(f)
WHERE f > m2) m3
FROM cte2)
SELECT *
FROM cte3
Online Demo
This is return null values in some of the column.
– Mano
Mar 6 at 6:20
Please insert one row like '5','1','5'
– Mano
Mar 6 at 6:22
You can check the updated query here .. rextester.com/STKZ87454
– PSK
Mar 6 at 6:27
add a comment |
Another way of doing it using ROW_NUMBER()
like following
SELECT (SELECT x
FROM (SELECT x,
Row_number()
OVER(
ORDER BY x) rn
FROM (VALUES(col1),
(col2),
(col3))f(x))t
WHERE rn = 1) c1,
(SELECT x
FROM (SELECT x,
Row_number()
OVER(
ORDER BY x) rn
FROM (VALUES(col1),
(col2),
(col3))f(x))t
WHERE rn = 2) c2,
(SELECT x
FROM (SELECT x,
Row_number()
OVER(
ORDER BY x) rn
FROM (VALUES(col1),
(col2),
(col3))f(x))t
WHERE rn = 3) c3
FROM @table
OR using nested CTE
like following.
;WITH cte1
AS (SELECT (SELECT Min(f)
FROM (VALUES (col1),
(col2),
(col3)) AS Fields(f)) m1,
*
FROM @table),
cte2
AS (SELECT (SELECT COALESCE(Min(f), M1) AS M2
FROM (VALUES (col1),
(col2),
(col3)) AS Fields(f)
WHERE f > m1) m2,
*
FROM cte1),
cte3
AS (SELECT m1,
m2,
(SELECT COALESCE(Min(f),m2) as m3
FROM (VALUES (col1),
(col2),
(col3)) AS Fields(f)
WHERE f > m2) m3
FROM cte2)
SELECT *
FROM cte3
Online Demo
This is return null values in some of the column.
– Mano
Mar 6 at 6:20
Please insert one row like '5','1','5'
– Mano
Mar 6 at 6:22
You can check the updated query here .. rextester.com/STKZ87454
– PSK
Mar 6 at 6:27
add a comment |
Another way of doing it using ROW_NUMBER()
like following
SELECT (SELECT x
FROM (SELECT x,
Row_number()
OVER(
ORDER BY x) rn
FROM (VALUES(col1),
(col2),
(col3))f(x))t
WHERE rn = 1) c1,
(SELECT x
FROM (SELECT x,
Row_number()
OVER(
ORDER BY x) rn
FROM (VALUES(col1),
(col2),
(col3))f(x))t
WHERE rn = 2) c2,
(SELECT x
FROM (SELECT x,
Row_number()
OVER(
ORDER BY x) rn
FROM (VALUES(col1),
(col2),
(col3))f(x))t
WHERE rn = 3) c3
FROM @table
OR using nested CTE
like following.
;WITH cte1
AS (SELECT (SELECT Min(f)
FROM (VALUES (col1),
(col2),
(col3)) AS Fields(f)) m1,
*
FROM @table),
cte2
AS (SELECT (SELECT COALESCE(Min(f), M1) AS M2
FROM (VALUES (col1),
(col2),
(col3)) AS Fields(f)
WHERE f > m1) m2,
*
FROM cte1),
cte3
AS (SELECT m1,
m2,
(SELECT COALESCE(Min(f),m2) as m3
FROM (VALUES (col1),
(col2),
(col3)) AS Fields(f)
WHERE f > m2) m3
FROM cte2)
SELECT *
FROM cte3
Online Demo
Another way of doing it using ROW_NUMBER()
like following
SELECT (SELECT x
FROM (SELECT x,
Row_number()
OVER(
ORDER BY x) rn
FROM (VALUES(col1),
(col2),
(col3))f(x))t
WHERE rn = 1) c1,
(SELECT x
FROM (SELECT x,
Row_number()
OVER(
ORDER BY x) rn
FROM (VALUES(col1),
(col2),
(col3))f(x))t
WHERE rn = 2) c2,
(SELECT x
FROM (SELECT x,
Row_number()
OVER(
ORDER BY x) rn
FROM (VALUES(col1),
(col2),
(col3))f(x))t
WHERE rn = 3) c3
FROM @table
OR using nested CTE
like following.
;WITH cte1
AS (SELECT (SELECT Min(f)
FROM (VALUES (col1),
(col2),
(col3)) AS Fields(f)) m1,
*
FROM @table),
cte2
AS (SELECT (SELECT COALESCE(Min(f), M1) AS M2
FROM (VALUES (col1),
(col2),
(col3)) AS Fields(f)
WHERE f > m1) m2,
*
FROM cte1),
cte3
AS (SELECT m1,
m2,
(SELECT COALESCE(Min(f),m2) as m3
FROM (VALUES (col1),
(col2),
(col3)) AS Fields(f)
WHERE f > m2) m3
FROM cte2)
SELECT *
FROM cte3
Online Demo
edited Mar 6 at 7:10
answered Mar 6 at 6:18
PSKPSK
12.3k31633
12.3k31633
This is return null values in some of the column.
– Mano
Mar 6 at 6:20
Please insert one row like '5','1','5'
– Mano
Mar 6 at 6:22
You can check the updated query here .. rextester.com/STKZ87454
– PSK
Mar 6 at 6:27
add a comment |
This is return null values in some of the column.
– Mano
Mar 6 at 6:20
Please insert one row like '5','1','5'
– Mano
Mar 6 at 6:22
You can check the updated query here .. rextester.com/STKZ87454
– PSK
Mar 6 at 6:27
This is return null values in some of the column.
– Mano
Mar 6 at 6:20
This is return null values in some of the column.
– Mano
Mar 6 at 6:20
Please insert one row like '5','1','5'
– Mano
Mar 6 at 6:22
Please insert one row like '5','1','5'
– Mano
Mar 6 at 6:22
You can check the updated query here .. rextester.com/STKZ87454
– PSK
Mar 6 at 6:27
You can check the updated query here .. rextester.com/STKZ87454
– PSK
Mar 6 at 6:27
add a comment |
This need for row-wise sorting is usually a sign that your tables could benefit from a new structure. What are you really trying to accomplish? It can probably be better done by normalizing col1, col2, and col3 to look more vertical (this is what the 'unpivoted' CTE is forcing below, but the table should look something like that in the first place).
If you must do this, consider adding a row identifier (basically a primary key) to your table.
declare @tab table(
rowId int identity(1,1),
col1 varchar(10),
col2 varchar(10),
col3 varchar(20)
);
insert @tab values
('4','6','1'),
-- etc
Then you can avoid a bunch of case statements and more easily extend to more than just three columns with something like the following:
with
unpivoted as (
select rowId,
val,
ord = row_number() over(partition by rowId order by val)
from @tab
cross apply (values (col1), (col2), (col3)) ap (val)
)
select rowId,
col1 = [1],
col2 = [2],
col3 = [3]
from unpvioted
pivot (max(val) for ord in ([1],[2],[3])) piv
You can see it in action here.
are you a wizard?
– Manfred Wippel
Mar 6 at 7:12
When I use data type as varchar it return the error like " The type of column "col3" conflicts with the type of other columns specified in the UNPIVOT list."
– Mano
Mar 6 at 7:20
1
Aww thanks @ManfredWippel. But if you're familiar with pivots and unpivots then this isn't too magical. Unfortunately, I've worked in environments with severely non-normalized data, so I've used them a lot.
– pwilcox
Mar 6 at 15:10
@Mano, there is another way to unpivot. I changed to that in my answer. It's has the same effect except it is less limiting when converting the values for you.
– pwilcox
Mar 6 at 15:26
@pwilcox, This is working thank you.
– Mano
Mar 7 at 4:15
add a comment |
This need for row-wise sorting is usually a sign that your tables could benefit from a new structure. What are you really trying to accomplish? It can probably be better done by normalizing col1, col2, and col3 to look more vertical (this is what the 'unpivoted' CTE is forcing below, but the table should look something like that in the first place).
If you must do this, consider adding a row identifier (basically a primary key) to your table.
declare @tab table(
rowId int identity(1,1),
col1 varchar(10),
col2 varchar(10),
col3 varchar(20)
);
insert @tab values
('4','6','1'),
-- etc
Then you can avoid a bunch of case statements and more easily extend to more than just three columns with something like the following:
with
unpivoted as (
select rowId,
val,
ord = row_number() over(partition by rowId order by val)
from @tab
cross apply (values (col1), (col2), (col3)) ap (val)
)
select rowId,
col1 = [1],
col2 = [2],
col3 = [3]
from unpvioted
pivot (max(val) for ord in ([1],[2],[3])) piv
You can see it in action here.
are you a wizard?
– Manfred Wippel
Mar 6 at 7:12
When I use data type as varchar it return the error like " The type of column "col3" conflicts with the type of other columns specified in the UNPIVOT list."
– Mano
Mar 6 at 7:20
1
Aww thanks @ManfredWippel. But if you're familiar with pivots and unpivots then this isn't too magical. Unfortunately, I've worked in environments with severely non-normalized data, so I've used them a lot.
– pwilcox
Mar 6 at 15:10
@Mano, there is another way to unpivot. I changed to that in my answer. It's has the same effect except it is less limiting when converting the values for you.
– pwilcox
Mar 6 at 15:26
@pwilcox, This is working thank you.
– Mano
Mar 7 at 4:15
add a comment |
This need for row-wise sorting is usually a sign that your tables could benefit from a new structure. What are you really trying to accomplish? It can probably be better done by normalizing col1, col2, and col3 to look more vertical (this is what the 'unpivoted' CTE is forcing below, but the table should look something like that in the first place).
If you must do this, consider adding a row identifier (basically a primary key) to your table.
declare @tab table(
rowId int identity(1,1),
col1 varchar(10),
col2 varchar(10),
col3 varchar(20)
);
insert @tab values
('4','6','1'),
-- etc
Then you can avoid a bunch of case statements and more easily extend to more than just three columns with something like the following:
with
unpivoted as (
select rowId,
val,
ord = row_number() over(partition by rowId order by val)
from @tab
cross apply (values (col1), (col2), (col3)) ap (val)
)
select rowId,
col1 = [1],
col2 = [2],
col3 = [3]
from unpvioted
pivot (max(val) for ord in ([1],[2],[3])) piv
You can see it in action here.
This need for row-wise sorting is usually a sign that your tables could benefit from a new structure. What are you really trying to accomplish? It can probably be better done by normalizing col1, col2, and col3 to look more vertical (this is what the 'unpivoted' CTE is forcing below, but the table should look something like that in the first place).
If you must do this, consider adding a row identifier (basically a primary key) to your table.
declare @tab table(
rowId int identity(1,1),
col1 varchar(10),
col2 varchar(10),
col3 varchar(20)
);
insert @tab values
('4','6','1'),
-- etc
Then you can avoid a bunch of case statements and more easily extend to more than just three columns with something like the following:
with
unpivoted as (
select rowId,
val,
ord = row_number() over(partition by rowId order by val)
from @tab
cross apply (values (col1), (col2), (col3)) ap (val)
)
select rowId,
col1 = [1],
col2 = [2],
col3 = [3]
from unpvioted
pivot (max(val) for ord in ([1],[2],[3])) piv
You can see it in action here.
edited Mar 6 at 15:24
answered Mar 6 at 5:49
pwilcoxpwilcox
702514
702514
are you a wizard?
– Manfred Wippel
Mar 6 at 7:12
When I use data type as varchar it return the error like " The type of column "col3" conflicts with the type of other columns specified in the UNPIVOT list."
– Mano
Mar 6 at 7:20
1
Aww thanks @ManfredWippel. But if you're familiar with pivots and unpivots then this isn't too magical. Unfortunately, I've worked in environments with severely non-normalized data, so I've used them a lot.
– pwilcox
Mar 6 at 15:10
@Mano, there is another way to unpivot. I changed to that in my answer. It's has the same effect except it is less limiting when converting the values for you.
– pwilcox
Mar 6 at 15:26
@pwilcox, This is working thank you.
– Mano
Mar 7 at 4:15
add a comment |
are you a wizard?
– Manfred Wippel
Mar 6 at 7:12
When I use data type as varchar it return the error like " The type of column "col3" conflicts with the type of other columns specified in the UNPIVOT list."
– Mano
Mar 6 at 7:20
1
Aww thanks @ManfredWippel. But if you're familiar with pivots and unpivots then this isn't too magical. Unfortunately, I've worked in environments with severely non-normalized data, so I've used them a lot.
– pwilcox
Mar 6 at 15:10
@Mano, there is another way to unpivot. I changed to that in my answer. It's has the same effect except it is less limiting when converting the values for you.
– pwilcox
Mar 6 at 15:26
@pwilcox, This is working thank you.
– Mano
Mar 7 at 4:15
are you a wizard?
– Manfred Wippel
Mar 6 at 7:12
are you a wizard?
– Manfred Wippel
Mar 6 at 7:12
When I use data type as varchar it return the error like " The type of column "col3" conflicts with the type of other columns specified in the UNPIVOT list."
– Mano
Mar 6 at 7:20
When I use data type as varchar it return the error like " The type of column "col3" conflicts with the type of other columns specified in the UNPIVOT list."
– Mano
Mar 6 at 7:20
1
1
Aww thanks @ManfredWippel. But if you're familiar with pivots and unpivots then this isn't too magical. Unfortunately, I've worked in environments with severely non-normalized data, so I've used them a lot.
– pwilcox
Mar 6 at 15:10
Aww thanks @ManfredWippel. But if you're familiar with pivots and unpivots then this isn't too magical. Unfortunately, I've worked in environments with severely non-normalized data, so I've used them a lot.
– pwilcox
Mar 6 at 15:10
@Mano, there is another way to unpivot. I changed to that in my answer. It's has the same effect except it is less limiting when converting the values for you.
– pwilcox
Mar 6 at 15:26
@Mano, there is another way to unpivot. I changed to that in my answer. It's has the same effect except it is less limiting when converting the values for you.
– pwilcox
Mar 6 at 15:26
@pwilcox, This is working thank you.
– Mano
Mar 7 at 4:15
@pwilcox, This is working thank you.
– Mano
Mar 7 at 4:15
add a comment |
use case when like below
select case when col1>col2>col3 then col1
when col2>col3 then col2
else col3 end as col1, -- this is the condition for first column
you have to write this way for 3columns
add a comment |
use case when like below
select case when col1>col2>col3 then col1
when col2>col3 then col2
else col3 end as col1, -- this is the condition for first column
you have to write this way for 3columns
add a comment |
use case when like below
select case when col1>col2>col3 then col1
when col2>col3 then col2
else col3 end as col1, -- this is the condition for first column
you have to write this way for 3columns
use case when like below
select case when col1>col2>col3 then col1
when col2>col3 then col2
else col3 end as col1, -- this is the condition for first column
you have to write this way for 3columns
answered Mar 6 at 5:43
Zaynul Abadin TuhinZaynul Abadin Tuhin
16.1k21033
16.1k21033
add a comment |
add a comment |
If all columns are Integer type then the below query will works,
SELECT c1 = CASE
WHEN c1 <= c2 AND c1 <= c3 THEN c1
WHEN c2 <= c1 AND c2 <= c3 THEN c2
ELSE c3 END,
c2 = CASE
WHEN c1 <= c2 AND c1 <= c3 THEN
CASE WHEN c2 <= c3 THEN c2 ELSE c3 END
WHEN c2 <= c1 AND c2 <= c3 THEN
CASE WHEN c1 <= c3 THEN c1 ELSE c3 END
ELSE
CASE WHEN c1 <= c2 THEN c1 ELSE c2 END
END,
c3 = CASE
WHEN c1 >= c2 AND c1 >= c3 THEN c1
WHEN c2 >= c1 AND c2 >= c3 THEN c2
ELSE c3 END
FROM temp_x;
If any character then you can use ASCII value to cpompare.
add a comment |
If all columns are Integer type then the below query will works,
SELECT c1 = CASE
WHEN c1 <= c2 AND c1 <= c3 THEN c1
WHEN c2 <= c1 AND c2 <= c3 THEN c2
ELSE c3 END,
c2 = CASE
WHEN c1 <= c2 AND c1 <= c3 THEN
CASE WHEN c2 <= c3 THEN c2 ELSE c3 END
WHEN c2 <= c1 AND c2 <= c3 THEN
CASE WHEN c1 <= c3 THEN c1 ELSE c3 END
ELSE
CASE WHEN c1 <= c2 THEN c1 ELSE c2 END
END,
c3 = CASE
WHEN c1 >= c2 AND c1 >= c3 THEN c1
WHEN c2 >= c1 AND c2 >= c3 THEN c2
ELSE c3 END
FROM temp_x;
If any character then you can use ASCII value to cpompare.
add a comment |
If all columns are Integer type then the below query will works,
SELECT c1 = CASE
WHEN c1 <= c2 AND c1 <= c3 THEN c1
WHEN c2 <= c1 AND c2 <= c3 THEN c2
ELSE c3 END,
c2 = CASE
WHEN c1 <= c2 AND c1 <= c3 THEN
CASE WHEN c2 <= c3 THEN c2 ELSE c3 END
WHEN c2 <= c1 AND c2 <= c3 THEN
CASE WHEN c1 <= c3 THEN c1 ELSE c3 END
ELSE
CASE WHEN c1 <= c2 THEN c1 ELSE c2 END
END,
c3 = CASE
WHEN c1 >= c2 AND c1 >= c3 THEN c1
WHEN c2 >= c1 AND c2 >= c3 THEN c2
ELSE c3 END
FROM temp_x;
If any character then you can use ASCII value to cpompare.
If all columns are Integer type then the below query will works,
SELECT c1 = CASE
WHEN c1 <= c2 AND c1 <= c3 THEN c1
WHEN c2 <= c1 AND c2 <= c3 THEN c2
ELSE c3 END,
c2 = CASE
WHEN c1 <= c2 AND c1 <= c3 THEN
CASE WHEN c2 <= c3 THEN c2 ELSE c3 END
WHEN c2 <= c1 AND c2 <= c3 THEN
CASE WHEN c1 <= c3 THEN c1 ELSE c3 END
ELSE
CASE WHEN c1 <= c2 THEN c1 ELSE c2 END
END,
c3 = CASE
WHEN c1 >= c2 AND c1 >= c3 THEN c1
WHEN c2 >= c1 AND c2 >= c3 THEN c2
ELSE c3 END
FROM temp_x;
If any character then you can use ASCII value to cpompare.
answered Mar 6 at 5:46
DevDev
464
464
add a comment |
add a comment |
The fastest way is likely to be case
expressions, but that doesn't generalize.
I would go for apply
as a good balance between performance and scalability:
select v.*
from t cross apply
(select max(case when seqnum = 1 then col end) as col1,
max(case when seqnum = 2 then col end) as col2,
max(case when seqnum = 3 then col end) as col3
from (select v.*,
row_number() over (order by col) as seqnum
from (values (t.col1), (t.col2), (t.col3)) v(col)
) v
) v;
This can also readily handle NULL
values and ties, which greatly complicate a case
approach.
add a comment |
The fastest way is likely to be case
expressions, but that doesn't generalize.
I would go for apply
as a good balance between performance and scalability:
select v.*
from t cross apply
(select max(case when seqnum = 1 then col end) as col1,
max(case when seqnum = 2 then col end) as col2,
max(case when seqnum = 3 then col end) as col3
from (select v.*,
row_number() over (order by col) as seqnum
from (values (t.col1), (t.col2), (t.col3)) v(col)
) v
) v;
This can also readily handle NULL
values and ties, which greatly complicate a case
approach.
add a comment |
The fastest way is likely to be case
expressions, but that doesn't generalize.
I would go for apply
as a good balance between performance and scalability:
select v.*
from t cross apply
(select max(case when seqnum = 1 then col end) as col1,
max(case when seqnum = 2 then col end) as col2,
max(case when seqnum = 3 then col end) as col3
from (select v.*,
row_number() over (order by col) as seqnum
from (values (t.col1), (t.col2), (t.col3)) v(col)
) v
) v;
This can also readily handle NULL
values and ties, which greatly complicate a case
approach.
The fastest way is likely to be case
expressions, but that doesn't generalize.
I would go for apply
as a good balance between performance and scalability:
select v.*
from t cross apply
(select max(case when seqnum = 1 then col end) as col1,
max(case when seqnum = 2 then col end) as col2,
max(case when seqnum = 3 then col end) as col3
from (select v.*,
row_number() over (order by col) as seqnum
from (values (t.col1), (t.col2), (t.col3)) v(col)
) v
) v;
This can also readily handle NULL
values and ties, which greatly complicate a case
approach.
answered Mar 6 at 13:04
Gordon LinoffGordon Linoff
785k35310417
785k35310417
add a comment |
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.
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%2f55016188%2frow-wise-sorting%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
how this is sorting i mean what is the logic behind ?
– Zaynul Abadin Tuhin
Mar 6 at 5:34
Why a in last row?
– Suraj Kumar
Mar 6 at 5:36
If row 2 started as (2, 7,3) instead of (2,1,3) like it is now, I assume the result should be (2, 3, 7), but which row should it be in final table- still row 2 or at the bottom?
– DancingFool
Mar 6 at 5:39
This shouldn't be downvoted. It is a completely legitimate question. Just because the answer is "you shouldn't do this" doesn't mean that the question isn't clear and on-topic for the site.
– pwilcox
Mar 6 at 5:57
This seems like a bad design
– Gurwinder Singh
Mar 6 at 6:24