← 返回题库
初级

查询同一客户持有的所有账户

未完成
初级参考 完整示例代码供参考,建议自己理解后重新输入
def solve():
    from pyodide.http import open_url
    offices_csv = open_url("https://data.zuihe.com/dbd/core/state_01/offices.csv").read()
    staff_csv = open_url("https://data.zuihe.com/dbd/core/state_01/staff.csv").read()
    currencies_csv = open_url("https://data.zuihe.com/dbd/core/state_01/currencies.csv").read()
    savings_products_csv = open_url("https://data.zuihe.com/dbd/core/state_01/savings_products.csv").read()
    interest_rate_charts_csv = open_url("https://data.zuihe.com/dbd/core/state_01/interest_rate_charts.csv").read()
    clients_csv = open_url("https://data.zuihe.com/dbd/core/state_01/clients.csv").read()
    savings_accounts_csv = open_url("https://data.zuihe.com/dbd/core/state_01/savings_accounts.csv").read()
    import sqlite3
    import pandas as pd
    from io import StringIO

    conn = sqlite3.connect(':memory:')
    pd.read_csv(StringIO(clients_csv)).to_sql('clients', conn, index=False, if_exists='replace')
    pd.read_csv(StringIO(savings_accounts_csv)).to_sql('savings_accounts', conn, index=False, if_exists='replace')

    result = pd.read_sql_query("""
        SELECT c.client_no, c.name, COUNT(a.id) AS account_count
        FROM clients c
        LEFT JOIN savings_accounts a ON a.client_id = c.id
        GROUP BY c.id, c.client_no, c.name
        HAVING account_count >= 1
        ORDER BY account_count DESC, c.client_no
    """, conn)
    print(result.to_string(index=False))
    conn.close()

示例

输入
solve()
期望输出
client_no       name  account_count
    CL001        张伟民              1
    CL002        李秀英              1
    CL003        王建国              1
    CL004        陈丽华              1
    CL005        赵志强              1
    CL006        刘桂英              1
    CL007         孙明              1
    CL008         周红              1
    CL009         吴刚              1
    CL010         郑艳              1
    CL011         林伟              1
    CL012         黄娟              1
    CL013         周华              1
    CL014        吴秀兰              1
    CL015        张三丰              1
    CL016        李明亮              1
    CL017        王大力              1
    CL018        陈美丽              1
    CL019        赵国庆              1
    CL020        刘晓燕              1
    CL021 深圳阳光科技有限公司              1
    CL022 北京鑫盛贸易有限公司              1
    CL023 上海恒通实业有限公司              1
    CL024 广州南方物流有限公司              1
    CL025 深圳华晟电子有限公司              1
Python 代码 🔒 登录后使用
🔒

登录后即可练习

注册免费账号,在浏览器中直接运行 Python 代码