Solved

Sum these rows

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
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…
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…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

821 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