我正在尝试使用水晶报告创建自定义损益表。我正在使用以前的一组sql脚本来修改它们以满足我的需要。 我使用2个视图,一个函数和一个存储过程来实现这一点。 (代码有点长)
vw_Transactions
USE [EUROPA_PROD_DB]
GO
/****** Object: View [dbo].[vw_Transactions] Script Date: 05/04/2012 12:51:24 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE VIEW [dbo].[vw_Transactions] AS
SELECT
TA.AcctCode,TA.AcctName,TA.GroupMask,TA.Levels,TA.ActType,TA.L1Parent,TA.L2Parent,
TA.L3Parent,TA.L4Parent,
TH.TransId,TH.TransType,TH.RefDate PostingDate,TH.DueDate,
TH.TaxDate,TD.Account,TD.Debit,TD.Credit
FROM OJDT TH
INNER JOIN JDT1 TD ON TH.TransId=TD.TransId
INNER JOIN vw_AccountDetails TA ON TD.Account=TA.AcctCode
GO
上述视图的示例结果
vw_AccountDetails - 此视图使我能够获得各种级别(即父级),如上图所示。 GroupMask列引用如下:
1 - assets
2 - liabilities
4 - turnover
5 - Cost of sales
6 - operating costs.
fn_CustomProfit_And_Loss - 功能
ALTER FUNCTION fn_CustomProfit_And_Loss
(
@BeginDate DATETIME,
@EndDate DATETIME
)
RETURNS @table TABLE(
L1Parent VARCHAR(72),
L2Parent VARCHAR(72),
Balance MONEY,
RowOrder INT
)
AS
BEGIN
SET @BeginDate=CAST(YEAR(@BeginDate) AS CHAR(4))+'-'+CAST(MONTH(@BeginDate) AS CHAR(2)) +'-'+ CAST(DAY(@BeginDate) AS CHAR(2))
SET @EndDate=CAST(YEAR(@EndDate) AS CHAR(4))+'-'+CAST(MONTH(@EndDate) AS CHAR(2)) +'-'+ CAST(DAY(@EndDate) AS CHAR(2))
INSERT INTO @table
SELECT
CASE L1Parent WHEN 'Turnover' THEN 'Income' ELSE L1Parent END L1Parent,
L2Parent,
SUM(Credit-Debit) Balance,
1 RowOrder
FROM vw_Transactions
WHERE GroupMask=4--Income
AND PostingDate BETWEEN @BeginDate AND @EndDate
GROUP BY L1Parent,L2Parent
UNION ALL
SELECT
CASE L1Parent WHEN 'Cost of Sales' THEN 'Cost Of Goods Sold' ELSE L1Parent END L1Parent,
L2Parent,
SUM(Debit-Credit) Balance,
2 RowOrder
FROM vw_Transactions
WHERE GroupMask=5--COGS
AND PostingDate BETWEEN @BeginDate AND @EndDate
GROUP BY L1Parent,L2Parent
UNION ALL
SELECT
CASE L1Parent WHEN 'Operating Costs' THEN 'Expenses' ELSE L1Parent END L1Parent,
L2Parent,
SUM(Debit-Credit) Balance,
3 RowOrder
FROM vw_Transactions
WHERE GroupMask IN(6)--Expenses
AND PostingDate BETWEEN @BeginDate AND @EndDate
GROUP BY L1Parent,L2Parent
RETURN
END
sp_CustomProfit_And_Loss - 存储过程
CREATE PROCEDURE sp_CustomProfit_And_Loss
(
@BeginDate DATETIME,
@EndDate DATETIME
)
AS
BEGIN
SELECT L1Parent,L2Parent,Balance,RowOrder
FROM fn_CustomProfit_And_Loss(@BeginDate,@EndDate)
order by RowOrder
END
我基本上需要的是拥有一个P& L语句按部门分组。即
BRANDING1 BRANDING2 PROMOS
Sales-Gross Revenue 18237 121143 91092
COST OF SALES
Direct Purchases
Labour
KEBS Levy
Marketing
Packaging Materials
Clearing
Car hire and transport
Licence Local Authority
Courier freight & postage
Travel and Accomodation
- -
如何修改我的SQL语句才能拥有P& P& L声明每个部门分组?任何帮助表示赞赏。
答案 0 :(得分:1)
它也是一个非常长的混乱。
我会尝试在我理解的范围内提供解决方案。
如果您在报告中查看,则上述解决方案有效。
如果您的要求不同,请告知我们将尝试提供帮助