开发者

How to view a string or update a column on a database table depending on the values of other columns?

开发者 https://www.devze.com 2023-01-23 05:35 出处:网络
I have a database table like that :- Table1 Column1 : Varchar(50) Column2 : Varchar(50) Column3 : Boolean

I have a database table like that :-

Table1

Column1 : Varchar(50)
Column2 : Varchar(50)
Column3 : Boolean
Column4 : Varchar(50)
Column5 : Varchar(50)

I would like to create a dummy column -or add a new column to the table- when running a SQL statement so that the value is that column will hold a string indicating the satus of the row item depeing on more than one of these columns. I mean wether these columns have Null values or not.

A logical way to represent that:-

Select switch DummyColumn

"status1" Case column1 IS NOT NULL
"status2" Case column2 IS NOT NULL
"status3" Case column3 IS NOT NULL
"status4" Case column1 IS NOT NULL AND column2 IS NOT NULL
"status5" Case column2 IS NOT NULL AND column3 IS NULL
"status6" Case column2 IS NOT NULL OR column3 IS NOT NULL

From Table1;

In Business Logic, I can say I want to make display a string indicating the status of an Item in the database depeding on many other status columns which may be boolen or string and this status can have many values according to the values stored in these table columns.

My database engine is Sybase.

How I can build my SQL query to do that ? what may be this matter called ? How I can find or search for that开发者_运维百科 ? I do not know in what topic I may find this ? I search about Merging, Concatening, Replacing, NVL, and Union but i do not find what I seek for..

Thanks in Advance and I hope that I express my question well and that it is a duplicate and I hope to find a good answer for it as soon as possible ..


Using a single CASE clause:

SELECT  Column1,
        Column2,
        Column3,
        Column4,       
        Column5,
        Column6,
        CASE WHEN Column1 IS NOT NULL AND Column2 IS NOT NULL THEN 'status4'
             WHEN Column2 IS NOT NULL AND Column3 IS NULL THEN 'status5'
             WHEN Column1 IS NOT NULL THEN 'status1'
             WHEN Column2 IS NOT NULL THEN 'status2'
             WHEN Column3 IS NOT NULL THEN 'status3'
             WHEN Column2 IS NOT NULL OR Column3 IS NOT NULL THEN 'status6'
        END  AS Dummy_Column
FROM    Table1

NB. Status6 will never be returned because it will be overridden by Status1 or Status2, even where Status4 and Status5 are not applicable.


There's more than one way to skin a cat, this is one of them.

SELECT  t.Column1
        , t.Column2
        , t.Column3
        , t.Column4        
        , t.Column5
        , t.Column6
        , COALESCE(Status1, Status2, Status3, Status4, Status5, Status6) AS DummyColumn
FROM    (
          SELECT  *
                  , CASE WHEN Column1 IS NOT NULL THEN 'status1' ELSE NULL END AS Status1
                  , CASE WHEN Column2 IS NOT NULL THEN 'status2' ELSE NULL END AS Status2
                  , CASE WHEN Column3 IS NOT NULL THEN 'status3' ELSE NULL END AS Status3
                  , CASE WHEN Column1 IS NOT NULL AND Column2 IS NOT NULL THEN 'status4' ELSE NULL END AS Status4
                  , CASE WHEN Column2 IS NOT NULL AND Column3 IS NULL THEN 'status5' ELSE NULL END AS Status5
                  , CASE WHEN Column2 IS NOT NULL OR Column3 IS NOT NULL THEN 'status6' ELSE NULL END AS Status6
          FROM    Table1
        ) t

Note that this works using SQL Server but as there are no arcane constructions involved, it should be relatively easy to convert this to SyBase.

0

精彩评论

暂无评论...
验证码 换一张
取 消