按经理递归汇总调查结果
我有一个 StaffLookup 表,如下所示。
UserSrn | UserName | ManagerSrn
===============================
ABC1 | Jerome | NULL
ABC2 | Joe | ABC1
ABC3 | Paul | ABC2
ABC4 | Jack | ABC3
ABC5 | Daniel | ABC3
ABC6 | David | ABC2
ABC7 | Ian | ABC6
ABC8 | Helen | ABC6
人员结构是这样的。
|- Jerome
|
|- Joe
||
||- Paul
|||
|||- Jack
|||
|||- Daniel
||
||- David
|||
|||- Ian
|||
|||- Helen
我有一个如下所示的调查回复列表。
UserSrn | QuestionId | ResponseScore
====================================
ABC2 | 1 | 5
ABC2 | 3 | 4
ABC4 | 16 | 3
...
我想做的事情听起来很简单,但我正在努力寻找一种简洁、快速的方法。我想创建一个存储过程,它接受一个 Srn 并返回结构中该 Srn 下的所有员工。
如果 QuestionId 的得分为 16,则表明调查已完成。我想返回输入的 Srn(最高管理者)的一行,以及该经理下属的直接下属已完成的调查的计数。在此基础上,我希望原经理下的每位经理都可以统计他们每个直接下属的已完成调查的数量,等等。
当我将最高管理者设置为 Joe (ABC2) 时,我希望看到如下数据。
UserName | Completed | Total
============================
Joe | 2 | 2
Paul | 1 | 2
David | 0 | 2
TOTAL | 3 | 6
I have a StaffLookup table which looks like this.
UserSrn | UserName | ManagerSrn
===============================
ABC1 | Jerome | NULL
ABC2 | Joe | ABC1
ABC3 | Paul | ABC2
ABC4 | Jack | ABC3
ABC5 | Daniel | ABC3
ABC6 | David | ABC2
ABC7 | Ian | ABC6
ABC8 | Helen | ABC6
The staff structure looks like this.
|- Jerome
|
|- Joe
||
||- Paul
|||
|||- Jack
|||
|||- Daniel
||
||- David
|||
|||- Ian
|||
|||- Helen
I have a list of SurveyResponses that looks like this.
UserSrn | QuestionId | ResponseScore
====================================
ABC2 | 1 | 5
ABC2 | 3 | 4
ABC4 | 16 | 3
...
What I am trying to do sounds pretty simple but I am struggling to find a neat, quick way of doing it. I want to create a sproc that takes an Srn and returns back all the staff under that Srn in the structure.
If there is a score for QuestionId of 16 then that indicates a completed survey. I would like to return a line for the Srn entered (The top manager) with a count of completed surveys for the direct reports under that manager. Under that I would like each manager under the original manager with a count of completed surveys for each of their direct reports and so on.
I would like to see the data as such below when I set the top manager to be Joe (ABC2).
UserName | Completed | Total
============================
Joe | 2 | 2
Paul | 1 | 2
David | 0 | 2
TOTAL | 3 | 6
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
根据所提供的信息,我相信这是有效的。将其转换为以
@mgrSrn
作为输入参数的 SP 应该不会太困难。I believe this works, based on the information provided. It shouldn't be too difficult to turn this into an SP with
@mgrSrn
as the input parameter.试试这个:
输出:
编辑在OP的评论之后:
输出:
我仍然看不到给定的数据如何导致乔完成=2和保罗完成1。我从
('ABC4',16,3) 到
('ABC6',16,3)
,这样结果集中的某个人就会完成一个。try this:
OUTPUT:
EDIT after OP's comments:
OUTPUT:
I still can not see how the given data, results in Joe having completed=2 and Paul having completed 1. I changed the given data from
('ABC4',16,3)
to('ABC6',16,3)
so someone in the result set would have one completed.编辑:我使用 SQL Server 2008 生成 INSERT 语句...
我可以生成您的层次结构,但不能生成结果。抱歉,示例输入和输出数据没有关联。
您需要 LevelNum 将最有可能与层次结构相关的结果联系起来
Edit: I used SQL Server 2008 to generate the INSERT statements...
I can generate your hierarchy, but not the results. The sample input and output data don't tie up, sorry.
You'll need LevelNum to tie in the results most likely to the hierarchy