SQL for Smarties | SQL Programming Style | Trees and Hierarchies in SQL | SQL Puzzles and Answers | Data and Databases


Wednesday, April 12, 2006

Query help...

SQL Apprentice Question
have a table with two columns...

Inital Number, End Number

(sample data)
1, 1
1, 3
2, 4
1, 5
3, 2
etc

I want to make another column that provides the following information..
1-1
1-3
2-4
1-5
3-2

I figured I could make hundreds of case statements... eg:
case when initialnumber=1 and endnumber=1 then '1-1' end as Column3
case when initialnumber=1 and endnumber=2 then '1-2' end as Column3

But is there a better way?
Thanks


Celko Answers
Please post DDL, so that people do not have to guess what the keys,
constraints, Declarative Referential Integrity, data types, etc. in
your schema are. Sample data is also a good idea, along with clear
specifications. It is very hard to debug code when you do not let us
see it.

CREATE TABLE Foobar
(start_nbr INTEGER NOT NULL,
end_nbr INTEGER NOT NULL,
CHECK (start_nbr <= end_nbr),
PRIMARY KEY (start_nbr, end_nbr));

>> I want to make another column that provides the following information.<<

What you showed seemed to be display and formatting. Repeat after me
the fundamental rule of a tiered architecture is that dispaly is NEVER
-- repeat NEVER -- done in the data base.

No comments: