473,386 Members | 2,129 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Order By values of a column

Hi,

I want the result of my query to be returned in a specific order based
upon values of a column. It will be more clear with example. Let's say
my table is as below

Col1 Col2 Col3
1 A Mark
2 A Rob
3 C Chris
4 B Tom
5 C Harry

I want the result to be returned sorted on Col2 in the order of values
B, C and A. So, I want to get it sorted as:

Col1 Col2 Col3
4 B Tom
3 C Chris
5 C Harry
1 A Mark
2 A Rob

Is this possible? How?

Thanks in advance,
Lalit

Nov 12 '05 #1
18 5063
singlal wrote:
Is this possible? How?


select * from .... where .... order by col2 ASC

(or DESC for descending order)

Bernd

--
"Ja, alles meine Herren" sprach Fürst Lichnowsky. "Ooch det roochen?"
"Ja, auch das Rauchen." "Ooch im Tiergarten?" "Ja, auch im Tiergarten
darf geraucht werden, meine Herren." Und so endeten die Barrikadenkämpfe
des 18. März in Berlin
Nov 12 '05 #2
Ian
singlal wrote:
Hi,

I want the result of my query to be returned in a specific order based
upon values of a column. It will be more clear with example. Let's say
my table is as below

Col1 Col2 Col3
1 A Mark
2 A Rob
3 C Chris
4 B Tom
5 C Harry

I want the result to be returned sorted on Col2 in the order of values
B, C and A. So, I want to get it sorted as:


select ...
order by
case col2
when 'B' then 1
when 'C' then 2
when 'A' then 3
else 4
end

Nov 12 '05 #3
singlal wrote:
Hi,

I want the result of my query to be returned in a specific order based
upon values of a column. It will be more clear with example. Let's say
my table is as below

Col1 Col2 Col3
1 A Mark
2 A Rob
3 C Chris
4 B Tom
5 C Harry

I want the result to be returned sorted on Col2 in the order of values
B, C and A. So, I want to get it sorted as:

Col1 Col2 Col3
4 B Tom
3 C Chris
5 C Harry
1 A Mark
2 A Rob

Is this possible? How?

Thanks in advance,
Lalit

order by translate(col2,'312','ABC'), col1
Nov 12 '05 #4
Thanks for your replies. Below are my problems:

While running with CASE logic written by IAN, I am getting SQLCODE
-214.

For Bob's translate function. it will work if I really had col2 with
length as one character. But I actually have values in col2 like 'EB',
'TVD', DIFFDE' etc. Bob, any clues on that?

Nov 12 '05 #5
In article <11**********************@g44g2000cwa.googlegroups .com>,
singlal (la************@gmail.com) says...
Thanks for your replies. Below are my problems:

While running with CASE logic written by IAN, I am getting SQLCODE
-214.

For Bob's translate function. it will work if I really had col2 with
length as one character. But I actually have values in col2 like 'EB',
'TVD', DIFFDE' etc. Bob, any clues on that?

Which DB2 version on which OS are you using?
Nov 12 '05 #6
DB2 version 7

Nov 12 '05 #7
Sorry for the incomplete info, I am using DB2 versions 7 on OS390.

Nov 12 '05 #8
singlal wrote:
Thanks for your replies. Below are my problems:

While running with CASE logic written by IAN, I am getting SQLCODE
-214.

For Bob's translate function. it will work if I really had col2 with
length as one character. But I actually have values in col2 like 'EB',
'TVD', DIFFDE' etc. Bob, any clues on that?


First thing popping into my head was to create a table which mapped col2 to
numerical weights, joining with that, and ordering based on the weights.
Allows you to separate out the data (col2 <==> order/weight) from the
logic, meaning that if you want to tweak the order, or add new types, you
can do it outside of your code.
Nov 12 '05 #9
ORDER BY POSSTR('EB ' || 'TVD ' || 'DIFFDE' || 'Bob ', col2) / 6

Nov 12 '05 #10
sweet and smart, nice Tomkuma

Nov 12 '05 #11
None of the functions like POSSTR, TRANSLATE, LOCATE or CASE statement
seems to be working with V7

Nov 12 '05 #12
POSSTR is definitely available in V7 zOS.

what error message do you get ?

Nov 12 '05 #13
Hope you are still following:

POSSTR() is not accepting a column name on the second position.
Use LOCATE instead, that does accept columns on both positions. Be
aware that the order of the arguments is the other way round, so the
statement should be:

