Solved

Sum these rows

Posted on 2013-01-27
1
312 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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

Naughty Me. While I was changing the database name from DB1 to DB_PROD1 (yep it's not real database name ^v^), I changed the database name and notified my application fellows that I did it. They turn on the application, and everything is working. A …
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 a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

773 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