Solved

SQL Help with Sum from 2 different tables

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

Expert Comment

by:Rey Obrero
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 142

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

Free Gift Card with Acronis Backup Purchase!

Backup any data in any location: local and remote systems, physical and virtual servers, private and public clouds, Macs and PCs, tablets and mobile devices, & more! For limited time only, buy any Acronis backup products and get a FREE Amazon/Best Buy gift card worth up to $200!

Join & Write a Comment

I see at least one EE question a week that pertains to using temporary tables in MS Access.  But surprisingly, I was unable to find a single article devoted solely to this topic. I don’t intend to describe all of the uses of temporary tables in t…
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…

706 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now