-- 
-- 売上実績表（得意先税率別）
-- 
CREATE OR REPLACE VIEW v_sales_actual_result_table_by_customer_tax_rate
AS 
SELECT
    sales_date_y,
    sales_date_m,
    sales_date_ym,
    customer_code,
    customer_name,
    tax_rate_name,
    SUM(
        total_sales_amount
    ) AS 'sum_total_sales_amount', -- 総売上額(sd.line_type="1:通常,4:訂正"の時に表示)
    SUM(
        return_amount
    ) AS 'sum_return_amount', -- 返品額(sd.line_type="5:返品"の時に表示)
    SUM(
        normal_discount_amount
    ) AS 'sum_normal_discount_amount', -- 通常値引額(sd.line_type="2:値引"の時に表示)
    SUM(
        policy_discount_amount
    ) AS 'sum_policy_discount_amount', -- 政策値引額(sd.line_type="3:政策値引"の時に表示)
    SUM(
        tax
    ) AS 'sum_tax_amount'  -- 消費税額

-- ｜売上実績表
FROM v_sales_actual_result_table

-- ｜1: 課税 / 2: 非課税
WHERE tax_type IN (1, 2)

GROUP BY 
    customer_code,
    customer_name,
    tax_rate_name,
    sales_date_y,
    sales_date_m,
    sales_date_ym