If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > PostgreSQL > Calculating balance in stock statement

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 08-25-11, 09:57
arvindps arvindps is offline
Registered User
 
Join Date: Aug 2011
Posts: 27
Post Calculating balance in stock statement

Hi folks,

I am a recent dotnet and php programmer. I have been assigned to a project that involves inventory management. It is based on postgresql server as backend on windows platform.

My problem involves the following tables

a) Stock Issue (stock_issue) with columns Entryno, Date, Itemname, Qty

b) Stock Receipt (stock_receipt) with columns Entryno, Date, Itemname, Qty

Both tables have similar definations. A entry is made in stock_issue whenever items go out and vice-versa in stock receipt.


I have to generate a report with gives the sum of all issues and receipts , grouped itemwise. There is a extra fourth column with difference between the issue and receipt qty. Entryno and date are not required in the report.


I have already generated a report by using the following SQL statement.

Code:
SELECT issue_stock.itemname, SUM(DISTINCT issue_stock.qty) AS QtyOut, SUM(DISTINCT stock_receipt.qty) AS QtyIn
FROM issue_stock INNER JOIN
stock_receipt ON issue_stock.itemname = stock_receipt.itemname
GROUP BY issue_stock.itemname
the output is in the following format



Itemname QtyOut QtyIn

Item1 20 12
Item2 13 3
Item3 8 1


It is working ok.

My problem is only to add a fourth column that displays the difference between QtyOut and QtyIn
The related output must be as follows

Itemname QtyOut QtyIn Balance

Item1 20 12 8
Item2 13 3 10
Item3 8 1 7

Please suggest any solution, related to, how to add a Balance column


Thanx
Reply With Quote
  #2 (permalink)  
Old 08-25-11, 13:24
loquin loquin is offline
Super Moderator
 
Join Date: Jun 2004
Location: Arizona, USA
Posts: 1,797
wouldn't it be...
Code:
SELECT issue_stock.itemname, SUM(DISTINCT issue_stock.qty) AS QtyOut, SUM(DISTINCT stock_receipt.qty) AS QtyIn, SUM(DISTINCT issue_stock.qty) - SUM(DISTINCT stock_receipt.qty) as Balance
...
__________________
Lou
使大吃一惊
"Lisa, in this house, we obey the laws of thermodynamics!" - Homer Simpson
"I have my standards. They may be low, but I have them!" - Bette Middler
"It's a book about a Spanish guy named Manual. You should read it." - Dilbert

Reply With Quote
  #3 (permalink)  
Old 08-26-11, 04:18
arvindps arvindps is offline
Registered User
 
Join Date: Aug 2011
Posts: 27
Thumbs up

Quote:
Originally Posted by loquin View Post
wouldn't it be...
Code:
SELECT issue_stock.itemname, SUM(DISTINCT issue_stock.qty) AS QtyOut, SUM(DISTINCT stock_receipt.qty) AS QtyIn, SUM(DISTINCT issue_stock.qty) - SUM(DISTINCT stock_receipt.qty) as Balance
...
Oh, thanx,

will it calculate sum of both quantities again, because i tried to place QtyOut-Qtyin as balance (since they are already being calculated) and it gives an error.

anyway this solution works , it is splendid. I was really in need of it urgently.
Reply With Quote
  #4 (permalink)  
Old 08-26-11, 10:20
shammat shammat is offline
Registered User
 
Join Date: Nov 2003
Posts: 2,408
Quote:
Originally Posted by arvindps View Post
will it calculate sum of both quantities again
No they will only be calculated once.

Quote:
because i tried to place QtyOut-Qtyin as balance (since they are already being calculated) and it gives an error.
You need to use a derived table in order to be able to use the column alias for the aggregate functions:

Code:
SELECT itemname, 
       QtyOut, 
       QtyIn,
       QtyOut - Qtyin as balance
FROM (
    SELECT issue_stock.itemname, 
           SUM(DISTINCT issue_stock.qty) AS QtyOut, 
           SUM(DISTINCT stock_receipt.qty) AS QtyIn
    FROM issue_stock INNER JOIN
    stock_receipt ON issue_stock.itemname = stock_receipt.itemname
    GROUP BY issue_stock.itemname
) t
Reply With Quote
  #5 (permalink)  
Old 08-27-11, 10:36
arvindps arvindps is offline
Registered User
 
Join Date: Aug 2011
Posts: 27
Thumbs up

Quote:
Originally Posted by shammat View Post
No they will only be calculated once.

You need to use a derived table in order to be able to use the column alias for the aggregate functions:

Code:
SELECT itemname, 
       QtyOut, 
       QtyIn,
       QtyOut - Qtyin as balance
FROM (
    SELECT issue_stock.itemname, 
           SUM(DISTINCT issue_stock.qty) AS QtyOut, 
           SUM(DISTINCT stock_receipt.qty) AS QtyIn
    FROM issue_stock INNER JOIN
    stock_receipt ON issue_stock.itemname = stock_receipt.itemname
    GROUP BY issue_stock.itemname
) t
Thanx a zillion, that query made me look great in my department.
Although the time consumed was only slightly lower, but i have been to modify this query to suit other similar calculative output.

I just hope this is a faster method. I will give it another try on monday.
Reply With Quote
  #6 (permalink)  
Old 08-27-11, 12:28
shammat shammat is offline
Registered User
 
Join Date: Nov 2003
Posts: 2,408
Quote:
Originally Posted by arvindps View Post
I just hope this is a faster method. I will give it another try on monday.
Faster than loquin's solution?
I doubt it. The optimizer will most probably generate the same execution plan for both queries.
Reply With Quote
Reply

Tags
inventory, postgres, sql, statement

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On