P15GEN2\59518
2025-10-10 9f6890646993d16260d4201d613c092132856127
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
<?xml version="1.0" encoding="UTF-8" ?>
    
<sqls>
    <dataSpace name="flow">
    
        <sql name="insertQuarterFlows">
            <![CDATA[
                INSERT INTO tgt_flow_last3 (
                    id, year, quarter, quarter_no, 
                    territory_code, co_manage_ratio, 
                    product_code, product_name, sku_code, sku_name, customer_code, 
                    qty, amt, sales_category) 
                select 
                    md5(UUID_SHORT()) id, hm_fiscal_month.fiscal_year year, hm_fiscal_month.quarter_no quarter, hm_fiscal_month.fiscal_quarter quarter_no, 
                    hm_territory_hospital_product_relation.territory_code, hm_territory_hospital_product_relation.co_manage_ratio, 
                    hm_sales_flow.product_code, product_name, product_specs_code sku_code, product_specs_name sku_name, hm_territory_hospital_product_relation.hospital_code customer_code, 
                    sum(ifnull(qty,0))  qty, sum(ifnull(amt,0))  amt, sales_category
                from dscn_crm.hm_sales_flow 
                inner join dscn_crm.hm_fiscal_month on hm_fiscal_month.month_no = hm_sales_flow.data_version 
                left join dscn_crm.hm_institution_affiliated on hm_institution_affiliated.pharmacy_code = hm_sales_flow.institution_code and  hm_institution_affiliated.product_code = hm_sales_flow.product_code
                inner join dscn_crm.hm_territory_hospital_product_relation on hm_territory_hospital_product_relation.hospital_code = ifnull(hm_institution_affiliated.institution_code, hm_sales_flow.institution_code) and hm_territory_hospital_product_relation.product_code = hm_sales_flow.product_code
                where sales_category='MONTH_END' and @{filter}
                 group by hm_territory_hospital_product_relation.territory_code, hm_territory_hospital_product_relation.hospital_code,institution_type,hm_sales_flow.product_code,product_specs_code, hm_fiscal_month.fiscal_quarter
            ]]>    
        </sql>
        
        <sql name="updateCustomerInfoByCode">
            <![CDATA[
                update tgt_flow_last3
                inner join dscn_crm.hm_institution on hm_institution.institution_code = tgt_flow_last3.customer_code
                set tgt_flow_last3.customer_name = hm_institution.institution_name ,tgt_flow_last3.customer_id = hm_institution.id 
                where tgt_flow_last3.quarter_no = '@{quarterNo}'
            ]]>    
        </sql>
        
        <sql name="updateTerritoryIdByCode">
            <![CDATA[
                update tgt_flow_last3
                inner join dscn_crm.hm_territory on hm_territory.code = tgt_flow_last3.territory_code
                set tgt_flow_last3.territory_id = hm_territory.id 
                where tgt_flow_last3.quarter_no = '@{quarterNo}'
            ]]>    
        </sql>
        
        <sql name="updatePositionIdByTerritory">
            <![CDATA[
                update tgt_flow_last3 
                inner join dscn_crm.hm_position_territory on hm_position_territory.territory_id = tgt_flow_last3.territory_id 
                set tgt_flow_last3.position_id = hm_position_territory.position_id 
                where tgt_flow_last3.quarter_no = '@{quarterNo}'
            ]]>    
        </sql>
        
        <sql name="updateSkuInfoByCode">
            <![CDATA[
                update tgt_flow_last3
                inner join dscn_crm.md_product_sku on md_product_sku.sku_code = tgt_flow_last3.sku_code
                set tgt_flow_last3.sku_id = md_product_sku.sku_id ,tgt_flow_last3.product_id = md_product_sku.product_id
                where tgt_flow_last3.quarter_no = '@{quarterNo}'
            ]]>    
        </sql>
        
        <sql name="updateTerritorySales">
            <![CDATA[
                update tgt_flow_last3 
                set territory_qty = qty * co_manage_ratio  ,territory_amt = amt * co_manage_ratio ,position_qty = qty * co_manage_ratio  ,position_amt = amt * co_manage_ratio
                where tgt_flow_last3.quarter_no = '@{quarterNo}'
            ]]>    
        </sql>
 
    </dataSpace>
</sqls>