从ofpr表构建报告日历年月表

时间:2012-02-02 03:12:21

标签: tsql crystal-reports sqlcmd sapb1

只需发布此内容即可供其他人使用。 下面的T-sql代码用于在forpr表中创建SAP B1中的新表以扩展日历用法。在我的情况下报告因此名称ReportCalendar使其独特。 此代码用于三个SAP B1生产系统。随意发布设计改进和想法。请注意,我在这里使用sqlcmd进行sql交互。

/* :r ReportCalendar.table.sql
   Build ReportCalendar year month table.  ofpr table is for finanical year only.

   Confirmation
   :out ReportCalendar.table.txt
   select * from ReportCalendar;
   go 
   :out stdout
*/
if exists (select name from dbo.sysobjects
           where id = object_id(N'[dbo].[ReportCalendar]') and
                 objectproperty(id, N'IsUserTable')=1)
    drop table [dbo].[ReportCalendar];
go

create table [dbo].[ReportCalendar]
    (Year                  int          not null,
         Month                 int          not null,
     MonthName             varchar(9)   not null,
     MonthShortName        varchar(3)   not null,
     MonthFrom             datetime     not null,
     MonthTo               datetime     not null,
     Quarter               int          not null,
     HalfYear              int          not null,
     FinancialYear         varchar(9)   not null,
     FinancialYearShort    varchar(5)   not null,
     FinancialYearQuarter  int          not null,
     FinancialHalfYear     int          not null
     constraint pk_year_month primary key clustered (Year, Month));
go

-- Build yearly ReportCalendar from Posting Period table.
insert into [dbo].[ReportCalendar]
select
datename(Year, f_refdate) as [Year],
datepart(Month, f_refdate) as [Month],
datename(Month, f_refdate) as [MonthName],
substring(datename(Month, f_refdate), 1, 3) as [MonthShortName],
f_refdate as [MonthFrom],
t_refdate as [MonthTo],
datename(Quarter, f_refdate) as [Quarter],
case when datename(Quarter, f_refdate) > 2 then 2 else 1 end as [HalfYear],
case when datepart(Month, f_refdate) > 6
     then (datename(Year, f_refdate) + '-' + datename(Year, dateadd(Year, 1, f_refdate)))
     else (datename(Year, dateadd(Year, -1, f_refdate)) + '-' + datename(Year, f_refdate))
     end as [FinancialYear],  -- month > 6 year+(year+1) else (year-1)+year
case when datepart(Month, f_refdate) > 6
     then (substring(datename(Year, f_refdate), 3, 2) + '-' + substring(datename(Year, dateadd(Year, 1, f_refdate)), 3, 2))
     else (substring(datename(Year, dateadd(Year, -1, f_refdate)), 3, 2) + '-' + substring(datename(Year, f_refdate), 3, 2))
     end as [FinancialYear],
case when datename(Quarter, f_refdate) > 2 then (datename(Quarter, f_refdate) + 2) - 4
     else datename(Quarter, f_refdate) + 2
     end as [Quarter],  -- add 2 to Quarter
case when datename(Quarter, f_refdate) > 2 then 1 else 2 end as [FinancialHalfYear]  -- opposite HalfYear
from [dbo].[ofpr]  -- Posting Period
order by f_refdate;
go

0 个答案:

没有答案