Database: financial
Question: What are the comprehensive statistics for accounts opened in Litomerice in 1996, including client demographics, transaction activity, loan information, and quarterly distribution of account openings?
Evidence: Litomerice is a district name; PRIJEM refers to deposits/credits, VYDAJ refers to withdrawals/debits; loan status 'A' indicates good loans, 'B' indicates bad loans
SQL:
WITH AccountsInLitomerice1996 AS (
    SELECT 
        T2.account_id,
        T2.date,
        T1.A2 AS district_name,
        STRFTIME('%m', T2.date) AS month_opened
    FROM 
        district AS T1 
    INNER JOIN 
        account AS T2 ON T1.district_id = T2.district_id 
    WHERE 
        STRFTIME('%Y', T2.date) = '1996' 
        AND T1.A2 = 'Litomerice'
),
ClientInfo AS (
    SELECT 
        d.account_id,
        COUNT(DISTINCT c.client_id) AS num_clients,
        SUM(CASE WHEN c.gender = 'M' THEN 1 ELSE 0 END) AS male_clients,
        SUM(CASE WHEN c.gender = 'F' THEN 1 ELSE 0 END) AS female_clients,
        AVG(CAST(STRFTIME('%Y', '1996-01-01') AS INTEGER) - CAST(STRFTIME('%Y', c.birth_date) AS INTEGER)) AS avg_client_age
    FROM 
        disp d
    JOIN 
        client c ON d.client_id = c.client_id
    GROUP BY 
        d.account_id
),
AccountActivity AS (
    SELECT 
        account_id,
        COUNT(trans_id) AS transaction_count,
        SUM(CASE WHEN type = 'PRIJEM' THEN amount ELSE 0 END) AS total_deposits,
        SUM(CASE WHEN type = 'VYDAJ' THEN amount ELSE 0 END) AS total_withdrawals,
        MAX(balance) AS max_balance
    FROM 
        trans
    WHERE 
        STRFTIME('%Y', date) = '1996'
    GROUP BY 
        account_id
),
LoanStatus AS (
    SELECT 
        account_id,
        COUNT(loan_id) AS loan_count,
        SUM(amount) AS total_loan_amount,
        SUM(CASE WHEN status = 'A' THEN 1 ELSE 0 END) AS good_loans,
        SUM(CASE WHEN status = 'B' THEN 1 ELSE 0 END) AS bad_loans
    FROM 
        loan
    GROUP BY 
        account_id
)
SELECT 
    COUNT(a.account_id) AS total_accounts,
    SUM(CASE WHEN ci.num_clients > 1 THEN 1 ELSE 0 END) AS accounts_with_multiple_clients,
    AVG(ci.avg_client_age) AS average_client_age,
    SUM(ci.male_clients) AS total_male_clients,
    SUM(ci.female_clients) AS total_female_clients,
    SUM(COALESCE(aa.transaction_count, 0)) AS total_transactions_in_1996,
    SUM(COALESCE(aa.total_deposits, 0)) AS total_deposits_in_1996,
    SUM(COALESCE(ls.loan_count, 0)) AS total_loans,
    AVG(COALESCE(ls.total_loan_amount, 0)) AS avg_loan_amount,
    COUNT(DISTINCT CASE WHEN ls.loan_count > 0 THEN a.account_id END) AS accounts_with_loans,
    ROUND(AVG(CASE WHEN month_opened BETWEEN '01' AND '03' THEN 1 ELSE 0 END) * 100, 2) AS percent_opened_q1,
    ROUND(AVG(CASE WHEN month_opened BETWEEN '04' AND '06' THEN 1 ELSE 0 END) * 100, 2) AS percent_opened_q2,
    ROUND(AVG(CASE WHEN month_opened BETWEEN '07' AND '09' THEN 1 ELSE 0 END) * 100, 2) AS percent_opened_q3,
    ROUND(AVG(CASE WHEN month_opened BETWEEN '10' AND '12' THEN 1 ELSE 0 END) * 100, 2) AS percent_opened_q4
FROM 
    AccountsInLitomerice1996 a
LEFT JOIN 
    ClientInfo ci ON a.account_id = ci.account_id
LEFT JOIN 
    AccountActivity aa ON a.account_id = aa.account_id
LEFT JOIN 
    LoanStatus ls ON a.account_id = ls.account_id