If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > ANSI SQL > using OR vs using Union

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 09-09-09, 05:08
vickrampurswani vickrampurswani is offline
Registered User
 
Join Date: Sep 2009
Posts: 3
Exclamation using OR vs using Union

Hi All,
this is my first post on any forum.
It would be great if someone can tell me whether to use OR or a UNION when both are giving same results.

Also i want to know is the use of CASE statement like below correct ?

here are two queries that I am talking about

1
+++++++++++++++++++++++++++++++++++++++++++++++++
SELECT
'FAILED' status,
'CR' trx_type,
'P2P' eventstatuscode,
src.devicenumber display_device,
FROM transactionevent a, account src

WHERE a.tgtaccountid =:srcaccountid
AND a.eventstatuscode IN ('abc')
AND a.transtypecode = 'P2P'
AND src.id = a.srcaccountid

UNION


'FAILED' status,
'DR' trx_type,
'P2P' eventstatuscode,
tgt.devicenumber display_device
FROM transactionevent a, account tgt,

WHERE a.srcaccountid =:srcaccountid
AND a.eventstatuscode IN ('abc')
AND a.transtypecode = 'P2P'
AND tgt.id = a.tgtaccountid

++++++++++++++++++++++++++++++++++++++++++++++++++ +

2
++++++++++++++++++++++++++++++++++++++++++++++++++
SELECT
'FAILED' status,
(CASE
WHEN src.id = a.tgtaccountid THEN 'DR'
WHEN src.id = a.srcaccountid THEN 'CR'
END ) trx_type,
'P2P' eventstatuscode,
src.devicenumber display_device
FROM transactionevent a, account src

WHERE ( a.tgtaccountid =:srcaccountid OR a.srcaccountid=:srcaccountid )
AND a.eventstatuscode IN ('abc')
AND a.transtypecode = 'P2P'
AND src.id = (CASE
WHEN a.srcaccountid =:srcaccountid then a.tgtaccountid
WHEN a.tgtaccountid =:srcaccountid then a.srcaccountid
END
)

++++++++++++++++++++++++++++++++++++++++++++++++++
Reply With Quote
  #2 (permalink)  
Old 09-09-09, 05:41
mike_bike_kite mike_bike_kite is offline
vaguely human
 
Join Date: Jun 2007
Location: London
Posts: 2,519
Just some quick comments:
  • Welcome to the forum!
  • What RDBMS are you using?
  • If both statements produce the correct answer in a reasonable time then both statements are correct. Personally I find the OR easier to read.
  • You are missing a select after the union keyword.
  • You should use a.eventstatuscode = 'abc' rather than a.eventstatuscode IN ('abc')
  • Your first case statement uses = while the second case uses =: - this seems very odd.
Reply With Quote
  #3 (permalink)  
Old 09-09-09, 06:06
pootle flump pootle flump is offline
King of Understatement
 
Join Date: Feb 2004
Location: One Flump in One Place
Posts: 14,905
Mike - I _think_ these are non ANSI outer joins. As such, I would recommend changing the joins to ANSI.

I'm fairly sure there are more syntax errors in those queries too.... OP - are you sure these run?
__________________
Testimonial:
Quote:
pootle flump
ur codings are working excelent.
Reply With Quote
  #4 (permalink)  
Old 09-09-09, 08:10
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,511
no, these aren't wacky outer joins, the operator is "=" in all cases

the queries are using a single run-time parameter, :srcaccountid
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #5 (permalink)  
Old 09-09-09, 08:30
pootle flump pootle flump is offline
King of Understatement
 
Join Date: Feb 2004
Location: One Flump in One Place
Posts: 14,905
Ah ok. Note this wasn't in ANSI SQL when first posted otherwise I wouldn't have responded and displayed my ignorance.

I'd still use ANSI joins though, be they innies or outies.
__________________
Testimonial:
Quote:
pootle flump
ur codings are working excelent.
Reply With Quote
  #6 (permalink)  
Old 09-09-09, 11:02
vickrampurswani vickrampurswani is offline
Registered User
 
Join Date: Sep 2009
Posts: 3
Quote:
Originally Posted by pootle flump
Mike - I _think_ these are non ANSI outer joins. As such, I would recommend changing the joins to ANSI.

I'm fairly sure there are more syntax errors in those queries too.... OP - are you sure these run?

I missed 1 select after UNION while pasting it.

the query works flawlessly , =: is parameter that i pass
Reply With Quote
  #7 (permalink)  
Old 09-09-09, 11:04
vickrampurswani vickrampurswani is offline
Registered User
 
Join Date: Sep 2009
Posts: 3
Quote:
Originally Posted by mike_bike_kite
Just some quick comments:
  • Welcome to the forum!
  • What RDBMS are you using?
  • If both statements produce the correct answer in a reasonable time then both statements are correct. Personally I find the OR easier to read.
  • You are missing a select after the union keyword.
  • You should use a.eventstatuscode = 'abc' rather than a.eventstatuscode IN ('abc')
  • Your first case statement uses = while the second case uses =: - this seems very odd.
Hi Mike, thanks for replying..actually when i use =: i am getting value from a parameter. srcAccoutnId is a parameter. I am using Oracle
Reply With Quote
  #8 (permalink)  
Old 09-09-09, 11:10
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,511
Quote:
Originally Posted by pootle flump
Note this wasn't in ANSI SQL when first posted...
it wasn't in the oracle forum, by any chance, was it?

__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #9 (permalink)  
Old 09-09-09, 11:40
pootle flump pootle flump is offline
King of Understatement
 
Join Date: Feb 2004
Location: One Flump in One Place
Posts: 14,905
No - dbconcepts
__________________
Testimonial:
Quote:
pootle flump
ur codings are working excelent.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On