473,406 Members | 2,217 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,406 software developers and data experts.

Query help...

Is there a way to use SELECT to reassign the value of a column so it
displays differently?

For instance, I have a column called status. It is an integer value
in the database (i.e. valid values are 1, 2, 3, 4). I want to display
them for what them mean (i.e. 1 = NORMAL, 2 = DEGRADED, 3 = ERROR, 4 =
DEAD). So I want to display them for the name or maybe an initial (N
= normal).

So if my table looks like this

TABLE1
--------------

NAME STATUS
---------- ------------
ROW1 2
ROW2 3
ROW3 1
ROW4 1
ROW5 4
I would like to see it as

TABLE1
--------------

NAME STATUS
---------- ------------
ROW1 DEGRADED
ROW2 ERROR
ROW3 NORMAL
ROW4 NORMAL
ROW5 DEAD

Is there a way to do this?

Jul 4 '07 #1
3 1617
On Jul 4, 2:11 am, shorti <lbrya...@juno.comwrote:
Is there a way to use SELECT to reassign the value of a column so it
displays differently?

For instance, I have a column called status. It is an integer value
in the database (i.e. valid values are 1, 2, 3, 4). I want to display
them for what them mean (i.e. 1 = NORMAL, 2 = DEGRADED, 3 = ERROR, 4 =
DEAD). So I want to display them for the name or maybe an initial (N
= normal).

So if my table looks like this

TABLE1
--------------

NAME STATUS
---------- ------------
ROW1 2
ROW2 3
ROW3 1
ROW4 1
ROW5 4

I would like to see it as

TABLE1
--------------

NAME STATUS
---------- ------------
ROW1 DEGRADED
ROW2 ERROR
ROW3 NORMAL
ROW4 NORMAL
ROW5 DEAD

Is there a way to do this?
As an alternative to the solution provided by Jan, you may consider
defining a Status 'Lookup' table, containing status_id and
status_desc, and joining this with your original table.
This way it will probably be easier to add additional status values.
Depending on your environment, it may have performance consequences
(positive or negative).

HTH.

--
Jeroen

Jul 4 '07 #2
On Tue, 03 Jul 2007 17:11:00 -0700, shorti scribbled:
Is there a way to use SELECT to reassign the value of a column so it
displays differently?

For instance, I have a column called status. It is an integer value in
the database (i.e. valid values are 1, 2, 3, 4). I want to display them
for what them mean (i.e. 1 = NORMAL, 2 = DEGRADED, 3 = ERROR, 4 = DEAD).
So I want to display them for the name or maybe an initial (N =
normal).

So if my table looks like this

TABLE1
--------------

NAME STATUS
---------- ------------
ROW1 2
ROW2 3
ROW3 1
ROW4 1
ROW5 4
I would like to see it as

TABLE1
--------------

NAME STATUS
---------- ------------
ROW1 DEGRADED
ROW2 ERROR
ROW3 NORMAL
ROW4 NORMAL
ROW5 DEAD

Is there a way to do this?
Jan and Jeroen have already suggested perfectly valid ways of doing this,
but I thought I'd flesh out the suggestions a bit and demonstrate another
(rather esoteric) way of solving this in DB2's SQL dialect :-)

First, Jan's suggestion of use a CASE expression:

SELECT
NAME,
CASE STATUS
WHEN 1 THEN 'NORMAL'
WHEN 2 THEN 'DEGRADED'
WHEN 3 THEN 'ERROR'
WHEN 4 THEN 'DEAD'
ELSE 'INVALID'
END AS STATUS
FROM
TABLE1;

Note that an ELSE clause is used to ensure that even invalid status codes
will be meaningfully labelled in the result.

Next, Jeroen's suggestion of using a lookup table (which can eliminate
the possibility of an invalid status, i.e. the ELSE clause in the CASE
expression above, by defining a foreign key to the lookup table):

CREATE TABLE STATUSES (
STATUS_CODE INTEGER NOT NULL PRIMARY KEY,
STATUS_LABEL VARCHAR(8) NOT NULL
);

