下面是sql语句:
SELECT
p.ID,
p.TheName0,
(SELECT IFNULL(SUM(att.S_FinalAmount),0) From tbl_groups_classes_att att
INNER JOIN tbl_students st
ON st.ID = att.StudentID
INNER JOIN tbl_groups_classes cls
ON cls.ID = att.ClassID
WHERE st.ParentID = p.ID
and cls.TheDate BETWEEN @Date1 and @Date2
and att.TheStatus <> 'absent'
) as CurrMost,
(SELECT IFNULL(SUM(att.S_FinalAmount),0) From tbl_groups_classes_att att
INNER JOIN tbl_students st
ON st.ID = att.StudentID
INNER JOIN tbl_groups_classes cls
ON cls.ID = att.ClassID
WHERE st.ParentID = p.ID and cls.TheDate< @Date1 and att.TheStatus <> 'absent'
) as PrevMost,
(SELECT IFNULL(SUM(pay.TheAmount),0) From tbl_parents_payments pay Where p.ID = pay.ParentID
AND pay.TheDate BETWEEN @Date1 and @Date2
) as CurrMadf,
(SELECT IFNULL(SUM(pay.TheAmount),0) From tbl_parents_payments pay Where p.ID = pay.ParentID
AND pay.TheDate < @Date1
) as PrevMadf,
(SELECT CurrMost + PrevMost) as AllMost,
(SELECT CurrMadf + PrevMadf) as AllMadf,
(SELECT AllMost - AllMadf) AS FinalTotal
from tbl_parents p
我想在最后定购,我想:
from tbl_parents p order by FinalTotal
但不会受到影响。
我怎么分类?请注意,我在网上尝试了很多解决方案,但都没有结果。提前谢谢
1条答案
按热度按时间g0czyy6m1#
故障保护解决方案是将整个查询作为一个表表达式来生成列名,然后排序就变得很简单了。
例如: