1

我想从 SQL Server 2005 数据库的三个表中提取一些数据。虽然这肯定可以在代码中完成,但似乎这可以在 SQL 中相当好地完成(LINQ 的加分点!)。

基本上,我想知道每个月每个员工与每个客户进行了多少次电话会议和会议。像这样的东西:

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

数据分布在三个表中。为简单起见,我们只显示相关列:

通讯表

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

人际交流表

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

人表

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

所以,问题

  1. 这可以在没有可怕代码或大性能问题的 SQL 中完成吗?
  2. 如果是这样,怎么做?我什至会满足于一个好的高级策略。我猜支点将在这里发挥重要作用(尤其是“复杂 PIVOT 示例”)。 DATEPART(MONTH, Date)似乎是按照以下方式按月划分通信的好​​方法:
SELECT DATEPART(MONTH, Date), COUNT(*) 
FROM [CommunicationTable]
WHERE DATEPART(YEAR, Date) = '2009'
GROUP BY DATEPART(MONTH, Date)
ORDER BY DATEPART(MONTH, Date)

...这让我得到了 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
4

3 回答 3

1

非 PIVOT,CASE 使用语法:

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

使用WHERE ct.date BETWEEN '2009-01-01' AND '2009-12-31',因为WHERE DATEPART(YEAR, Date) = '2009'如果列上存在索引,则不能使用索引date

于 2010-02-24T15:56:56.997 回答
0

This should get you started I did one month for one year for you, you can also add in the date range restrictions:

SELECT PE.PersonID as EmployeeID,PC2.PersonID as CustomerID,
 SUM(CASE WHEN DATEPART(MONTH, C.[Date]) = 1 
           AND DATEPART(YEAR,C.[Date]) = 2009 
           AND C.[type] = 'C' THEN 1 ELSE 0 END) AS [Jan 2009 Calls]
FROM PersonTable PE
JOIN PersonCommunicationTable PC ON PE.PersonID = PC.PersonID
JOIN CommunicationsTable C ON PC.CommunicationID = C.CommunicationID
JOIN PersonCommunicationTable PC2 ON PC.CommunicationID = PC2.CommunicationID AND NOT PC2.PersonID = PC.PersonID 
WHERE PE.Type = 'E'
于 2010-02-24T16:28:19.653 回答
0

这是使用 Pivot 的相当等价的解决方案。

Declare @Comm TABLE
(
    [CommunicationId] uniqueidentifier PRIMARY KEY DEFAULT NEWID(),
    [Type]  nvarchar(1), -- ('C' for call, 'M' for meeting, etc.)
    [Date] datetime
)

Declare @Person TABLE
(
    [PersonId]  uniqueidentifier PRIMARY KEY DEFAULT NEWID(),
    [Type]      Nvarchar(1) -- ('E' for employee, 'C' for customer)
)
Declare @PersonComm TABLE
(
    [PersonId] uniqueidentifier, -- (Can contain GUIDs for employees or clients, see Person Table below)
    [CommunicationId] uniqueidentifier
)

INSERT INTO @Person(Type)
Select 'C' UNION ALL Select 'E'  UNION ALL Select 'C' UNION ALL Select 'E'

INSERT INTO @Comm([Type],[Date])
Select 'C', '01/04/2010' UNION ALL Select 'C', '01/04/2010'
UNION ALL Select 'C', '04/04/2010' UNION ALL Select 'C', '05/01/2010'
UNION ALL Select 'C', '08/04/2009' UNION ALL Select 'C', '09/01/2009'
UNION ALL Select 'M', '01/04/2010' UNION ALL Select 'M', '03/20/2010'
UNION ALL Select 'M', '04/04/2010' UNION ALL Select 'M', '06/01/2010'
UNION ALL Select 'M', '04/10/2009' UNION ALL Select 'M', '04/10/2009'

INSERT INTO @PersonComm
Select  E.PersonID ,  Comm.[CommunicationId]
FROM    @Person E 
        ,@Comm Comm
Where E.[Type] = 'E' 

INSERT INTO @PersonComm
Select  E.PersonID ,  Comm.[CommunicationId]
FROM    @Person E 
        ,@Comm Comm
Where E.[Type] = 'C' 

Select  EmployeeID, 
        ClientID,
        Year, 
        [JanuaryC] AS [Jan Calls], 
        [JanuaryM] AS [Jan Meetings],
        [FebruaryC], 
        [FebruaryM],
        [MarchC], 
        [MarchM], 
        [AprilC], 
        [AprilM], 
        [MayC], 
        [MayM], 
        [JuneC], 
        [JuneM], 
        [JulyC], 
        [JulyM],
        [AugustC], 
        [AugustM],
        [SeptemberC] ,
        [SeptemberM],
        [OctoberC] ,
        [OctoberM],
        [NovemberC],
        [NovemberM],
        [DecemberC], 
        [DecemberM]

FROM 
(
Select P.PersonId EmployeeID, Client.PersonId ClientID, YEAR(C.Date) Year, DateName(m,C.Date) Month,  COUNT(*) Amount, C.Type CommType,
       DateName(m,C.Date) + C.Type PivotColumn -- JanuaryC
FROM    @Comm C
        INNER JOIN @PersonComm PC
            ON PC.CommunicationId = C.CommunicationId
        INNER JOIN @Person P
            ON P.PersonId = PC.PersonId 
        INNER JOIN @PersonComm PCC
            ON PCC.CommunicationId = PC.CommunicationId
        INNER JOIN @Person Client
            ON Client.PersonId = PCC.PersonId AND Client.Type = 'C' 
Where P.Type = 'E'      
Group By P.PersonId, CLient.PersonId, YEAR(C.Date), DateName(m,C.Date), C.Type
) SourceTable
PIVOT (
MAX(Amount)
FOR PivotColumn IN 
    ([JanuaryC], [JanuaryM],[FebruaryC], [FebruaryM],[MarchC], [MarchM], [AprilC], [AprilM], [MayC], [MayM], [JuneC], [JuneM], [JulyC], [JulyM],
     [AugustC], [AugustM],[SeptemberC] , [SeptemberM],[OctoberC] ,[OctoberM],[NovemberC], [NovemberM], [DecemberC], [DecemberM]

)
)As PivotTable
于 2010-02-25T07:36:45.970 回答