Solved

SQL Help with Sum from 2 different tables

Posted on 2013-02-05
4
374 Views
Last Modified: 2013-02-06
I'm trying to combine two SQL statments to find for an item:
1. How many units were sold (in the invoiceline table)
2. How many units were returned (in the creditmemoline table)

select sum(InvoiceLineQuantity) as QSold from InvoiceLine where txnDate <= #11/1/2012# and txnDate >= #8/3/2012# and InvoiceLineItemRefFullName = 'CB:CB001' 

Open in new window


select sum(CreditmemoLineQuantity) as CMSold from CreditmemoLine where txnDate <= #11/1/2012# and txnDate >= #8/3/2012# and CreditMemoLineItemRefFullName = 'CB:CB001'

Open in new window


Is it possible to combine these two statments together?  The only thing they really have in common is the ItemRefFullName -

This loops through a bunch of items, so I'd rather only make one call the the datbase each time if possible.

Thanks!
0
Comment
Question by:slightlyoff
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 26

Expert Comment

by:jerryb30
ID: 38856726
select sum(InvoiceLineQuantity) as QSold, "QSold" as Type from InvoiceLine where txnDate <= #11/1/2012# and txnDate >= #8/3/2012# and InvoiceLineItemRefFullName = 'CB:CB001'
Union All
select sum(CreditmemoLineQuantity) as CMSold, "CMSold" as Type from CreditmemoLine where txnDate <= #11/1/2012# and txnDate >= #8/3/2012# and CreditMemoLineItemRefFullName = 'CB:CB001'
0
 
LVL 120

Expert Comment

by:Rey Obrero (Capricorn1)
ID: 38856728
you can create a union query like this


select 'InvoiceLine' As Source,sum(InvoiceLineQuantity) as QSold from InvoiceLine where txnDate <= #11/1/2012# and txnDate >= #8/3/2012# and InvoiceLineItemRefFullName = 'CB:CB001'
union
select 'CreditmemoLine' As Source,sum(CreditmemoLineQuantity) as QSold from CreditmemoLine where txnDate <= #11/1/2012# and txnDate >= #8/3/2012# and CreditMemoLineItemRefFullName = 'CB:CB001'
0
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 38856746
if you have another table (which I presume) which has the itemFillnames...

select t.InvoiceLineItemRefFullName 
 , ( select sum( il.InvoiceLineQuantity) 
    from InvoiceLine il  
   where il.txnDate <= #11/1/2012# and il.txnDate >= #8/3/2012# and   il.InvoiceLineItemRefFullName = t.InvoiceLineItemRefFullName
 )  as QSold 
, ( select sum(cl.CreditmemoLineQuantity) 
    from CreditmemoLine cl
  where cl.txnDate <= #11/1/2012# and cl.txnDate >= #8/3/2012# and cl.CreditMemoLineItemRefFullName = t.InvoiceLineItemRefFullName
 ) as CMSold 
from your_table t 

Open in new window


you may want to use the DSUM() function instead, eventually, but normally that will result in less efficient code.

if you don't have such a table, you should do that...
0
 
LVL 1

Author Comment

by:slightlyoff
ID: 38856901
Thanks for the quick reply!

I tried:
"select t.FullName, ( select sum( il.InvoiceLineQuantity) from InvoiceLine il  where(il.txnDate <= #11/1/2012# And il.txnDate >= #8/3/2012# And il.InvoiceLineItemRefFullName = t.FullName))  as QSold, ( select sum(cl.CreditmemoLineQuantity) from CreditmemoLine cl where(cl.txnDate <= #11/1/2012# And cl.txnDate >= #8/3/2012# And cl.CreditMemoLineItemRefFullName = t.FullName)) as CMSold from ItemInventory t where t.fullName = 'CB:CB002'"

Open in new window


And it worked in Access, but in VB.NET at the line I fill the dataAdapter:
da2.Fill(ds2, "table1")

Open in new window


I get the following error:
Conversion failed because the Decimal data value overflowed the type specified for the Decimal value part in the consumer's buffer.

I'm not sure what decimal value this would be talking about, since i don't have decimals in the database...  and I don't know what the consumer's buffer means...  is this an OleDB issue?
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A simple tool to export all objects of two Access files as text and compare it with Meld, a free diff tool.
This article describes a method of delivering Word templates for use in merging Access data to Word documents, that requires no computer knowledge on the part of the recipient -- the templates are saved in table fields, and are extracted and install…
Basics of query design. Shows you how to construct a simple query by adding tables, perform joins, defining output columns, perform sorting, and apply criteria.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question