在Sql中的帐户分类帐查询

时间:2014-12-07 11:57:26

标签: sql sql-server

我需要一个查询来维护我的帐户分类帐

这是我的表格

CREATE TABLE [dbo].[Accounts](
    [Account_ID] [int] primary key,
    [Account_Name] [nvarchar](80),
    [Account_Type] [nvarchar](15))

CREATE TABLE [dbo].[Purchase_Invoice](
    [Purchase_ID] [int] primary key,
    [Purchase_Date] [datetime],
    [Account_ID] [int],
    [Purchase_SalesTax] [int],
    [Purchase_ExtraTax] [int],
    [Purchase_FutherTax] [int],
    [Purchase_Billno] [nvarchar](50),
    [Purchase_Amount] [decimal](9, 2))

CREATE TABLE [dbo].[Sale_Invoice](
    [Invoice_ID] [int] primary key,
    [Invoice_Date] [datetime],
    [Account_ID] [int],
    [Invoice_SalesTax] [int],
    [Invoice_ExtraTax] [int],
    [Invoice_FurtherTax] [int],
    [Invoice_Amount] [decimal](10, 2))

CREATE TABLE [dbo].[Transection](
    [Trans_ID] [int] primary key,
    [Trans_Date] [datetime],
    [Trans_Desc] [nvarchar](200))

CREATE TABLE [dbo].[DebitCredit](
    [Id] [int] IDENTITY(1,1),
    [Trans_ID] [int],
    [Account_ID] [int],
    [Amount] [decimal](9, 2),
    [Status] [varchar](5))

Insert into Accounts values(1,'Alicia', 'Customer')
Insert into Accounts values(2,'Mike', 'Supplier')
Insert into Accounts values(3,'AbcBank', 'Bank')

Insert into Purchase_Invoice values (1,'12/7/2014',2,0,0,0,'1254',5000)--Payment Debit in Supplier Mike Account

Insert into Sale_Invoice values (1,'12/7/2014',1,0,0,0,1500) --Payment Credit in Customer Alicia Account

Insert into Transection values (1,'12/7/2014','Payment Recevie From Customer')
insert into DebitCredit values (1,1,1500,'DB')--Payment Debit in Customer Alicia Account
insert into DebitCredit values (1,3,1500,'CR')--Payment Credit in Bank AbcBank Account

Insert into Transection values (2,'12/7/2014','Payment Send to Supplier')
insert into DebitCredit values (2,3,1500,'DB')--Payment Debit in Bank AbcBank Account
insert into DebitCredit values (2,2,1500,'CR')--Payment Credit in Supplier Mike Account

我从供应商那里购买了一些物品'迈克'发票金额是5000,金额是迈克的信用证然后我向Alicia销售物品1500,金额借记给艾丽西亚。

在交易表中,我从Alicia收到1500金额,现在1500金额借记到Alicia账户并收到abcbank的金额。

现在在交易表中,我向Mike发送金额1500,金额1500现在贷记给Mike账户并借记到abcbank。

结果可能是这样的

Account_Name | Debit | Credit | Balance
Mike-----------1500-----5000----3500
Alicia---------1500-----1500----0
Abcbank---------1500-----1500----0

请帮助我

由于

3 个答案:

答案 0 :(得分:0)

重做您的桌面模型以使其更有意义。我一般只需要一个Transaction表。你也应该 - 并且通常在法律上要求 - 保持一个正在运行的号码和总的后期交易。提高性能。

您似乎避免让帐户类别包含大量表格 - 每个帐户类别一个。而且您似乎认为按需计算所有内容的模型将扩展到最小示例级别之上。

答案 1 :(得分:0)

SELECT
       al.Account_id,
        SUM(CASE Status WHEN 'DB' THEN al.AMOUNT * -1 WHEN 'CR' THEN al.AMOUNT * 1 ELSE 0 END) AS TRANSACTION_AMOUNT,
        SUM(CASE WHEN al.Status = 'DB' THEN al.Amount   ELSE 0 END) AS Debit_Amount,
        SUM(CASE WHEN al.Status = 'CR' THEN al.Amount ELSE 0 END) AS Credit_Amount
    FROM  [dbo].[DebitCredit]  al
    GROUP  BY Account_id

答案 2 :(得分:0)

select Account_Name, Credit-Debit as Amount from (select distinct Account_Name,
       isnull((select sum(Amount) as Debit from [dbo].[DebitCredit] where Account_ID=DC.Account_ID and Status='DB'),0) as Debit,
       isnull((select sum(Amount) as Credit from [dbo].[DebitCredit] where Account_ID=DC.Account_ID and Status='CR'),0) as Credit
from
       [dbo].[Accounts] a join [dbo].[DebitCredit] DC on a.Account_ID=DC.Account_ID) a