ORDER BY LOCATE(cols, 'EB ' || 'TVD ' || 'DIFFDE' || 'Bob ') / 6

Promise that does the job.

SELECT DEPTNO, LOCATE(DEPTNO,'B01' || 'A00' || 'C01' ||
'D01' || 'D11' || 'D21' || 'E01' || 'E11' || 'E21')
FROM XXXXXXXX.DEPT
ORDER BY 2

returns:

DEPTNO COL1
------ -----------
B01 1
A00 4
C01 7
D01 10
D11 13
D21 16
E01 19
E11 22
E21 25

Nov 12 '05 #14
Hope you are still following:

POSSTR() is not accepting a column name on the second position.
Use LOCATE instead, that does accept columns on both positions. Be
aware that the order of the arguments is the other way round, so the
statement should be:

ORDER BY LOCATE(cols, 'EB ' || 'TVD ' || 'DIFFDE' || 'Bob ') / 6

Promise that does the job.

SELECT DEPTNO, LOCATE(DEPTNO,'B01' || 'A00' || 'C01' ||
'D01' || 'D11' || 'D21' || 'E01' || 'E11' || 'E21')
FROM XXXXXXXX.DEPT
ORDER BY 2

returns:

DEPTNO COL1
------ -----------
B01 1
A00 4
C01 7
D01 10
D11 13
D21 16
E01 19
E11 22
E21 25

Nov 12 '05 #15
If none of the other solutions work well:

SELECT Col1, Col2, Col3, 1 Sort_Order FROM Table WHERE Col2 = 'B'
UNION ALL
SELECT Col1, Col2, Col3, 2 Sort_Order FROM Table WHERE Col2 = 'C'
UNION ALL
SELECT Col1, Col2, Col3, 3 Sort_Order FROM Table WHERE Col2 = 'A'
ORDER BY 4

B.

Nov 12 '05 #16
singlal wrote "I am using DB2 versions 7 on OS390."
It is written in DB2 UDB SQL Reference Version 7 that a column name can
be used as search-string for POSSTR.
But, I failed in another query to use a column as search-string.
So, you are right. Perhaps manual's bug.

Nov 12 '05 #17
Well,

for z/OS there is an own SQL reference, which states it is not
possible.
I got confused as well, and I was surprised it works with locate :-)

See V8 zOS docs:
http://publib.boulder.ibm.com/infoce...jnrmstr280.htm

Nov 12 '05 #18
I saw your referenced doc and PDF version of SQL Reference Version 8.
Column name was removed from search-string for POSSTR. I guess manual
bug of Version 7 was corrected.
For LOCATE, column name is included usable expression for
search-string in both docs.
So, your result is consist with Version 8 manual.

Nov 12 '05 #19

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: James Lee | last post by:
I am doing a simple query like col1, col2, date, col4 from table1. All four colums are of type blob. For date column, I store a string like this: Fri Feb 13 11:01:24 2004 I store records as...
1
by: Yasaswi Pulavarti | last post by:
Is it possible to order by specific values in a column. For example a column may contain values like N, N1, N2, S, S1, S2, R, T. I want to order by in such a way that all the records with N, N1, or...
7
by: Yannick Turgeon | last post by:
Hello all, I'm using SS2K on W2k. I'v got a table say, humm, "Orders" with two fields in the PK: OrderDate and CustomerID. I would like to add an "ID" column which would be auto-increment...
6
by: Mike MacSween | last post by:
I have a report based on: PARAMETERS Forms!frmProductions!ProdID Long; SELECT Sum(qryRoleEvent.Fee) AS SumOfFee, First(qryMusician.LastName) AS LastName, First(qryMusician.FirstName) AS...
5
by: Dragan Matic | last post by:
If I have a table t with column c which is defined as char(5) and fill it with following values: insert into t (c) values (' 1') insert into t (c) values (' 2') insert into t (c) values...
0
by: Kaur | last post by:
Hi, I have created a report based on a cross tab query and would like to Sort the column heading of the report based on the sort order of another field. I have three tables called survey...
13
by: bevanward | last post by:
Hi All I am finding unexpected results when inserted into a newly created table that has a field of datatype int identity (1,1). Basically the order I sort on when inserting into the table is...
3
by: Beowulf | last post by:
I was just messing around with some ad hoc views and table returning UDFs today so I could look at and print out data from a small table and noticed something strange. If I stick my select...
11
by: ankitmathur | last post by:
Hi, I'm trying to overcome a situation whereby I have to search through 4-5 columns and produce the results with an order by according to the values matched in these columns. Example: My...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.