我正在尝试使用水晶报表创建自定义损益表。我正在使用以前的一组 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
我基本上需要的是有一个按部门分组的损益表。IE
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 & L 语句?任何帮助表示赞赏。