Link to home
Start Free TrialLog in
Avatar of Sudees
SudeesFlag for Canada

asked on

Lifo SQL Query

Hai,

I want to generate the simple LIFO based SQL query for my Purchase table:

      The sample data would be in Purchase table like that:

      Order No.   Date                 Item Code         Quantity       Purchase Price
      1               02-Aug-2012          A                          97000        14.50
      2               03-Aug-2012          B                          12800        99.80
      3               04-Aug-2012          B                          27500        95.80
      4               05-Aug-2012          A                          45000        14.60
      5               05-Aug-2012          A                          55000        13.80
      6               06-Aug-2012          B                          45000        97.00
      7               07-Aug-2012          A                          11400        10.50
      8               08-Aug-2012          B                          11600        95.60
      9               09-Aug-2012          A                          12000        12.60


I want to get the purchase cost of 60000 Lots of Item A using LIFO based method, it means that lastest purchases on the basis of date will be calculated for cost so the aspected result will be:
      
      Purchase cost of 60000 lots of item A will be 799980/-

            Following is the record wise breakup of getting result of 799980/-
            09-Aug-2012      105      12000          14.60      
            07-Aug-2012      105      11400          10.50
            05-Aug-2012      105      36600          13.80


Following is the SQL script for generating Purchase table and Inserting data in it:

      create table purchase (order_number number(5), order_date date,
        item_code varchar2(10), Quantity number(10), Price number(8,2));

      insert into purchase values  ('1','02-Aug-2012','A',97000,14.50);
      insert into purchase values  ('2','03-Aug-2012','B',12800,99.80);
      insert into purchase values ('3','04-Aug-2012','B',27500,95.80);
      insert into purchase values ('4','05-Aug-2012','A',45000,14.60);
      insert into purchase values ('5','05-Aug-2012','A',55000,13.80);
      insert into purchase values ('6','06-Aug-2012','B',45000,97.00);
      insert into purchase values ('7','07-Aug-2012','A',11400,10.50);
      insert into purchase values ('8','08-Aug-2012','B',11600,95.60);
      insert into purchase values ('9','09-Aug-2012','A',12000,12.60);

Looking forward for prompt and positive feedback in this regard.

Thank you...
Avatar of elevationkevin
elevationkevin
Flag of United States of America image

ASKER CERTIFIED SOLUTION
Avatar of Sujith
Sujith
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Sudees

ASKER

Thanks Sir for your quick and proper response
Great! Glad to know it helps
Let me know if you need help in understanding the query.