Home » SQL & PL/SQL » SQL & PL/SQL » Linkage problem when summing values in 2 tables.(merged)
Linkage problem when summing values in 2 tables.(merged) [message #412214] Wed, 08 July 2009 04:22 Go to next message
jallen@haynes.co.uk
Messages: 5
Registered: January 2009
Location: Somerset
Junior Member
I have an issue whereby I need to sum values from two tables where there will not always be a 1 to 1 link. i.e.

Table A link to table b and to table c. I need to sum a value in table b and a value in table c as shown below. I think because of the linkage I am getting incorrect values. i.e. if table b has 2 rows and table c has 3 then the values returned in the sum is 3 times to big.

Can anyone please suggest the correst linkage or a better way of summing the values?

select sp.part_no ISBN,
sp.c_product_code Product,
sp.catalog_desc Title,
sp.sales_price_group_id SalesGroup,
sum(ipis.qty_onhand - ipis.qty_reserved) StockQty,
sum(cois.invoiced_qty) SalesQty,
case when sum(ipis.qty_onhand - ipis.qty_reserved) < sum(cois.invoiced_qty * 3) then '*' else '' end Problem
from ifsapp.sales_part sp,
ifsapp.inventory_part_in_stock ipis,
ifsapp.cust_ord_invo_stat cois
where (sp.sales_price_group_id = upper('&sales_group') or 0 < instr(sp.sales_price_group_id,upper('&sales_group')) or '&sales_group' is null) and
(sp.c_product_code = upper('&Product') or '&Product' is null) and
(sp.part_no = upper('&ISBN') or '&ISBN' is null) and
(sp.catalog_desc = upper('&Title') or 0 < instr(sp.catalog_desc,upper('&Title')) or '&Title' is null) and
sp.part_no = cois.part_no and
sp.part_no = ipis.part_no and
sp.contract = ipis.contract and
cois.invoice_date >= add_months(TO_DATE(sysdate), -1)
group by sp.catalog_no,
sp.part_no,
sp.c_product_code,
sp.catalog_desc,
sp.sales_price_group_id
order by sp.catalog_no
Linkage problem when summing values in 2 tables. [message #412217 is a reply to message #412214] Wed, 08 July 2009 04:24 Go to previous messageGo to next message
jallen@haynes.co.uk
Messages: 5
Registered: January 2009
Location: Somerset
Junior Member
I have an issue whereby I need to sum values from two tables where there will not always be a 1 to 1 link. i.e.

Table A link to table b and to table c. I need to sum a value in table b and a value in table c as shown below. I think because of the linkage I am getting incorrect values. i.e. if table b has 2 rows and table c has 3 then the values returned in the sum is 3 times to big.

Can anyone please suggest the correst linkage or a better way of summing the values?

select sp.part_no ISBN,
sp.c_product_code Product,
sp.catalog_desc Title,
sp.sales_price_group_id SalesGroup,
sum(ipis.qty_onhand - ipis.qty_reserved) StockQty,
sum(cois.invoiced_qty) SalesQty,
case when sum(ipis.qty_onhand - ipis.qty_reserved) < sum(cois.invoiced_qty * 3) then '*' else '' end Problem
from ifsapp.sales_part sp,
ifsapp.inventory_part_in_stock ipis,
ifsapp.cust_ord_invo_stat cois
where (sp.sales_price_group_id = upper('&sales_group') or 0 < instr(sp.sales_price_group_id,upper('&sales_group')) or '&sales_group' is null) and
(sp.c_product_code = upper('&Product') or '&Product' is null) and
(sp.part_no = upper('&ISBN') or '&ISBN' is null) and
(sp.catalog_desc = upper('&Title') or 0 < instr(sp.catalog_desc,upper('&Title')) or '&Title' is null) and
sp.part_no = cois.part_no and
sp.part_no = ipis.part_no and
sp.contract = ipis.contract and
cois.invoice_date >= add_months(TO_DATE(sysdate), -1)
group by sp.catalog_no,
sp.part_no,
sp.c_product_code,
sp.catalog_desc,
sp.sales_price_group_id
order by sp.catalog_no
Re: Linkage problem when summing values in 2 tables.(merged) [message #412219 is a reply to message #412214] Wed, 08 July 2009 04:31 Go to previous messageGo to next message
Michel Cadot
Messages: 64151
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator
Please read OraFAQ Forum Guide, especially "How to format your post?" section.
Make sure that lines of code do not exceed 80 characters when you format.
Indent the code (See SQL Formatter), use code tags and align the columns in result.
Use the "Preview Message" button to verify.
Also always post your Oracle version (4 decimals).

Regards
Michel
Re: Linkage problem when summing values in 2 tables. [message #412220 is a reply to message #412217] Wed, 08 July 2009 04:32 Go to previous message
ayush_anand
Messages: 417
Registered: November 2008
Senior Member
Post a working Test case: create table and insert statements along with the result you want with these data.
Use SQL*Plus and copy and paste what you already tried.

Also always post your Oracle version (4 decimals).

Before posting any code, please read OraFAQ Forum Guide, especially "How to format your post?" section.
Make sure that lines of code do not exceed 80 characters when you format.
Indent the code (See SQL Formatter), use code tags.
Use the "Preview Message" button to verify.
Previous Topic: updating based on multiple condition
Next Topic: Rename or switching the table name
Goto Forum:
  


Current Time: Fri Dec 09 23:38:03 CST 2016

Total time taken to generate the page: 0.07559 seconds