Search code examples
sqlsql-serversql-server-2000

modified SQL query not showing all results


i have a simple query that list clients that have more than one open service job.

select link_to_client, count(call_ref) 'Services'
from calls
where call_type='PM'
and last_event_status not in ('RD','C','X')
group by link_to_client
having count(call_ref)>1

this produces 11 results.

G838AH/24   2
G21/311     2
G328RE/18   29
G328RE      4
G849RJ/6    2
ML110PQ/2   2
G21/505     2
G15PR       2
G21/314     2
ML60BN/2    2
G214AE/12   2

i need to add another field to display (link_to_contract_header)

select link_to_client, link_to_contract_header, count(call_ref) 'Services'
from calls
where call_type='PM'
and last_event_status not in ('RD','C','X')
group by link_to_client, link_to_contract_header
having count(call_ref)>1

but when i add this to the query i only end up with only 9 results

ML60BN/2    CLYVAL-2-1  2
G849RJ/6    127-3       2
G21/505     NGLA-1      2
G21/311     NGLA-1      2
G15PR       WOS-1       2
G214AE/12   NGLA-1      2
G328RE      CTSP-1      4
G21/314     NGLA-1      2
G328RE/18   CTSP-1      29

so where am i going wrong?


Solution

  • in your 2 other line you haven't the same link_to_client, link_to_contract_header, if you group by only link_to_client then you have the count >1 but if you group by link_to_client, link_to_contract_header, the two line deletet dont have count >1

    Update

    you can check the values of link_to_contract_header in the 2 line of G838AH/24 (you will find different value)