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 |
noblemfd
Starting Member
38 Posts |
Posted - 2013-05-16 : 23:34:39
|
I have written this before, but was told to make it clearer:(My company is using MSSQL SERVER 2000)Table1: InvoiceDetailTable2: StockCardCREATE TABLE [dbo].[invoicedetail]( [serialno] [bigint] IDENTITY(1,1) NOT NULL, [itemid] [nvarchar](50) NULL, [date1] [datetime] NULL, [qty] [float] NULL, [inv#] [nvarchar](50) NULL, [price] [money] NULL) ON [PRIMARY]INSERT invoicedetail (itemid, date1, qty, inv#, price)VALUES ('0001', '01/01/2013', 10, 'INV001', 30)VALUES ('0002', '01/01/2013', 5, 'INV001', 50)VALUES ('0001', '01/01/2013', 10, 'INV001', 32)VALUES ('0001', '02/01/2013', 5, 'INV001', 32)VALUES ('0003', '01/01/2013', 10, 'INV001', 15)VALUES ('0003', '04/01/2013', 20, 'INV001', 15)VALUES ('0002', '11/01/2013', 10, 'INV001', 55)VALUES ('0001', '11/01/2013', 10, 'INV001', 35)VALUES ('0001', '18/01/2013', 20, 'INV001', 32)VALUES ('0002', '20/01/2013', 10, 'INV001', 55)VALUES ('0003', '20/01/2013', 10, 'INV001', 17)CREATE TABLE [dbo].[stockcard]( [serialno] [bigint] IDENTITY(1,1) NOT NULL, [itemid] [nvarchar](50) NULL, [date1] [datetime] NULL, [qtyIN] [float] NULL, [qtyIN] [float] NULL) ON [PRIMARY]I want to use stored procedure to get the average price(stock valuation based on selected dates.Once the parameters are entered, it wll go to table1(invoicedetail table) and check the concerned dateThis is the formula my boss gave meFormula:AveragePrice = ItemValue/ItemBalanceNOTE THAT(=> as per chosen date):ItemValue(from table1:invoicedetail) = qty * price ItemBalance(from table2:stockcard) = SUM(qtyIN - qtyOUT)i HAVE DONE IT IN VB6 Using array, but need to transform it to MS SQL SERVER 2000. Please helpI need Something like this ...CREATE PROCEDURE Valuation(@StartDate DATETIME, @EndDate DATETMIE, @ItemID)AS.....EXPECTED OUTPUT:ItemID | AvgPrice | TotalQtyIN | TotalQtyOUT | TotalBalnceVB6 SAMPLE Count_i = 0 Do While Val(cumQty) <= Val(ItmBal1) cumQty = Val(cumQty) + (Stock_Array(Count_i, 2)) If Val(cumQty) <= Val(ItmBal1) Then itmVal = Stock_Array(Count_i, 2) * Val(Stock_Array(Count_i, 4)) + itmVal Else ItmVal2 = Val(ItmBal1) - (cumQty - Stock_Array(Count_i, 2)) itmVal = (ItmVal2 * Val(Stock_Array(Count_i, 4))) + itmVal Exit Do End If If Count_i = Count_J - 1 Then Exit Do Count_i = Count_i + 1 Loop getAvgPrice = 0 If Val(ItmBal1) <> 0 Then getAvgPrice = Val(itmVal) / Val(ItmBal1) |
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-05-17 : 00:21:00
|
[code]CREATE PROCEDURE Valuation@StartDate DATETIME, @EndDate DATETMIE, @ItemID intASSELECT m.itemid,SUM(ItemValue)/NULLIF(SUM(ItemBalance,0)FROM(SELECT itemid,[date1],SUM(qty * price) AS ItemValueFROM invoicedetail iWHERE i.[date1] >= @startdate AND i.[date1] < @enddate+1AND i.[itemid] = @ItemIDGROUP BY itemid,[date1])mINNER JOIN(SELECT itemid,[date1],SUM([qtyIN]-[qtyOUT]) AS ItemBalanceFROM [dbo].[stockcard]GROUP BY itemid,[date1])nON n.itemid = m.itemidAND n.[date1] = m.[date1]GROUP BY m.itemidGO[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
folumike
Starting Member
24 Posts |
Posted - 2013-05-21 : 15:05:08
|
Great! Am very grateful. I have one more question. What is the replacement for a "For Next Loop" in MSSQL 2000. Can u give a brief example?quote: Originally posted by visakh16
CREATE PROCEDURE Valuation@StartDate DATETIME, @EndDate DATETMIE, @ItemID intASSELECT m.itemid,SUM(ItemValue)/NULLIF(SUM(ItemBalance,0)FROM(SELECT itemid,[date1],SUM(qty * price) AS ItemValueFROM invoicedetail iWHERE i.[date1] >= @startdate AND i.[date1] < @enddate+1AND i.[itemid] = @ItemIDGROUP BY itemid,[date1])mINNER JOIN(SELECT itemid,[date1],SUM([qtyIN]-[qtyOUT]) AS ItemBalanceFROM [dbo].[stockcard]GROUP BY itemid,[date1])nON n.itemid = m.itemidAND n.[date1] = m.[date1]GROUP BY m.itemidGO ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs
|
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-05-22 : 00:59:05
|
loops in SQL 2000 are implemented using WHILE or using a curor.------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
Transact Charlie
Master Smack Fu Yak Hacker
3451 Posts |
Posted - 2013-05-22 : 04:25:50
|
but you shouldn't use them in almost any circumstance (bar admin) -- what's the use case. There will almost certainly be a better declarative way to do it.also -- sql server 2000 is pretty damn long in the tooth now. Any chance to upgrade? 2005 has a lot of improvements over 2000.Transact CharlieMsg 3903.. The ROLLBACK TRANSACTION request has no corresponding BEGIN TRANSACTION. http://nosqlsolution.blogspot.co.uk/ |
|
|
noblemfd
Starting Member
38 Posts |
Posted - 2013-05-27 : 22:21:57
|
There is an error code that the aggregate allows only one argument. And this came from NULLIF(SUM(ItemBalance,0). Please helpquote: Originally posted by folumike Great! Am very grateful. I have one more question. What is the replacement for a "For Next Loop" in MSSQL 2000. Can u give a brief example?quote: Originally posted by visakh16
CREATE PROCEDURE Valuation@StartDate DATETIME, @EndDate DATETMIE, @ItemID intASSELECT m.itemid,SUM(ItemValue)/NULLIF(SUM(ItemBalance,0)FROM(SELECT itemid,[date1],SUM(qty * price) AS ItemValueFROM invoicedetail iWHERE i.[date1] >= @startdate AND i.[date1] < @enddate+1AND i.[itemid] = @ItemIDGROUP BY itemid,[date1])mINNER JOIN(SELECT itemid,[date1],SUM([qtyIN]-[qtyOUT]) AS ItemBalanceFROM [dbo].[stockcard]GROUP BY itemid,[date1])nON n.itemid = m.itemidAND n.[date1] = m.[date1]GROUP BY m.itemidGO ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs
|
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2013-05-28 : 00:53:54
|
its a typo. it should beCREATE PROCEDURE Valuation@StartDate DATETIME, @EndDate DATETMIE, @ItemID intASSELECT m.itemid,SUM(ItemValue)/NULLIF(SUM(ItemBalance),0)FROM(SELECT itemid,[date1],SUM(qty * price) AS ItemValueFROM invoicedetail iWHERE i.[date1] >= @startdate AND i.[date1] < @enddate+1AND i.[itemid] = @ItemIDGROUP BY itemid,[date1])mINNER JOIN(SELECT itemid,[date1],SUM([qtyIN]-[qtyOUT]) AS ItemBalanceFROM [dbo].[stockcard]GROUP BY itemid,[date1])nON n.itemid = m.itemidAND n.[date1] = m.[date1]GROUP BY m.itemidGO ------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/https://www.facebook.com/VmBlogs |
|
|
|
|
|
|
|