Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Sql sum statement

Posted on 2013-05-15
7
Medium Priority
?
441 Views
Last Modified: 2013-05-15
I have the following sql statement

SELECT Invoice.datInvoiceDate, Invoice.intInvoiceNumber, Customers.txtCompanyName as Cname, Customers.txtFirstName + ' '+ Customers.txtLastName as Name,
IIF (Cname Is Null or Cname = '',Name, IIF (Name Is Null or Name = '',Cname,Cname+', '+ Name)) as FullName
FROM Customers INNER JOIN Invoice ON Customers.intCustomerID = Invoice.intCustomerID
ORDER BY Invoice.datInvoiceDate;

I have another table named InvoiceLines.  I want to add to the above sql statement the sum of invoicelines.quantity *invoicelines.intsalesprice where invoicelines.intInvoiceNumber=Invoice.intInvoiceNumber
0
Comment
Question by:Moed
[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
  • 3
  • 2
  • 2
7 Comments
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 39167498
have you tried:

SELECT Invoice.datInvoiceDate, Invoice.intInvoiceNumber, Customers.txtCompanyName as Cname, Customers.txtFirstName + ' '+ Customers.txtLastName as Name,
IIF (Cname Is Null or Cname = '',Name, IIF (Name Is Null or Name = '',Cname,Cname+', '+ Name)) as FullName
, (select invoicelines.quantity *invoicelines.intsalesprice from invoicelines where invoicelines.intInvoiceNumber=Invoice.intInvoiceNumber) as invoicetotal
FROM Customers INNER JOIN Invoice ON Customers.intCustomerID = Invoice.intCustomerID
ORDER BY Invoice.datInvoiceDate;
0
 
LVL 2

Author Comment

by:Moed
ID: 39167512
That brings up a dialog asking for the parameter invoicelines.quantity
0
 
LVL 2

Author Comment

by:Moed
ID: 39167603
I've tried this but get a missing operator error in the group by clause

SELECT Invoice.datInvoiceDate, Invoice.intInvoiceNumber, Customers.txtCompanyName AS Cname, Customers.txtFirstName+' '+Customers.txtLastName AS Name, IIf(Cname Is Null Or Cname='',Name,IIf(Name Is Null Or Name='',Cname,Cname+', '+Name)) AS FullName, Sum(InvoiceLines.intquantity*InvoiceLines.intSalesPrice) AS InvoiceTotal
FROM (Customers INNER JOIN Invoice ON Customers.intCustomerID = Invoice.intCustomerID) INNER JOIN InvoiceLines ON Invoice.intInvoiceNumber = InvoiceLines.intInvoiceNumber
Group BY  Invoice.datInvoiceDate, Invoice.intInvoiceNumber, Customers.txtCompanyName AS Cname, Customers.txtFirstName+' '+Customers.txtLastName AS Name
Order BY Invoice.datInvoiceDate;
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 6

Accepted Solution

by:
BurundiLapp earned 2000 total points
ID: 39167659
Try changing your group by clause to:

Group BY  Invoice.datInvoiceDate, Invoice.intInvoiceNumber, Customers.txtCompanyName, Customers.txtFirstName, Customers.txtLastName
0
 
LVL 2

Author Closing Comment

by:Moed
ID: 39167689
This was not really an answer to the original question but it did resolve the issue.
0
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 39167694
how do you run that query? in MS SQL?
0
 
LVL 6

Expert Comment

by:BurundiLapp
ID: 39167851
I think I should have an assist there and the answer awarded to emoreau.
0

Featured Post

Independent Software Vendors: 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

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

610 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