Solved

SyBase SQL Syntax

Posted on 2013-10-25
4
694 Views
Last Modified: 2013-10-25
Hey guys,
 
I have 2 tables using SyBase SQL Anywhere 10
Employee Table
EmpNum
Firstname
Lastname
 
Sales Table
Transact
WhoStart (EmpNum)
WhoClosed   (EmpNum)
 
I want to return the transaction number and the first name’s of the employee who started and closed the sale. Usually the person who starts and closes is the same, but sometimes it’s not.
 
Help?
0
Comment
Question by:triphen
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 26

Assisted Solution

by:wilcoxon
wilcoxon earned 250 total points
ID: 39601768
This should do what you want...
select s.Transact, es.Firstname, ec.Firstname
from Sales s, Employee es, Employee ec
where s.WhoStart = es.EmpNum
and s.WhoClosed = ec.EmpNum

Open in new window

0
 
LVL 49

Accepted Solution

by:
PortletPaul earned 250 total points
ID: 39601812
I'd prefer to see ANSI join syntax used, the following is the equivalent of the prior comment:
SELECT
        s.Transact
      , es.Firstname AS StartByName
      , ec.Firstname AS ClosedByName
FROM Sales s
INNER JOIN Employee AS es ON s.WhoStart = es.EmpNum
INNER JOIN Employee AS ec ON s.WhoClosed = ec.EmpNum /* but might need LEFT JOIN ?? */
;

Open in new window

whilst it may be unlikely, if there is any chance that a record in that Sales tables does not have a value for WhoClosed, then you would want to use a LEFT JOIN

(I do hope you would always have values in WhoStart, but if some are missing then it too might need a left join)
0
 
LVL 26

Expert Comment

by:wilcoxon
ID: 39601850
Tangent...  Personally I *HATE* the ANSI join syntax.  I find it much harder to read than the "old" join syntax.  For this query, it's not bad but, as it gets more complex, the less I like ANSI joins.
0
 

Author Closing Comment

by:triphen
ID: 39601907
Thanks guys! I ended up using the ANSI join syntax.

left outer join dba.employee as es
on es.empnum = header.whostart

left outer join dba.employee as ec
on ec.empnum = header.whoclose
0

Featured Post

Will your db performance match your db growth?

In Percona’s white paper “Performance at Scale: Keeping Your Database on Its Toes,” we take a high-level approach to what you need to think about when planning for database scalability.

Question has a verified solution.

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

There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
Compliance and data security require steps be taken to prevent unauthorized users from copying data.  Here's one method to prevent data theft via USB drives (and writable optical media).
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

617 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