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

  • Home
  • SEARCH
  • 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 623595
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T19:06:34+00:00 2026-05-13T19:06:34+00:00

I would like to extract some data from three tables in a SQL Server

  • 0

I would like to extract some data from three tables in a SQL Server 2005 database. While this can surely be done in code, it seems like this could be done reasonably well in SQL (bonus points for LINQ!).

Basically, I would like to know for each month how many calls and meetings each employee has held with each of our clients. Something like this:

Employee GUID   Customer GUID   Jan calls   Jan mtgs   Feb calls      Feb mtgs...
[a guid]        [another guid]  5           0          7              3

The data is spread across three tables. For simplicity’s sake, let’s just show the relevant columns:

Communications Table

[CommunicationId]  (PK, uniqueidentifier)
[Type]             (nvarchar(1)) ('C' for call, 'M' for meeting, etc.)
[Date]             (datetime)

Person-Communication Table

[PersonId]         (PK, FK, uniqueidentifier) (Can contain GUIDs for employees or clients, see Person Table below)
[CommunicationId]  (PK, FK, uniqueidentifier)

Person Table

[PersonId]         (PK, uniqueidentifier)
[Type]             (nvarchar(1)) ('E' for employee, 'C' for customer)

So, the questions:

  1. Can this be done in SQL without horrendous code or big performance problems?
  2. If so, how? I’d even settle for a good high-level strategy. I’m guessing pivots will play a big role here (particularly the “Complex PIVOT Example”). DATEPART(MONTH, Date) seems like a good method for partitioning the communications by month along the lines of:
SELECT DATEPART(MONTH, Date), COUNT(*) 
FROM [CommunicationTable]
WHERE DATEPART(YEAR, Date) = '2009'
GROUP BY DATEPART(MONTH, Date)
ORDER BY DATEPART(MONTH, Date)

… which gets me the number of communications in each month in 2009:

1    2871
2    2639
3    3654
4    2751
5    1773
6    2575
7    2906
8    2398
9    2621
10   2638
11   1705
12   2290
  • 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-13T19:06:35+00:00Added an answer on May 13, 2026 at 7:06 pm

    Non PIVOT, CASE using syntax:

    WITH summary AS (
          SELECT emp.personid AS emp_guid,
                 cust.personid AS cust_guid,
                 DATEPART(MONTH, ct.date) AS mon, 
                 ct.type,
                 COUNT(*) AS num_count
            FROM COMMUNICATIONTABLE ct
       LEFT JOIN PERSON_COMMUNICATION pc ON pc.communicationid = ct.communicationid
            JOIN PERSON emp ON emp.personid = pc.personid
                           AND emp.type = 'E'
            JOIN PERSON cust ON cust.personid = p.personid
                            AND cust.type = 'C'
           WHERE ct.date BETWEEN '2009-01-01' AND '2009-12-31'
        GROUP BY emp.personid, cust.personid, DATEPART(MONTH, ct.ate), ct.type)
    SELECT s.emp_guid,
           s.cust_guid,
           MAX(CASE WHEN s.mon = 1 AND s.type = 'C' THEN s.num_count ELSE 0 END) AS "Jan calls",
           MAX(CASE WHEN s.mon = 1 AND s.type = 'M' THEN s.num_count ELSE 0 END) AS "Jan mtgs",
           ... --Copy/Paste two lines, update the month check... and the col alias
      FROM summary s
    GROUP BY s.emp_guid, s.cust_guid
    

    Use WHERE ct.date BETWEEN '2009-01-01' AND '2009-12-31' because WHERE DATEPART(YEAR, Date) = '2009' can’t use an index if one exists on the date column.

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

Sidebar

Related Questions

I would like to extract items from this sample html, more specificly, i would
I would like to extract a value or its inverse from database. I would
I would like to extract the data groups from the PHP arrays depending on
I have some data files from a legacy system that I would like to
I want to use ADO.net to extract some data from an Excel file. This
I would like to extract and construct some strings after identifying a substring that
I would like to extract the GPS EXIF tag from pictures using php. I'm
I would like to extract only tar.gz from the following strings. /root/abc/xzy/file_tar_src-5.2.8.23.tar.gz or /root/abc/xyz/file_tar_src-5.2.tar.gz
Hello! I would like to extract all citations from a text. Additionally, the name
I'm writing a Java program where I have to extract some data from 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.