多表查询SQL中的多个计数
16lz
2021-01-22
I would like to count The different requests by survey Id's and grouping it by SubjectValue
我想通过调查ID来计算不同的请求,并按SubjectValue对其进行分组
I have done this on just the one table with a sub query, but I'm not too sure to do it with several. Could anyone help me out?
我在一个带有子查询的表上完成了这个,但是我不太确定要用几个。任何人都可以帮我吗?
This is how the 3 tables are joined. The only values of note are
这就是3个表的连接方式。值得注意的唯一值
subjectValue - Table A Request_Id - Table A Survey_Id - Table C
SELECT TableA.SubjectValue FROM TableB INNER JOIN TableA ON TableB.ID = TableA.Request_ID INNER JOIN Table C ON TableB.Details_ID = TableC.ID
May I also add that all counts should be returned in the same row.
我还要补充说,所有计数都应该在同一行中返回。
there are 3 different survey Id's so the count will need a where clause on the survey_id.
有3个不同的调查ID,因此计数需要在survey_id上有where子句。
Hope that makes sense.
希望有道理。
Many thanks in advance.
提前谢谢了。
2 个解决方案
#1
4
You can use generic Cross-tab method
您可以使用通用的交叉表方法
select
TableA.SubjectValue,
SUM(case when somecol='request1' then 1 else 0 end) as request1,
SUM(case when somecol='request2' then 1 else 0 end) as request2,
.
TableB INNER JOIN
TableA ON TableB.ID = TableA.Request_ID INNER JOIN
Table C ON TableB.Details_ID = TableC.ID
group by
TableA.SubjectValue
更多相关文章
- Mysql替代解决方案Cassandra
- [置顶] Android屏幕适配解决方案
- 物流货运移动APP解决方案
- 转:Android Studio Error:Connection timed out: connect.解决方
- android.os.NetworkOnMainThreadException的解决方案
- Android极光推送jPush混淆解决方案终极篇
- 【Android开发学习39】VideoView在GLSurfaceView之上显示的解决
- Android 3.2 以上转屏,切换屏幕,横竖屏(onConfigurationChanged)会
- 王家林最受欢迎的一站式云计算大数据和移动互联网解决方案课程 V