Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7672897
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T16:23:22+00:00 2026-05-31T16:23:22+00:00

I am new to sql and I have come across a problem on joining

  • 0

I am new to sql and I have come across a problem on joining 3 tables.

I have 3 tables

1) USER_MASTER (userid,fname,lname)
This is the call center agent table

2) CALL_MASTER (callid, customername,phone,userid,calltype)
this is where the customer calls go into , user id is used to link which agent
picked up the call , calltype is the outcome of the call

3) CALL_TYPE_MASTER as

calltype typename
1        transferred
2        routed
3        disconnected

I want to list it like this

Agent name           Transferred    routed  disconnected
Balang Hector           7          1          2
Bonus Donna             0          1          2
Dalino Marie            8          1          1
Dela Cruz               0          0          1

I tried this query:

SELECT
USER_MASTER.FirstName,COUNT(CALL_MASTER.CallType) as Transferred 
FROM
(USER_MASTER LEFT JOIN CALL_MASTER ON CALL_MASTER.UserID=USER_MASTER.USERID) 
WHERE
CALL_MASTER.CallType=1
GROUP BY
USER_MASTER.FirstName

But the problem is I have to list another row as “routed” and “transferred”, but that would mean changing the where clause, and it would obviously not output “0” if it is not found.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T16:23:23+00:00Added an answer on May 31, 2026 at 4:23 pm

    This is, of course, a fairly standard PIVOT query. Here’s a version more idiomatic for SQL Server:

    SELECT User_Master.name, [1] AS Transferred, [2] AS Routed, [3] AS Disconnected
    FROM (SELECT userId, callType
          FROM Call_Master) Call_Master
    PIVOT(COUNT(callType) FOR callType IN ([1], [2], [3])) Pivoted
    JOIN User_Master
      ON User_Master.userId = Pivoted.userId
    

    SQL Fiddle Demo

    The aggregate appears to be happening before the join to User_Master, so the query should be able to use indices to fulfill the COUNT(*). Unfortunately there’s no way to automatically populate column aliases – you’d need dynamic SQL for that.

    EDIT:

    Explanations –

    (SELECT userId, callType
     FROM Call_Master) Call_Master
    

    This subquery is getting a list of columns over which the aggregate will be grouped and run. You can do any conditions for range checks, and similar. The intent is that the query should be what you’d write for a GROUP BY… just without that clause, and an aggregate.

    PIVOT(COUNT(callType) FOR callType IN ([1], [2], [3])) Pivoted
    

    This clause is telling the system “for the previous table-reference, run the given aggregate over the listed column, and for every change in the other (potentially different) column, put the result in a new column”. There are some caveats:

    • All other columns will be included in what essentially amounts to a GROUP BY clause
    • You can only specify one aggregate function
    • You can only specify one column in the aggregate, and only a column (no math here). You’d need to do anything like that in the subquery
    • The list of columns in the IN clause must contain all values your subquery returns, but can also have columns without results. Leaving out [2] will get a runtime error, but adding [4] just gets you a column with 0s
    • The brackets are required, and if the column given in the FOR clause is character, you don’t use quotes around the values.
    • An alias for the result table (Pivoted here) is required, and is in effect for all columns involved. There’s no Call_Master.userId column at this point in the query.

      JOIN User_Master
      ON User_Master.userId = Pivoted.userId

    … And finally, a join to User_Master to translate user ids to names. Note that, because the aggregate happens “inside” the reference generated as part of the Pivot query, you don’t have to worry about strange things happening to the rest of your data.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm a little new to SQL and have come across the following problem. I
I am new to VB programming and have come across an problem:( after a
This is a problem I have come across a number of times and I'm
I'm new to python and have hit a problem with an SQL query I'm
I am working on a new payment module for Magento and have come across
I just come across a strange problem where i cannot retrieve the sql stored
I have come a across a few different methods for inserting data into SQL
I'm relatively new to sql so this may probably come off as a simple
I have come across a weird problem concerning composite keys and one-to-many relationship. I
I'm a bit new to subsonic (i.e. evaluating 3.0.0.3) and have come across a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.