feat: sql,增值税计算,添加注释以及开启开头注释掉的有用代码
This commit is contained in:
parent
74a0d754f5
commit
ce2282e673
@ -1,10 +1,6 @@
|
|||||||
-- CREATE OR REPLACE VIEW
|
CREATE OR REPLACE VIEW
|
||||||
-- public.view_invoice_tax AS
|
public.view_invoice_tax AS
|
||||||
-- 第一步-NO.1: 发票进项表和完税凭证表:
|
-- 发票进项表和完税凭证表: 1. 筛选所需字段并统一命名,2. 将发票进项表和完税凭证表, 两个表的数据合并
|
||||||
-- 输入: invoice_input, invoice_receipt
|
|
||||||
-- 1. 筛选所需字段并统一命名
|
|
||||||
-- 2. 将发票进项表和完税凭证表, 两个表的数据合并
|
|
||||||
-- 输出: A_INPUT
|
|
||||||
WITH RECURSIVE
|
WITH RECURSIVE
|
||||||
A_INPUT AS (
|
A_INPUT AS (
|
||||||
SELECT
|
SELECT
|
||||||
@ -21,10 +17,7 @@ WITH RECURSIVE
|
|||||||
FROM
|
FROM
|
||||||
invoice_receipt
|
invoice_receipt
|
||||||
),
|
),
|
||||||
-- 第一步-NO.2:发票销项表:
|
-- 发票销项表: 筛选所需字段并统一命名
|
||||||
-- 输入: invoice_output
|
|
||||||
-- 1. 筛选所需字段并统一命名
|
|
||||||
-- 输出: A_OUTPUT
|
|
||||||
A_OUTPUT AS (
|
A_OUTPUT AS (
|
||||||
SELECT
|
SELECT
|
||||||
seller_id AS company_id, -- 发票销项取销售方id,作为本公司id标识
|
seller_id AS company_id, -- 发票销项取销售方id,作为本公司id标识
|
||||||
@ -33,12 +26,7 @@ WITH RECURSIVE
|
|||||||
FROM
|
FROM
|
||||||
invoice_output
|
invoice_output
|
||||||
),
|
),
|
||||||
-- 第二步-NO.1: 聚合发票进项表:
|
-- 聚合A_INPUT: 1. 筛选出日期在'2022-01-01'之后的数据 2. 按照 company_id 和 date 聚合计算后的月份 "MONTH" 3. 获取对应月份的进项税额 input_tax_amount
|
||||||
-- 输入: A_INPUT
|
|
||||||
-- 1. 筛选出日期在'2022-01-01'之后的数据
|
|
||||||
-- 2. 按照 company_id 和 date 聚合计算后的月份 "MONTH"
|
|
||||||
-- 3. 获取对应月份的进项税额 input_tax_amount
|
|
||||||
-- 输出: B_INPUT
|
|
||||||
B_INPUT AS (
|
B_INPUT AS (
|
||||||
SELECT
|
SELECT
|
||||||
company_id,
|
company_id,
|
||||||
@ -52,12 +40,7 @@ WITH RECURSIVE
|
|||||||
company_id,
|
company_id,
|
||||||
"MONTH"
|
"MONTH"
|
||||||
),
|
),
|
||||||
-- 第二步-NO.2: 聚合B_OUTPUT:
|
-- 聚合B_OUTPUT: 1.筛选出日期在'2022-01-01'之后的数据 2. 按照 company_id 和 date 聚合计算后的月份 "MONTH" 3. 获取对应月份的销项税额 output_tax_amount
|
||||||
-- 输入: A_OUTPUT
|
|
||||||
-- 1. 筛选出日期在'2022-01-01'之后的数据
|
|
||||||
-- 2. 按照 company_id 和 date 聚合计算后的月份 "MONTH"
|
|
||||||
-- 3. 获取对应月份的销项税额 output_tax_amount
|
|
||||||
-- 输出: B_OUTPUT
|
|
||||||
B_OUTPUT AS (
|
B_OUTPUT AS (
|
||||||
SELECT
|
SELECT
|
||||||
company_id,
|
company_id,
|
||||||
@ -71,11 +54,7 @@ WITH RECURSIVE
|
|||||||
company_id,
|
company_id,
|
||||||
"MONTH"
|
"MONTH"
|
||||||
),
|
),
|
||||||
-- 第三步: JOIN 两个表,B_INPUT, B_OUTPUT:
|
-- JOIN 两个表,B_INPUT, B_OUTPUT: 1. 聚合两个表,保留全部记录 2. 聚合后的表,如果某个表的对应取值字段为null,则置为0
|
||||||
-- 1. 聚合两个表,保留全部记录
|
|
||||||
-- 2. 聚合后的表,如果某个表的对应取值字段为null,则置为0
|
|
||||||
-- 输入: B_INPUT, B_OUTPUT
|
|
||||||
-- 输出: C_INTIAL_TAX
|
|
||||||
C_INTIAL_TAX AS (
|
C_INTIAL_TAX AS (
|
||||||
SELECT
|
SELECT
|
||||||
COALESCE(B_INPUT.company_id, B_OUTPUT.company_id) AS company_id,
|
COALESCE(B_INPUT.company_id, B_OUTPUT.company_id) AS company_id,
|
||||||
@ -87,11 +66,7 @@ WITH RECURSIVE
|
|||||||
FULL JOIN B_OUTPUT ON B_INPUT.company_id = B_OUTPUT.company_id
|
FULL JOIN B_OUTPUT ON B_INPUT.company_id = B_OUTPUT.company_id
|
||||||
AND B_INPUT."MONTH" = B_OUTPUT."MONTH"
|
AND B_INPUT."MONTH" = B_OUTPUT."MONTH"
|
||||||
),
|
),
|
||||||
-- 第4步: JOIN 两个表,B_INPUT, B_OUTPUT:
|
-- JOIN 两个表,B_INPUT, B_OUTPUT: 1. 聚合两个表,保留全部记录 2. 聚合后的表,如果某个表的对应取值字段为null,则置为0
|
||||||
-- 1. 聚合两个表,保留全部记录
|
|
||||||
-- 2. 聚合后的表,如果某个表的对应取值字段为null,则置为0
|
|
||||||
-- 输入: B_INPUT, B_OUTPUT
|
|
||||||
-- 输出: C_INTIAL_TAX
|
|
||||||
D_INTIAL_TAX AS (
|
D_INTIAL_TAX AS (
|
||||||
SELECT
|
SELECT
|
||||||
company_id,
|
company_id,
|
||||||
@ -105,6 +80,7 @@ WITH RECURSIVE
|
|||||||
company_id,
|
company_id,
|
||||||
"MONTH"
|
"MONTH"
|
||||||
),
|
),
|
||||||
|
-- 加工 D_INTIAL_TAX 给每行数据添加row_num,用于下边递归计算的时候进行累加定位
|
||||||
E_INTIAL_TAX AS (
|
E_INTIAL_TAX AS (
|
||||||
SELECT
|
SELECT
|
||||||
company_id,
|
company_id,
|
||||||
@ -125,7 +101,7 @@ WITH RECURSIVE
|
|||||||
company_id,
|
company_id,
|
||||||
"MONTH"
|
"MONTH"
|
||||||
),
|
),
|
||||||
-- 第四步: 定义一个递归来根据上一个应缴税额,计算当前的应缴税额
|
-- 定义一个递归来根据上一个应缴税额,计算当前的应缴税额
|
||||||
F_INTIAL_TAX AS (
|
F_INTIAL_TAX AS (
|
||||||
-- 初始查询部分
|
-- 初始查询部分
|
||||||
(
|
(
|
||||||
@ -183,7 +159,7 @@ WITH RECURSIVE
|
|||||||
)
|
)
|
||||||
)
|
)
|
||||||
),
|
),
|
||||||
-- 求附加税和合计税额
|
-- 拿到当月应缴税后, 求附加税和合计税额
|
||||||
G_INTIAL_TAX AS (
|
G_INTIAL_TAX AS (
|
||||||
SELECT
|
SELECT
|
||||||
row_num,
|
row_num,
|
||||||
@ -211,9 +187,7 @@ WITH RECURSIVE
|
|||||||
"MONTH"
|
"MONTH"
|
||||||
)
|
)
|
||||||
-- 主查询
|
-- 主查询
|
||||||
-- 第五步: 根据计算后的表 D_FINAL_TAX,获取最终的视图
|
-- 根据计算后的表,获取最终的视图
|
||||||
-- 输入: D_FINAL_TAX
|
|
||||||
-- 输出: 最终主查询视图, public.view_invoice_tax
|
|
||||||
SELECT
|
SELECT
|
||||||
*
|
*
|
||||||
FROM
|
FROM
|
||||||
|
Loading…
Reference in New Issue
Block a user