[SP]
-- exec MIS_1 '2021', '01', '01'
CREATE PROCEDURE [dbo].[MIS_1]
@setYear varchar(4) = ''
, @setMonth varchar(2) = ''
, @setDay varchar(2) = ''
AS
BEGIN
SET NOCOUNT ON;
if (@setYear = '' or @setYear is null)
set @setYear = '2022'
if (@setMonth = '' or @setMonth is null)
set @setMonth = '01'
else
set @setMonth = RIGHT('00' + CAST(@setMonth AS VARCHAR), 2)
if (@setDay = '' or @setDay is null)
set @setDay = '01'
else
set @setDay = RIGHT('00' + CAST(@setDay AS VARCHAR), 2)
declare @i int
, @lastvalue int
-----------------------
-- 목표금액 세팅
-----------------------
--declare @YearGoalTable TABLE ([sYear] varchar(4), [목표금액] bigint)
create table #YearGoalTable ([sYear] varchar(4), [목표금액] bigint)
insert into #YearGoalTable
select '2017', 10000000000 union
select '2018', 15000000000 union
select '2019', 22000000000 union
select '2020', 28000000000 union
select '2021', 35000000000 union
select '2022', 40000000000 union
select '2023', 45000000000
--select * from @YearGoalTable
--declare @MonthGoalTable TABLE ([sYear] varchar(4), [sMonth] varchar(2), [목표금액] bigint)
create table #MonthGoalTable ([sYear] varchar(4), [sMonth] varchar(2), [목표금액] bigint)
declare @m_SLIMIT bigint -- 범위 시작
, @m_ELIMIT bigint -- 범위 마지막
set @m_SLIMIT = 2000000000
set @m_ELIMIT = 3100000000
-- 해당 년의 월수 값
set @lastvalue = 12
set @i = 1
while (@i <= @lastvalue)
begin
insert into #MonthGoalTable
select @setYear
, RIGHT('00' + CAST(@i AS VARCHAR), 2)
, ROUND(((@m_ELIMIT + 1) - @m_SLIMIT) * RAND() + @m_SLIMIT, 0, 1)
set @i = @i + 1
end
--declare @DayGoalTable TABLE ([sYear] varchar(4), [sMonth] varchar(2), [sDay] varchar(2), [목표금액] bigint)
create table #DayGoalTable ([sYear] varchar(4), [sMonth] varchar(2), [sDay] varchar(2), [목표금액] bigint)
declare @d_SLIMIT bigint -- 범위 시작
, @d_ELIMIT bigint -- 범위 마지막
set @d_SLIMIT = 100000000
set @d_ELIMIT = 150000000
-- 해당 월의 마지막 일자 값
set @lastvalue = convert(int,substring(convert(varchar,EOMONTH(@setYear + '-' + @setMonth + '-01')),9,2))
--SELECT substring(convert(varchar,EOMONTH('2020-09-09')),9,2) AS result
--select substring(convert(varchar,EOMONTH(@setYear + '-' + @setMonth + '-01')),9,2) AS result
-- 문자 자리수 0 SELECT RIGHT('00000' + CAST(12 AS NVARCHAR), 5);
set @i = 1
while (@i <= @lastvalue)
begin
insert into #DayGoalTable
select @setYear
, @setMonth
, RIGHT('00' + CAST(@i AS VARCHAR), 2)
, ROUND(((@d_ELIMIT + 1) - @d_SLIMIT) * RAND() + @d_SLIMIT, 0, 1)
set @i = @i + 1
end
create table #HhGoalTable ([sYear] varchar(4), [sMonth] varchar(2), [sDay] varchar(2), [sHh] varchar(2), [목표금액] bigint)
insert into #HhGoalTable
select @setYear, @setMonth, @setDay, '01', 0 union
select @setYear, @setMonth, @setDay, '02', 0 union
select @setYear, @setMonth, @setDay, '03', 0 union
select @setYear, @setMonth, @setDay, '04', 0 union
select @setYear, @setMonth, @setDay, '05', 0 union
select @setYear, @setMonth, @setDay, '06', 0 union
select @setYear, @setMonth, @setDay, '07', 0 union
select @setYear, @setMonth, @setDay, '08', 0 union
select @setYear, @setMonth, @setDay, '09', 0 union
select @setYear, @setMonth, @setDay, '10', 0 union
select @setYear, @setMonth, @setDay, '11', 0 union
select @setYear, @setMonth, @setDay, '12', 0 union
select @setYear, @setMonth, @setDay, '13', 0 union
select @setYear, @setMonth, @setDay, '14', 0 union
select @setYear, @setMonth, @setDay, '15', 0 union
select @setYear, @setMonth, @setDay, '16', 0 union
select @setYear, @setMonth, @setDay, '17', 0 union
select @setYear, @setMonth, @setDay, '18', 0 union
select @setYear, @setMonth, @setDay, '19', 0 union
select @setYear, @setMonth, @setDay, '20', 0 union
select @setYear, @setMonth, @setDay, '21', 0 union
select @setYear, @setMonth, @setDay, '22', 0 union
select @setYear, @setMonth, @setDay, '23', 0 union
select @setYear, @setMonth, @setDay, '24', 0
-----------------------
-- 년별
-----------------------
select q2.sYear as [년도]
, '' as [월]
, '' as [일]
, '' as [시간]
, isnull(ord_price, 0) as [총매출액]
, isnull(ord_price - isnull(LAG(ord_price) OVER(ORDER BY q2.sYear), 0), 0) as [차이금액]
, q2.sYear + '년' as [xLabel]
, q2.sYear as [xLabel2]
, isnull(q2.[목표금액], 0) as [목표금액]
into #YearDatas
from #YearGoalTable q2
left join (
select sYear, sum(ord_price) as ord_price
from (
select
left(oi.settle_date,4) as sYear
, substring(convert(varchar,oi.settle_date),5,2) as sMonth
, right(oi.settle_date,2) as sDay
, substring(convert(varchar,oi.settle_time),1,2) as sHh
, convert(bigint, case when (orp.ord_pnum is not null) then (op.ord_price * op.ord_cnt) - orp.price else (op.ord_price * op.ord_cnt) end) as ord_price
from TABLE_A oi nolock
join TABLE_B op on oi.ord_num = op.ord_num
left join TABLE_C orp on op.ord_pnum = orp.ord_pnum and orp.[State] = 3 and orp.[State1] = 1
where oi.settle_flag = 1
and op.ord_state in (2,4,5,6,7)
and op.prod_type = 0
) q1
group by sYear
) q3 on q2.sYear = q3.sYear
order by q2.sYear
-----------------------
-- 월별
-----------------------
select q2.sYear as [년도]
, q2.sMonth as [월]
, '' as [일]
, '' as [시간]
, isnull(ord_price, 0) as [총매출액]
, isnull(ord_price - isnull(LAG(ord_price) OVER(ORDER BY q2.sYear, q2.sMonth), 0), 0) as [차이금액]
, q2.sYear + '년 ' + q2.sMonth + '월' as [xLabel]
, q2.sYear + '.' + q2.sMonth as [xLabel2]
, isnull(q2.[목표금액], 0) as [목표금액]
into #MonthDatas
from #MonthGoalTable q2
left join (
select sYear, sMonth, sum(ord_price) as ord_price
from (
select
left(oi.settle_date,4) as sYear
, substring(convert(varchar,oi.settle_date),5,2) as sMonth
, right(oi.settle_date,2) as sDay
, substring(convert(varchar,oi.settle_time),1,2) as sHh
, convert(bigint, case when (orp.ord_pnum is not null) then (op.ord_price * op.ord_cnt) - orp.price else (op.ord_price * op.ord_cnt) end) as ord_price
from TABLE_A oi
join TABLE_B op on oi.ord_num = op.ord_num
left join TABLE_C orp on op.ord_pnum = orp.ord_pnum and orp.[State] = 3 and orp.[State1] = 1
where oi.settle_flag = 1
and op.ord_state in (2,4,5,6,7)
and op.prod_type = 0
and left(oi.settle_date,4) = @setYear
) q1
group by sYear, sMonth
) q3 on q2.sYear = q3.sYear and q2.sMonth = q3.sMonth
order by q2.sYear, q2.sMonth
-----------------------
-- 일별
-----------------------
select q2.sYear as [년도]
, q2.sMonth as [월]
, q2.sDay as [일]
, '' as [시간]
, isnull(ord_price, 0) as [총매출액]
, isnull(ord_price - isnull(LAG(ord_price) OVER(ORDER BY q2.sYear, q2.sMonth, q2.sDay), 0), 0) as [차이금액]
, q2.sYear + '년 ' + q2.sMonth + '월 ' + q2.sDay + '일' as [xLabel]
--, q2.sYear + '.' + q2.sMonth + '.' + q2.sDay as [xLabel2]
, q2.sMonth + '.' + q2.sDay as [xLabel2]
, isnull(q2.[목표금액], 0) as [목표금액]
into #DayDatas
from #DayGoalTable q2
left join (
select sYear, sMonth, sDay, sum(ord_price) as ord_price
from (
select
left(oi.settle_date,4) as sYear
, substring(convert(varchar,oi.settle_date),5,2) as sMonth
, right(oi.settle_date,2) as sDay
, substring(convert(varchar,oi.settle_time),1,2) as sHh
, convert(bigint, case when (orp.ord_pnum is not null) then (op.ord_price * op.ord_cnt) - orp.price else (op.ord_price * op.ord_cnt) end) as ord_price
from TABLE_A oi
join TABLE_B op on oi.ord_num = op.ord_num
left join TABLE_C orp on op.ord_pnum = orp.ord_pnum and orp.[State] = 3 and orp.[State1] = 1
where oi.settle_flag = 1
and op.ord_state in (2,4,5,6,7)
and op.prod_type = 0
and left(oi.settle_date,4) = @setYear
and substring(convert(varchar,oi.settle_date),5,2) = @setMonth
) q1
group by sYear, sMonth, sDay
) q3 on q2.sYear = q3.sYear and q2.sMonth = q3.sMonth and q2.sDay = q3.sDay
order by q2.sYear, q2.sMonth, q2.sDay
-----------------------
-- 시간별
-----------------------
select q2.sYear as [년도]
, q2.sMonth as [월]
, q2.sDay as [일]
, q2.sHh as [시간]
, isnull(ord_price, 0) as [총매출액]
, isnull(ord_price - isnull(LAG(ord_price) OVER(ORDER BY q2.sYear, q2.sMonth, q2.sDay, q2.sHh), 0), 0) as [차이금액]
, q2.sYear + '년 ' + q2.sMonth + '월 ' + q2.sDay + '일' + q2.sHh + '시간' as [xLabel]
--, q2.sYear + '.' + q2.sMonth + '.' + q2.sDay as [xLabel2]
, q2.sHh + '시' as [xLabel2]
, isnull(q2.[목표금액], 0) as [목표금액]
into #HhDatas
from #HhGoalTable q2
left join (
select sYear, sMonth, sDay, sHh, sum(ord_price) as ord_price
from (
select
left(oi.settle_date,4) as sYear
, substring(convert(varchar,oi.settle_date),5,2) as sMonth
, right(oi.settle_date,2) as sDay
, substring(convert(varchar,oi.settle_time),1,2) as sHh
, convert(bigint, case when (orp.ord_pnum is not null) then (op.ord_price * op.ord_cnt) - orp.price else (op.ord_price * op.ord_cnt) end) as ord_price
from TABLE_A oi
join TABLE_B op on oi.ord_num = op.ord_num
left join TABLE_C orp on op.ord_pnum = orp.ord_pnum and orp.[State] = 3 and orp.[State1] = 1
where oi.settle_flag = 1
and op.ord_state in (2,4,5,6,7)
and op.prod_type = 0
--and left(oi.settle_date,4) = @setYear
--and substring(convert(varchar,oi.settle_date),5,2) = @setMonth
--and substring(convert(varchar,oi.settle_date),7,2) = @setDay
and oi.settle_date = @setYear + @setMonth + @setDay
) q1
group by sYear, sMonth, sDay, sHh
) q3 on q2.sYear = q3.sYear and q2.sMonth = q3.sMonth and q2.sDay = q3.sDay and q2.sHh = q3.sHh
order by q2.sYear, q2.sMonth, q2.sDay, q2.sHh
--select * from #YearDatas
--select * from #MonthDatas
--select * from #DayDatas
--select * from #HhDatas order by 시간
-----------------------
-- 년 (JSON 형식)
-----------------------
select '[' + stuff((select ',"' + xLabel + '"' from #YearDatas order by [년도] for xml path('')), 1, 1, '') + ']' as y_xLabels
, '[' + stuff((select ',' + CONVERT(varchar, 총매출액) from #YearDatas order by [년도] for xml path('')), 1, 1, '') + ']' as y_datas1
, '[' + stuff((select ',' + CONVERT(varchar, 목표금액) from #YearDatas order by [년도] for xml path('')), 1, 1, '') + ']' as y_datas2
, '[' + stuff((select ',' + CONVERT(varchar, 차이금액) from #YearDatas order by [년도] for xml path('')), 1, 1, '') + ']' as y_datas3
, '[' + (select CONVERT(varchar, isnull(sum(convert(bigint, 총매출액)),0)) from #YearDatas) + ']' as y_datas1_sum
-----------------------
-- 월 (JSON 형식)
-----------------------
select '[' + stuff((select ',"' + xLabel + '"' from #MonthDatas order by [월] for xml path('')), 1, 1, '') + ']' as m_xLabels
, '[' + stuff((select ',' + CONVERT(varchar, 총매출액) from #MonthDatas order by [월] for xml path('')), 1, 1, '') + ']' as m_datas1
, '[' + stuff((select ',' + CONVERT(varchar, 목표금액) from #MonthDatas order by [월] for xml path('')), 1, 1, '') + ']' as m_datas2
, '[' + stuff((select ',' + CONVERT(varchar, 차이금액) from #MonthDatas order by [월] for xml path('')), 1, 1, '') + ']' as m_datas3
, '[' + (select CONVERT(varchar, isnull(sum(convert(bigint, 총매출액)),0)) from #MonthDatas) + ']' as m_datas1_sum
-----------------------
-- 일 (JSON 형식)
-----------------------
--select '[' + stuff((select ',"' + xLabel + '"' from #DayDatas for xml path('')), 1, 1, '') + ']' as d_xLabels
select '[' + stuff((select ',"' + xLabel2 + '"' from #DayDatas order by [일] for xml path('')), 1, 1, '') + ']' as d_xLabels
, '[' + stuff((select ',' + CONVERT(varchar, 총매출액) from #DayDatas order by [일] for xml path('')), 1, 1, '') + ']' as d_datas1
, '[' + stuff((select ',' + CONVERT(varchar, 목표금액) from #DayDatas order by [일] for xml path('')), 1, 1, '') + ']' as d_datas2
, '[' + stuff((select ',' + CONVERT(varchar, 차이금액) from #DayDatas order by [일] for xml path('')), 1, 1, '') + ']' as d_datas3
, '[' + (select CONVERT(varchar, isnull(sum(convert(bigint, 총매출액)),0)) from #DayDatas) + ']' as d_datas1_sum
-----------------------
-- 시간 (JSON 형식)
-----------------------
--select '[' + stuff((select ',"' + xLabel + '"' from #DayDatas for xml path('')), 1, 1, '') + ']' as hh_xLabels
select '[' + stuff((select ',"' + xLabel2 + '"' from #HhDatas order by [시간] for xml path('')), 1, 1, '') + ']' as hh_xLabels
, '[' + stuff((select ',' + CONVERT(varchar, 총매출액) from #HhDatas order by [시간] for xml path('')), 1, 1, '') + ']' as hh_datas1
, '[' + stuff((select ',' + CONVERT(varchar, 목표금액) from #HhDatas order by [시간] for xml path('')), 1, 1, '') + ']' as hh_datas2
, '[' + stuff((select ',' + CONVERT(varchar, 차이금액) from #HhDatas order by [시간] for xml path('')), 1, 1, '') + ']' as hh_datas3
, '[' + (select CONVERT(varchar, isnull(sum(convert(bigint, 총매출액)),0)) from #HhDatas) + ']' as hh_datas1_sum
-----------------------
-- #임시 테이블 삭제
-----------------------
if OBJECT_ID('tempdb..#MonthGoalTable') is not null drop table #MonthGoalTable
if OBJECT_ID('tempdb..#MonthGoalTable') is not null drop table #MonthGoalTable
if OBJECT_ID('tempdb..#DayGoalTable') is not null drop table #DayGoalTable
if OBJECT_ID('tempdb..#HhGoalTable') is not null drop table #HhGoalTable
if OBJECT_ID('tempdb..#YearDatas') is not null drop table #YearDatas
if OBJECT_ID('tempdb..#MonthDatas') is not null drop table #MonthDatas
if OBJECT_ID('tempdb..#DayDatas') is not null drop table #DayDatas
if OBJECT_ID('tempdb..#HhDatas') is not null drop table #HhDatas
END