本文介绍了SQL分组依据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下查询

从tbl_loans中选择SELECT tbl_loans.loanID,tbl_Borrowers.BorrowerName
内联tbl_Borrowers ON tbl_loans.loanID = tbl_Borrowers.loanID"

结果如下:

loanID BorrowerName
----------- --------------------------------------- -----------
1塞瓦克
1 Arsineh
1萨门
2阿帕
2加伦
3 Varoosh
4奥贝尔

我需要添加一个GROUP BY并显示有多少人具有相同的loadID
例如:
负载ID BorrowerName
------- ------------
1 3
2 2
3 1
4 1

Hi guys I have the following query

"SELECT tbl_loans.loanID,tbl_Borrowers.BorrowerName FROM tbl_loans
INNER JOIN tbl_Borrowers ON tbl_loans.loanID = tbl_Borrowers.loanID"

with the following result:

loanID BorrowerName
----------- --------------------------------------------------
1 Sevak
1 Arsineh
1 Sarmen
2 Arpa
2 Garen
3 Varoosh
4 Orbel

I need to add a GROUP BY and show how many persons have the same loadID
for example:
Load ID BorrowerName
------- ------------
1 3
2 2
3 1
4 1

推荐答案

SELECT
    tbl_loans.loanID, COUNT(*) AS CountInGroup
FROM tbl_loans
JOIN tbl_Borrowers
    ON tbl_loans.loanID = tbl_Borrowers.loanID
GROUP BY
    tbl_loans.loanID


请注意,只要您在GROUP BY和SELECT之间保持一致,选择哪一个借贷ID都没有关系.


Note that it does not matter which loanID you choose so long as you are consistent between your GROUP BY and SELECT.


SELECT tbl_loans.loanID, Count(*)
FROM tbl_loans
Group By tbl_loans



这篇关于SQL分组依据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-12 06:29