在 SQL 或 LINQ 中根据月份对三个表中的条目进行计数
我想从 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 中完成此操作,而不会出现可怕的代码或大的性能问题吗?
- 如果是这样,怎么办?我什至愿意制定一个好的高级策略。我猜测 枢轴 将在这里发挥重要作用(特别是“复杂的 PIVOT 示例”)。 DATEPART(MONTH, Date) 似乎是一种很好的分区方法按月进行的通信,大致如下:
选择日期部分(月份,日期),计数(*) 来自 [通讯表] WHERE DATEPART(年份,日期)= '2009' 按日期部分分组(月、日) 按日期部分排序(月、日)
...这让我得到 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
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:
- Can this be done in SQL without horrendous code or big performance problems?
- 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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
非 PIVOT、CASE 使用语法:
使用
WHERE ct.date BETWEEN '2009-01-01' AND '2009-12-31'
因为WHERE DATEPART(YEAR, Date) = '2009'如果
无法使用索引。date
列中存在索引,则Non PIVOT, CASE using syntax:
Use
WHERE ct.date BETWEEN '2009-01-01' AND '2009-12-31'
becauseWHERE DATEPART(YEAR, Date) = '2009'
can't use an index if one exists on thedate
column.这应该可以帮助您开始,我为您做了一年一个月,您还可以添加日期范围限制:
This should get you started I did one month for one year for you, you can also add in the date range restrictions:
这是使用 Pivot 的相当等效的解决方案。
Here is a reasonably equivalent solution using Pivot.