我无法找到适合我特定情况的任何内容,所以如果在其他地方已经回答了这个问题我会道歉。
我有两个表(下面显示了相关的列):
类别
|===============================| | ID | Name | lft | rgt |....| |===============================| | 6 | Book | 3 | 6 |....| | 10 | Poster | 7 | 12 |....| |===============================|
响应
|=======================================================| | ID | CatId | Points | Status | Calculated Points | |=======================================================| | 14 | 6 | 10 | Approved | 10 | | 32 | 6 | 10 | Approved | 10 | | 14 | 6 | 0 | Denied | 15 | | 32 | 10 | 0 | Denied | 10 | | 14 | 10 | 0 | Submitted | 20 | | 32 | 10 | 20 | Approved | 20 | |=======================================================|
现在我总结了给定类别的所有响应点。以下是执行此操作的当前SQL语句:
SELECT category.id, category.name,
SUM(CASE WHEN response.status
IN ("Approved, "Denied")
THEN response.points
ELSE response.calculated_points) AS 'Points'
FROM category
JOIN (SELECT * FROM response)
ON category.id IN (SELECT tmpCategory.id
FROM categories AS tmpCategory
WHERE tmpCategory.lft >= category.lft
AND tmpCategory.rgt <= category.rgt)
在某些类别中,我需要将点数的总和限制为最高{N}条记录。
这可以在纯SQL中完成,还是必须在我的应用程序代码中执行此操作?