langjiu-project/FineReport/销售主题/看板/首页.sql

977 lines
37 KiB
SQL
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

-- ======================================
-- 报表上新
-- ======================================
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
LIMIT 30
-- ======================================
-- 合同量
-- ======================================
select
'当期' AS time_type
,sum(ct_amt)/10000 AS amt
from dm.v_ag_sal_contract a
-- 权限控制
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 -- 城市单元
)
)
AND ct_status = '已终审'
${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 +"'","")}
UNION ALL
select
'同期' AS time_type
,sum(ct_amt)/10000 AS amt
from dm.v_ag_sal_contract a
-- 权限控制
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 -- 城市单元
)
)
AND ct_status = '已终审'
${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 +"'","")}
-- ======================================
-- 回款
-- ======================================
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(sPeriod=1,"AND to_char(stat_date,'yyyy') ='"+ FORMAT(sStart_date,'yyyy') +"'","")}
${IF(sPeriod=1,"AND to_char(stat_date,'yyyy-MM-dd') <='"+ FORMAT(sStart_date,'yyyy-MM-dd') +"'","")}
-- 季
${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') +"'","")}
-- ${IF(sPeriod=2,"AND to_char(stat_date,'yyyy-MM-dd') <='"+ sEnd_date +"'","")}
-- 月
${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') <= '"+ FORMAT(sStart_date,'yyyy-MM-dd') +"'","")}
-- ${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 +"'","")}
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(sPeriod=1,"AND to_char(stat_date,'yyyy') = '" + left(yeardelta(sStart_date,-1),4) + "'","")}
-- ${IF(sPeriod=1,"AND to_char(stat_date,'yyyy-MM-dd') <='"+ FORMAT(yeardelta(sStart_date,-1),'yyyy-MM-dd') +"'","")}
-- 去年同季
${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)) +"'","")}
-- ${IF(sPeriod=2,"AND to_char(stat_date,'yyyy-MM-dd') <='"+ FORMAT(yeardelta(sStart_date,-1),'yyyy-MM-dd') +"'","")}
-- ${IF(sPeriod=2,"AND to_char(stat_date,'yyyy-MM-dd') <='"+ yeardelta(sEnd_date,-1) +"'","")}
-- 去年同月
${IF(sPeriod=3,"AND to_char(stat_date,'yyyy-MM') ='"+ FORMAT(yeardelta(sStart_date,-1),'yyyy-MM') +"'","")}
-- ${IF(sPeriod=3,"AND to_char(stat_date,'yyyy-MM-dd') <='"+ FORMAT(yeardelta(sStart_date,-1),'yyyy-MM-dd') +"'","")}
-- ${IF(sPeriod=3,"AND to_char(stat_date,'yyyy-MM-dd') <='"+ yeardelta(sEnd_date,-1) +"'","")}
-- 去年同日
${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) +"'","")}
UNION
select
'目标' time_type,
sum(amt_bu) AS amt
from dm.v_sal_indicator_bu a
where ${switch(sPeriod
,1,"indi_type='年度目标_事业部_回款'"
,2,"indi_type='季度目标_事业部_回款'"
,3,"indi_type='月度目标_事业部_回款'"
,4,"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(sPeriod=1,"AND to_char(stat_date,'yyyy') ='"+ FORMAT(sStart_date,'yyyy') +"'","")}
-- 季
${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 +"'","")}
-- 月
${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 +"'","")}
-- 日
${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 +"'","")}
-- ======================================
-- 经销商出货
-- ======================================
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(sPeriod=1,"AND to_char(stat_date,'yyyy') ='"+ FORMAT(sStart_date,'yyyy') +"'","")}
${IF(sPeriod=1,"AND to_char(stat_date,'yyyy-MM-dd') <='"+ FORMAT(sStart_date,'yyyy-MM-dd') +"'","")}
-- 季
${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') +"'","")}
-- ${IF(sPeriod=2,"AND to_char(stat_date,'yyyy-MM-dd') <='"+ sEnd_date +"'","")}
-- 月
${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') <= '"+ FORMAT(sStart_date,'yyyy-MM-dd') +"'","")}
-- ${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 +"'","")}
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(sPeriod=1,"AND to_char(stat_date,'yyyy') = '" + left(yeardelta(sStart_date,-1),4) + "'","")}
-- ${IF(sPeriod=1,"AND to_char(stat_date,'yyyy-MM-dd') <='"+ FORMAT(yeardelta(sStart_date,-1),'yyyy-MM-dd') +"'","")}
-- 去年同季
${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)) +"'","")}
-- ${IF(sPeriod=2,"AND to_char(stat_date,'yyyy-MM-dd') <='"+ FORMAT(yeardelta(sStart_date,-1),'yyyy-MM-dd') +"'","")}
-- ${IF(sPeriod=2,"AND to_char(stat_date,'yyyy-MM-dd') <='"+ yeardelta(sEnd_date,-1) +"'","")}
-- 去年同月
${IF(sPeriod=3,"AND to_char(stat_date,'yyyy-MM') ='"+ FORMAT(yeardelta(sStart_date,-1),'yyyy-MM') +"'","")}
-- ${IF(sPeriod=3,"AND to_char(stat_date,'yyyy-MM-dd') <='"+ FORMAT(yeardelta(sStart_date,-1),'yyyy-MM-dd') +"'","")}
-- ${IF(sPeriod=3,"AND to_char(stat_date,'yyyy-MM-dd') <='"+ yeardelta(sEnd_date,-1) +"'","")}
-- 去年同日
${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) +"'","")}
UNION
select
'目标' time_type
,sum(amt_bu) AS amt
from dm.v_sal_indicator_bu a
where ${switch(sPeriod
,1,"indi_type='年度目标_事业部_出货'"
,2,"indi_type='季度目标_事业部_出货'"
,3,"indi_type='月度目标_事业部_出货'"
,4,"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(sPeriod=1,"AND to_char(stat_date,'yyyy') ='"+ FORMAT(sStart_date,'yyyy') +"'","")}
-- 季
${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 +"'","")}
-- 月
${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 +"'","")}
-- 日
${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 +"'","")}
-- ======================================
-- 发货量
-- ======================================
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(sPeriod=1,"AND to_char(stat_date,'yyyy') ='"+ FORMAT(sStart_date,'yyyy') +"'","")}
${IF(sPeriod=1,"AND to_char(stat_date,'yyyy-MM-dd') <='"+ FORMAT(sStart_date,'yyyy-MM-dd') +"'","")}
-- 季
${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') +"'","")}
-- ${IF(sPeriod=2,"AND to_char(stat_date,'yyyy-MM-dd') <='"+ sEnd_date +"'","")}
-- 月
${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') <= '"+ FORMAT(sStart_date,'yyyy-MM-dd') +"'","")}
-- ${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 +"'","")}
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(sPeriod=1,"AND to_char(stat_date,'yyyy') = '" + left(yeardelta(sStart_date,-1),4) + "'","")}
-- ${IF(sPeriod=1,"AND to_char(stat_date,'yyyy-MM-dd') <='"+ FORMAT(yeardelta(sStart_date,-1),'yyyy-MM-dd') +"'","")}
-- 去年同季
${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)) +"'","")}
-- ${IF(sPeriod=2,"AND to_char(stat_date,'yyyy-MM-dd') <='"+ FORMAT(yeardelta(sStart_date,-1),'yyyy-MM-dd') +"'","")}
-- ${IF(sPeriod=2,"AND to_char(stat_date,'yyyy-MM-dd') <='"+ yeardelta(sEnd_date,-1) +"'","")}
-- 去年同月
${IF(sPeriod=3,"AND to_char(stat_date,'yyyy-MM') ='"+ FORMAT(yeardelta(sStart_date,-1),'yyyy-MM') +"'","")}
-- ${IF(sPeriod=3,"AND to_char(stat_date,'yyyy-MM-dd') <='"+ FORMAT(yeardelta(sStart_date,-1),'yyyy-MM-dd') +"'","")}
-- ${IF(sPeriod=3,"AND to_char(stat_date,'yyyy-MM-dd') <='"+ yeardelta(sEnd_date,-1) +"'","")}
-- 去年同日
${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) +"'","")}
UNION
select
'目标' time_type
,sum(amt_bu) AS amt
from dm.v_sal_indicator_bu a
where ${switch(sPeriod
,1,"indi_type IN('年度目标_事业部_费用发货','年度目标_事业部_销售发货')"
,2,"indi_type IN('季度目标_事业部_费用发货','季度目标_事业部_销售发货')"
,3,"indi_type IN('月度目标_事业部_费用发货','月度目标_事业部_销售发货')"
,4,"indi_type IN('不存在')")}
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') +"'","")}
-- 季
${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 +"'","")}
-- 月
${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 +"'","")}
-- 日
${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 +"'","")}
-- ======================================
-- 经销商库存
-- ======================================
-- 要取时点值
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
-- 年
-- ${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') +"'","")}
-- 季
-- ${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') +"'","")}
-- ${IF(sPeriod=2,"AND to_char(stat_date,'yyyy-MM-dd') <='"+ sEnd_date +"'","")}
-- 月
-- ${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') + "'","")}
-- ${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 +"'","")}
-- ======================================
-- 财务资金状况
-- ======================================
-- 资金计划与执行
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 -- 城市单元
)
)
-- 年
${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') +"'","")}
-- 月
${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') +"'","")}
-- 日
-- ${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') +"'","")}
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 -- 城市单元
)
)
-- 年
${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') +"'","")}
-- 月
${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') +"'","")}
-- 日
-- ${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') +"'","")}
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')
-- 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','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 -- 城市单元
)
)
-- 年
-- ${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') +"'","")}
-- 月
${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(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') +"'","")}
-- ${IF(sPeriod_fin=3,"AND to_char(stat_date,'yyyy-MM-dd') <='"+ FORMAT(MONTHDELTA(sStart_date_fin,-1),'yyyy-MM-dd') +"'","")}
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 -- 城市单元
)
)
-- 上年
${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') +"'","")}
-- 上月
${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') +"'","")}
-- 昨日
-- ${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') +"'","")}
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 -- 城市单元
)
)
-- 上年
${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') +"'","")}
-- 上月
${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') +"'","")}
-- 昨日
-- ${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') +"'","")}
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')
-- AND owner_bank_account NOT IN ('20351059900100000512291'
-- ,'22195101040012008'
-- ,'信用证保证金'
-- ,'22195162750000012'
-- ,'22195162650000017'
-- ,'22195162350000014'
-- ,'22195162050000015'
-- ,'951018033000447470'
-- ,'22195162950000025')
-- 权限控制
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 -- 城市单元
)
)
AND a.is_usable = 1
-- 上年
-- ${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') +"'","")}
-- 上月
${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,-2),'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') +"'","")}
-- ${IF(sPeriod_fin=3,"AND to_char(stat_date,'yyyy-MM-dd') <= '"+ FORMAT(DATEDELTA(MONTHDELTA(sStart_date_fin,-1),-1),'yyyy-MM-dd') +"'","")}
-- ======================================
-- 财务资金月度
-- ======================================
SELECT
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 -- 余额
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'
-- 权限控制
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 -- 城市单元
)
)
${IF(len(sStart_mon) > 0,"AND to_char(stat_date,'yyyy') ='"+ FORMAT(sStart_mon,'yyyy') +"'","")}
-- 年
-- ${IF(sPeriod_fin=1,"AND to_char(stat_date,'yyyy') ='"+ FORMAT(sStart_date_fin,'yyyy') +"'","")}
-- 月
-- ${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 +"'","")}
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'
-- 权限控制
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 -- 城市单元
)
)
${IF(len(sStart_mon) > 0,"AND to_char(stat_date,'yyyy') ='"+ FORMAT(sStart_mon,'yyyy') +"'","")}
-- 年
-- ${IF(sPeriod_fin=1,"AND to_char(stat_date,'yyyy') ='"+ FORMAT(sStart_date_fin,'yyyy') +"'","")}
-- 月
-- ${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 +"'","")}
GROUP BY stat_date
UNION ALL
-- 月初资金余额要取上月的月初的年初要取上年末12月份的
SELECT
stat_date AS stat_date
, '余额' AS fee_type
,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
)t
${IF(len(sStart_mon) > 0,"where to_char(t.stat_date,'yyyy-MM') <= '"+ FORMAT(sStart_mon,'yyyy-MM') +"'","")}
ORDER BY to_char(t.stat_date,'MM')