Solved

SQL Help with Sum from 2 different tables

Posted on 2013-02-05
4
377 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

Suggested Solutions

Title # Comments Views Activity
ACESS 2010 Query Criteria 23 57
T-SQL: Need to trim a single leading space 7 63
T-SQL: need to reset a declared variable 4 33
Outlook mail to Access 8 31
Preparing an email is something we should all take special care with – especially when the email is for somebody you may not know very well. The pressures of everyday working life stacked with a hectic office environment can make this a real challen…
In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
In Microsoft Access, learn how to “cascade” or have the displayed data of one combo control depend upon what’s entered in another. Base the dependent combo on a query for its row source: Add a reference to the first combo on the form as criteria i…
In Microsoft Access, when working with VBA, learn some techniques for writing readable and easily maintained code.

752 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