?
Solved

Sum these rows

Posted on 2013-01-27
1
Medium Priority
?
324 Views
Last Modified: 2013-01-27
I have the SQL statement below and a table. I want to sum up the "totalprice" column and show one row. As it is now...2 rows show up.

This is sample of data:

 SELECT  
             businessnameId = CAST(sd.Id AS VARCHAR(MAX)),
          NAME = sd.BusinessName,
          TotalNumOfHCPs =(SELECT COUNT(*) FROM dbo.SignupHCProvider WHERE BusinessNameId = sd.id) ,
          NumPaidHCPs = (SELECT COUNT(*) FROM dbo.PaymentSubscriptionLog WHERE BusinessNameId = sd.id) ,
          UnpaidHCPs = (SELECT COUNT(*) FROM dbo.SignupHCProvider WHERE BusinessNameId = sd.id) - 
                    (SELECT COUNT(*) FROM dbo.PaymentSubscriptionLog WHERE BusinessNameId = sd.id),
       
          website= website, 
          datecreate= CreateDate,
           isnull(p.TotalPrice,10) AS price
           FROM dbo.SignupDescription sd
         LEFT JOIN dbo.OfficePrice p ON p.BusinessNameId = sd.Id
 WHERE sd.Id = 478 -- added for testing to show you how the data looks like
  ORDER BY NAME 

Open in new window


OfficePrice table

BusinessnameID              discount    price          totalPrice
478                                     20               100                  80      
478                                      10               100                  90      

SQL Above brings back 2 rows because i have 2 rows in OfficePrice. But i want one row to come back with totalPrice column added as 170

BusinessName Id               Name           TotalNumOfHCPs ........ price
478                                         xyz                        2                              170
0
Comment
Question by:Camillia
[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
1 Comment
 
LVL 4

Accepted Solution

by:
mcmahon_s earned 2000 total points
ID: 38825423
Just replace:

LEFT JOIN dbo.OfficePrice p ON p.BusinessNameId = sd.Id

with

LEFT JOIN (SELECT BusinessNameID, SUM(TotalPrice) TotalPrice FROM dbo.OfficePrice GROUP BY BusinessNameID) p ON p.BusinessNameId = sd.Id
0

Featured Post

Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

Question has a verified solution.

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

Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
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…

771 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