0

I couldnt find this, i'm sure its simple.

Table (196 rows)

tech, name, area, manager
------------------------------
Joe,mary,pa,sally
Kim,brad,ga,tim
kelly,Joe,pa,sally
Joe,jose,pa,sally

a tech is assigned to multiple name/area/managers. I want to do a report that shows all the rows of the table with a column showing the number of assignments for the tech.

My desired results

tech, name, area, manager, (count of number of tech assignments)
Joe,mary,pa,sally,2
Kim,brad,ga,tim,1
kelly,Joe,pa,sally,1
Joe,jose,pa,sally,2
8
  • There are 4 techs, 196 names, 12 areas and 20 managers. My result set should be 196 rows. Commented Apr 18, 2012 at 15:02
  • 2
    Your question isn't very clear. Can you give an example? Commented Apr 18, 2012 at 15:03
  • Table Tech,name,area, manager Joe, john, PA, Sally Joe, Mary, GA, Jose Commented Apr 18, 2012 at 15:05
  • So it will be select with join to add count column Commented Apr 18, 2012 at 15:06
  • 1
    I don't get it. If you have 196 distinct names, each with one tech assigned, that tech has exactly 1 assignment in that context. Commented Apr 18, 2012 at 15:07

4 Answers 4

3

I'm guessing you want a subquery on the SELECT clause:

SELECT 
   name, 
   area, 
   manager, 
   (SELECT COUNT(*) FROM tablename WHERE tech = x.tech) AS assignments
FROM tablename x

And here is a possibly more efficient way to do the same thing:

SELECT 
   t.name, 
   t.area, 
   t.manager,
    sub.assignments 
FROM tablename t
INNER JOIN (
    SELECT tech, COUNT(*) AS assignments 
    FROM tablename 
    GROUP BY tech
 ) sub
ON sub.tech = t.tech
Sign up to request clarification or add additional context in comments.

Comments

1
select 
    a.tech, a.name, a.area, a.manager,
    b.cnt
from table a, (
    select count(*) cnt, tech
    from table
    group by tech) b
where a.tech=b.tech;

Is this what you want?

4 Comments

Now that I rewrote my query I realize we're saying the same thing. +1
@bfavaretto but I like your first solution, it looks tidy.
Doesnt work, bad syntax, not sure where you are trying to go.
I think joining a subquery will perform better, as my first solution will always rerun the subquery for each row.
0
SELECT tech, count(name) AS total_records FROM db_table GROUP BY tech

Not sure this is what you're looking for though. Oh yeah, looks like you really need to use subquery. Thanks for the examples, that helped.

2 Comments

That's a very sane way to count assignments per tech, but strangely the OP seems to be asking for something else.
Yes, you're correct, bfavaretto. The time i posted that answer, there were no examples and i misunderstood it :) I've already voted up another answer with subquery method. Thanks
0
SELECT tech, name, area, manager, assignments  
FROM table
  INNER JOIN ( 
    SELECT tech, COUNT(*) AS assignments  
    FROM table  
    GROUP BY tech 
  ) t 
USING (tech)

Explanation:

Select all columns from table and join them with table containing tech column and count of rows from table having particular tech. Keyword USING says how those two table merge.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.