0

Scenario: I am trying to build a query that will return the SUM of a COUNT.
Purpose of the Query: I need to identify all of our Participants that are currently listed on 2 or more Positions.

Current Query:

select pa.firstname, pa.lastname, pos.name as "POSITION", count(pos.name) as "POSITION COUNT"

from cs_participant pa, cs_position pos

where pa.payeeseq = pos.payeeseq
and pa.firstname like '%DEV%'
and pa.lastname like '%TEST%'

group by pa.firstname, pa.lastname, pos.name

Results:
enter image description here

I need to SUM the POSITION COUNT and somehow return only FIRSTNAME, LASTNAME, and SUM. So, if a Participant is currently on 2 or more Positions, the query would return the Participant's FIRSTNAME, LASTNAME, and SUM of POSITIONS.

If I could also return the POSITION in the far column without creating duplicate rows for each unique POSITION, that would be a huge help!

For example, a Participant with 2 POSITIONS would return the Participant's FIRSTNAME, LASTNAME, SUM OF POSITIONS, and POSITION_1 on the first row, while the second row would have blank values for the Participant's FIRSTNAME, LASTNAME, and SUM of POSITIONS, but return POSITION_2 in the 4th column (Similar to a "break" function in Web Intelligence).

Thanks!

3
  • Looks like you want to probably store position in a separate joint_table?
    – xcoder
    Dec 9, 2014 at 16:43
  • Sounds like a job for HAVING, not SUM.
    – Mr. Llama
    Dec 9, 2014 at 16:45
  • Why do you think you need a stored procedure?
    – user330315
    Dec 9, 2014 at 18:00

3 Answers 3

1

You would just use aggregation with a having clause:

select pa.firstname, pa.lastname, count(*) as "POSITION COUNT"
from cs_participant pa join
     cs_position pos
     on pa.payeeseq = pos.payeeseq
group by pa.firstname, pa.lastname
having count(*) >= 2;

I removed the where conditions, because it is not clear what those are for. You can, of course, put them back in if they are relevant to your query.

1
  • Thanks Gordon -- The having clause seems to be pulling me closer. However, the count values are higher than they should be. For example, one Participant in particular returned as having a Position Count of 6, when they actually have a Position Count of 2. I'm assuming there might be some duplication occurring, but I'm not sure of the cause. This is a vendor database, so the scheme is unfamiliar to me. Any ideas off the top? Otherwise, I might need to speak with our vendor. Thanks again for your help!
    – jdgaub
    Dec 9, 2014 at 17:05
0

This query uses analytic functions to return the first name, last name, position and (total) position count for each participant. It uses row_number to only print the first and last name once per participant (and leaves it blank for rows after the 1st row).

select
    case when rn = 1 then firstname else '' end firstname,
    case when rn = 1 then lastname else '' end lastname,
    name as "POSITION",
    position_count as "POSITION COUNT"
from (
    select pa.firstname, pa.lastname, pos.name,
    count(*) over (partition by pa.firstname, pa.lastname) position_count,
    row_number() over (partition by pa.firstname, pa.lastname order by pos.name) rn
    from cs_participant pa
    join cs_position pos on pa.payeeseq = pos.payeeseq
) t1 where position_count > 1
order by firstname, lastname, rn
0

As far as I understand your problem, check this out. The inner SQL is quite the same as you have in your question. I wrapped it in another SQL to get the sum:

select firstname, lastname, sum(position_count)
  from (select pa.firstname firstname,
               pa.lastname lastname,
               pos.name as position,
               count(pos.name) as position_count
          from cs_participant pa, cs_position pos
         where pa.payeeseq = pos.payeeseq
           and pa.firstname like '%DEV%'
           and pa.lastname like '%TEST%'
         group by pa.firstname, pa.lastname, pos.name)
 group by firstname, lastname
 having sum(position_count) >= 2;

The result is:

FIRSTNAME            LASTNAME             SUM(POSITION_COUNT)
-------------------- -------------------- -------------------
DEV                  TEST                                   2
1
  • Thanks for the help @PT_STAR -- Your query is working, but I'm retrieving inflated values for position_count SUM. I'm finding out that there are additional fields in the Position table accounting for duplication. I will have to solve that through our vendor.
    – jdgaub
    Dec 9, 2014 at 18:10

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.