商店帐户分类帐

时间:2019-05-29 10:56:00

标签: sql-server

我有四个表,表的数据库结构如下 Table Structure

    SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[tblShops](
    [ShopID] [int] IDENTITY(1,1) NOT NULL,
    [fkAreaID] [int] NOT NULL,
    [ShopName] [nvarchar](256) NULL,
    [CustomerName] [nvarchar](256) NULL,
    [PhoneNo] [varchar](50) NULL,
    [Address] [varchar](256) NULL,
    [OtherDetails] [varchar](512) NULL,
    [AmountDue] [int] NULL,
 CONSTRAINT [PK_tblShops] PRIMARY KEY CLUSTERED 
(
    [ShopID] 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
SET ANSI_PADDING OFF
GO
/****** Object:  Table [dbo].[tblAmountPaid]    Script Date: 05/29/2019 16:00:28 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[tblAmountPaid](
    [AmountID] [int] IDENTITY(1,1) NOT NULL,
    [fkShopID] [int] NULL,
    [fkSaleManID] [int] NULL,
    [fkProductID] [int] NULL,
    [Dated] [date] NULL,
    [AmountPaid] [varchar](50) NULL,
 CONSTRAINT [PK_tblAmountPaid] PRIMARY KEY CLUSTERED 
(
    [AmountID] 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
SET ANSI_PADDING OFF
GO
/****** Object:  Table [dbo].[tblInvoices]    Script Date: 05/29/2019 16:00:28 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[tblInvoices](
    [InvoiceID] [int] IDENTITY(1,1) NOT NULL,
    [fkShopID] [int] NOT NULL,
    [SaleManID] [int] NULL,
    [Dated] [date] NULL,
    [DisCount] [varchar](50) NULL,
 CONSTRAINT [PK_tblInvoices] PRIMARY KEY CLUSTERED 
(
    [InvoiceID] 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
SET ANSI_PADDING OFF
GO
/****** Object:  Table [dbo].[tblInvoiceDetail]    Script Date: 05/29/2019 16:00:28 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[tblInvoiceDetail](
    [DetailID] [int] IDENTITY(1,1) NOT NULL,
    [fkINvoiceID] [int] NULL,
    [ProductID] [int] NULL,
    [Price] [varchar](50) NULL,
    [Qty] [int] NULL,
    [PurchasePrice] [varchar](50) NULL,
 CONSTRAINT [PK_tblInvoiceDetail] PRIMARY KEY CLUSTERED 
(
    [DetailID] 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
SET ANSI_PADDING OFF
GO
/****** Object:  Default [DF_tblInvoices_DisCount]    Script Date: 05/29/2019 16:00:28 ******/
ALTER TABLE [dbo].[tblInvoices] ADD  CONSTRAINT [DF_tblInvoices_DisCount]  DEFAULT ((0)) FOR [DisCount]
GO
/****** Object:  Default [DF_tblShops_AmountDue]    Script Date: 05/29/2019 16:00:28 ******/
ALTER TABLE [dbo].[tblShops] ADD  CONSTRAINT [DF_tblShops_AmountDue]  DEFAULT ((0)) FOR [AmountDue]
GO
/****** Object:  ForeignKey [FK_tblAmountPaid_tblShops]    Script Date: 05/29/2019 16:00:28 ******/
ALTER TABLE [dbo].[tblAmountPaid]  WITH CHECK ADD  CONSTRAINT [FK_tblAmountPaid_tblShops] FOREIGN KEY([fkShopID])
REFERENCES [dbo].[tblShops] ([ShopID])
GO
ALTER TABLE [dbo].[tblAmountPaid] CHECK CONSTRAINT [FK_tblAmountPaid_tblShops]
GO
/****** Object:  ForeignKey [FK_tblInvoiceDetail_tblInvoices]    Script Date: 05/29/2019 16:00:28 ******/
ALTER TABLE [dbo].[tblInvoiceDetail]  WITH CHECK ADD  CONSTRAINT [FK_tblInvoiceDetail_tblInvoices] FOREIGN KEY([fkINvoiceID])
REFERENCES [dbo].[tblInvoices] ([InvoiceID])
GO
ALTER TABLE [dbo].[tblInvoiceDetail] CHECK CONSTRAINT [FK_tblInvoiceDetail_tblInvoices]
GO
/****** Object:  ForeignKey [FK_tblInvoices_tblShops]    Script Date: 05/29/2019 16:00:28 ******/
ALTER TABLE [dbo].[tblInvoices]  WITH CHECK ADD  CONSTRAINT [FK_tblInvoices_tblShops] FOREIGN KEY([fkShopID])
REFERENCES [dbo].[tblShops] ([ShopID])
GO
ALTER TABLE [dbo].[tblInvoices] CHECK CONSTRAINT [FK_tblInvoices_tblShops]
GO

我需要根据已付款表以及发票和发票明细表生成借项和贷项的帐户分类帐,例如

ShopID: 1 or Shop Name

InvoiceID |    Dated    | TotalDebit | TotalCredit | Balance
------------------------------------------------------------
     35   | 1-05-2019   |    10      |      5      |    5
     120  | 3-05-2019   |    20      |      0      |    25
     0    | 6-05-2019   |    0       |      10     |    15

是否有可能使用image中提到的表结构在sql中生成类似上面的输出。任何帮助将不胜感激。

更新: 以下是我使用“联合所有操作员”成功收集的示例数据

select fkinvoiceID, 'Debit' as 'Status', Dated, SUM(cast(price as float)) BillAmount from tblInvoices
Inner join tblInvoiceDetail on tblInvoices.InvoiceID = tblInvoiceDetail.fkINvoiceID where fkShopID = 160
group by tblinvoices.Dated, fkINvoiceID
    union all
    select amountID, 'Credit', Dated, Sum(Cast(amountpaid as float)) from tblAmountPaid where fkShopID = 160
    group by Dated, AmountID

从表中

VoucherNo   Dated       Status  Amount
357        2013-07-30   Credit   2000
451        2013-08-06   Debit   5220
611        2013-08-13   Debit   7044
645        2013-08-06   Credit  2000
843        2013-08-27   Debit   11708
871        2013-08-13   Credit  4220
1125       2013-08-20   Credit  7000
1253       2013-08-27   Credit  5000
1406       2013-09-24   Debit   12910
1447       2013-09-03   Credit  5000

现在,我如何才能根据借方和贷方对上述数据进行累计。希望现在您了解问题了吗?

0 个答案:

没有答案