select 'local' as type
, cs.student_id
, cs.student_code
, cs.student_name
, cs.parent_identity
, csc.school_code
, (select ds.school_name
from data_school ds
where ds.school_code = csc.school_code) as school_name
, csc.year_term_code
, dyt.year_start
, dyt.term_code
, concat(dyt.year_start, '-', dyt.year_end, '学年第', dyt.term_code,
'学期') as year_term_name
, csc.student_status
, csc.phase_code
, csc.course_code
, csc.grade_code
, (select GROUP_CONCAT(distinct dsd.node_name separator ',')
from bill_student bs
inner join data_school_department dsd
on dsd.deleted_status = 0 and dsd.node_type = 1 and
dsd.school_code = bs.school_code and dsd.phase_code = bs.phase_code
where bs.school_code = csc.school_code
and bs.year_term_code = csc.year_term_code
and bs.student_id = cs.student_id
and bs.status in (50, 60, 70, 80)) as bill_phase_code
, (select GROUP_CONCAT(distinct case when bill_type = 'TERM' then '学期' else '月度' end separator
',')
from bill_student bs
where bs.school_code = csc.school_code
and bs.year_term_code = csc.year_term_code
and bs.student_id = cs.student_id
and bs.status in (50, 60, 70, 80)) as bill_type
, (select if(count(1) > 0, true, false)
from bill_student bs
where bs.school_code = csc.school_code
and bs.year_term_code = csc.year_term_code
and bs.student_id = cs.student_id
and bs.status in (50, 60, 70, 80)) as has_valid_bill
, (select if(count(1) > 0, false, true)
from bill_student bs
where bs.school_code = csc.school_code
and bs.year_term_code = csc.year_term_code
and bs.student_id = cs.student_id
and bs.is_closed = 0
and bs.status in (50, 60)) as has_finish_bill
# 未关闭的待缴费之后的账单应缴金额
, (select ifnull(sum(bscp.need_paid_cost), 0)
from bill_student bs
inner join bill_student_charging_project bscp
on bs.bill_student_code = bscp.student_bill_code
and bscp.charging_category_code in
('00001', 'BC0007', 'BC0018', 'BC0022', 'BC0020')
where bs.school_code = csc.school_code
and bs.year_term_code = csc.year_term_code
and bs.student_id = cs.student_id
and bs.is_closed = 0
and bs.status in (50, 60, 70, 80)) as xzf_yj
已关闭的待缴费之后的账单收款金额
, (select ifnull(sum(bcd.collection_amount), 0)
from bill_student bs
inner join bill_collection bc
on bs.bill_student_code = bc.bill_student_code and bc.status = 10
inner join bill_collection_detail bcd on bc.id = bcd.collection_id
and bcd.charging_category_code in ('00001', 'BC0007', 'BC0018', 'BC0022', 'BC0020') and
bcd.collection_amount >= 0
where bs.school_code = csc.school_code
and bs.year_term_code = csc.year_term_code
and bs.student_id = cs.student_id
and bs.is_closed = 1
and bs.status in (50, 60, 70, 80)) as xzf_sk_c
# 待缴费之后的账单抵扣金额 (收款方式=余额支付)
, (select ifnull(sum(bcd.collection_amount), 0)
from bill_student bs
inner join bill_collection bc
on bs.bill_student_code = bc.bill_student_code and bc.status = 10 and
bc.collection_type = 'BalanceDeduction'
inner join bill_collection_detail bcd on bc.id = bcd.collection_id
and bcd.charging_category_code in ('00001', 'BC0007', 'BC0018', 'BC0022', 'BC0020') and
bcd.collection_amount >= 0
where bs.school_code = csc.school_code
and bs.year_term_code = csc.year_term_code
and bs.student_id = cs.student_id
and bs.status in (50, 60, 70, 80)) as xzf_dk
# 待缴费之后的账单收款金额 (收款方式!=余额支付)
, (select ifnull(sum(bcd.collection_amount), 0)
from bill_student bs
inner join bill_collection bc
on bs.bill_student_code = bc.bill_student_code and bc.status = 10 and
bc.collection_type != 'BalanceDeduction'
inner join bill_collection_detail bcd on bc.id = bcd.collection_id
and bcd.charging_category_code in ('00001', 'BC0007', 'BC0018', 'BC0022', 'BC0020') and
bcd.collection_amount >= 0
where bs.school_code = csc.school_code
and bs.year_term_code = csc.year_term_code
and bs.student_id = cs.student_id
and bs.status in (50, 60, 70, 80)) as xzf_sk
# 待缴费之后的账单内退款金额
, (select ifnull(abs(sum(bcd.collection_amount)), 0)
from bill_student bs
inner join bill_collection bc
on bs.bill_student_code = bc.bill_student_code and bc.status = 10
inner join bill_collection_detail bcd on bc.id = bcd.collection_id
and bcd.charging_category_code in ('00001', 'BC0007', 'BC0018', 'BC0022', 'BC0020') and
bcd.collection_amount < 0
where bs.school_code = csc.school_code
and bs.year_term_code = csc.year_term_code
and bs.student_id = cs.student_id
and bs.status in (50, 60, 70, 80)) as xzf_tk
, (select ifnull(sum(bscp.need_paid_cost), 0)
from bill_student bs
inner join bill_student_charging_project bscp
on bs.bill_student_code = bscp.student_bill_code
and bscp.charging_category_code in ('00003')
where bs.school_code = csc.school_code
and bs.year_term_code = csc.year_term_code
and bs.student_id = cs.student_id
and bs.is_closed = 0
and bs.status in (50, 60, 70, 80)) as hsf_yj
, (select ifnull(sum(bcd.collection_amount), 0)
from bill_student bs
inner join bill_collection bc
on bs.bill_student_code = bc.bill_student_code and bc.status = 10
inner join bill_collection_detail bcd on bc.id = bcd.collection_id
and bcd.charging_category_code in ('00003') and bcd.collection_amount >= 0
where bs.school_code = csc.school_code
and bs.year_term_code = csc.year_term_code
and bs.student_id = cs.student_id
and bs.is_closed = 1
and bs.status in (50, 60, 70, 80)) as hsf_sk_c
, (select ifnull(sum(bcd.collection_amount), 0)
from bill_student bs
inner join bill_collection bc
on bs.bill_student_code = bc.bill_student_code and bc.status = 10 and
bc.collection_type = 'BalanceDeduction'
inner join bill_collection_detail bcd on bc.id = bcd.collection_id
and bcd.charging_category_code in ('00003') and bcd.collection_amount >= 0
where bs.school_code = csc.school_code
and bs.year_term_code = csc.year_term_code
and bs.student_id = cs.student_id
and bs.status in (50, 60, 70, 80)) as hsf_dk
, (select ifnull(sum(bcd.collection_amount), 0)
from bill_student bs
inner join bill_collection bc
on bs.bill_student_code = bc.bill_student_code and bc.status = 10 and
bc.collection_type != 'BalanceDeduction'
inner join bill_collection_detail bcd on bc.id = bcd.collection_id
and bcd.charging_category_code in ('00003') and bcd.collection_amount >= 0
where bs.school_code = csc.school_code
and bs.year_term_code = csc.year_term_code
and bs.student_id = cs.student_id
and bs.status in (50, 60, 70, 80)) as hsf_sk
, (select ifnull(abs(sum(bcd.collection_amount)), 0)
from bill_student bs
inner join bill_collection bc
on bs.bill_student_code = bc.bill_student_code and bc.status = 10
inner join bill_collection_detail bcd on bc.id = bcd.collection_id
and bcd.charging_category_code in ('00003') and bcd.collection_amount < 0
where bs.school_code = csc.school_code
and bs.year_term_code = csc.year_term_code
and bs.student_id = cs.student_id
and bs.status in (50, 60, 70, 80)) as hsf_tk
, (select ifnull(sum(bscp.need_paid_cost), 0)
from bill_student bs
inner join bill_student_charging_project bscp
on bs.bill_student_code = bscp.student_bill_code
and bscp.charging_category_code in ('00002', 'BC0023')
where bs.school_code = csc.school_code
and bs.year_term_code = csc.year_term_code
and bs.student_id = cs.student_id
and bs.is_closed = 0
and bs.status in (50, 60, 70, 80)) as zsf_yj
, (select ifnull(sum(bcd.collection_amount), 0)
from bill_student bs
inner join bill_collection bc
on bs.bill_student_code = bc.bill_student_code and bc.status = 10
inner join bill_collection_detail bcd on bc.id = bcd.collection_id
and bcd.charging_category_code in ('00002', 'BC0023') and bcd.collection_amount >= 0
where bs.school_code = csc.school_code
and bs.year_term_code = csc.year_term_code
and bs.student_id = cs.student_id
and bs.is_closed = 1
and bs.status in (50, 60, 70, 80)) as zsf_sk_c
, (select ifnull(sum(bcd.collection_amount), 0)
from bill_student bs
inner join bill_collection bc
on bs.bill_student_code = bc.bill_student_code and bc.status = 10 and
bc.collection_type = 'BalanceDeduction'
inner join bill_collection_detail bcd on bc.id = bcd.collection_id
and bcd.charging_category_code in ('00002', 'BC0023') and bcd.collection_amount >= 0
where bs.school_code = csc.school_code
and bs.year_term_code = csc.year_term_code
and bs.student_id = cs.student_id
and bs.status in (50, 60, 70, 80)) as zsf_dk
, (select ifnull(sum(bcd.collection_amount), 0)
from bill_student bs
inner join bill_collection bc
on bs.bill_student_code = bc.bill_student_code and bc.status = 10 and
bc.collection_type != 'BalanceDeduction'
inner join bill_collection_detail bcd on bc.id = bcd.collection_id
and bcd.charging_category_code in ('00002', 'BC0023') and bcd.collection_amount >= 0
where bs.school_code = csc.school_code
and bs.year_term_code = csc.year_term_code
and bs.student_id = cs.student_id
and bs.status in (50, 60, 70, 80)) as zsf_sk
, (select ifnull(abs(sum(bcd.collection_amount)), 0)
from bill_student bs
inner join bill_collection bc
on bs.bill_student_code = bc.bill_student_code and bc.status = 10
inner join bill_collection_detail bcd on bc.id = bcd.collection_id
and bcd.charging_category_code in ('00002', 'BC0023') and bcd.collection_amount < 0
where bs.school_code = csc.school_code
and bs.year_term_code = csc.year_term_code
and bs.student_id = cs.student_id
and bs.status in (50, 60, 70, 80)) as zsf_tk
# 校车费
, (select ifnull(sum(bscp.need_paid_cost), 0)
from bill_student bs
inner join bill_student_charging_project bscp
on bs.bill_student_code = bscp.student_bill_code
and bscp.charging_category_code in ('00005')
where bs.school_code = csc.school_code
and bs.year_term_code = csc.year_term_code
and bs.student_id = cs.student_id
and bs.is_closed = 0
and bs.status in (50, 60, 70, 80)) as xcf_yj
, (select ifnull(sum(bcd.collection_amount), 0)
from bill_student bs
inner join bill_collection bc
on bs.bill_student_code = bc.bill_student_code and bc.status = 10
inner join bill_collection_detail bcd on bc.id = bcd.collection_id
and bcd.charging_category_code in ('00005') and bcd.collection_amount >= 0
where bs.school_code = csc.school_code
and bs.year_term_code = csc.year_term_code
and bs.student_id = cs.student_id
and bs.is_closed = 1
and bs.status in (50, 60, 70, 80)) as xcf_sk_c
, (select ifnull(sum(bcd.collection_amount), 0)
from bill_student bs
inner join bill_collection bc
on bs.bill_student_code = bc.bill_student_code and bc.status = 10 and
bc.collection_type = 'BalanceDeduction'
inner join bill_collection_detail bcd on bc.id = bcd.collection_id
and bcd.charging_category_code in ('00005') and bcd.collection_amount >= 0
where bs.school_code = csc.school_code
and bs.year_term_code = csc.year_term_code
and bs.student_id = cs.student_id
and bs.status in (50, 60, 70, 80)) as xcf_dk
, (select ifnull(sum(bcd.collection_amount), 0)
from bill_student bs
inner join bill_collection bc
on bs.bill_student_code = bc.bill_student_code and bc.status = 10 and
bc.collection_type != 'BalanceDeduction'
inner join bill_collection_detail bcd on bc.id = bcd.collection_id
and bcd.charging_category_code in ('00005') and bcd.collection_amount >= 0
where bs.school_code = csc.school_code
and bs.year_term_code = csc.year_term_code
and bs.student_id = cs.student_id
and bs.status in (50, 60, 70, 80)) as xcf_sk
, (select ifnull(abs(sum(bcd.collection_amount)), 0)
from bill_student bs
inner join bill_collection bc
on bs.bill_student_code = bc.bill_student_code and bc.status = 10
inner join bill_collection_detail bcd on bc.id = bcd.collection_id
and bcd.charging_category_code in ('00005') and bcd.collection_amount < 0
where bs.school_code = csc.school_code
and bs.year_term_code = csc.year_term_code
and bs.student_id = cs.student_id
and bs.status in (50, 60, 70, 80)) as xcf_tk
, (select ifnull(sum(bscp.need_paid_cost), 0)
from bill_student bs
inner join bill_student_charging_project bscp
on bs.bill_student_code = bscp.student_bill_code
and bscp.charging_category_code not in
('00001', 'BC0007', 'BC0018', 'BC0022', 'BC0020', '00003', '00002',
'BC0023', '00005')
where bs.school_code = csc.school_code
and bs.year_term_code = csc.year_term_code
and bs.student_id = cs.student_id
and bs.is_closed = 0
and bs.status in (50, 60, 70, 80)) as qt_yj
, (select ifnull(sum(bcd.collection_amount), 0)
from bill_student bs
inner join bill_collection bc
on bs.bill_student_code = bc.bill_student_code and bc.status = 10
inner join bill_collection_detail bcd on bc.id = bcd.collection_id
and bcd.charging_category_code not in
('00001', 'BC0007', 'BC0018', 'BC0022', 'BC0020', '00003', '00002', 'BC0023',
'00005') and
bcd.collection_amount >= 0
where bs.school_code = csc.school_code
and bs.year_term_code = csc.year_term_code
and bs.student_id = cs.student_id
and bs.is_closed = 1
and bs.status in (50, 60, 70, 80)) as qt_sk_c
, (select ifnull(sum(bcd.collection_amount), 0)
from bill_student bs
inner join bill_collection bc
on bs.bill_student_code = bc.bill_student_code and bc.status = 10 and
bc.collection_type = 'BalanceDeduction'
inner join bill_collection_detail bcd on bc.id = bcd.collection_id
and bcd.charging_category_code not in
('00001', 'BC0007', 'BC0018', 'BC0022', 'BC0020', '00003', '00002', 'BC0023',
'00005') and
bcd.collection_amount >= 0
where bs.school_code = csc.school_code
and bs.year_term_code = csc.year_term_code
and bs.student_id = cs.student_id
and bs.status in (50, 60, 70, 80)) as qt_dk
, (select ifnull(sum(bcd.collection_amount), 0)
from bill_student bs
inner join bill_collection bc
on bs.bill_student_code = bc.bill_student_code and bc.status = 10 and
bc.collection_type != 'BalanceDeduction'
inner join bill_collection_detail bcd on bc.id = bcd.collection_id
and bcd.charging_category_code not in
('00001', 'BC0007', 'BC0018', 'BC0022', 'BC0020', '00003', '00002', 'BC0023',
'00005') and
bcd.collection_amount >= 0
where bs.school_code = csc.school_code
and bs.year_term_code = csc.year_term_code
and bs.student_id = cs.student_id
and bs.status in (50, 60, 70, 80)) as qt_sk
, (select ifnull(abs(sum(bcd.collection_amount)), 0)
from bill_student bs
inner join bill_collection bc
on bs.bill_student_code = bc.bill_student_code and bc.status = 10
inner join bill_collection_detail bcd on bc.id = bcd.collection_id
and bcd.charging_category_code not in
('00001', 'BC0007', 'BC0018', 'BC0022', 'BC0020', '00003', '00002', 'BC0023',
'00005') and
bcd.collection_amount < 0
where bs.school_code = csc.school_code
and bs.year_term_code = csc.year_term_code
and bs.student_id = cs.student_id
and bs.status in (50, 60, 70, 80)) as qt_tk
, (select ifnull(sum(bc.collection_amount - bc.collection_entry_amount), 0)
from bill_student bs
inner join bill_collection bc
on bs.bill_student_code = bc.bill_student_code and bc.status = 10 and
bc.collection_amount >= 0
where bs.school_code = csc.school_code
and bs.year_term_code = csc.year_term_code
and bs.student_id = cs.student_id
and bs.status in (50, 60, 70, 80)) as my
from core_student cs
inner join core_student_current csc on cs.student_id = csc.student_id
inner join data_year_term dyt
on dyt.school_code = csc.school_code and dyt.year_term_code = csc.year_term_code
where csc.school_code = ${schoolCode}
and csc.year_term_code = ${yearTermCode}
and exists(select 1
from bill_student bs
where bs.school_code = csc.school_code
and bs.year_term_code = csc.year_term_code
and bs.student_id = cs.student_id
and bs.status in (50, 60, 70, 80))
union all
# 学校跨校账单统计
select 'cross' as type
, cs.student_id
, cs.student_code
, cs.student_name
, cs.parent_identity
, csc.school_code
, (select ds.school_name
from data_school ds
where ds.school_code = csc.school_code) as school_name
on bs.bill_student_code = bc.bill_student_code and bc.status = 10 and
bc.collection_amount >= 0
where bs.school_code = csc.school_code
and bs.year_term_code = csc.year_term_code
and bs.student_id = cs.student_id
and bs.status in (50, 60, 70, 80)) as my
from (select bs.student_id
, bs.school_code
, bs.year_term_code
, min(bs.phase_code) as phase_code
, min(bs.course_code) as course_code
, min(bs.grade_code) as grade_code
from bill_student bs
where bs.status in (50, 60, 70, 80)
and bs.school_code = ${schoolCode}
and bs.year_term_code = ${yearTermCode}
and not exists(select 1
from core_student_current csc
where bs.student_id = csc.student_id
and csc.school_code = bs.school_code
and bs.year_term_code = csc.year_term_code)
group by bs.student_id, bs.school_code, bs.year_term_code) csc
inner join core_student cs on cs.student_id = csc.student_id
inner join data_year_term dyt
on dyt.school_code = csc.school_code and
dyt.year_term_code = csc.year_term_code
) tt
and cs.student_code = 'N202402000088';
where tt.student_code = 'N202402000088';
This text is a SQL (Structured Query Language) query that is intended to retrieve comprehensive information from a database, possibly related to students, their school, and billing information. Here's a breakdown of the different components:
Structure of the Query
Main Query (SELECT ... FROM (...) tt):
The main query selects a wide variety of columns (fields) from a subquery encapsulated as tt. This subquery aggregates student details, their status, school information, and billing information.
Subquery (SELECT ... FROM core_student ...):
Within the parentheses, the original data is being pulled from multiple tables such as core_student, core_student_current, data_year_term, and various billing-related tables.
This subquery focuses on students that are currently enrolled at a school, along with their associated billing and status information.
Selected Fields
The query retrieves fields related to:
Student Information: student_id, student_code, student_name, etc.
Parent Identity: Obtained via a subquery that maps status codes to user-friendly names (parent_identity_name).
School Information: Includes school_code, school_name, current academic year, term codes, and year-term names.
Student Status: Extracts both the raw student_status code and a human-readable version (student_status_name).
Course and Grade Information: Each connected to the relevant course and grade system.
Billing Details: A series of calculated fields that combine various billing components, including payments due, amounts collected, and refunds, separated by different billing categories (e.g., xzf, hsf, zsf, xcf, qt).
Complex Calculations
The billing information involves a lot of aggregation functions (such as SUM()) and conditions, with values representing amounts expected, collected, or refunded being calculated by summing various fields.
The data seems to cater to different types of fees, including tuition or other categories, judging by the various acronym prefixes (like xzf, hsf).
Filtering and Restrictions
The outer query ends with a WHERE clause filtering results to show data for a specific student based on their student code ('N202402000088').
This SQL is likely used in an educational context, managing student billing and course enrollment data.
Conclusion
This SQL query is extensive and complex, designed to pull detailed records involving student data, their current status, courses and grades, as well as billing and payment information from multiple related tables in the system. It summarizes financial information concisely while ensuring that every aspect of a student’s school experience is accounted for, catering particularly to billing oversight within an educational institution.