我想从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)
所以,问题:
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
答案 0 :(得分:1)
Non 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
无法使用索引。
答案 1 :(得分:0)
这应该让你开始我为你做了一个月一年,你也可以添加日期范围限制:
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'
答案 2 :(得分: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