SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


need help with CASE statement and table joins


need help with CASE statement and table joins

Author
Message
3graces
3graces
Forum Newbie
Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)

Group: General Forum Members
Points: 7 Visits: 10
Hi, can someone help me figure out the best syntax for this query? Much appreciated -


SELECT sa.accountno AS accountno, s.saledate AS SaleDate, s.saleprice AS saleprice,
s.improvedflag AS improvedflag, s.grantor AS grantor, s.grantee AS grantee,
s.receptionno AS recpno, d.deeddescription AS deed,
CASE
when s.saleprice = '0' AND s.improvedflag = '0' then s.improvedflag = 'N/A'
when s.saleprice > '0' AND s.improvedflag = '0' then s.improvedflag = 'Vacant'
when s.saleprice > '0' AND s.improvedflag = '1' then s.improvedflag = 'Improved'
else NULL
end
from tblsale s, tblsaleacct sa, tlkpdeedtype d
where sa.receptionno = s.receptionno
and s.deedcode = d.deedcode
and s.verend = '99999999999'
and sa.verend = '99999999999'
order by sa.accountno, s.saledate;
Lynn Pettis
Lynn Pettis
SSC Guru
SSC Guru (52K reputation)SSC Guru (52K reputation)SSC Guru (52K reputation)SSC Guru (52K reputation)SSC Guru (52K reputation)SSC Guru (52K reputation)SSC Guru (52K reputation)SSC Guru (52K reputation)

Group: General Forum Members
Points: 52144 Visits: 38684
First, please write your code using the ANSI-92 style joins, not the ANSI-89 style joins. Putting your join criteria in the FROM clause and your filter criteria in the WHERE clause makes reading your code easier.

Here is what I think you are attempting to accomplish. If not, you need to give us more detail as to what you are trying to accomplish:


SELECT
sa.accountno AS accountno,
s.saledate AS SaleDate,
s.saleprice AS saleprice,
s.improvedflag AS improvedflag,
s.grantor AS grantor,
s.grantee AS grantee,
s.receptionno AS recpno,
d.deeddescription AS deed,
CASE
when s.saleprice = '0' AND s.improvedflag = '0' then 'N/A'
when s.saleprice > '0' AND s.improvedflag = '0' then 'Vacant'
when s.saleprice > '0' AND s.improvedflag = '1' then 'Improved'
else NULL
end ImprovedFlagDescription
from
tblsale s
inner join tblsaleacct sa
on (sa.receptionno = s.receptionno)
inner join tlkpdeedtype d
on (sa.receptionno = s.receptionno)
where
s.verend = '99999999999'
and sa.verend = '99999999999'
order by
sa.accountno,
s.saledate;



Cool
Lynn Pettis

For better assistance in answering your questions, click here
For tips to get better help with Performance Problems, click here
For Running Totals and its variations, click here or when working with partitioned tables
For more about Tally Tables, click here
For more about Cross Tabs and Pivots, click here and here
Managing Transaction Logs

SQL Musings from the Desert Fountain Valley SQL (My Mirror Blog)
3graces
3graces
Forum Newbie
Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)

Group: General Forum Members
Points: 7 Visits: 10
Yes, this is perfect, thank you. The second inner join was on deedcode, but with that change it works exactly the way I need it to.
Thanks so much!
3graces
3graces
Forum Newbie
Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)

Group: General Forum Members
Points: 7 Visits: 10
I have another join question for you SQL pros. The query below produces the accurate result set, but I'm guessing the syntax isn't up to current standards. Should the first where clause (where p.personcode = o.personcode) be an inner join? Or a nested query? And if so, how would you write it? Thanks so much for your help -

tbladdress contains addresscode, addresses
tblperson contains personcode, names
tblaccount contains account #, other stuff needed
tblacctowneraddress contains personcode, addresscode and account #

select distinct a.accountno, a.parcelno, p.privateflag, p.name1, p.name2, a.businessname,
ad.address1, ad.address2, ad.city, ad.statecode, ad.zipcode, ad.province, ad.country, ad.postalcode
from tbladdress ad
inner join tblperson p
on (p.personcode = ad.personcode)
inner join tblacctowneraddress o
on (o.addresscode = ad.addresscode)
inner join tblacct a
on (a.accountno = o.accountno)
where p.personcode = o.personcode
and a.acctstatuscode = 'A'
and a.accountno not like 'P%'
and p.privateflag != '1'
and o.primaryownerflag = '1'
and a.verend = '99999999999'
and p.verend = '99999999999'
and o.verend = '99999999999'
and ad.verend = '99999999999'
order by a.accountno;
ALZDBA
ALZDBA
SSCoach
SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)SSCoach (16K reputation)

Group: General Forum Members
Points: 16078 Visits: 8971
Because you're only using inner joins, just let the sql engine determine at which step in the process it will handle your where clause.
(It will take indexes and statistics into account for that to determine its sqlplan )

select distinct
a.accountno
, a.parcelno
, p.privateflag
, p.name1
, p.name2
, a.businessname
, ad.address1
, ad.address2
, ad.city
, ad.statecode
, ad.zipcode
, ad.province
, ad.country
, ad.postalcode
from tbladdress ad
inner join tblperson p
on p.personcode = ad.personcode
inner join tblacctowneraddress o
on o.addresscode = ad.addresscode
and o.personcode = p.personcode
inner join tblacct a
on a.accountno = o.accountno
where a.acctstatuscode = 'A'
and a.accountno not like 'P%'
and p.privateflag != '1'
and o.primaryownerflag = '1'
and a.verend = '99999999999'
and p.verend = '99999999999'
and o.verend = '99999999999'
and ad.verend = '99999999999'
order by a.accountno ;



Johan


Dont drive faster than your guardian angel can fly ...
but keeping both feet on the ground wont get you anywhere w00t

- How to post Performance Problems
- How to post data/code to get the best help


- How to prevent a sore throat after hours of presenting ppt ?


press F1 for solution, press shift+F1 for urgent solution :-D


Need a bit of Powershell? How about this

Who am I ? Sometimes this is me Alien but most of the time this is me Hehe
3graces
3graces
Forum Newbie
Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)

Group: General Forum Members
Points: 7 Visits: 10
Thank you so much, Johan. I really appreciate your help.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search