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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
<?xml version="1.0" encoding="UTF-8" ?>
    
<sqls>
    <dataSpace name="target">
 
        <sql name="summaryMonthTarget">
            INSERT INTO business_data_summary (
                id, idx, title_code, title_name, position_id, year, season, month, 
                month_amt_target, month_amt_achieve, month_qty_target, month_qty_achieve) 
            select @{guid} id, null idx, '@{code}' title_code, '@{name}' title_name, target.position_id, 
                @{getYear(}CURRENT_DATE) year, FLOOR((@{month}-1)/3) +1 season, @{month} month, 
                month_amt_target, @{IfEmpty}(month_amt_achieve,0) month_amt_achieve,
                month_qty_target, @{IfEmpty}(month_qty_achieve,0) month_qty_achieve
            from (
                select position_id,sum(@{IfEmpty}(@{monthAmtTarget},0)) month_amt_target, sum(@{IfEmpty}(@{monthQtyTarget},0)) month_qty_target
                from @{targetTable} target 
                left join @{positionTable} positionTable on @{joinSql}
                where target.state_code = 'Open' and year = @{getYear(}CURRENT_DATE) and position_id is not null 
                group by position_id ) target
            left join (
                    select @{documentPosition} position_id, 
                        sum(@{IfEmpty}(@{amtAchieve},0)) month_amt_achieve, 
                        sum(@{IfEmpty}(@{qtyAchieve},0)) month_qty_achieve  
                    from @{document} document @{documentDetail}
                    where document.state_code in ('Open','Whole','Partial') and @{getMonth(}doc_date) = @{month} and @{getYear(}CURRENT_DATE) = @{getYear(}doc_date) 
                    group by @{documentPosition} 
            ) document on document.position_id = target.position_id
        </sql>
        
        <sql name="summaryMonthAchieveWithoutTarget">
            INSERT INTO business_data_summary (
                id, idx, title_code, title_name, position_id, year, season, month, 
                month_amt_target, month_amt_achieve, month_qty_target, month_qty_achieve) 
            select @{guid} id, null idx, '@{code}' title_code, '@{name}' title_name, document.position_id, 
                @{getYear(}CURRENT_DATE) year, FLOOR((@{month}-1)/3) +1 season, @{month} month, 
                @{IfEmpty}(month_amt_target, 0) month_amt_target, @{IfEmpty}(month_amt_achieve,0) month_amt_achieve,
                @{IfEmpty}(month_qty_target, 0) month_qty_target, @{IfEmpty}(month_qty_achieve,0) month_qty_achieve
            from (
                    select @{documentPosition} position_id, 
                        sum(@{IfEmpty}(@{amtAchieve},0)) month_amt_achieve, 
                        sum(@{IfEmpty}(@{qtyAchieve},0)) month_qty_achieve  
                    from @{document} document @{documentDetail}
                    where document.state_code in ('Open','Whole','Partial') and @{getMonth(}doc_date) = @{month} and @{getYear(}CURRENT_DATE) = @{getYear(}doc_date) 
                    group by @{documentPosition} 
            ) document 
            left join (
                select position_id, sum(@{IfEmpty}(@{monthAmtTarget},0)) month_amt_target, sum(@{IfEmpty}(@{monthQtyTarget},0)) month_qty_target
                from @{targetTable} target 
                left join @{positionTable} positionTable on @{joinSql}
                where target.state_code = 'Open' and year = @{getYear(}CURRENT_DATE) and position_id is not null 
                group by position_id ) target on document.position_id = target.position_id
            where target.position_id is null
        </sql>
            
        <sql name="summarySeasonTarget">
            update business_data_summary
            set business_data_summary.season_amt_target = (
                    select season.season_amt_target from (
                        select year, season ,position_id,title_code, sum(month_amt_target) season_amt_target
                        from business_data_summary 
                        where title_code =  '@{code}' AND year = @{getYear(}CURRENT_DATE)
                        group by year, season, position_id, title_code )  season 
                    where season.season = business_data_summary.season 
                        and season.year = business_data_summary.year
                        and season.position_id = business_data_summary.position_id
                        and season.title_code = business_data_summary.title_code),
                business_data_summary.season_amt_achieve = (
                    select season.season_amt_achieve from (
                        select year, season ,position_id,title_code,sum(month_amt_achieve) season_amt_achieve
                        from business_data_summary 
                        where title_code =  '@{code}' AND year = @{getYear(}CURRENT_DATE)
                        group by year, season, position_id, title_code )  season 
                    where season.season = business_data_summary.season 
                        and season.year = business_data_summary.year
                        and season.position_id = business_data_summary.position_id
                        and season.title_code = business_data_summary.title_code),
                business_data_summary.season_qty_target = (
                    select season.season_qty_target from (
                        select year, season ,position_id,title_code,sum(month_qty_target) season_qty_target
                        from business_data_summary 
                        where title_code =  '@{code}' AND year = @{getYear(}CURRENT_DATE)
                        group by year, season, position_id, title_code )  season 
                    where season.season = business_data_summary.season 
                        and season.year = business_data_summary.year
                        and season.position_id = business_data_summary.position_id
                        and season.title_code = business_data_summary.title_code),
                business_data_summary.season_qty_achieve = (
                    select season.season_qty_achieve from (
                        select year, season ,position_id,title_code, sum(month_qty_achieve) season_qty_achieve
                        from business_data_summary 
                        where title_code =  '@{code}' AND year = @{getYear(}CURRENT_DATE)
                        group by year, season, position_id, title_code )  season 
                    where season.season = business_data_summary.season 
                        and season.year = business_data_summary.year
                        and season.position_id = business_data_summary.position_id
                        and season.title_code = business_data_summary.title_code)
                where exists (
                    select 1 from (
                        select year, season ,position_id,title_code from business_data_summary 
                        where title_code =  '@{code}' AND year = @{getYear(}CURRENT_DATE)
                        group by  year, season, position_id, title_code )  season 
                    where season.season = business_data_summary.season 
                        and season.year = business_data_summary.year
                        and season.position_id = business_data_summary.position_id
                        and season.title_code = business_data_summary.title_code)
        </sql>        
            
        <sql name="summaryYearTarget">
            update business_data_summary
            set business_data_summary.year_amt_target = (
                    select year_amt_target from (
                        select year, position_id,title_code, sum(month_amt_target) year_amt_target
                        from business_data_summary 
                        where title_code = '@{code}' AND year = @{getYear(}CURRENT_DATE)
                        group by year, position_id, title_code
                    ) year where year.year = business_data_summary.year
                        and year.position_id = business_data_summary.position_id
                        and year.title_code = business_data_summary.title_code),
                business_data_summary.year_amt_achieve = (
                    select year_amt_achieve from (
                        select year, position_id,title_code,sum(month_amt_achieve) year_amt_achieve
                        from business_data_summary 
                        where title_code = '@{code}' AND year = @{getYear(}CURRENT_DATE)
                        group by year, position_id, title_code
                    ) year where year.year = business_data_summary.year
                        and year.position_id = business_data_summary.position_id
                        and year.title_code = business_data_summary.title_code),
                business_data_summary.year_qty_target = (
                    select year_qty_target from (
                        select year, position_id,title_code, sum(month_qty_target) year_qty_target
                        from business_data_summary 
                        where title_code = '@{code}' AND year = @{getYear(}CURRENT_DATE)
                        group by year, position_id, title_code
                    ) year where year.year = business_data_summary.year
                        and year.position_id = business_data_summary.position_id
                        and year.title_code = business_data_summary.title_code),
                business_data_summary.year_qty_achieve = (
                    select year_qty_achieve from (
                        select year, position_id,title_code,sum(month_qty_achieve) year_qty_achieve
                        from business_data_summary 
                        where title_code = '@{code}' AND year = @{getYear(}CURRENT_DATE)
                        group by year, position_id, title_code
                    ) year where year.year = business_data_summary.year
                        and year.position_id = business_data_summary.position_id
                        and year.title_code = business_data_summary.title_code)
            where exists (
                    select 1 from (
                        select year, position_id,title_code from business_data_summary 
                        where title_code = '@{code}' AND year = @{getYear(}CURRENT_DATE)
                        group by year, position_id, title_code
                    ) year where year.year = business_data_summary.year
                        and year.position_id = business_data_summary.position_id
                        and year.title_code = business_data_summary.title_code)
        </sql>        
            
        <sql name="getAuthorizeDealer">
            select year, md_org_account_hospital.bu_id,md_org_account_hospital.company_id,md_org_account_hospital.org_id, account_id, org_name, org_code 
            from @{targetTable} temp_md_target_dealer_hospital 
            inner join md_org_account_hospital on md_org_account_hospital.hospital_id = temp_md_target_dealer_hospital.hospital_id 
                    and @{getYear(}md_org_account_hospital.date_to) = temp_md_target_dealer_hospital.year 
                    and @{IfEmpty}(md_org_account_hospital.department, '--')  = @{IfEmpty}(temp_md_target_dealer_hospital.department, '--')
            where md_org_account_hospital.category_code = 'Dealer' 
                and md_org_account_hospital.is_active = 'T' 
                and md_org_account_hospital.state_code = 'Open'
                and @{filter}
            group by year, md_org_account_hospital.bu_id,md_org_account_hospital.company_id,md_org_account_hospital.org_id, account_id, org_name, org_code
        </sql>    
            
        <sql name="collectDealerTarget">
            select 
                @{guid} id, md_target_dealer_hospital.year year, md_target_dealer_hospital.company_id, 
                md_target_dealer_hospital.bu_id, md_org_account_hospital.org_id, md_org_account_hospital.account_id,
                'Open'state_code, '生效' state_name, '@{typeCode}' type_code, '@{typeName}' type_name, 
                md_target_dealer_hospital.product_id, max(md_target_dealer_hospital.product_name) product_name, sum(amt_year) amt_year, 
                sum(amt_q1) amt_q1, sum(amt_q2) amt_q2, sum(amt_q3) amt_q3, sum(amt_q4)amt_q4, 
                sum(amt_m1) amt_m1, sum(amt_m2) amt_m2, sum(amt_m3) amt_m3, sum(amt_m4) amt_m4, sum(amt_m5) amt_m5, sum(amt_m6) amt_m6, 
                sum(amt_m7) amt_m7, sum(amt_m8) amt_m8, sum(amt_m9) amt_m9, sum(amt_m10) amt_m10, sum(amt_m11) amt_m11, sum(amt_m12) amt_m12, 
                sum(qty_year) qty_year, sum(qty_q1) qty_q1, sum(qty_q2) qty_q2, sum(qty_q3) qty_q3, sum(qty_q4) qty_q4, 
                sum(qty_m1) qty_m1, sum(qty_m2) qty_m2, sum(qty_m3) qty_m3, sum(qty_m4) qty_m4, sum(qty_m5) qty_m5, sum(qty_m6) qty_m6, 
                sum(qty_m7) qty_m7, sum(qty_m8) qty_m8, sum(qty_m9) qty_m9, sum(qty_m10) qty_m10, sum(qty_m11) qty_m11, sum(qty_m12) qty_m12, 
                max(md_target_dealer_hospital.creator_id) creator_id, max(md_target_dealer_hospital.creator_name) creator_name, 
                max(md_target_dealer_hospital.create_time) create_time, max(md_target_dealer_hospital.update_time) update_time 
            from @{targetTable} md_target_dealer_hospital
            inner join (
                select distinct @{getYear(}date_to) year,  bu_id,company_id,org_id, account_id, org_name, org_code,hospital_id,department
                from md_org_account_hospital
                where category_code = 'Dealer' and is_active = 'T' and state_code = 'Open'
                 and md_org_account_hospital.bu_id  = '@{buId}' 
                 and md_org_account_hospital.company_id  = '@{companyId}' 
                 and md_org_account_hospital.org_id  = '@{orgId}' 
            ) md_org_account_hospital on md_org_account_hospital.hospital_id = md_target_dealer_hospital.hospital_id 
                    and md_org_account_hospital.bu_id = md_target_dealer_hospital.bu_id 
                    and md_org_account_hospital.company_id = md_target_dealer_hospital.company_id 
                    and md_org_account_hospital.year = md_target_dealer_hospital.year
                    and @{IfEmpty}(md_org_account_hospital.department, '--')  = @{IfEmpty}(md_target_dealer_hospital.department, '--')
            where md_target_dealer_hospital.year = @{year}
            group by md_target_dealer_hospital.year,md_org_account_hospital.account_id, md_target_dealer_hospital.product_id,
                md_target_dealer_hospital.bu_id,md_target_dealer_hospital.company_id,md_org_account_hospital.org_id
        </sql>    
            
        <sql name="createIssueTargetDimension">
            insert into @{targetTableName} (id, year, @{dimension})
            select @{guid} id,  '@{year}' year, @{dimension} from @{tableName}
        </sql>
        
        <sql name="updateYTDAchieved">
        <![CDATA[
            update @{targetTableName} 
            left join (
                select year, @{dimension} ,sum(@{valueField}) value 
                from @{dataName}
                where year = @{currentYear} - @{aheadYears} and month < @{currentMonth}
                group by year, @{dimension}) @{dataName} on @{matchCondition}
                set ahead_@{aheadYears}y_ytd = @{IfEmpty}(value, 0)
        ]]>    
        </sql>
        
        <sql name="updateAnnualAchieved">
        <![CDATA[
            update @{targetTableName}  
            left join (
                select year, @{dimension} ,sum(@{valueField}) value from @{dataName}
                where year = @{currentYear} - @{aheadYears}
                group by year, @{dimension}) @{dataName} on @{matchCondition}
                set ahead_@{aheadYears}y_annual = @{IfEmpty}(value, 0)
        ]]>    
        </sql>
        
        <sql name="updatePrevious">
        <![CDATA[
            update @{targetTableName}
            set previous_recommend = ahead_2y_annual*(1 + @{growthrate}/100),
                previous_estimate = ahead_1y_annual*(1 + @{growthrate}/100),
                previous_growthrate = ahead_1y_growthrate,
                previous_growthrate_ytd = case when ahead_1y_ytd != 0 then ahead_2y_ytd/ahead_1y_ytd - 1 else 1 end
        ]]>    
        </sql>
        
        <sql name="updateGrowthRate">
        <![CDATA[
            update @{targetTableName}
            set ahead_1y_growthrate = case when ahead_2y_annual != 0 then ahead_1y_annual/ahead_2y_annual - 1 else 1 end,
                ahead_2y_growthrate = case when ahead_3y_annual != 0 then ahead_2y_annual/ahead_3y_annual - 1 else 1 end,
                recent_years_growthrate = case when ahead_1y_annual != 0 then ahead_1y_annual/ahead_3y_annual - 1 else 1 end
        ]]>    
        </sql>
        
        <sql name="updateCurrentRecommend">
        <![CDATA[
            update @{targetTableName}
            set current_recommend = previous_recommend * (1 + @{growthrate}/100),
                current_estimate = previous_estimate * (1 + @{growthrate}/100),
                current_growthrate = @{growthrate}
        ]]>    
        </sql>
        
        <sql name="matchHistoryPositionTarget">
        <![CDATA[
            update md_position_target 
            left join md_position_target history  on history.type_code = md_position_target.type_code 
                    and history.bu_id = md_position_target.bu_id 
                    and history.position_id = md_position_target.position_id 
                    and  history.year = history.year - @{beforeYear}
            set md_position_target.n@{beforeYear}_year = ifnull(history.qty_year,0)
            where md_position_target.type_code = '@{typeCode}' and md_position_target.bu_id = '@{buId}'
            and md_position_target.year = '@{year}'
        ]]>    
        </sql>
        
        <sql name="setTargetIncreaseRate">
        <![CDATA[
            update md_position_target 
            set increase_rate = if( n1_year =0 , 1, round((qty_year - n1_year)/n1_year,4))
            where md_position_target.type_code = '@{typeCode}' and md_position_target.bu_id = '@{buId}'
            and md_position_target.year = '@{year}'
        ]]>    
        </sql>
    </dataSpace>        
</sqls>