Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

FIFO Cost of Goods Sold

$
0
0

I thought I had this problem solved already, but turned out my solution returned wrong info and also I can not figure out what that solution is doing by looking at it. So I'll present the problem again and hopefully some bright minds can help me to solve it.

We have an #Inventory table and I will only show 3 rows for now for simplicity of the problem

USE [tempdb] GO /****** Object: Table [dbo].[Inventory] Script Date: 07/22/2013 11:56:09 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[Inventory]( [ref_no] [numeric](17, 0) NOT NULL, [locatn_id] [int] NOT NULL, [date_time] [datetime] NOT NULL, [fifo_rank] [bigint] NULL, [InvNo] [bigint] NULL, [department] [char](10) NOT NULL, [category] [char](10) NOT NULL, [item] [char](10) NOT NULL, [invent_id] [int] NOT NULL, [trans_type] [char](1) NOT NULL, [quantity] [numeric](8, 2) NOT NULL, [unit_cost] [money] NOT NULL ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO INSERT [dbo].[Inventory] ([ref_no], [locatn_id], [date_time],

[fifo_rank], [InvNo], [department], [category], [item], [invent_id], [trans_type],

[quantity], [unit_cost]) VALUES (CAST(53 AS Numeric(17, 0)), 1,

CAST(0x0000A20000FF6D74 AS DateTime), 1, 1, N'RETAIL ',

N'BK-CHILD ', N'DSCATTEST ', 0, N'P', CAST(40.00 AS Numeric(8, 2)), 10.0000) INSERT [dbo].[Inventory] ([ref_no], [locatn_id], [date_time],

[fifo_rank], [InvNo], [department], [category], [item], [invent_id],

[trans_type], [quantity], [unit_cost]) VALUES (CAST(53 AS Numeric(17, 0)), 1,

CAST(0x0000A20000FF6D74 AS DateTime), 2, 1, N'RETAIL ',

N'BK-CHILD ', N'DSCATTEST ', 0, N'P', CAST(40.00 AS Numeric(8, 2)), 5.0000) INSERT [dbo].[Inventory] ([ref_no], [locatn_id], [date_time],

[fifo_rank], [InvNo], [department], [category], [item], [invent_id], [trans_type],

[quantity], [unit_cost]) VALUES (CAST(136005001 AS Numeric(17, 0)), 1,

CAST(0x0000A200011967D8 AS DateTime), 3, 1, N'RETAIL ', N'BK-CHILD ',

N'DSCATTEST ', 0, N'S', CAST(-50.00 AS Numeric(8, 2)), 0.0000)


So, we have one item here (I added a new key InvNo so I don't have to deal with 4 columns (department, category, item, location)). Trans_Type column defines the type of the transaction (P - purchase, A - adjustment, T - transfer and S - sale).

So, types P,A,T (when quantity >0) are movements in and S or negative quantity are movements out. 

I also added Fifo_Rank column to order by datetime for each item

My task is to find the cost of goods sold. In this particular scenario we purchased 40 items by $10, then again 40 by $5 and then sold 50 items. So, the cost of goods sold will be 40*10 + 10*5 = $450.

I need solutions for both (SQL Server 2005-2008) and SQL Server 2012.

Can you help with some ideas?

Thanks in advance.


For every expert, there is an equal and opposite expert. - Becker's Law


My blog


My TechNet articles


Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>