473,383 Members | 1,739 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,383 software developers and data experts.

To subtract two columns which are in two different tables

I have 2 tables, items and sold . In items table I have itemid , item name , quantity and in sold table I have itemid , peopleid, itemname , soldquantity. I want the output as itemid, itemname,remquantity(quantity-soldquantity) and where will the final output will store?
Mar 17 '15 #1
7 5801
Rabbit
12,516 Expert Mod 8TB
Join the two tables on itemid and do the subtraction there.

You shouldn't store the final output. It is better to create it as a view so the data is always up to date.
Mar 17 '15 #2
Luuk
1,047 Expert 1GB
Is your only question where to store the output?
Because that answer depends on your needs, and Rabit gave a good answer already.....

But if your question is also about how to retrieve the data from MySQL than, please give the SQL statement you tried but 'did not work for this purpose'
Mar 17 '15 #3
can u please provide sql query to my question?
Mar 18 '15 #4
Rabbit
12,516 Expert Mod 8TB
Please make an attempt at the code using the algorithm provided. If you run into any problems, post the query you tried and any error messages you get.
Mar 18 '15 #5
Expand|Select|Wrap|Line Numbers
  1. select items.iid, SUM(items.quantity)-sum(soldtable.soldquantity) as remquantity from items inner join soldable on items.iid = soldtable.iid group by iid,pid;
  2. My tables are:
  3. items:
  4. iid    iname    quantity
  5. 1    Rice    40
  6. 2    Sugar    20
  7. 3    Dall    30
  8. soldtable:
  9. iid    Pid    Soldquantity
  10. 1    P1    5
  11. 1    P2    6
  12. 2    P1    4
  13. 2    P2    5
  14. I'm getting the output as:
  15. iid    Remquantity
  16. 1    35
  17. 1    34
  18. 2    16
  19. 2    15
  20. But I want the output as 
  21. iid    remquantity
  22. 1    29
  23. 2    11
please give me query for this one.
Mar 19 '15 #6
Rabbit
12,516 Expert Mod 8TB
1) Don't group by pid
2) Use the max of the items quantity, not the sum
Mar 19 '15 #7
Ok sir its working. Thank you for your help.
Mar 19 '15 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: manning_news | last post by:
Using SQL2000. How do I format my select statement to choose one out of 24 different tables? Each table is slightly different and I was hoping I could use one select statement and format it...
7
by: Paolo | last post by:
I know I should not be doing this, but I find it very useful. I have a database in Access which stores data for a small company. Sometimes we need to add similar information to different tables....
10
by: Marizel | last post by:
I'm not sure there's an easy solution to this, but thought I'd ask. I often find myself with a query which I'd like to reuse, but with a different datasource. These datasources generally have...
20
by: Dennis Gearon | last post by:
I am working on a design where a location for something can have: Fully qualified address with even building names, room numbers, and booth numbers. **-OR-** GPS location **-OR-** Both ...
4
by: satish | last post by:
Values of two columns in two different tables--presentation using select Hi Everyone, i have two tables in the database . One is called address table and one is adressPhone Table. Below...
0
by: Jerms | last post by:
Hello all, I've been using this site quite a bit since starting my project and have found it very helpful. I have run into a roadblock though that I cant seem to scrounge up a solution to. I...
1
by: Ben Rendel | last post by:
Hi I'm trying to list all columns of tables having a column named 'ContactServiceID' The following code does not work error: Invalid object name 'sysobjects.name' use MSSmallBusiness...
6
HaLo2FrEeEk
by: HaLo2FrEeEk | last post by:
I have two different tables which havea different number of columns. One of the tables gets a new row every day and is populated with a st of 4 IDs from another table, along with today's date and an...
14
by: PreethiGowri | last post by:
I want to subtract 2 column of two different tables and update in one of the table say, i have two tables 'main' and 'sub' both tables have a column named 'quantity' so i have to do it in this way:...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.