Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
folumike
Starting Member
24 Posts |
Posted - 2012-05-23 : 16:20:30
|
Am developing an inventory application using VB6 and MSSQL2000.I have two tables namely dbo.AStockCard01 and dbo.StockCard02.Each table has the same number of columns and names, but values.I created a view using UNION ALL in order to merge the two tables.QtyIN is the Input value (including beginning balance) while QtyOUTis the Output value or sales.I want it in such a way that those Warehouses that doesn't have forward balance for a particular item, should insert a row,and fill the fields with the same date of the available warehouse of the same item.SUMMARY1. Order by ItemID, Date12. When no forward balance(FWB), should insert a row. use the same date with the other warehouse(s) of the same ItemID. Then let QtyIN = 0, QtyOUT = 0, DocID = 'FWB', DocType = Forward Balance.See the script below:if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[vwStockCard]') and OBJECTPROPERTY(id, N'IsView') = 1)drop view [dbo].[vwStockCard]GOif exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[StockCard01]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)drop table [dbo].[StockCard01]GOif exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[StockCard02]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)drop table [dbo].[StockCard02]GOCREATE TABLE [dbo].[StockCard01] ([Date1] [datetime] NULL ,[ItemID] [nvarchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,[QtyIN] [float] NULL ,[QtyOUT] [float] NULL ,[Warehouse] [nvarchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,[WayBillID] [nvarchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,[DeliveryID] [nvarchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ) ON [PRIMARY]GOCREATE TABLE [dbo].[StockCard02] ([Date1] [datetime] NULL ,[ItemID] [nvarchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,[QtyIN] [float] NULL ,[QtyOUT] [float] NULL ,[Warehouse] [nvarchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,[WayBillID] [nvarchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,[DeliveryID] [nvarchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ) ON [PRIMARY]GOSET QUOTED_IDENTIFIER ON GOSET ANSI_NULLS ON GOCREATE VIEW vwStockCardASSelect Date1,ItemID,QtyIN,QtyOUT,(QtyIN-QtyOUT) AS Bal,Warehouse,DocID =CASEWHEN WayBillID <> ''THEN WayBillIDWHEN DeliveryID <> ''THEN DeliveryIDELSE'FWB'END,DocType = CASEWHEN WayBillID <> ''THEN 'Way Bill'WHEN DeliveryID <> ''THEN 'Delivery Notes'ELSE'Forward Balance'ENDFROM StockCard01UNION ALLSelect Date1,ItemID,QtyIN,QtyOUT,(QtyIN-QtyOUT) AS Bal,Warehouse,DocID =CASEWHEN WayBillID <> ''THEN WayBillIDWHEN DeliveryID <> ''THEN DeliveryIDELSE'FWB'END,DocType = CASEWHEN WayBillID <> ''THEN 'Way Bill'WHEN DeliveryID <> ''THEN 'Delivery Notes'ELSE'Forward Balance'ENDFROM StockCard02GOSET QUOTED_IDENTIFIER OFF GOSET ANSI_NULLS ON GOTABLE 1: StockCard01Date1 ItemID QtyIN QtyOUT Warehouse WayBillID DeliveryID03/09/2011 000001 10 0 WH1 03/09/2011 000001 40 0 WH2 03/09/2011 000001 30 0 WH3 03/09/2011 000002 100 0 WH1 04/09/2011 000001 35 0 WH1 D0000104/09/2011 000001 20 0 WH1 D0000104/09/2011 000002 50 0 WH3 D0000106/09/2011 000003 15 0 WH2 10/09/2011 000001 0 30 WH1 W00001 TABLE 2: StockCard02Date1 ItemID QtyIN QtyOUT Warehouse WayBillID DeliveryID05/10/2011 000001 20 0 WH2 D00004RESULTDate1 ItemID QtyIN QtyOUT Bal Warehouse DocID DocType03/09/2011 000001 10 0 10 WH1 FWB Forward Balance03/09/2011 000001 40 0 40 WH2 FWB Forward Balance03/09/2011 000001 30 0 30 WH3 FWB Forward Balance03/09/2011 000002 100 0 100 WH1 FWB Forward Balance04/09/2011 000001 35 0 35 WH1 D00001 Delivery Notes04/09/2011 000001 20 0 20 WH1 D00001 Delivery Notes04/09/2011 000002 50 0 50 WH3 D00001 Delivery Notes06/09/2011 000003 15 0 15 WH2 FWB Forward Balance10/09/2011 000001 0 30 -30 WH1 W00001 Way Bill05/10/2011 000001 20 0 20 WH2 D00004 Delivery NotesEXPECTED RESULTDate1 ItemID QtyIN QtyOUT Bal Warehouse DocID DocType03/09/2011 000001 10 0 10 WH1 FWB Forward Balance03/09/2011 000001 40 0 40 WH2 FWB Forward Balance03/09/2011 000001 30 0 30 WH3 FWB Forward Balance03/09/2011 000002 100 0 100 WH1 FWB Forward Balance03/09/2011 000002 0 0 0 WH2 FWB Forward Balance03/09/2011 000002 0 0 0 WH3 FWB Forward Balance04/09/2011 000001 35 0 35 WH1 D00001 Delivery Notes04/09/2011 000001 20 0 20 WH1 D00001 Delivery Notes04/09/2011 000002 50 0 50 WH3 D00001 Delivery Notes06/09/2011 000003 0 0 0 WH1 FWB Forward Balance06/09/2011 000003 15 0 15 WH2 FWB Forward Balance06/09/2011 000003 0 0 0 WH3 FWB Forward Balance10/09/2011 000001 0 30 -30 WH1 W00001 Way Bill05/10/2011 000001 20 0 20 WH2 D00004 Delivery Notes |
|
robvolk
Most Valuable Yak
15732 Posts |
Posted - 2012-05-23 : 16:29:10
|
Please do not cross post:http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=175077 |
|
|
|
|
|
|
|