INSERT INTO STATUSES (STATUS_CODE, STATUS_LABEL)
VALUES
(1, 'NORMAL'),
(2, 'DEGRADED'),
(3, 'ERROR'),
(4, 'DEAD');

ALTER TABLE TABLE1
ADD CONSTRAINT STATUS_FK
FOREIGN KEY (STATUS) REFERENCES STATUSES(STATUS_CODE);

SELECT
T1.NAME,
S.STATUS_LABEL AS STATUS
FROM
TABLE1 T1 INNER JOIN STATUSES S
ON T1.STATUS = S.STATUS_CODE;

However, the INSERT syntax above gives a clue for another intriguing
possibility (although not as useful as having the status codes lookup
table): generate the lookup table on the fly...

SELECT
T1.NAME,
S.LABEL AS STATUS
FROM
TABLE1 T1 INNER JOIN (
VALUES
(1, 'NORMAL'),
(2, 'DEGRADED'),
(3, 'ERROR'),
(4, 'DEAD')
) AS S(CODE, LABEL)
ON T1.STATUS = S.CODE;

This isn't quite the same as using the lookup table: there's no foreign
key, hence invalid statuses would be eliminated by the INNER JOIN. This
could be changed to include invalid statuses (like the CASE expression in
the first example) like so:

SELECT
T1.NAME,
COALESCE(S.LABEL, 'INVALID') AS STATUS
FROM
TABLE1 T1 LEFT OUTER JOIN (
VALUES
(1, 'NORMAL'),
(2, 'DEGRADED'),
(3, 'ERROR'),
(4, 'DEAD')
) AS S(CODE, LABEL)
ON T1.STATUS = S.CODE;

Finally, one could make the query a bit neater by using a CTE (common
table expression) instead of a sub-query:

WITH

STATUSES (CODE, LABEL) AS (
VALUES
(1, 'NORMAL'),
(2, 'DEGRADED'),
(3, 'ERROR'),
(4, 'DEAD')
)

SELECT
T1.NAME,
COALESCE(S.LABEL, 'INVALID') AS STATUS
FROM
TABLE1 T1 LEFT OUTER JOIN STATUSES S
ON T1.STATUS = S.CODE;
Cheers,

Dave.
Jul 5 '07 #3
Thanks for all your help!!
Jul 9 '07 #4

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

Similar topics

9
by: netpurpose | last post by:
I need to extract data from this table to find the lowest prices of each product as of today. The product will be listed/grouped by the name only, discarding the product code - I use...
7
by: Simon Bailey | last post by:
How do you created a query in VB? I have a button on a form that signifies a certain computer in a computer suite. On clicking on this button i would like to create a query searching for all...
4
by: d.p. | last post by:
Hi all, I'm using MS Access 2003. Bare with me on this description....here's the situation: Imagine insurance, and working out premiums for different insured properties. The rates for calculating...
4
by: Alan Lane | last post by:
Hello world: I'm including both code and examples of query output. I appologize if that makes this message longer than it should be. Anyway, I need to change the query below into a pivot table...
36
by: Liam.M | last post by:
hey guys, I have one last problem to fix, and then my database is essentially done...I would therefore very much appreciate any assistance anyone would be able to provide me with. Currently I...
5
by: elitecodex | last post by:
Hey everyone. I have this query select * from `TableName` where `SomeIDField` 0 I can open a mysql command prompt and execute this command with no issues. However, Im trying to issue the...
10
by: aaronrm | last post by:
I have a real simple cross-tab query that I am trying to sum on as the action but I am getting the "data type mismatch criteria expression" error. About three queries up the food chain from this...
11
by: funky | last post by:
hello, I've got a big problem ad i'm not able to resolve it. We have a server running oracle 10g version 10.1.0. We usually use access as front end and connect database tables for data extraction....
4
by: Doris | last post by:
It does not look like my message is posting....if this is a 2nd or 3rd message, please forgive me as I really don't know how this site works. I want to apologize ahead of time for being a novice...
6
by: jsacrey | last post by:
Hey everybody, got a secnario for ya that I need a bit of help with. Access 97 using linked tables from an SQL Server 2000 machine. I've created a simple query using two tables joined by one...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.