tags:

views:

64

answers:

4

In DB2, using the following left join

select a.deptno, a.deptname, b.empno
from #dept a
left join #emp b
on a.deptno = b.workdept

on two tables, gets me a list like:

dpt  name         emp
----------------------
A01  ACCOUNTING   5001
A02  PAYROLL      NULL
A03  OPERATIONS   5003
A03  OPERATIONS   5004
A03  OPERATIONS   5007
A05  MAINTENANCE  NULL

but I want only the first instance of any dpt. Is there a way to code the left join to pull only the first occurrence, so that it would look like:

dpt  name         emp
----------------------
A01  ACCOUNTING   5001
A02  PAYROLL      NULL
A03  OPERATIONS   5003
A05  MAINTENANCE  NULL
A: 

did you try distinct/unique?

stu
DISTINCT operates on all columns in the SELECT clause, so it wouldn't make any difference.
Ben Dunlap
+2  A: 

You can try something like

select a.deptno, a.deptname, min(b.empno)
from #dept a
left join #emp b
on a.deptno = b.workdept
group by a.deptno, a.deptname
Mladen Jablanović
This worked exactly as needed, thanks!
Cyberherbalist
+3  A: 
select a.deptno, a.deptname, b.empno
from #dept a
left join #emp b
on a.deptno = b.workdept
group by a.deptno
having b.empno = min(b.empno)

Something like this should work.

Anna
+1 for adding a "having" clause, which keeps the user in control of /which/ "first row" is returned.
Ben Dunlap
This doesn't actually work because b.empno and a.deptname don't occur in the group by clause... and when I put them in there, the results are exactly the same as the original.
Cyberherbalist
This is really interesting that it doesn't work. Probably the group by statement works a bit differently in DB2 than what I'm used to.Adding b.empno to the group by surely won't work, since the "having" groups will be of size 1. Peculiarly, a.deptname is compulsory in the group by statement. Hmm.
Anna
Now, it might work in DB2. Because I was asking the question for a coworker who had never heard of StackOverflow, and I don't actually have DB2, I was substituting your Sql in Sql Server Management Studio and that is where it didn't work. I directed him to this page so he can see the answers and possibly what you posted will worK in DB2. Thanks for your response in any case!
Cyberherbalist
I don't think selecting columns you don't have in "GROUP BY" is allowed in most SQL implementations (except by using aggregate functions on them, of course). I guess that you're using MySQL, Anna?
Mladen Jablanović
+1  A: 

It really depends on how you define "first". In the result set you've generated, the empno is essentially random. If the empno doesn't matter, why not leave it out? So:

SELECT DISTINCT a.deptno, a.deptname
FROM #dept aleft 
JOIN #emp bon a.deptno = b.workdept;

If the empno does matter, then you need to define what quality of empno is most important and test for it. So:

SELECT a.deptno, a.deptname, b.empno
FROM #dept aleft 
JOIN #emp bon a.deptno = b.workdept
GROUP BY a.deptno
HAVING  b.empno = some_criteria(b.empno);//where some_criteria is the appropriate function
dnagirl
You make a good point, but the case in the question is more along the lines of a generic sample; in the underlying real-world situation all the columns are needed, even if some column values get excluded in the final result.
Cyberherbalist