0

i have two tables with names stock_in and stock_out with following colums

stock_in (recid,itemid,units)

stock_out (recid,itemid,units)

what i want is to get sum of units from both tables and then calculate remaining units from stock_in to stock_out where i can specify the itemid for any item

Thanks

3
  • 2
    When you ask for help writing database queries, you must tag the question with your RDBMS. SQL is SQL but in the real world, all queries do not run on all RDBMS -- the syntax and available functions differ. Commented Feb 3, 2011 at 8:04
  • 1
    No, don't tag it with the word "RDBMS". Tag it with which relational database management system you're using -- SQL Server, Sybase, Oracle, Postgres, MySQL, etc? Commented Feb 3, 2011 at 8:17
  • 1
    sql on its own is still a valid tag if (1) you want vendor-agnostic solutions or (2) you want to know about "standard" SQL. Commented Feb 3, 2011 at 8:23

4 Answers 4

4
SELECT
  (SELECT SUM(units) FROM stock_in WHERE itemid = 7) -
  (SELECT SUM(units) FROM stock_out WHERE itemid = 7)
  AS difference;
Sign up to request clarification or add additional context in comments.

5 Comments

add WHERE itemid = # clauses to the subqueries
@Dan Grossman I think the poster wants results as (itemid, difference) or similar. It's not really useful to take the difference of all items when getting the difference for a single item.
actually i want balance of stock but only for some specified item as item id ... but not sure how to put where clause...
@air The total balance of a set of items or the balance for each item for a set of items? Some small sample data/results in the question may clear up the exact intent.
i need balance for any specified item, which i specify in php
2
select
    stock_in.itemid,
    sum_stock_in.sum as sum_in,
    sum_stock_in.sum as sum_out
from
    stock_in
    left join (
        select itemid, sum(units) as sum from stock_in group by itemid
    ) as sum_stock_in on sum_stock_in.itemid = stock_in.itemid
    left join (
        select itemid, sum(units) as sum from stock_out group by itemid
    ) as sum_stock_out on sum_stock_out.itemid = stock_in.itemid
where
    stock_in.itemid in (1, 2, 3)

-- edit:
group by
    stock_in.itemid

In this query, it is assumed that stock_out is a subset of stock_in, i.e., stock_in contains every possible itemid.

2 Comments

+1 Not sure if this is really what the poster wants, but it at least takes the items themselves into account :)
I actually like Dan's solution (difference between results of 2 subqueries), but most of time more data than just a single number is needed. This might be useful if author needs (1) more information than just a difference about (2) list of items.
1

Make a stored procedure out of it

SELECT @SUM1=SUM(*)
FROM STOCK_IN
SELECT @SUM2=SUM(*)
FROM STOCK_OUT

SELECT @SUM1-@SUM2

Thats the best solution I can think of, the one mentioned by Dan is not working since using two from, the actual work done is inner join

Comments

1

Since this is under PHP:

$queryString = "SELECT
  SUM(stock_in.units) - SUM(stock_out.units) as difference
FROM
  stock_in
INNER JOIN stock_out
ON stock_in.itemid=stock_out.itemid
WHERE stock_in.itemid = ".$value_to_query;
$result = mysql_query($queryString);

Tests:

create database sumgetter;
use sumgetter;
CREATE TABLE stock_in (
         recid INT,
         itemid INT,
         units INT
);
CREATE TABLE stock_out (
         recid INT,
         itemid INT,
         units INT
);
INSERT INTO stock_in VALUES (1, 1, 2);
INSERT INTO stock_out VALUES (1, 1, 3);
INSERT INTO stock_in VALUES (2, 2, 2);
INSERT INTO stock_out VALUES (2, 2, 2);

SELECT
  SUM(stock_in.units) - SUM(stock_out.units) as difference
FROM
  stock_in
INNER JOIN stock_out
ON stock_in.itemid=stock_out.itemid
WHERE stock_in.itemid = 1;

//result

+------------+
| difference |
+------------+
|         -1 |
+------------+
1 row in set (0.00 sec)

3 Comments

Sorry about that. I have a new machine - just installling MySQL now so I can test it properly.
I've added some test SQL above
I'm going to go with Dan Grossman's solution. You can use the exact same approach that I've just listed, just update the query string. The problem with my solution is that the join selects the value twice.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.