在 SQL 或 LINQ 中基于月份计算三个表中的条目
Posted
技术标签:
【中文标题】在 SQL 或 LINQ 中基于月份计算三个表中的条目【英文标题】:Count entries across three tables based on month in SQL or LINQ 【发布时间】:2010-02-24 15:45:34 【问题描述】:我想从 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)
那么,问题:
-
这是否可以在 SQL 中完成而没有可怕的代码或大的性能问题?
如果是这样,怎么做?我什至会满足于一个好的高级策略。我猜pivots 将在这里发挥重要作用(尤其是“复杂 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
【问题讨论】:
@Hogan:uniqueidentifier
数据类型并不意味着 UNIQUE
约束。
@Hogan: uniqueidentifier
是 SQL Server 的 GUID 术语。多个人可以与多个通信相关联(但只有一次,例如,与一个电话关联三次是荒谬的,并且是不允许的)。
哎呀!我收回它......没关系。
我想你可能想用char(1)
替换数据类型nvarchar(1)
:你不需要保持长度,当然也不需要支持更多的ASCII。尤其是当您搜索这些列时,您可能会有包含它们的索引,其大小会影响性能(通常是表大小)
【参考方案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'
,因为如果date
列中存在索引,WHERE DATEPART(YEAR, Date) = '2009'
就不能使用索引。
【讨论】:
@OMG Ponies:看我的回答——这不是根据个人和客户得出总数,还是我搞砸了? @OMG Ponies:是的,它在 Communications 表中,但不在 PersonCommunication 表中——我想我不明白你所说的求和是什么意思。 @OMG Ponies:您需要在外部选择(group by emp_guid, cust_guid
)和sum()
上为每种情况再添加一个分组,否则您会在需要时获得很多行,您将获得范围内每个月的呼叫总数和 mtg 总数为一行。
@Hogan:一点也不讽刺。如果没有反馈,我将无法变得更好。【参考方案2】:
这应该可以让你开始我为你做了一个月一年,你也可以添加日期范围限制:
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'
【讨论】:
【参考方案3】:这是一个使用 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
【讨论】:
以上是关于在 SQL 或 LINQ 中基于月份计算三个表中的条目的主要内容,如果未能解决你的问题,请参考以下文章