Solved

SQL Help with Sum from 2 different tables

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
It’s been over a month into 2017, and there is already a sophisticated Gmail phishing email making it rounds. New techniques and tactics, have given hackers a way to authentically impersonate your contacts.How it Works The attack works by targeti…
Familiarize people with the process of utilizing SQL Server views from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Access…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…

840 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