MS SQL 查询按状态列出计数
我想达到下面的结果。我想列出状态为“待批准”的所有记录以及此条件:
例如,对于用户名“Leo”
if ProcedureLevel="Propose" then count all userId of 'Leo' from ProposedCheckBy field
if ProcedureLevel="Endorse" then count all userId of 'Leo' from EndorsedCheckBy field
if ProcedureLevel="Approve" then count all userId 'Leo' from ApprovedCheckBy field
,然后将所有具有“待批准”状态(即检查器为“Leo”)的状态与上述条件相加
I want to achieved the result like this
-----------------------------------------
Username | No of For Approval |
Leo | 3 |
Taurus | 2 |
Capricorn | 1 |
-----------------------------------------
Tables
Users
Id, userName
Limits
Id, [Description] ,[status],[procedurelevel] checkbyby1,checkbyby2, checkby3
Users
Id [UserName]
1 Leo
2 Taurus
3 Capricorn
Limits
Id, [Description] ,[status] ,[procedurelevel] ProposedCheckedBy1 ,EndorsedCheckedBy2 , ApprovedCheckBy3
1 Limits1 For Approval Propose 1 null null
2 Limits2 For Approval Propose 1 null null
3 Limits3 For Approval Endorse 1 2 null
4 Limits4 For Approval Approve 1 2 1
5 Limits5 For Approval Approve 2 3 2
5 Limits5 For Approval Approve 1 2 3
I want to achieved the result below. I want to list all records having a status of "For Approval" together with this condition:
For example for Username 'Leo'
if ProcedureLevel="Propose" then count all userId of 'Leo' from ProposedCheckBy field
if ProcedureLevel="Endorse" then count all userId of 'Leo' from EndorsedCheckBy field
if ProcedureLevel="Approve" then count all userId 'Leo' from ApprovedCheckBy field
Then sum all the status having "For Approval" which is the checker is 'Leo' with the condition above
I want to achieved the result like this
-----------------------------------------
Username | No of For Approval |
Leo | 3 |
Taurus | 2 |
Capricorn | 1 |
-----------------------------------------
Tables
Users
Id, userName
Limits
Id, [Description] ,[status],[procedurelevel] checkbyby1,checkbyby2, checkby3
Users
Id [UserName]
1 Leo
2 Taurus
3 Capricorn
Limits
Id, [Description] ,[status] ,[procedurelevel] ProposedCheckedBy1 ,EndorsedCheckedBy2 , ApprovedCheckBy3
1 Limits1 For Approval Propose 1 null null
2 Limits2 For Approval Propose 1 null null
3 Limits3 For Approval Endorse 1 2 null
4 Limits4 For Approval Approve 1 2 1
5 Limits5 For Approval Approve 2 3 2
5 Limits5 For Approval Approve 1 2 3
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我想这就是你所追求的;如果不是,我已经创建了表格和数据插入,这将帮助任何为您提供最终解决方案的人。我认为合并就是你正在寻找的东西;使用状态 1、2、3 列,您可以一次向后查找当前用户。
目前它返回
I think this is what you are after; if its not i have created tables and data inserts which will help whoever gives you the final solution. I think coalesce is the thing you are looking for you; using the status 1,2,3 columns you can work backwards to find the current user in one pass.
It currently returns
这是我的答案,问题是 Leo 有 2 次否决,而不是 3 次。如果我错了,请纠正
Here is my answer, the problem is Leo has 2 No of Approval instead of 3. Please correct if what I've been wrong