?
Solved

SQL Query Issue

Posted on 2009-02-09
2
Medium Priority
?
171 Views
Last Modified: 2012-05-06
Experts,

The attached query works until the last line.  I'm trying to add a condition where the transaction type ID is 13 and the account type ID is 31, in addition to what is already there.  However, I lose the date logic on the WHERE clause when I add it.

In other words, without that last OR statment, I only get transactions date for 20090203.  But when I add that last line, I get transactions for all dates.

What am I misssing?
SELECT cu_acct_num, tr_trans_number, tr_eff_date, tr_amount, tr_Notes, c.acct_type_id, ct.trans_type_id ,tt.trans_code 
from customer_transaction ct join customer c on ct.customer_id = c.customer_id  
JOIN TRANSACTION_TYPE tt  ON tt.Trans_Type_ID=ct.Trans_Type_ID 
where tr_eff_date='20090203'  
AND ct.Trans_Type_ID IN (6,170,181,191,203,208,230,253,268,402)  
OR (ct.Trans_Type_ID = 13 AND c.acct_type_id <> 31)

Open in new window

0
Comment
Question by:NigelRocks
2 Comments
 
LVL 28

Accepted Solution

by:
TextReport earned 2000 total points
ID: 23592215
I think your bracket aren't quite right.
Cheers, Andrew
SELECT cu_acct_num, tr_trans_number, tr_eff_date, tr_amount, tr_Notes, c.acct_type_id, ct.trans_type_id ,tt.trans_code 
from customer_transaction ct join customer c on ct.customer_id = c.customer_id  
JOIN TRANSACTION_TYPE tt  ON tt.Trans_Type_ID=ct.Trans_Type_ID 
where tr_eff_date='20090203'  
AND (   ct.Trans_Type_ID IN (6,170,181,191,203,208,230,253,268,402)  
     OR (ct.Trans_Type_ID = 13 AND c.acct_type_id <> 31)
    )

Open in new window

0
 
LVL 25

Expert Comment

by:reb73
ID: 23592225
You have to enclose check for ct.Trans_Type_ID within extra set of brackets. Try -


SELECT cu_acct_num, tr_trans_number, tr_eff_date, tr_amount, tr_Notes, c.acct_type_id, ct.trans_type_id ,tt.trans_code 
from customer_transaction ct join customer c on ct.customer_id = c.customer_id  
JOIN TRANSACTION_TYPE tt  ON tt.Trans_Type_ID=ct.Trans_Type_ID 
where tr_eff_date='20090203'  
AND (ct.Trans_Type_ID IN (6,170,181,191,203,208,230,253,268,402)  OR (ct.Trans_Type_ID = 13 AND c.acct_type_id <> 31))

Open in new window

0

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
An alternative to the "For XML" way of pivoting and concatenating result sets into strings, and an easy introduction to "common table expressions" (CTEs). Being someone who is always looking for alternatives to "work your data", I came across this …
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…

621 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question