473,385 Members | 1,523 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,385 software developers and data experts.

how to select data in a table using case statements:

7
Hi,

I don't know if I'm posting this question in the right forum.

I'm new to SQL, and I'm hoping to get some help in selecting a value based on a hierachy.

What I'm trying to achieve is get the campus phone number in the selected values, and if this is not available, then go down the hierarchy:
if campus phone is null then get business_phone
if business_phone is null then get permanent_phone

columns in the table are defined as follows:
emp_id, tele_code, tele_phone
1234 CA (campus) NULL
1234 PR(permanent) xxx-xxxx
1234 BU(business) xxx-xxxx

I'm thinking I have to use a case statement, but if there are any suggestion, I would appreciate the input.

Thank you in advance for all the help.
salex
Oct 4 '06 #1
5 1658
r035198x
13,262 8TB
Hi,

I don't know if I'm posting this question in the right forum.

I'm new to SQL, and I'm hoping to get some help in selecting a value based on a hierachy.

What I'm trying to achieve is get the campus phone number in the selected values, and if this is not available, then go down the hierarchy:
if campus phone is null then get business_phone
if business_phone is null then get permanent_phone

columns in the table are defined as follows:
emp_id, tele_code, tele_phone
1234 CA (campus) NULL
1234 PR(permanent) xxx-xxxx
1234 BU(business) xxx-xxxx

I'm thinking I have to use a case statement, but if there are any suggestion, I would appreciate the input.

Thank you in advance for all the help.
salex
no this is not the correct forum.
no do not use case.
Use if else statements

if campus phone is not null get it
else if business_phone is not null get it
else if permanent_phone is not null get it
Oct 4 '06 #2
salex
7
Can you use an if statement inside a sql select statement?
Is it decode() function?

Thank you,
salex
Oct 5 '06 #3
ronverdonk
4,258 Expert 4TB
This is a programming question and does not, strictly speaking, belong in this forum.
Anyway: in MySQL's case: DECODE() is a MySql extension to standard SQL.

It would be better to use your code to determine what telephone no to use. Since we cannot see what programming language and what type of database you are using, I would advise you to post your code here, so we can have a look.

Example: if you use PHP/MySQL something like the following could be used:
[PHP]$res=mysql_query("your_select_statement");
while ($row = mysql_fetch_assoc($res)) {
if ($row['business_phone'] == NULL) $phone = $row['business_phone']);
if ($row['campus_phone'] == NULL) $phone = $row['campus_phone']);
if ($row['permanent_phone'] == NULL) $phone = $row['permanent_phone']);
// other assignments
}[/PHP]

Ronald :cool:
Oct 5 '06 #4
ronverdonk
4,258 Expert 4TB
That previous reply was a BIG BLOOPER!!
[php]
$res=mysql_query("your_select_statement");
while ($row = mysql_fetch_assoc($res)) {
if ($row['campus_phone'] != NULL) $phone = $row['campus_phone']);
else
if ($row['business_phone'] != NULL) $phone = $row['business_phone']);
else
if ($row['permanent_phone'] != NULL) $phone = $row['permanent_phone']
else $phone = 'no phone';
// other assignments
}
[/php]

Ronald :cool:
Oct 10 '06 #5
salex
7
Thank you guys for your help, and I apologize for the wrong forum!
Oct 16 '06 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Hans Maurer | last post by:
>Description: We're running our current TTS application with MySQL (on Unix). All database, table and column names are in lower-case. However, we need to access this database with a new...
2
by: Edwinah63 | last post by:
Hi Everyone, All the very best for 2004!! i need urgent help with this problem, the users are about to skin me alive!! we have an access front end with linked to sql server 2k tables. ...
1
by: avinash | last post by:
hi myself avi i am developing one appliacaion in which i am using vb 6 as front end, adodb as database library and sql sever 7 as backend. i want to update one table for which i required data from...
15
by: grunar | last post by:
After some thought on what I need in a Python ORM (multiple primary keys, complex joins, case statements etc.), and after having built these libraries for other un-named languages, I decided to...
12
by: TP | last post by:
Here is my problem. I need to display a table about which I have no information except the table name. Using metadata I can somehow show the column names and record values. But my table has 1...
3
by: Tcs | last post by:
My backend is DB2 on our AS/400. While I do HAVE DB2 PE for my PC, I haven't loaded it yet. I'm still using MS Access. And no, I don't believe this is an Access question. (But who knows? I...
2
by: Marco Lazzeri | last post by:
What about a SELECT syntax like that? SELECT CASE WHEN bar.foo_id IS NULL THEN bar.* ELSE foo.* FROM foo, bar Anyway, I need a SELECT query that gets data from a FIRST TABLE if a specific...
4
by: Ed L. | last post by:
I think I'm seeing table-level lock contention in the following function when I have many different concurrent callers, each with mutually distinct values for $1. Is there a way to reimplement...
29
by: pb648174 | last post by:
I have the following basic statements being executed: Create a temp table, #TempPaging Insert Into #TempPaging (Col1, Col2) Select Col1, Col2 From SomeOtherTable Order By Col2, Col1 Select...
6
by: jazpar | last post by:
Could anyone help med with a select statement with a join between to tables. It is to be used in a OLAP cube. I Havde table LedgerBudget and Table Admin. In table admin I can setup a from and to...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.