我的解决方案看起来容易出问题,但对我而言却不是,我对这个问题感到失望,我希望你们能帮助我:
第一个问题是它将不同的值作为一个平衡点从分析账户到具有累积方式的sintetic账户。例如: 帐号#1它是1.1的总和,接收1.1.10和1.1.20之和的值,依此类推。
seccound问题让我最担心,在另一个下创建一个新帐户!
如果我想在另一个帐户下创建一个帐户,如果上级帐户是分析帐户,我必须为此创建另一个帐户的due_date:一个具有相同编号且没有截止日期的sintetic和实际帐户在它下面的分析,通过在它们之间进行借记和转移来转移价值。 更好地看看是否有和帐户X - 2.1.1.20.05分析与200.00 得到另一个Y - 2.1.1.20.05 sintetic 在X上输入200.00的借方 然后创建实际账户Z - 2.1.1.20.05.01解析 和200.00的cretid
这是弄乱了平衡并尝试解决问题我已将活动和非活动的值分开但我没有得到我正在寻找的结果。
这是我的查询,位于脚本下方,用于创建和模拟我的问题。
提前致谢!
select test.account_number,test.account_type, test.id, Start, Debit, Credit , (Start +( Credit + Debit )) balance
from ( SELECT accounts.account_number,accounts.account_type, accounts.id, DATEPART(yy,entrie_date) [Year],
--DATEPART(mm,entrie_date) [Month],
SUM( CASE WHEN entrie_date < '2015-02-01' THEN VALUE ELSE 0 END) [Start],
SUM( CASE WHEN (Map.value < 0 ) and (Map.entrie_date between '2015-02-01' and '2015-02-28') THEN Map.value ELSE 0 END) [Debit],
SUM( CASE WHEN (Map.value > 0 ) and (Map.entrie_date between '2015-02-01' and '2015-02-28') THEN Map.value ELSE 0 END) [Credit]
FROM accounts
left join (select accounts.account_number,accounting_entries.value,entrie_date
from accounts
inner join accounting_entries on (accounting_entries.account_id = accounts.id)
) Map ON RTRIM(LTRIM(Map.account_number)) like RTRIM(LTRIM(accounts.account_number)) + '%'
where accounts.account_number like '2%'
GROUP BY accounts.account_number, accounts.account_type, accounts.id,DATEPART(yy,entrie_date)--,DATEPART(mm,entrie_date)
) test
order by 1,2,3
脚本
USE [测试] GO
/****** Object: Table [dbo].[accounts] Script Date: 11/16/2015 11:12:54 PM ******/
DROP TABLE [dbo].[accounts]
GO
/****** Object: Table [dbo].[accounts] Script Date: 11/16/2015 11:12:54 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[accounts](
[id] [int] IDENTITY(1,1) NOT NULL,
[account_number] [nchar](20) NULL,
[account_type] [nchar](10) NULL,
[start_date] [date] NULL,
[due_date] [date] NULL,
CONSTRAINT [PK_accounts] PRIMARY KEY CLUSTERED
(
[id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
USE [Test]
GO
/****** Object: Table [dbo].[accounting_entries] Script Date: 11/16/2015 11:13:12 PM ******/
DROP TABLE [dbo].[accounting_entries]
GO
/****** Object: Table [dbo].[accounting_entries] Script Date: 11/16/2015 11:13:12 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[accounting_entries](
[id] [int] IDENTITY(1,1) NOT NULL,
[account_id] [int] NOT NULL,
[entrie_date] [date] NOT NULL,
[value] [decimal](18, 2) NOT NULL,
CONSTRAINT [PK_accounting_entries] PRIMARY KEY CLUSTERED
(
[id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
insert into accounts(account_number, account_type, start_date, due_date)
values('1', 'SINTETIC', '2014-01-07', null)
insert into accounts(account_number, account_type, start_date, due_date)
values('1.1', 'SINTETIC', '2014-01-07', null)
insert into accounts(account_number, account_type, start_date, due_date)
values('1.1.1', 'SINTETIC', '2014-01-07', null)
insert into accounts(account_number, account_type, start_date, due_date)
values('1.1.1.10', 'SINTETIC', '2014-01-07', null)
insert into accounts(account_number, account_type, start_date, due_date)
values('1.1.1.90', 'SINTETIC', '2014-01-07', null)
insert into accounts(account_number, account_type, start_date, due_date)
values('1.1.2', 'SINTETIC', '2014-01-07', null)
insert into accounts(account_number, account_type, start_date, due_date)
values('1.1.2.30', 'ANALYTICAL', '2014-01-07', null)
insert into accounts(account_number, account_type, start_date, due_date)
values('1.1.2.92', 'ANALYTICAL', '2014-01-07', null)
insert into accounts(account_number, account_type, start_date, due_date)
values('1.1.3', 'SINTETIC', '2014-01-07', null)
insert into accounts(account_number, account_type, start_date, due_date)
values('1.1.3.10', 'ANALYTICAL', '2014-01-07', null)
insert into accounts(account_number, account_type, start_date, due_date)
values('2', 'SINTETIC', '2014-01-07', null)
insert into accounts(account_number, account_type, start_date, due_date)
values('2.1', 'SINTETIC', '2014-01-07', null)
insert into accounts(account_number, account_type, start_date, due_date)
values('2.1.1', 'SINTETIC', '2014-01-07', null)
insert into accounts(account_number, account_type, start_date, due_date)
values('2.1.1.10', 'ANALYTICAL', '2014-01-07', null)
insert into accounts(account_number, account_type, start_date, due_date)
values('2.1.1.20', 'SINTETIC', '2014-01-07', null)
insert into accounts(account_number, account_type, start_date, due_date)
values('2.1.1.20.05', 'ANALYTICAL', '2014-01-07', '2015-02-28')
insert into accounts(account_number, account_type, start_date, due_date)
values('2.1.1.20.05', 'SINTETIC', '2015-02-01', NULL)
insert into accounts(account_number, account_type, start_date, due_date)
values('2.1.1.20.05.01', 'ANALYTICAL', '2015-02-01', NULL)
INSERT INTO accounting_entries(account_id, entrie_date, value)
VALUES (7, '2015-05-01', -500.00)
INSERT INTO accounting_entries(account_id, entrie_date, value)
VALUES (8, '2015-01-01', 100.00)
INSERT INTO accounting_entries(account_id, entrie_date, value)
VALUES (8, '2015-02-01', 200.00)
INSERT INTO accounting_entries(account_id, entrie_date, value)
VALUES (5, '2015-01-01', -500.00)
INSERT INTO accounting_entries(account_id, entrie_date, value)
VALUES (10, '2015-01-01', 100.00)
INSERT INTO accounting_entries(account_id, entrie_date, value)
VALUES (10, '2015-01-01', 200.00)
INSERT INTO accounting_entries(account_id, entrie_date, value)
VALUES (10, '2015-01-01', -500.00)
INSERT INTO accounting_entries(account_id, entrie_date, value)
VALUES (14, '2015-01-01', 100.00)
INSERT INTO accounting_entries(account_id, entrie_date, value)
VALUES (14, '2015-01-01', 200.00)
INSERT INTO accounting_entries(account_id, entrie_date, value)
VALUES (14, '2015-01-01', -500.00)
INSERT INTO accounting_entries(account_id, entrie_date, value)
VALUES (16, '2015-01-01', 100.00)
INSERT INTO accounting_entries(account_id, entrie_date, value)
VALUES (16, '2015-01-01', 200.00)
INSERT INTO accounting_entries(account_id, entrie_date, value)
VALUES (7, '2015-02-05', -500.00)
INSERT INTO accounting_entries(account_id, entrie_date, value)
VALUES (8, '2015-02-02', 100.00)
INSERT INTO accounting_entries(account_id, entrie_date, value)
VALUES (8, '2015-02-02', 200.00)
INSERT INTO accounting_entries(account_id, entrie_date, value)
VALUES (8, '2015-02-02', -500.00)
INSERT INTO accounting_entries(account_id, entrie_date, value)
VALUES (10, '2015-02-02', 100.00)
INSERT INTO accounting_entries(account_id, entrie_date, value)
VALUES (10, '2015-02-02', 200.00)
INSERT INTO accounting_entries(account_id, entrie_date, value)
VALUES (10, '2015-02-02', -500.00)
INSERT INTO accounting_entries(account_id, entrie_date, value)
VALUES (14, '2015-02-02', 100.00)
INSERT INTO accounting_entries(account_id, entrie_date, value)
VALUES (14, '2015-02-02', 200.00)
INSERT INTO accounting_entries(account_id, entrie_date, value)
VALUES (14, '2015-02-02', -500.00)
INSERT INTO accounting_entries(account_id, entrie_date, value)
VALUES (16, '2015-02-02', -300.00)
INSERT INTO accounting_entries(account_id, entrie_date, value)
VALUES (18, '2015-02-02', 300.00)
INSERT INTO accounting_entries(account_id, entrie_date, value)
VALUES (18, '2015-02-02', 100.00)