Solved

Sum these rows

Posted on 2013-01-27
1
307 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
1 Comment
 
LVL 4

Accepted Solution

by:
mcmahon_s earned 500 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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

760 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

17 Experts available now in Live!

Get 1:1 Help Now