在`SQLite`中按类别和子类别分组的表中获取子类别的计数。

huangapple go评论63阅读模式
英文:

Getting the count of subcategories in a table grouped by categories and subcategories in `SQLite`

问题

我有以下的 Loans 表格:

EmployeeId	LoanCatId	LoanSubCatId	LoanDate
------------------------------------------------
1 			4			1				19990125
3			3			2				20101210
6			1			1				19910224
4			4			2				20120219
1			3			1				19920214
2			4			2				19930614
1			3			2				19840705
6			1			1				20030917
5			1			1				19900204
3			1			2				20181113

其中字段 EmployeeIdLoanCatIdLoanSubCatId 分别作为外键引用以下表格的ID:

表格 Employees

EmployeeId	Name
------------------
1			John
2			Jack
3			Alex
4			Fred
5			Danny
6			Russel

表格 LoanCategories

CategoryId	CategoryName
------------------------
1			CA
2			CB
3			CC
4			CD

表格 LoanSubCategories

CategoryId	CategoryName
------------------------
1			SCA
2			SCB

我试图通过指定一个 LoanDate,例如 '19990125'(在 Loans 表格中的第一行)来获得以下表格:

CategoryName	SubCategoryName	Count
-------------------------------------
CA				SCA				0
CA				SCB				0
CB				SCA				0
CB				SCB				0
CC				SCA				0
CC				SCB				0
CD				SCA				1
CD				SCB				0

数据库是 SQLite。感谢提前帮助。

英文:

I have the following Loans table:

EmployeeId	LoanCatId	LoanSubCatId	LoanDate
------------------------------------------------
1 			4			1				19990125
3			3			2				20101210
6			1			1				19910224
4			4			2				20120219
1			3			1				19920214
2			4			2				19930614
1			3			2				19840705
6			1			1				20030917
5			1			1				19900204
3			1			2				20181113

where the fields EmployeeId, LoanCatId, and LoanSubCatId reference the ID of the following tables, as foreign keys, respectively:

Table Employees:

EmployeeId	Name
------------------
1			John
2			Jack
3			Alex
4			Fred
5			Danny
6			Russel

Table LoanCategories:

CategoryId	CategoryName
------------------------
1			CA
2			CB
3			CC
4			CD

Table LoanSubCategories:

CategoryId	CategoryName
------------------------
1			SCA
2			SCB

I'm trying to get the following table by specifying a LoanDate for example, '19990125' (the first row in the Loans table):

CategoryName	SubCategoryName	Count
-------------------------------------
CA				SCA				0
CA				SCB				0
CB				SCA				0
CB				SCB				0
CC				SCA				0
CC				SCB				0
CD				SCA				1
CD				SCB				0

The database is SQLite.
Thanks in advance.

答案1

得分: 1

你需要对 LoanCategoriesLoanSubCategories 进行 CROSS 连接,以获取所有类别和子类别的组合,并对 Loans 进行 LEFT 连接,以获取特定日期的数据。最后,你需要对每个类别/子类别进行汇总:

SELECT c.CategoryName, 
       s.CategoryName AS SubCategoryName,
       COUNT(l.LoanDate) AS Count
FROM LoanCategories c CROSS JOIN LoanSubCategories s
LEFT JOIN Loans l ON l.LoanCatId = c.CategoryId AND l.LoanSubCatId = s.CategoryId AND l.LoanDate = '19990125'
GROUP BY c.CategoryId, s.CategoryId;

查看演示

英文:

You need a CROSS join of LoanCategories and LoanSubCategories to get all the combinations of categories and subcategories and a LEFT join to Loans for the specific date that you want.<br/>
Finally, you must aggregate for each category/subcategory:

SELECT c.CategoryName, 
       s.CategoryName AS SubCategoryName,
       COUNT(l.LoanDate) AS Count
FROM LoanCategories c CROSS JOIN LoanSubCategories s
LEFT JOIN Loans l ON l.LoanCatId = c.CategoryId AND l.LoanSubCatId = s.CategoryId AND l.LoanDate = &#39;19990125&#39;
GROUP BY c.CategoryId, s.CategoryId;

See the demo.<br/>

答案2

得分: 0

Use a CROSS JOIN on the categories tables to obtain all possible combinations, and right join to combine the data, and conditional aggregation to obtain the desired outcome.

使用CROSS JOIN在类别表上获取所有可能的组合,并使用right join来合并数据,然后使用条件聚合来获取所需的结果。

select s.catName, s.subCatName, count(case when l.LoanDate = 19990125 then 1 end) as count_
from Loans l
inner join Employees e on e.EmployeeId = l.EmployeeId
right join (
select c.CategoryId as catId, c.CategoryName as catName, s.CategoryId as subCatId, s.CategoryName as subCatName
FROM LoanCategories c
CROSS JOIN LoanSubCategories s
) as s on s.catId = l.LoanCatId and s.subCatId = l.LoanSubCatId
group by s.catId, s.subCatId

在此处查看演示

英文:

Use a CROSS JOIN on the categories tables to obtain all possible combinations, and right join to combine the data, and conditional aggregation to obtain the desired outcome.

select s.catName, s.subCatName, count(case when l.LoanDate = 19990125 then 1 end) as count_
from Loans l
inner join Employees e on e.EmployeeId = l.EmployeeId 
right join (
  select c.CategoryId as catId, c.CategoryName as catName, s.CategoryId as subCatId, s.CategoryName as subCatName
  FROM LoanCategories c 
  CROSS JOIN LoanSubCategories s 
) as s on s.catId = l.LoanCatId and s.subCatId = l.LoanSubCatId
group by s.catId, s.subCatId

Demo here

huangapple
  • 本文由 发表于 2023年5月6日 23:56:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/76189832.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定