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 > MAX vs COUNT - subselect

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 04-24-06, 05:26
riojan riojan is offline
Registered User
 
Join Date: Mar 2005
Posts: 15
MAX vs COUNT - subselect

I have a table where is recorded each employee carreer in my enterprise.

This is my table:

employee_id | date | function


The last date I have for each employee corresponds to the present function of that employee in my enterprise.

How can I know how many employees I have in each function at the present time?

I tried this one without success :


select id_function, count(id_function) from
(select id_employee, max(id_function) from carreer group by id_employee) group by id_function;


Thanks in advance.
Reply With Quote
  #2 (permalink)  
Old 04-24-06, 05:48
pootle flump pootle flump is offline
King of Understatement
 
Join Date: Feb 2004
Location: One Flump in One Place
Posts: 14,905
I wish I worked for you - I would love to only have one function in my enterprise

You would be looking to find the last date for each employee in one query and then match this last date to your table to find the last function your employees have been essigned.

HTH
__________________
Testimonial:
Quote:
pootle flump
ur codings are working excelent.
Reply With Quote
  #3 (permalink)  
Old 04-24-06, 06:56
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
match? like with a join? we don' need no steenkin joins!
Code:
select function
     , count(*) as employees
  from (
       select function
            , employee_id
         from carreer as T
        where date
            = ( select max(date)
                  from carreer
                 where employee_id = T.employee_id )  
       ) as latest
group
    by function
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #4 (permalink)  
Old 04-24-06, 07:34
pootle flump pootle flump is offline
King of Understatement
 
Join Date: Feb 2004
Location: One Flump in One Place
Posts: 14,905
Quote:
Originally Posted by r937
match? like with a join?
Or match to corrolated subQ result

Is that your latest NZDF mission - eliminate joins from RFH responses?
__________________
Testimonial:
Quote:
pootle flump
ur codings are working excelent.
Reply With Quote
  #5 (permalink)  
Old 04-26-06, 11:14
riojan riojan is offline
Registered User
 
Join Date: Mar 2005
Posts: 15
max count

Maybe I didn't explain myself quite well. What I'd like to know is:

How many employees I have in each level of my organization chart (function) at the present date.
So what I need is to count how many employees I have in each "function" of this select -

Select employee_id, max(funtion) from carreer group by employee_id
Reply With Quote
  #6 (permalink)  
Old 04-26-06, 11:32
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
did you at least try the query i suggested? what was the result?
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
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