2024-11-24 15:21:04 +08:00
-- ======================================
-- 报表上新
-- ======================================
SELECT
op1 . entry_id AS " 报表id " ,
op2 . displayname AS " 报表名称 " ,
TO_CHAR ( op1 . create_time , ' YYYY-MM-DD ' ) AS " 上新时间 "
FROM
sdi_fine . input_fr_report_list op1
LEFT JOIN sdi_fine . fine_authority_object op2 ON op1 . entry_id = op2 . id
WHERE
report_status = ' 0 '
-- AND STRING_TO_ARRAY(op1.role_list,',') && STRING_TO_ARRAY('${fine_role}',',')
AND op1 . create_time > = CURRENT_TIMESTAMP - INTERVAL ' 7 days '
ORDER BY
op1 . create_time DESC
-- ======================================
-- 收藏目录表
-- ======================================
-- 收藏目录表
SELECT
IF ( LENGTH ( LEFT ( op3 . path , POSITION ( ' / ' IN op3 . path ) - 1 ) ) > 0 , LEFT ( op3 . path , POSITION ( ' / ' IN op3 . path ) - 1 ) , " 其他 " ) AS ` 所 属 根 目 录 名 称 ` ,
op3 . path AS ` 报 表 全 路 径 ` ,
op1 . id ,
op1 . entryId AS ` 报 表 ID ` ,
op1 . userId AS ` 用 户 ID ` ,
op2 . realName AS ` 姓 名 ` ,
op2 . userName AS ` 用 户 名 ` ,
op3 . displayName AS ` 报 表 名 称 `
FROM
fine_favorite_entry op1
JOIN fine_user op2 ON op1 . userId = op2 . id
JOIN fine_authority_object op3 ON op1 . entryId = op3 . id
WHERE
op2 . userName = ' ${fine_username} '
-- 展示终端类型:此处需要再讨论一下PC及移动端定义
AND op3 . deviceType = 1
-- 平台类型:此处只展示链接/FR报表/BI报表
AND op3 . expandType IN ( 5 , 102 , 201 )
-- AND IFNULL(LEFT(op3.path,POSITION('/' IN op3.path) - 1),"其他") = '${sDefaultFile}'
-- ======================================
-- 最近浏览
-- ======================================
-- 近10天近30个目录
SELECT
op1 . tname AS ` 报 表 全 路 径 ` ,
op2 . id AS ` 报 表 ID ` ,
SUBSTRING_INDEX ( op2 . displayName , ' / ' , - 1 ) AS ` 模 版 名 称 ` ,
-- FROM_UNIXTIME(MAX(op1.time) / 1000,'%Y-%m-%d %H:%i:%s') AS `访问时间`
FROM_UNIXTIME ( MAX ( op1 . time ) / 1000 , ' %Y-%m-%d ' ) AS ` 访 问 时 间 `
FROM
logdb . fine_record_execute op1
LEFT JOIN finedb . fine_authority_object op2 ON op1 . tname = op2 . path
WHERE
op1 . username = ( SELECT CONCAT ( realName , ' ( ' , userName , ' ) ' ) AS username FROM finedb . fine_user WHERE userName = ' ${fine_username} ' )
AND op1 . time > = UNIX_TIMESTAMP ( DATE_SUB ( CURDATE ( ) , INTERVAL 10 DAY ) ) * 1000
GROUP BY
op2 . displayName ,
op2 . id
ORDER BY
op1 . time DESC
2024-11-25 21:23:57 +08:00
LIMIT 30
-- ======================================
-- 合同量
-- ======================================
select
' 当期 ' AS time_type
, sum ( ct_amt ) / 10000 AS amt
from dm . v_ag_sal_contract a
2024-11-27 09:03:05 +08:00
-- 权限控制
where exists (
select
1
from
dm . dm_bi_user_permisson
where
bi_user = ' ${fine_username} '
and per_model IN ( ' all ' , ' sal ' )
and (
is_all = 1
OR per_code = a . bu_code -- 事业部
OR per_code = a . region_code -- 大区
OR per_code = a . office_code -- 办事处
-- OR per_code = a.city_unit_code -- 城市单元
)
)
2024-11-29 18:04:02 +08:00
AND ct_status = ' 已终审 '
2024-11-27 09:03:05 +08:00
$ { IF ( len ( sStart_date ) > 0 , " AND ct_year =' " + FORMAT ( sStart_date , ' yyyy ' ) + " ' " , " " ) }
-- 年
-- ${IF(sPeriod=1,"AND ct_year ='"+ FORMAT(sStart_date,'yyyy') +"'","")}
-- 季
-- ${IF(sPeriod=2,"AND to_char(signed_date,'yyyy-MM-dd') >='"+ sStart_date +"'","")}
-- ${IF(sPeriod=2,"AND to_char(signed_date,'yyyy-MM-dd') <='"+ sEnd_date +"'","")}
-- 月
-- ${IF(sPeriod=3,"AND to_char(signed_date,'yyyy-MM-dd') >='"+ sStart_date +"'","")}
- -- ${IF(sPeriod=3,"AND to_char(signed_date,'yyyy-MM-dd') <='"+ sEnd_date +"'","")}
-- 日
-- ${IF(sPeriod=4,"AND to_char(signed_date,'yyyy-MM-dd') >='"+ sStart_date +"'","")}
-- ${IF(sPeriod=4,"AND to_char(signed_date,'yyyy-MM-dd') <='"+ sEnd_date +"'","")}
2024-11-25 21:23:57 +08:00
UNION ALL
select
' 同期 ' AS time_type
, sum ( ct_amt ) / 10000 AS amt
from dm . v_ag_sal_contract a
2024-11-27 09:03:05 +08:00
-- 权限控制
where exists (
select
1
from
dm . dm_bi_user_permisson
where
bi_user = ' ${fine_username} '
and per_model IN ( ' all ' , ' sal ' )
and (
is_all = 1
OR per_code = a . bu_code -- 事业部
OR per_code = a . region_code -- 大区
OR per_code = a . office_code -- 办事处
OR per_code = a . city_unit_code -- 城市单元
)
)
2024-11-29 18:04:02 +08:00
AND ct_status = ' 已终审 '
2024-11-27 09:03:05 +08:00
$ { IF ( len ( sStart_date ) > 0 , " AND ct_year = ' " + left ( yeardelta ( sStart_date , - 1 ) , 4 ) + " ' " , " " ) }
-- 去年
-- ${IF(sPeriod=1,"AND ct_year = '" + left(yeardelta(sStart_date,-1),4) + "'","")}
-- 去年同季
-- ${IF(sPeriod=2,"AND to_char(signed_date,'yyyy-MM-dd') >='"+ yeardelta(sStart_date,-1) +"'","")}
-- ${IF(sPeriod=2,"AND to_char(signed_date,'yyyy-MM-dd') <='"+ yeardelta(sEnd_date,-1) +"'","")}
-- 去年同月
-- ${IF(sPeriod=3,"AND to_char(signed_date,'yyyy-MM-dd') >='"+ yeardelta(sStart_date,-1) +"'","")}
-- ${IF(sPeriod=3,"AND to_char(signed_date,'yyyy-MM-dd') <='"+ yeardelta(sEnd_date,-1) +"'","")}
-- 去年同日
-- ${IF(sPeriod=4,"AND to_char(signed_date,'yyyy-MM-dd') >='"+ yeardelta(sStart_date,-1) +"'","")}
-- ${IF(sPeriod=4,"AND to_char(signed_date,'yyyy-MM-dd') <='"+ yeardelta(sEnd_date,-1) +"'","")}
UNION ALL
select
' 当期回款 ' time_type
, sum ( amt_bu ) AS amt
from dm . v_sal_indicator_bu a
where ( indi_type = ' 回款 ' OR indi_type = ' 电商订单 ' )
-- 权限控制
AND exists (
select
1
from
dm . dm_bi_user_permisson
where
bi_user = ' ${fine_username} '
and per_model IN ( ' all ' , ' sal ' )
and (
is_all = 1
OR per_code = a . bu_code -- 事业部
-- OR per_code = a.region_code -- 大区
-- OR per_code = a.office_code -- 办事处
-- OR per_code = a.city_unit_code -- 城市单元
)
)
$ { IF ( len ( sStart_date ) > 0 , " AND to_char(stat_date,'yyyy') =' " + FORMAT ( sStart_date , ' yyyy ' ) + " ' " , " " ) }
-- 年
-- ${IF(sPeriod=1,"AND to_char(stat_date,'yyyy') ='"+ FORMAT(sStart_date,'yyyy') +"'","")}
-- 季
-- ${IF(sPeriod=2,"AND to_char(stat_date,'yyyy-MM-dd') >='"+ sStart_date +"'","")}
-- ${IF(sPeriod=2,"AND to_char(stat_date,'yyyy-MM-dd') <='"+ sEnd_date +"'","")}
-- 月
-- ${IF(sPeriod=3,"AND to_char(stat_date,'yyyy-MM-dd') >='"+ sStart_date +"'","")}
-- ${IF(sPeriod=3,"AND to_char(stat_date,'yyyy-MM-dd') <='"+ sEnd_date +"'","")}
-- 日
-- ${IF(sPeriod=4,"AND to_char(stat_date,'yyyy-MM-dd') >='"+ sStart_date +"'","")}
-- ${IF(sPeriod=4,"AND to_char(stat_date,'yyyy-MM-dd') <='"+ sEnd_date +"'","")}
2024-11-25 21:23:57 +08:00
-- ======================================
-- 回款
-- ======================================
select
' 当期 ' time_type
, sum ( amt_bu ) AS amt
2024-11-27 09:03:05 +08:00
from dm . v_sal_indicator_bu a
2024-11-25 21:23:57 +08:00
where ( indi_type = ' 回款 ' OR indi_type = ' 电商订单 ' )
2024-11-27 09:03:05 +08:00
-- 权限控制
AND exists (
select
1
from
dm . dm_bi_user_permisson
where
bi_user = ' ${fine_username} '
and per_model IN ( ' all ' , ' sal ' )
and (
is_all = 1
OR per_code = a . bu_code -- 事业部
-- OR per_code = a.region_code -- 大区
-- OR per_code = a.office_code -- 办事处
-- OR per_code = a.city_unit_code -- 城市单元
)
)
-- 年
$ { IF ( sPeriod = 1 , " AND to_char(stat_date,'yyyy') =' " + FORMAT ( sStart_date , ' yyyy ' ) + " ' " , " " ) }
2024-11-29 18:04:02 +08:00
$ { IF ( sPeriod = 1 , " AND to_char(stat_date,'yyyy-MM-dd') <=' " + FORMAT ( sStart_date , ' yyyy-MM-dd ' ) + " ' " , " " ) }
2024-11-27 09:03:05 +08:00
-- 季
2024-11-27 17:06:11 +08:00
$ { IF ( sPeriod = 2 , " AND CONCAT(to_char(stat_date,'yyyy'),EXTRACT(QUARTER FROM stat_date)) =' " + CONCATENATE ( left ( sStart_date , 4 ) , roundup ( month ( sStart_date ) / 3 ) ) + " ' " , " " ) }
2024-11-29 18:04:02 +08:00
$ { IF ( sPeriod = 2 , " AND to_char(stat_date,'yyyy-MM-dd') <= ' " + FORMAT ( sStart_date , ' yyyy-MM-dd ' ) + " ' " , " " ) }
2024-11-27 17:06:11 +08:00
-- ${IF(sPeriod=2,"AND to_char(stat_date,'yyyy-MM-dd') <='"+ sEnd_date +"'","")}
2024-11-27 09:03:05 +08:00
-- 月
2024-11-27 17:06:11 +08:00
$ { IF ( sPeriod = 3 , " AND to_char(stat_date,'yyyy-MM') =' " + FORMAT ( sStart_date , ' yyyy-MM ' ) + " ' " , " " ) }
2024-11-29 18:04:02 +08:00
$ { IF ( sPeriod = 3 , " AND to_char(stat_date,'yyyy-MM-dd') <= ' " + FORMAT ( sStart_date , ' yyyy-MM-dd ' ) + " ' " , " " ) }
2024-11-27 17:06:11 +08:00
-- ${IF(sPeriod=3,"AND to_char(stat_date,'yyyy-MM-dd') <='"+ sEnd_date +"'","")}
2024-11-27 09:03:05 +08:00
-- 日
2024-11-27 17:06:11 +08:00
$ { IF ( sPeriod = 4 , " AND to_char(stat_date,'yyyy-MM-dd') =' " + sStart_date + " ' " , " " ) }
-- ${IF(sPeriod=4,"AND to_char(stat_date,'yyyy-MM-dd') <='"+ sEnd_date +"'","")}
2024-11-25 21:23:57 +08:00
UNION ALL
select
' 同期 ' time_type
, sum ( amt_bu ) AS amt
2024-11-27 09:03:05 +08:00
from dm . v_sal_indicator_bu a
2024-11-25 21:23:57 +08:00
where ( indi_type = ' 回款 ' OR indi_type = ' 电商订单 ' )
2024-11-27 09:03:05 +08:00
AND exists (
select
1
from
dm . dm_bi_user_permisson
where
bi_user = ' ${fine_username} '
and per_model IN ( ' all ' , ' sal ' )
and (
is_all = 1
OR per_code = a . bu_code -- 事业部
-- OR per_code = a.region_code -- 大区
-- OR per_code = a.office_code -- 办事处
-- OR per_code = a.city_unit_code -- 城市单元
)
)
2024-11-25 21:23:57 +08:00
2024-11-27 09:03:05 +08:00
-- 去年
$ { IF ( sPeriod = 1 , " AND to_char(stat_date,'yyyy') = ' " + left ( yeardelta ( sStart_date , - 1 ) , 4 ) + " ' " , " " ) }
2024-12-08 21:18:00 +08:00
-- ${IF(sPeriod=1,"AND to_char(stat_date,'yyyy-MM-dd') <='"+ FORMAT(yeardelta(sStart_date,-1),'yyyy-MM-dd') +"'","")}
2024-11-29 18:04:02 +08:00
2024-11-27 09:03:05 +08:00
-- 去年同季
2024-11-27 17:06:11 +08:00
$ { IF ( sPeriod = 2 , " AND CONCAT(to_char(stat_date,'yyyy'),EXTRACT(QUARTER FROM stat_date)) =' " + CONCATENATE ( left ( YEARDELTA ( sStart_date , - 1 ) , 4 ) , roundup ( month ( YEARDELTA ( sStart_date , - 1 ) ) / 3 ) ) + " ' " , " " ) }
2024-12-08 21:18:00 +08:00
-- ${IF(sPeriod=2,"AND to_char(stat_date,'yyyy-MM-dd') <='"+ FORMAT(yeardelta(sStart_date,-1),'yyyy-MM-dd') +"'","")}
2024-11-27 17:06:11 +08:00
-- ${IF(sPeriod=2,"AND to_char(stat_date,'yyyy-MM-dd') <='"+ yeardelta(sEnd_date,-1) +"'","")}
2024-11-27 09:03:05 +08:00
-- 去年同月
2024-11-27 17:06:11 +08:00
$ { IF ( sPeriod = 3 , " AND to_char(stat_date,'yyyy-MM') =' " + FORMAT ( yeardelta ( sStart_date , - 1 ) , ' yyyy-MM ' ) + " ' " , " " ) }
2024-12-08 21:18:00 +08:00
-- ${IF(sPeriod=3,"AND to_char(stat_date,'yyyy-MM-dd') <='"+ FORMAT(yeardelta(sStart_date,-1),'yyyy-MM-dd') +"'","")}
2024-11-27 17:06:11 +08:00
-- ${IF(sPeriod=3,"AND to_char(stat_date,'yyyy-MM-dd') <='"+ yeardelta(sEnd_date,-1) +"'","")}
2024-11-27 09:03:05 +08:00
-- 去年同日
2024-11-27 17:06:11 +08:00
$ { IF ( sPeriod = 4 , " AND to_char(stat_date,'yyyy-MM-dd') =' " + yeardelta ( sStart_date , - 1 ) + " ' " , " " ) }
-- ${IF(sPeriod=4,"AND to_char(stat_date,'yyyy-MM-dd') <='"+ yeardelta(sEnd_date,-1) +"'","")}
2024-11-25 21:23:57 +08:00
UNION
select
' 目标 ' time_type ,
sum ( amt_bu ) AS amt
2024-11-27 09:03:05 +08:00
from dm . v_sal_indicator_bu a
2024-11-25 21:23:57 +08:00
where $ { switch ( sPeriod
, 1 , " indi_type='年度目标_事业部_回款' "
, 2 , " indi_type='季度目标_事业部_回款' "
, 3 , " indi_type='月度目标_事业部_回款' "
, 4 , " indi_type='不存在' " ) }
2024-11-27 09:03:05 +08:00
AND exists (
select
1
from
dm . dm_bi_user_permisson
where
bi_user = ' ${fine_username} '
and per_model IN ( ' all ' , ' sal ' )
and (
is_all = 1
OR per_code = a . bu_code -- 事业部
-- OR per_code = a.region_code -- 大区
-- OR per_code = a.office_code -- 办事处
-- OR per_code = a.city_unit_code -- 城市单元
)
)
-- 年
$ { IF ( sPeriod = 1 , " AND to_char(stat_date,'yyyy') =' " + FORMAT ( sStart_date , ' yyyy ' ) + " ' " , " " ) }
-- 季
2024-11-27 17:06:11 +08:00
$ { IF ( sPeriod = 2 , " AND CONCAT(to_char(stat_date,'yyyy'),EXTRACT(QUARTER FROM stat_date)) =' " + CONCATENATE ( left ( sStart_date , 4 ) , roundup ( month ( sStart_date ) / 3 ) ) + " ' " , " " ) }
-- ${IF(sPeriod=2,"AND to_char(stat_date,'yyyy-MM-dd') <='"+ sEnd_date +"'","")}
2024-11-27 09:03:05 +08:00
-- 月
2024-11-27 17:06:11 +08:00
$ { IF ( sPeriod = 3 , " AND to_char(stat_date,'yyyy-MM') =' " + FORMAT ( sStart_date , ' yyyy-MM ' ) + " ' " , " " ) }
-- ${IF(sPeriod=3,"AND to_char(stat_date,'yyyy-MM-dd') <='"+ sEnd_date +"'","")}
2024-11-27 09:03:05 +08:00
-- 日
2024-11-27 17:06:11 +08:00
$ { IF ( sPeriod = 4 , " AND to_char(stat_date,'yyyy-MM-dd') =' " + sStart_date + " ' " , " " ) }
-- ${IF(sPeriod=4,"AND to_char(stat_date,'yyyy-MM-dd') <='"+ sEnd_date +"'","")}
2024-11-25 21:23:57 +08:00
-- ======================================
-- 经销商出货
-- ======================================
select
' 当期 ' time_type
, sum ( amt_bu ) AS amt
2024-11-27 09:03:05 +08:00
from dm . v_sal_indicator_bu a
2024-11-25 21:23:57 +08:00
where ( indi_type = ' 出货 ' OR indi_type = ' 电商订单 ' )
2024-11-27 09:03:05 +08:00
-- 权限控制
AND exists (
select
1
from
dm . dm_bi_user_permisson
where
bi_user = ' ${fine_username} '
and per_model IN ( ' all ' , ' sal ' )
and (
is_all = 1
OR per_code = a . bu_code -- 事业部
-- OR per_code = a.region_code -- 大区
-- OR per_code = a.office_code -- 办事处
-- OR per_code = a.city_unit_code -- 城市单元
)
)
-- 年
$ { IF ( sPeriod = 1 , " AND to_char(stat_date,'yyyy') =' " + FORMAT ( sStart_date , ' yyyy ' ) + " ' " , " " ) }
2024-11-29 18:04:02 +08:00
$ { IF ( sPeriod = 1 , " AND to_char(stat_date,'yyyy-MM-dd') <=' " + FORMAT ( sStart_date , ' yyyy-MM-dd ' ) + " ' " , " " ) }
2024-11-27 09:03:05 +08:00
-- 季
2024-11-27 17:06:11 +08:00
$ { IF ( sPeriod = 2 , " AND CONCAT(to_char(stat_date,'yyyy'),EXTRACT(QUARTER FROM stat_date)) =' " + CONCATENATE ( left ( sStart_date , 4 ) , roundup ( month ( sStart_date ) / 3 ) ) + " ' " , " " ) }
2024-11-29 18:04:02 +08:00
$ { IF ( sPeriod = 2 , " AND to_char(stat_date,'yyyy-MM-dd') <= ' " + FORMAT ( sStart_date , ' yyyy-MM-dd ' ) + " ' " , " " ) }
2024-11-27 17:06:11 +08:00
-- ${IF(sPeriod=2,"AND to_char(stat_date,'yyyy-MM-dd') <='"+ sEnd_date +"'","")}
2024-11-27 09:03:05 +08:00
-- 月
2024-11-27 17:06:11 +08:00
$ { IF ( sPeriod = 3 , " AND to_char(stat_date,'yyyy-MM') =' " + FORMAT ( sStart_date , ' yyyy-MM ' ) + " ' " , " " ) }
2024-11-29 18:04:02 +08:00
$ { IF ( sPeriod = 3 , " AND to_char(stat_date,'yyyy-MM-dd') <= ' " + FORMAT ( sStart_date , ' yyyy-MM-dd ' ) + " ' " , " " ) }
2024-11-27 17:06:11 +08:00
-- ${IF(sPeriod=3,"AND to_char(stat_date,'yyyy-MM-dd') <='"+ sEnd_date +"'","")}
2024-11-27 09:03:05 +08:00
-- 日
2024-11-27 17:06:11 +08:00
$ { IF ( sPeriod = 4 , " AND to_char(stat_date,'yyyy-MM-dd') =' " + sStart_date + " ' " , " " ) }
-- ${IF(sPeriod=4,"AND to_char(stat_date,'yyyy-MM-dd') <='"+ sEnd_date +"'","")}
2024-11-27 09:03:05 +08:00
2024-11-25 21:23:57 +08:00
UNION ALL
select
' 同期 ' time_type
, sum ( amt_bu ) AS amt
2024-11-27 09:03:05 +08:00
from dm . v_sal_indicator_bu a
2024-11-25 21:23:57 +08:00
where ( indi_type = ' 出货 ' OR indi_type = ' 电商订单 ' )
2024-11-27 09:03:05 +08:00
AND exists (
select
1
from
dm . dm_bi_user_permisson
where
bi_user = ' ${fine_username} '
and per_model IN ( ' all ' , ' sal ' )
and (
is_all = 1
OR per_code = a . bu_code -- 事业部
-- OR per_code = a.region_code -- 大区
-- OR per_code = a.office_code -- 办事处
-- OR per_code = a.city_unit_code -- 城市单元
)
)
-- 去年
2024-11-29 18:04:02 +08:00
$ { IF ( sPeriod = 1 , " AND to_char(stat_date,'yyyy') = ' " + left ( yeardelta ( sStart_date , - 1 ) , 4 ) + " ' " , " " ) }
2024-12-08 21:18:00 +08:00
-- ${IF(sPeriod=1,"AND to_char(stat_date,'yyyy-MM-dd') <='"+ FORMAT(yeardelta(sStart_date,-1),'yyyy-MM-dd') +"'","")}
2024-11-29 18:04:02 +08:00
2024-11-27 09:03:05 +08:00
-- 去年同季
2024-11-29 18:04:02 +08:00
$ { IF ( sPeriod = 2 , " AND CONCAT(to_char(stat_date,'yyyy'),EXTRACT(QUARTER FROM stat_date)) =' " + CONCATENATE ( left ( YEARDELTA ( sStart_date , - 1 ) , 4 ) , roundup ( month ( YEARDELTA ( sStart_date , - 1 ) ) / 3 ) ) + " ' " , " " ) }
2024-12-08 21:18:00 +08:00
-- ${IF(sPeriod=2,"AND to_char(stat_date,'yyyy-MM-dd') <='"+ FORMAT(yeardelta(sStart_date,-1),'yyyy-MM-dd') +"'","")}
2024-11-29 18:04:02 +08:00
2024-11-27 17:06:11 +08:00
-- ${IF(sPeriod=2,"AND to_char(stat_date,'yyyy-MM-dd') <='"+ yeardelta(sEnd_date,-1) +"'","")}
2024-11-27 09:03:05 +08:00
-- 去年同月
2024-11-27 17:06:11 +08:00
$ { IF ( sPeriod = 3 , " AND to_char(stat_date,'yyyy-MM') =' " + FORMAT ( yeardelta ( sStart_date , - 1 ) , ' yyyy-MM ' ) + " ' " , " " ) }
2024-12-08 21:18:00 +08:00
-- ${IF(sPeriod=3,"AND to_char(stat_date,'yyyy-MM-dd') <='"+ FORMAT(yeardelta(sStart_date,-1),'yyyy-MM-dd') +"'","")}
2024-11-29 18:04:02 +08:00
2024-11-27 17:06:11 +08:00
-- ${IF(sPeriod=3,"AND to_char(stat_date,'yyyy-MM-dd') <='"+ yeardelta(sEnd_date,-1) +"'","")}
2024-11-27 09:03:05 +08:00
-- 去年同日
2024-11-27 17:06:11 +08:00
$ { IF ( sPeriod = 4 , " AND to_char(stat_date,'yyyy-MM-dd') =' " + yeardelta ( sStart_date , - 1 ) + " ' " , " " ) }
-- ${IF(sPeriod=4,"AND to_char(stat_date,'yyyy-MM-dd') <='"+ yeardelta(sEnd_date,-1) +"'","")}
2024-11-27 09:03:05 +08:00
2024-11-25 21:23:57 +08:00
UNION
select
' 目标 ' time_type
, sum ( amt_bu ) AS amt
2024-11-27 09:03:05 +08:00
from dm . v_sal_indicator_bu a
2024-11-25 21:23:57 +08:00
where $ { switch ( sPeriod
, 1 , " indi_type='年度目标_事业部_出货' "
, 2 , " indi_type='季度目标_事业部_出货' "
, 3 , " indi_type='月度目标_事业部_出货' "
, 4 , " indi_type='不存在' " ) }
2024-11-27 09:03:05 +08:00
AND exists (
select
1
from
dm . dm_bi_user_permisson
where
bi_user = ' ${fine_username} '
and per_model IN ( ' all ' , ' sal ' )
and (
is_all = 1
OR per_code = a . bu_code -- 事业部
-- OR per_code = a.region_code -- 大区
-- OR per_code = a.office_code -- 办事处
-- OR per_code = a.city_unit_code -- 城市单元
)
)
-- 年
$ { IF ( sPeriod = 1 , " AND to_char(stat_date,'yyyy') =' " + FORMAT ( sStart_date , ' yyyy ' ) + " ' " , " " ) }
-- 季
2024-11-27 17:06:11 +08:00
$ { IF ( sPeriod = 2 , " AND CONCAT(to_char(stat_date,'yyyy'),EXTRACT(QUARTER FROM stat_date)) =' " + CONCATENATE ( left ( sStart_date , 4 ) , roundup ( month ( sStart_date ) / 3 ) ) + " ' " , " " ) }
-- ${IF(sPeriod=2,"AND to_char(stat_date,'yyyy-MM-dd') <='"+ sEnd_date +"'","")}
2024-11-27 09:03:05 +08:00
-- 月
2024-11-27 17:06:11 +08:00
$ { IF ( sPeriod = 3 , " AND to_char(stat_date,'yyyy-MM') =' " + FORMAT ( sStart_date , ' yyyy-MM ' ) + " ' " , " " ) }
-- ${IF(sPeriod=3,"AND to_char(stat_date,'yyyy-MM-dd') <='"+ sEnd_date +"'","")}
2024-11-27 09:03:05 +08:00
-- 日
2024-11-27 17:06:11 +08:00
$ { IF ( sPeriod = 4 , " AND to_char(stat_date,'yyyy-MM-dd') =' " + sStart_date + " ' " , " " ) }
-- ${IF(sPeriod=4,"AND to_char(stat_date,'yyyy-MM-dd') <='"+ sEnd_date +"'","")}
2024-11-25 21:23:57 +08:00
-- ======================================
-- 发货量
-- ======================================
select
' 当期 ' time_type
, sum ( amt_bu ) AS amt
2024-11-27 09:03:05 +08:00
from dm . v_sal_indicator_bu a
2024-11-25 21:23:57 +08:00
where ( indi_type = ' 发货 ' OR indi_type = ' 费用发货 ' )
2024-11-27 09:03:05 +08:00
-- 权限控制
AND exists (
select
1
from
dm . dm_bi_user_permisson
where
bi_user = ' ${fine_username} '
and per_model IN ( ' all ' , ' sal ' )
and (
is_all = 1
OR per_code = a . bu_code -- 事业部
-- OR per_code = a.region_code -- 大区
-- OR per_code = a.office_code -- 办事处
-- OR per_code = a.city_unit_code -- 城市单元
)
)
-- 年
$ { IF ( sPeriod = 1 , " AND to_char(stat_date,'yyyy') =' " + FORMAT ( sStart_date , ' yyyy ' ) + " ' " , " " ) }
2024-11-29 18:04:02 +08:00
$ { IF ( sPeriod = 1 , " AND to_char(stat_date,'yyyy-MM-dd') <=' " + FORMAT ( sStart_date , ' yyyy-MM-dd ' ) + " ' " , " " ) }
2024-11-27 09:03:05 +08:00
-- 季
2024-11-27 17:06:11 +08:00
$ { IF ( sPeriod = 2 , " AND CONCAT(to_char(stat_date,'yyyy'),EXTRACT(QUARTER FROM stat_date)) =' " + CONCATENATE ( left ( sStart_date , 4 ) , roundup ( month ( sStart_date ) / 3 ) ) + " ' " , " " ) }
2024-11-29 18:04:02 +08:00
$ { IF ( sPeriod = 2 , " AND to_char(stat_date,'yyyy-MM-dd') <= ' " + FORMAT ( sStart_date , ' yyyy-MM-dd ' ) + " ' " , " " ) }
2024-11-27 17:06:11 +08:00
-- ${IF(sPeriod=2,"AND to_char(stat_date,'yyyy-MM-dd') <='"+ sEnd_date +"'","")}
2024-11-27 09:03:05 +08:00
-- 月
2024-11-27 17:06:11 +08:00
$ { IF ( sPeriod = 3 , " AND to_char(stat_date,'yyyy-MM') =' " + FORMAT ( sStart_date , ' yyyy-MM ' ) + " ' " , " " ) }
2024-11-29 18:04:02 +08:00
$ { IF ( sPeriod = 3 , " AND to_char(stat_date,'yyyy-MM-dd') <= ' " + FORMAT ( sStart_date , ' yyyy-MM-dd ' ) + " ' " , " " ) }
2024-11-27 17:06:11 +08:00
-- ${IF(sPeriod=3,"AND to_char(stat_date,'yyyy-MM-dd') <='"+ sEnd_date +"'","")}
2024-11-27 09:03:05 +08:00
-- 日
2024-11-27 17:06:11 +08:00
$ { IF ( sPeriod = 4 , " AND to_char(stat_date,'yyyy-MM-dd') =' " + sStart_date + " ' " , " " ) }
-- ${IF(sPeriod=4,"AND to_char(stat_date,'yyyy-MM-dd') <='"+ sEnd_date +"'","")}
2024-11-25 21:23:57 +08:00
UNION ALL
select
' 同期 ' time_type
, sum ( amt_bu ) AS amt
2024-11-27 09:03:05 +08:00
from dm . v_sal_indicator_bu a
2024-11-25 21:23:57 +08:00
where ( indi_type = ' 发货 ' OR indi_type = ' 费用发货 ' )
2024-11-27 09:03:05 +08:00
AND exists (
select
1
from
dm . dm_bi_user_permisson
where
bi_user = ' ${fine_username} '
and per_model IN ( ' all ' , ' sal ' )
and (
is_all = 1
OR per_code = a . bu_code -- 事业部
-- OR per_code = a.region_code -- 大区
-- OR per_code = a.office_code -- 办事处
-- OR per_code = a.city_unit_code -- 城市单元
)
)
-- 去年
$ { IF ( sPeriod = 1 , " AND to_char(stat_date,'yyyy') = ' " + left ( yeardelta ( sStart_date , - 1 ) , 4 ) + " ' " , " " ) }
2024-12-08 21:18:00 +08:00
-- ${IF(sPeriod=1,"AND to_char(stat_date,'yyyy-MM-dd') <='"+ FORMAT(yeardelta(sStart_date,-1),'yyyy-MM-dd') +"'","")}
2024-11-29 18:04:02 +08:00
2024-11-27 09:03:05 +08:00
-- 去年同季
2024-11-27 17:06:11 +08:00
$ { IF ( sPeriod = 2 , " AND CONCAT(to_char(stat_date,'yyyy'),EXTRACT(QUARTER FROM stat_date)) =' " + CONCATENATE ( left ( YEARDELTA ( sStart_date , - 1 ) , 4 ) , roundup ( month ( YEARDELTA ( sStart_date , - 1 ) ) / 3 ) ) + " ' " , " " ) }
2024-12-08 21:18:00 +08:00
-- ${IF(sPeriod=2,"AND to_char(stat_date,'yyyy-MM-dd') <='"+ FORMAT(yeardelta(sStart_date,-1),'yyyy-MM-dd') +"'","")}
2024-11-29 18:04:02 +08:00
2024-11-27 17:06:11 +08:00
-- ${IF(sPeriod=2,"AND to_char(stat_date,'yyyy-MM-dd') <='"+ yeardelta(sEnd_date,-1) +"'","")}
2024-11-27 09:03:05 +08:00
-- 去年同月
2024-11-27 17:06:11 +08:00
$ { IF ( sPeriod = 3 , " AND to_char(stat_date,'yyyy-MM') =' " + FORMAT ( yeardelta ( sStart_date , - 1 ) , ' yyyy-MM ' ) + " ' " , " " ) }
2024-12-08 21:18:00 +08:00
-- ${IF(sPeriod=3,"AND to_char(stat_date,'yyyy-MM-dd') <='"+ FORMAT(yeardelta(sStart_date,-1),'yyyy-MM-dd') +"'","")}
2024-11-29 18:04:02 +08:00
2024-11-27 17:06:11 +08:00
-- ${IF(sPeriod=3,"AND to_char(stat_date,'yyyy-MM-dd') <='"+ yeardelta(sEnd_date,-1) +"'","")}
2024-11-27 09:03:05 +08:00
-- 去年同日
2024-11-27 17:06:11 +08:00
$ { IF ( sPeriod = 4 , " AND to_char(stat_date,'yyyy-MM-dd') =' " + yeardelta ( sStart_date , - 1 ) + " ' " , " " ) }
-- ${IF(sPeriod=4,"AND to_char(stat_date,'yyyy-MM-dd') <='"+ yeardelta(sEnd_date,-1) +"'","")}
2024-11-27 09:03:05 +08:00
2024-11-25 21:23:57 +08:00
UNION
select
' 目标 ' time_type
, sum ( amt_bu ) AS amt
2024-11-27 09:03:05 +08:00
from dm . v_sal_indicator_bu a
2024-11-25 21:23:57 +08:00
where $ { switch ( sPeriod
, 1 , " indi_type IN('年度目标_事业部_费用发货','年度目标_事业部_销售发货') "
, 2 , " indi_type IN('季度目标_事业部_费用发货','季度目标_事业部_销售发货') "
, 3 , " indi_type IN('月度目标_事业部_费用发货','月度目标_事业部_销售发货') "
, 4 , " indi_type IN('不存在') " ) }
2024-11-27 09:03:05 +08:00
AND exists (
select
1
from
dm . dm_bi_user_permisson
where
bi_user = ' ${fine_username} '
and per_model IN ( ' all ' , ' sal ' )
and (
is_all = 1
OR per_code = a . bu_code -- 事业部
-- OR per_code = a.region_code -- 大区
-- OR per_code = a.office_code -- 办事处
-- OR per_code = a.city_unit_code -- 城市单元
)
)
-- 年
$ { IF ( sPeriod = 1 , " AND to_char(stat_date,'yyyy') =' " + FORMAT ( sStart_date , ' yyyy ' ) + " ' " , " " ) }
-- 季
2024-11-27 17:06:11 +08:00
$ { IF ( sPeriod = 2 , " AND CONCAT(to_char(stat_date,'yyyy'),EXTRACT(QUARTER FROM stat_date)) =' " + CONCATENATE ( left ( sStart_date , 4 ) , roundup ( month ( sStart_date ) / 3 ) ) + " ' " , " " ) }
-- ${IF(sPeriod=2,"AND to_char(stat_date,'yyyy-MM-dd') <='"+ sEnd_date +"'","")}
2024-11-27 09:03:05 +08:00
-- 月
2024-11-27 17:06:11 +08:00
$ { IF ( sPeriod = 3 , " AND to_char(stat_date,'yyyy-MM') =' " + FORMAT ( sStart_date , ' yyyy-MM ' ) + " ' " , " " ) }
-- ${IF(sPeriod=3,"AND to_char(stat_date,'yyyy-MM-dd') <='"+ sEnd_date +"'","")}
2024-11-27 09:03:05 +08:00
-- 日
2024-11-27 17:06:11 +08:00
$ { IF ( sPeriod = 4 , " AND to_char(stat_date,'yyyy-MM-dd') =' " + sStart_date + " ' " , " " ) }
-- ${IF(sPeriod=4,"AND to_char(stat_date,'yyyy-MM-dd') <='"+ sEnd_date +"'","")}
2024-11-27 09:03:05 +08:00
2024-12-10 13:57:49 +08:00
-- ======================================
-- 经销商库存
-- ======================================
2024-12-12 14:10:32 +08:00
-- 要取时点值
2024-12-10 13:57:49 +08:00
with base_data AS
(
select
indi_type
, stat_date
, sum ( amt_bu ) AS amt
from dm . v_sal_indicator_bu a
where ( indi_type = ' 实物库存 ' OR indi_type = ' 在途库存 ' OR indi_type = ' 融资未填仓 '
OR indi_type = ' 已填仓未下单 ' OR indi_type = ' 未下单往来余额 ' OR indi_type = ' 费用余额 ' OR indi_type = ' 未发货 ' )
AND EXISTS (
SELECT
1
FROM
dm . dm_bi_user_permisson
WHERE
bi_user = ' ${fine_username} ' -- 替换为fine_username
AND per_model IN ( ' all ' , ' sal ' )
AND (
is_all = 1
OR per_code = a . bu_code
)
)
GROUP BY
indi_type , stat_date
)
SELECT
SUM ( amt ) AS amt -- 库存总量
, SUM ( CASE WHEN indi_type = ' 在途库存 ' THEN amt ELSE 0 END ) AS onway_amt -- 在途库存
, SUM ( CASE WHEN indi_type = ' 未发货 ' THEN amt ELSE 0 END ) AS not_ship_amt -- 未发货帐余
, SUM ( CASE WHEN indi_type = ' 融资未填仓 ' THEN amt ELSE 0 END ) AS not_fill_amt -- 融资未填仓
FROM base_data
WHERE 1 = 1
-- 年
2024-12-12 14:10:32 +08:00
-- ${IF(sPeriod=1,"AND to_char(stat_date,'yyyy') ='"+ FORMAT(sStart_date,'yyyy') +"'","")}
-- ${IF(sPeriod=1,"AND to_char(stat_date,'yyyy-MM-dd') = '"+ FORMAT(ENDOFMONTH(sStart_date),'yyyy-MM-dd') +"'","")}
2024-12-10 13:57:49 +08:00
-- 季
2024-12-12 14:10:32 +08:00
-- ${IF(sPeriod=2,"AND CONCAT(to_char(stat_date,'yyyy'),EXTRACT(QUARTER FROM stat_date)) ='"+ CONCATENATE(left(sStart_date,4),roundup(month(sStart_date)/3)) +"'","")}
-- ${IF(sPeriod=2,"AND to_char(stat_date,'yyyy-MM-dd') <= '"+ FORMAT(sStart_date,'yyyy-MM-dd') +"'","")}
2024-12-10 13:57:49 +08:00
-- ${IF(sPeriod=2,"AND to_char(stat_date,'yyyy-MM-dd') <='"+ sEnd_date +"'","")}
-- 月
2024-12-12 14:10:32 +08:00
-- ${IF(sPeriod=3,"AND to_char(stat_date,'yyyy-MM') ='"+ FORMAT(sStart_date,'yyyy-MM') +"'","")}
$ { IF ( len ( sStart_date ) > 0 , " AND to_char(stat_date,'yyyy-MM-dd') = ' " + FORMAT ( DATEINMONTH ( sStart_date , 1 ) , ' yyyy-MM-dd ' ) + " ' " , " " ) }
2024-12-10 13:57:49 +08:00
-- ${IF(sPeriod=3,"AND to_char(stat_date,'yyyy-MM-dd') <='"+ sEnd_date +"'","")}
-- 日
-- ${IF(sPeriod=4,"AND to_char(stat_date,'yyyy-MM-dd') ='"+ sStart_date +"'","")}
-- ${IF(sPeriod=4,"AND to_char(stat_date,'yyyy-MM-dd') <='"+ sEnd_date +"'","")}
2024-11-27 09:03:05 +08:00
-- ======================================
-- 财务资金状况
-- ======================================
-- 资金计划与执行
select
2024-11-29 18:04:02 +08:00
' 当期 ' AS time_type
, ' 收入 ' AS fee_type -- 收支方向 收入/支出
, SUM ( CASE WHEN trade_direction = ' 收入 ' THEN amt ELSE 0 END ) / 10000 AS amt
2024-11-27 09:03:05 +08:00
from dm . dm_fim_fud_indicator a
where corp_code not in ( ' 0101 ' , ' 0103 ' , ' 0102 ' )
2024-11-29 18:04:02 +08:00
AND trade_direction = ' 收入 '
AND indi_type = ' 执行 '
AND plan_item_name NOT IN ( ' 内部调拨收入 ' , ' 取得贷款 ' )
AND owner_bank_account < > ' 22195101040012008 '
2024-11-27 09:03:05 +08:00
-- 权限控制
2024-11-29 18:04:02 +08:00
AND exists (
2024-11-27 09:03:05 +08:00
select
2024-11-29 18:04:02 +08:00
1
2024-11-27 09:03:05 +08:00
from
dm . dm_bi_user_permisson
where
bi_user = ' ${fine_username} '
and per_model IN ( ' all ' , ' sal ' )
and (
is_all = 1
OR per_code = a . corp_code -- 事业部
-- OR per_code = a.region_code -- 大区
-- OR per_code = a.office_code -- 办事处
-- OR per_code = a.city_unit_code -- 城市单元
)
)
-- 年
2024-11-29 18:04:02 +08:00
$ { IF ( sPeriod_fin = 1 , " AND to_char(stat_date,'yyyy') =' " + FORMAT ( sStart_mon , ' yyyy ' ) + " ' " , " " ) }
$ { IF ( sPeriod_fin = 1 , " AND to_char(stat_date,'yyyy-MM') <= ' " + FORMAT ( sStart_mon , ' yyyy-MM ' ) + " ' " , " " ) }
2024-11-27 09:03:05 +08:00
-- 月
2024-11-29 18:04:02 +08:00
$ { IF ( sPeriod_fin = 2 , " AND to_char(stat_date,'yyyy-MM') = ' " + FORMAT ( sStart_mon , ' yyyy-MM ' ) + " ' " , " " ) }
-- ${IF(sPeriod_fin=2,"AND to_char(stat_date,'yyyy-MM') <= '"+ FORMAT(sStart_date_fin,'yyyy-MM-dd') +"'","")}
2024-11-27 09:03:05 +08:00
-- 日
2024-11-29 18:04:02 +08:00
-- ${IF(sPeriod_fin=3,"AND to_char(stat_date,'yyyy-MM-dd') >= '"+ FORMAT(sStart_date_fin,'yyyy-MM-dd') +"'","")}
-- ${IF(sPeriod_fin=3,"AND to_char(stat_date,'yyyy-MM-dd') <= '"+ FORMAT(sStart_date_fin,'yyyy-MM-dd') +"'","")}
2024-11-27 09:03:05 +08:00
UNION ALL
select
2024-11-29 18:04:02 +08:00
' 当期 ' AS time_type
, ' 支出 ' AS fee_type -- 收支方向 收入/支出
, SUM ( CASE WHEN trade_direction = ' 支出 ' THEN amt ELSE 0 END ) / 10000 AS amt
2024-11-27 09:03:05 +08:00
from dm . dm_fim_fud_indicator a
where corp_code not in ( ' 0101 ' , ' 0103 ' , ' 0102 ' )
AND trade_direction = ' 支出 '
AND indi_type = ' 执行 '
AND plan_item_name NOT IN ( ' 内部调拨支出 ' , ' 偿还贷款 ' )
AND owner_bank_account < > ' 22195101040012008 '
-- 权限控制
AND exists (
select
2024-11-29 18:04:02 +08:00
1
2024-11-27 09:03:05 +08:00
from
dm . dm_bi_user_permisson
where
bi_user = ' ${fine_username} '
and per_model IN ( ' all ' , ' sal ' )
and (
is_all = 1
OR per_code = a . corp_code -- 事业部
-- OR per_code = a.region_code -- 大区
-- OR per_code = a.office_code -- 办事处
-- OR per_code = a.city_unit_code -- 城市单元
)
)
-- 年
2024-11-29 18:04:02 +08:00
$ { IF ( sPeriod_fin = 1 , " AND to_char(stat_date,'yyyy') =' " + FORMAT ( sStart_mon , ' yyyy ' ) + " ' " , " " ) }
$ { IF ( sPeriod_fin = 1 , " AND to_char(stat_date,'yyyy-MM') <=' " + FORMAT ( sStart_mon , ' yyyy-MM ' ) + " ' " , " " ) }
2024-11-27 09:03:05 +08:00
-- 月
2024-11-29 18:04:02 +08:00
$ { IF ( sPeriod_fin = 2 , " AND to_char(stat_date,'yyyy-MM') =' " + FORMAT ( sStart_mon , ' yyyy-MM ' ) + " ' " , " " ) }
-- ${IF(sPeriod_fin=2,"AND to_char(stat_date,'yyyy-MM-dd') <= '"+ FORMAT(sStart_date_fin,'yyyy-MM-dd') +"'","")}
2024-11-27 09:03:05 +08:00
-- 日
2024-11-29 18:04:02 +08:00
-- ${IF(sPeriod_fin=3,"AND to_char(stat_date,'yyyy-MM-dd') >='"+ FORMAT(sStart_date_fin,'yyyy-MM-dd') +"'","")}
-- ${IF(sPeriod_fin=3,"AND to_char(stat_date,'yyyy-MM-dd') <='"+ FORMAT(sStart_date_fin,'yyyy-MM-dd') +"'","")}
2024-11-27 09:03:05 +08:00
UNION ALL
-- 月初资金余额要取上月的月初的, 年初要取上年末12月份的
select
2024-11-29 18:04:02 +08:00
' 当期 ' AS time_type
, ' 余额 ' AS fee_type
, sum ( amt ) / 10000 AS AMT
2024-11-27 09:03:05 +08:00
from dm . dm_fim_fud_balance a
where corp_code not in ( ' 0101 ' , ' 0103 ' , ' 0102 ' )
2024-12-08 21:18:00 +08:00
-- AND owner_bank_account NOT IN ('20351059900100000512291'
-- ,'22195101040012008'
-- ,'信用证保证金'
-- ,'22195162750000012'
-- ,'22195162650000017'
-- ,'22195162350000014'
-- ,'22195162050000015'
-- ,'951018033000447470'
-- ,'22195162950000025')
AND a . is_usable = 1
2024-11-27 09:03:05 +08:00
-- 权限控制
AND exists (
select
2024-11-29 18:04:02 +08:00
1
2024-11-27 09:03:05 +08:00
from
dm . dm_bi_user_permisson
where
bi_user = ' ${fine_username} '
and per_model IN ( ' all ' , ' sal ' )
and (
is_all = 1
OR per_code = a . corp_code -- 事业部
-- OR per_code = a.region_code -- 大区
-- OR per_code = a.office_code -- 办事处
-- OR per_code = a.city_unit_code -- 城市单元
)
)
-- 年
2024-12-08 21:18:00 +08:00
-- ${IF(sPeriod_fin=1,"AND to_char(stat_date,'yyyy') ='"+ FORMAT(sStart_mon,'yyyy') +"'","")}
$ { IF ( sPeriod_fin = 1 , " AND to_char(stat_date,'yyyy-MM') =' " + FORMAT ( sStart_mon , ' yyyy-MM ' ) + " ' " , " " ) }
2024-11-29 18:04:02 +08:00
2024-11-27 09:03:05 +08:00
-- 月
2024-12-08 21:18:00 +08:00
$ { IF ( sPeriod_fin = 2 , " AND to_char(stat_date,'yyyy-MM') =' " + FORMAT ( sStart_mon , ' yyyy-MM ' ) + " ' " , " " ) }
2024-11-29 18:04:02 +08:00
-- ${IF(sPeriod_fin=2,"AND to_char(stat_date,'yyyy-MM-dd') <= '"+ FORMAT(MONTHDELTA(sStart_date_fin,-1),'yyyy-MM-dd') +"'","")}
2024-11-27 09:03:05 +08:00
-- 日
2024-11-29 18:04:02 +08:00
-- ${IF(sPeriod_fin=3,"AND to_char(stat_date,'yyyy-MM-dd') >='"+ FORMAT(MONTHDELTA(sStart_date_fin,-1),'yyyy-MM-dd') +"'","")}
-- ${IF(sPeriod_fin=3,"AND to_char(stat_date,'yyyy-MM-dd') <='"+ FORMAT(MONTHDELTA(sStart_date_fin,-1),'yyyy-MM-dd') +"'","")}
2024-11-27 09:03:05 +08:00
UNION ALL
-- 资金计划与执行
select
' 上期 ' AS time_type
, ' 收入 ' AS fee_type -- 收支方向 收入/支出
, SUM ( CASE WHEN trade_direction = ' 收入 ' THEN amt ELSE 0 END ) / 10000 AS amt
from dm . dm_fim_fud_indicator a
where corp_code not in ( ' 0101 ' , ' 0103 ' , ' 0102 ' )
AND trade_direction = ' 收入 '
AND indi_type = ' 执行 '
AND plan_item_name NOT IN ( ' 内部调拨收入 ' , ' 取得贷款 ' )
AND owner_bank_account < > ' 22195101040012008 '
-- 权限控制
AND exists (
select
1
from
dm . dm_bi_user_permisson
where
bi_user = ' ${fine_username} '
and per_model IN ( ' all ' , ' sal ' )
and (
is_all = 1
OR per_code = a . corp_code -- 事业部
-- OR per_code = a.region_code -- 大区
-- OR per_code = a.office_code -- 办事处
-- OR per_code = a.city_unit_code -- 城市单元
)
)
-- 上年
2024-11-29 18:04:02 +08:00
$ { IF ( sPeriod_fin = 1 , " AND to_char(stat_date,'yyyy') =' " + FORMAT ( YEARDELTA ( sStart_mon , - 1 ) , ' yyyy ' ) + " ' " , " " ) }
$ { IF ( sPeriod_fin = 1 , " AND to_char(stat_date,'yyyy-MM') <=' " + FORMAT ( yeardelta ( sStart_mon , - 1 ) , ' yyyy-MM ' ) + " ' " , " " ) }
2024-11-27 09:03:05 +08:00
-- 上月
2024-11-29 18:04:02 +08:00
$ { IF ( sPeriod_fin = 2 , " AND to_char(stat_date,'yyyy-MM') = ' " + FORMAT ( MONTHDELTA ( sStart_mon , - 1 ) , ' yyyy-MM ' ) + " ' " , " " ) }
-- ${IF(sPeriod_fin=2,"AND to_char(stat_date,'yyyy-MM-dd') <='"+ FORMAT(MONTHDELTA(sStart_date_fin,-1),'yyyy-MM-dd') +"'","")}
2024-11-27 09:03:05 +08:00
-- 昨日
2024-11-29 18:04:02 +08:00
-- ${IF(sPeriod_fin=3,"AND to_char(stat_date,'yyyy-MM-dd') >= '"+ FORMAT(DATEDELTA(sStart_date_fin,-1),'yyyy-MM-dd') +"'","")}
-- ${IF(sPeriod_fin=3,"AND to_char(stat_date,'yyyy-MM-dd') <= '"+ FORMAT(DATEDELTA(sStart_date_fin,-1),'yyyy-MM-dd') +"'","")}
2024-11-27 09:03:05 +08:00
UNION ALL
select
' 上期 ' AS time_type
, ' 支出 ' AS fee_type -- 收支方向 收入/支出
, SUM ( CASE WHEN trade_direction = ' 支出 ' THEN amt ELSE 0 END ) / 10000 AS amt
from dm . dm_fim_fud_indicator a
where corp_code not in ( ' 0101 ' , ' 0103 ' , ' 0102 ' )
AND trade_direction = ' 支出 '
AND indi_type = ' 执行 '
AND plan_item_name NOT IN ( ' 内部调拨支出 ' , ' 偿还贷款 ' )
AND owner_bank_account < > ' 22195101040012008 '
-- 权限控制
AND exists (
select
1
from
dm . dm_bi_user_permisson
where
bi_user = ' ${fine_username} '
and per_model IN ( ' all ' , ' sal ' )
and (
is_all = 1
OR per_code = a . corp_code -- 事业部
-- OR per_code = a.region_code -- 大区
-- OR per_code = a.office_code -- 办事处
-- OR per_code = a.city_unit_code -- 城市单元
)
)
-- 上年
2024-11-29 18:04:02 +08:00
$ { IF ( sPeriod_fin = 1 , " AND to_char(stat_date,'yyyy') =' " + FORMAT ( YEARDELTA ( sStart_mon , - 1 ) , ' yyyy ' ) + " ' " , " " ) }
$ { IF ( sPeriod_fin = 1 , " AND to_char(stat_date,'yyyy-MM') <=' " + FORMAT ( yeardelta ( sStart_mon , - 1 ) , ' yyyy-MM ' ) + " ' " , " " ) }
2024-11-27 09:03:05 +08:00
-- 上月
2024-11-29 18:04:02 +08:00
$ { IF ( sPeriod_fin = 2 , " AND to_char(stat_date,'yyyy-MM') = ' " + FORMAT ( MONTHDELTA ( sStart_mon , - 1 ) , ' yyyy-MM ' ) + " ' " , " " ) }
-- ${IF(sPeriod_fin=2,"AND to_char(stat_date,'yyyy-MM-dd') <='"+ FORMAT(MONTHDELTA(sStart_date_fin,-1),'yyyy-MM-dd') +"'","")}
2024-11-27 09:03:05 +08:00
-- 昨日
2024-11-29 18:04:02 +08:00
-- ${IF(sPeriod_fin=3,"AND to_char(stat_date,'yyyy-MM-dd') >= '"+ FORMAT(DATEDELTA(sStart_date_fin,-1),'yyyy-MM-dd') +"'","")}
-- ${IF(sPeriod_fin=3,"AND to_char(stat_date,'yyyy-MM-dd') <= '"+ FORMAT(DATEDELTA(sStart_date_fin,-1),'yyyy-MM-dd') +"'","")}
2024-11-27 09:03:05 +08:00
UNION ALL
-- 月初资金余额要取上月的月初的, 年初要取上年末12月份的
select
' 上期 ' AS time_type
, ' 余额 ' AS fee_type
, sum ( amt ) / 10000 AS AMT
from dm . dm_fim_fud_balance a
where corp_code not in ( ' 0101 ' , ' 0103 ' , ' 0102 ' )
2024-12-08 21:18:00 +08:00
-- AND owner_bank_account NOT IN ('20351059900100000512291'
-- ,'22195101040012008'
-- ,'信用证保证金'
-- ,'22195162750000012'
-- ,'22195162650000017'
-- ,'22195162350000014'
-- ,'22195162050000015'
-- ,'951018033000447470'
-- ,'22195162950000025')
2024-11-27 09:03:05 +08:00
-- 权限控制
AND exists (
select
1
from
dm . dm_bi_user_permisson
where
bi_user = ' ${fine_username} '
and per_model IN ( ' all ' , ' sal ' )
and (
is_all = 1
OR per_code = a . corp_code -- 事业部
-- OR per_code = a.region_code -- 大区
-- OR per_code = a.office_code -- 办事处
-- OR per_code = a.city_unit_code -- 城市单元
)
)
2024-12-08 21:18:00 +08:00
AND a . is_usable = 1
2024-11-27 09:03:05 +08:00
-- 上年
2024-12-08 21:18:00 +08:00
-- ${IF(sPeriod_fin=1,"AND to_char(stat_date,'yyyy') ='"+ FORMAT(YEARDELTA(sStart_mon,-1),'yyyy') +"'","")}
$ { IF ( sPeriod_fin = 1 , " AND to_char(stat_date,'yyyy-MM') =' " + FORMAT ( YEARDELTA ( MONTHDELTA ( sStart_mon , - 1 ) , - 1 ) , ' yyyy-MM ' ) + " ' " , " " ) }
2024-11-29 18:04:02 +08:00
2024-11-27 09:03:05 +08:00
-- 上月
2024-12-08 21:18:00 +08:00
$ { IF ( sPeriod_fin = 2 , " AND to_char(stat_date,'yyyy-MM') =' " + FORMAT ( MONTHDELTA ( sStart_mon , - 1 ) , ' yyyy-MM ' ) + " ' " , " " ) }
2024-11-29 18:04:02 +08:00
-- ${IF(sPeriod_fin=2,"AND to_char(stat_date,'yyyy-MM-dd') <='"+ FORMAT(MONTHDELTA(sStart_date_fin,-2),'yyyy-MM-dd') +"'","")}
2024-11-27 09:03:05 +08:00
2024-11-29 18:04:02 +08:00
-- 昨日
2024-11-27 09:03:05 +08:00
2024-11-29 18:04:02 +08:00
-- ${IF(sPeriod_fin=3,"AND to_char(stat_date,'yyyy-MM-dd') >= '"+ FORMAT(DATEDELTA(MONTHDELTA(sStart_date_fin,-1),-1),'yyyy-MM-dd') +"'","")}
-- ${IF(sPeriod_fin=3,"AND to_char(stat_date,'yyyy-MM-dd') <= '"+ FORMAT(DATEDELTA(MONTHDELTA(sStart_date_fin,-1),-1),'yyyy-MM-dd') +"'","")}
2024-11-27 09:03:05 +08:00
-- ======================================
-- 财务资金月度
-- ======================================
SELECT
2024-11-29 18:04:02 +08:00
t . stat_date AS stat_date
, to_char ( t . stat_date , ' MM ' ) as month
, CASE WHEN t . fee_type = ' 收入 ' THEN amt ELSE 0 END AS revenue_fee -- 收入
, CASE WHEN t . fee_type = ' 支出 ' THEN amt ELSE 0 END AS expend_fee -- 支出
, CASE WHEN t . fee_type = ' 余额 ' THEN amt ELSE 0 END AS balance_fee -- 余额
2024-11-27 17:06:11 +08:00
FROM ( SELECT stat_date
, ' 收入 ' AS fee_type -- 收支方向 收入/支出
, SUM ( CASE WHEN trade_direction = ' 收入 ' THEN amt ELSE 0 END ) / 10000 AS amt
FROM dm . dm_fim_fud_indicator a
WHERE corp_code NOT IN ( ' 0101 ' , ' 0103 ' , ' 0102 ' )
AND trade_direction = ' 收入 '
AND indi_type = ' 执行 '
AND plan_item_name NOT IN ( ' 内部调拨收入 ' , ' 取得贷款 ' )
AND owner_bank_account < > ' 22195101040012008 '
2024-11-27 09:03:05 +08:00
-- 权限控制
2024-11-27 17:06:11 +08:00
AND exists ( select 1
FROM
2024-11-27 09:03:05 +08:00
dm . dm_bi_user_permisson
2024-11-27 17:06:11 +08:00
WHERE
2024-11-27 09:03:05 +08:00
bi_user = ' ${fine_username} '
2024-11-27 17:06:11 +08:00
AND per_model IN ( ' all ' , ' sal ' )
AND (
2024-11-27 09:03:05 +08:00
is_all = 1
OR per_code = a . corp_code -- 事业部
-- OR per_code = a.region_code -- 大区
-- OR per_code = a.office_code -- 办事处
-- OR per_code = a.city_unit_code -- 城市单元
)
)
2024-11-29 18:04:02 +08:00
$ { IF ( len ( sStart_mon ) > 0 , " AND to_char(stat_date,'yyyy') =' " + FORMAT ( sStart_mon , ' yyyy ' ) + " ' " , " " ) }
2024-11-27 17:06:11 +08:00
-- 年
-- ${IF(sPeriod_fin=1,"AND to_char(stat_date,'yyyy') ='"+ FORMAT(sStart_date_fin,'yyyy') +"'","")}
2024-11-27 09:03:05 +08:00
-- 月
-- ${IF(sPeriod_fin=2,"AND to_char(stat_date,'yyyy-MM') = '"+ FORMAT(sStart_date_fin,'yyyy-MM') +"'","")}
-- 日
-- ${IF(sPeriod_fin=3,"AND to_char(stat_date,'yyyy-MM-dd') >= '"+ sStart_date_fin +"'","")}
-- ${IF(sPeriod_fin=4,"AND to_char(stat_date,'yyyy-MM-dd') <= '"+ sStart_date_fin +"'","")}
2024-11-27 17:06:11 +08:00
GROUP BY stat_date
UNION ALL
SELECT stat_date
, ' 支出 ' AS fee_type -- 收支方向 收入/支出
, SUM ( CASE WHEN trade_direction = ' 支出 ' THEN amt ELSE 0 END ) / 10000 AS amt
FROM dm . dm_fim_fud_indicator a
WHERE corp_code NOT IN ( ' 0101 ' , ' 0103 ' , ' 0102 ' )
AND trade_direction = ' 支出 '
AND indi_type = ' 执行 '
AND plan_item_name NOT IN ( ' 内部调拨支出 ' , ' 偿还贷款 ' )
AND owner_bank_account < > ' 22195101040012008 '
2024-11-27 09:03:05 +08:00
-- 权限控制
2024-11-27 17:06:11 +08:00
AND exists ( select 1
FROM
2024-11-27 09:03:05 +08:00
dm . dm_bi_user_permisson
2024-11-27 17:06:11 +08:00
WHERE
2024-11-27 09:03:05 +08:00
bi_user = ' ${fine_username} '
2024-11-27 17:06:11 +08:00
AND per_model IN ( ' all ' , ' sal ' )
AND (
2024-11-27 09:03:05 +08:00
is_all = 1
OR per_code = a . corp_code -- 事业部
-- OR per_code = a.region_code -- 大区
-- OR per_code = a.office_code -- 办事处
-- OR per_code = a.city_unit_code -- 城市单元
)
)
2024-11-29 18:04:02 +08:00
$ { IF ( len ( sStart_mon ) > 0 , " AND to_char(stat_date,'yyyy') =' " + FORMAT ( sStart_mon , ' yyyy ' ) + " ' " , " " ) }
2024-11-27 17:06:11 +08:00
-- 年
-- ${IF(sPeriod_fin=1,"AND to_char(stat_date,'yyyy') ='"+ FORMAT(sStart_date_fin,'yyyy') +"'","")}
2024-11-27 09:03:05 +08:00
-- 月
-- ${IF(sPeriod_fin=2,"AND to_char(stat_date,'yyyy-MM') ='"+ FORMAT(sStart_date_fin,'yyyy-MM') +"'","")}
-- 日
-- ${IF(sPeriod_fin=3,"AND to_char(stat_date,'yyyy-MM-dd') >='"+ sStart_date_fin +"'","")}
-- ${IF(sPeriod_fin=4,"AND to_char(stat_date,'yyyy-MM-dd') <='"+ sStart_date_fin +"'","")}
2024-11-27 17:06:11 +08:00
GROUP BY stat_date
UNION ALL
2024-11-27 09:03:05 +08:00
-- 月初资金余额要取上月的月初的, 年初要取上年末12月份的
2024-11-27 17:06:11 +08:00
SELECT
2024-12-08 21:18:00 +08:00
stat_date AS stat_date
2024-11-29 18:04:02 +08:00
, ' 余额 ' AS fee_type
2024-12-08 21:18:00 +08:00
, SUM ( amt ) / 10000 AS AMT
FROM dm . dm_fim_fud_balance a
WHERE corp_code NOT IN ( ' 0101 ' , ' 0103 ' , ' 0102 ' )
-- AND owner_bank_account NOT IN
-- ('20351059900100000512291', '22195101040012008', '信用证保证金', '22195162750000012', '22195162650000017',
-- '22195162350000014', '22195162050000015', '951018033000447470','22195162950000025')
AND a . is_usable = 1
-- 权限控制
AND EXISTS ( SELECT 1
FROM dm . dm_bi_user_permisson
WHERE bi_user = ' ${fine_username} '
AND per_model IN ( ' all ' , ' fim ' )
AND (
is_all = 1
OR per_code = a . corp_code -- 事业部
) )
$ { IF ( len ( sStart_mon ) > 0 , " AND to_char(a.stat_date,'yyyy') =' " + FORMAT ( sStart_mon , ' yyyy ' ) + " ' " , " " ) }
GROUP BY stat_date
2024-11-27 17:06:11 +08:00
) t
2024-12-08 21:18:00 +08:00
$ { IF ( len ( sStart_mon ) > 0 , " where to_char(t.stat_date,'yyyy-MM') <= ' " + FORMAT ( sStart_mon , ' yyyy-MM ' ) + " ' " , " " ) }
2024-11-27 17:06:11 +08:00
ORDER BY to_char ( t . stat_date , ' MM ' )