| 
                
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 |  
                                    | folumikeStarting Member
 
 
                                        24 Posts | 
                                            
                                            |  Posted - 2012-05-23 : 16:05:23 
 |  
                                            | 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 |  |  
                                    | visakh16Very Important crosS Applying yaK Herder
 
 
                                    52326 Posts | 
                                        
                                          |  Posted - 2012-05-25 : 15:48:37 
 |  
                                          | looks like what you need is a cross join between Date1 ItemID fields and then left join result to your table to capture rest of table info if present or else fill them with 0 using isnull() or coalesce()------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |  
                                          |  |  |  
                                    | noblemfdStarting Member
 
 
                                    38 Posts | 
                                        
                                          |  Posted - 2012-05-27 : 15:05:16 
 |  
                                          | pls enlighten me more. how do i go about that quote:Originally posted by visakh16
 looks like what you need is a cross join between Date1 ItemID fields and then left join result to your table to capture rest of table info if present or else fill them with 0 using isnull() or coalesce()------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/
 
 |  
                                          |  |  |  
                                    | visakh16Very Important crosS Applying yaK Herder
 
 
                                    52326 Posts | 
                                        
                                          |  Posted - 2012-05-27 : 15:17:11 
 |  
                                          | [code]CREATE VIEW vw_YourViewASSELECT *FROM table1UNION ALLSELECT *FROM table2GOSELECT t.Date1,t.ItemID,t.Warehouse,COALESCE(t1.QtyIN,0) AS QtyIN,COALESCE(t1.QtyOUT,0) AS QtyOUT,COALESCE(t1.Bal,0) AS Bal,....FROM(SELECT Date1,ItemID,Warehouse FROM (SELECT DISTINCT Date1,ItemID FROM vw_YourView)mCROSS JOIN (SELECT DISTINCT Warehouse FROM vw_YourView)n)tLEFT JOIN vw_YourView t1ON t1.Date1 = t.Date1AND t1.ItemID = t.ItemIDAND t1.Warehouse = t.Warehouse [/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |  
                                          |  |  |  
                                |  |  |  |  |  |