473,385 Members | 1,645 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.

Using a condition in a SELECT statement

Is there a way to write a select statement so that it does the
following:

Selects all students with a preferred addresses. If a student does
not have a preferred address, then select the first address that the
student has listed in the database.

So far this is what I have:

SELECT DISTINCT
PERSON.PERSON_ID ,
PERSON.STREET1 ,
PERSON.STREET2 ,
PERSON.CITY ,
PERSON.STATE ,
PERSON.COUNTRY ,
PERSON.ZIP_CODE ,
FROM
PERSON_ADDRESS
WHERE
PERSON_ADDRESS.PREFERRED_STATUS = '1';
The script above only gives me students with preferred addresses. I
am looking to add something like this:

<select statement goes here>
WHERE
PERSON_ADDRESS.PREFERRED_STATUS = '1'
If PERSON_ADDRESS.PREFERRED_STATUS = '0' Then
---SELECT TOP
-----PERSON.PERSON_ID ,
-----PERSON.STREET1 ,
-----PERSON.STREET2 .....
---FROM
-----PERSON_ADDRESS
End If
My example is just a prototype. Any ideas if something like this can
be accomplished?

Marcus
******

Nov 13 '05 #1
1 2627
"Marcus" <to*******@yahoo.ca> wrote in message
news:11*********************@f14g2000cwb.googlegro ups.com...
Is there a way to write a select statement so that it does the
following:

Selects all students with a preferred addresses. If a student does
not have a preferred address, then select the first address that the
student has listed in the database.

So far this is what I have:

SELECT DISTINCT
PERSON.PERSON_ID ,
PERSON.STREET1 ,
PERSON.STREET2 ,
PERSON.CITY ,
PERSON.STATE ,
PERSON.COUNTRY ,
PERSON.ZIP_CODE ,
FROM
PERSON_ADDRESS
WHERE
PERSON_ADDRESS.PREFERRED_STATUS = '1';
The script above only gives me students with preferred addresses. I
am looking to add something like this:

<select statement goes here>
WHERE
PERSON_ADDRESS.PREFERRED_STATUS = '1'
If PERSON_ADDRESS.PREFERRED_STATUS = '0' Then
---SELECT TOP
-----PERSON.PERSON_ID ,
-----PERSON.STREET1 ,
-----PERSON.STREET2 .....
---FROM
-----PERSON_ADDRESS
End If
My example is just a prototype. Any ideas if something like this can
be accomplished?

Marcus
******

1. Assuming that PERSON_ID is unique to each person, then I would first
create a query that selects all of the persons with a preferred address (the
query you've described above).

2. Then create a 2nd query that selects everyone who is NOT in the 1st query
(I think this is called an outer-join on ID with the 2nd ID criteria set to
Null).

3. Then create a 3rd query which UNIONs the 1st & 2nd query together.

Does that make sense?
Fred Zuckerman
Nov 13 '05 #2

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

Similar topics

2
by: Chris | last post by:
I have tableA, defined as: field1 varchar2(10), field2 varchar2(10), field3 varchar2(10) I have host variables defined as: v1 pic x(10) varying v2 pic x(10) varying
6
by: Rudolf Bargholz | last post by:
Hi , I have the following tables ------------- PAX: Id Order_Id Name Position
2
by: mr_mach7 | last post by:
I have a situation in which I want to dynamically build a SQL statement based upon criteria. The statement would contain different fields depending upon the criteria. I created a Stored Procedure...
11
by: Grasshopper | last post by:
Hi, I am automating Access reports to PDF using PDF Writer 6.0. I've created a DTS package to run the reports and schedule a job to run this DTS package. If I PC Anywhere into the server on...
3
by: StBond | last post by:
Hi everyone, I am new to Access and Visual Basic so things my be getting across a bit cloudy. I only started using VB for one week. I am having a little problem with the database that I am...
8
by: rbg | last post by:
I did use query plans to find out more. ( Please see the thread BELOW) I have a question on this, if someone can help me with that it will be great. In my SQL query that selects data from table,...
6
by: charmgirl | last post by:
hi, i facing a problem in asp the code goes like this thereare two different database. i have to add multple combo in the page only if my one condition is true. eg var RepName;
0
by: db2admin | last post by:
Hi, I am getting error when exporting data from mainframe using db2 connect while i am on db2 on linux. Here is what error looks like...
2
by: imtmub | last post by:
I have textbox in application. If user type in text box it will search in the table. It basically ItemId here is my code Select top 1000 ima_itemid as ItemID, ima_itemName as ItemName from ima...
1
by: robin1983 | last post by:
Dear All, I got stuck in simple problem, I have a two php file one for registration form and one for to check and insert into the table. The problem is that when I get any kind error in...
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: 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
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?
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...

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.