Solved

Change query to sum in MySQL database

Posted on 2013-11-06
2
263 Views
Last Modified: 2013-11-06
Hi all. I have the following query, I have provided an Excel showing the data that is pumped out.

I want to be able to provide a sum of "slabs" and "dried ins". How can I do this keeping the same columns, but instead of "Job Type" I would want an additional 3 columns: Slabs, DriedIns and TotalCombined. The Excel file shows how it should look like.

Any help would be greatly appreciated. Thanks!

SELECT Builder, Community, Concat(Users.firstname, ' ', Users.lastname) As Super, Concat(ProjectManagers.FirstName, ' ', ProjectManagers.LastName) As ProjectManager, Concat(Salesperson.FirstName, ' ', Salesperson.LastName) As Salesperson ,jobtypedescription as JobType FROM BuilderCommunity LEFT OUTER JOIN FieldSuperDataEntry  ON BuilderCommunity.BuilderCommunityID = FieldSuperDataEntry.BuilderCommunityID LEFT OUTER JOIN FieldSuperAssignment ON BuilderCommunity.BuilderCommunityID = FieldSuperAssignment.BuilderCommunityID LEFT OUTER JOIN Users ON FieldSuperAssignment.UserID = Users.userid LEFT OUTER JOIN ProjectManagerAssignment ON BuilderCommunity.BuilderCommunityID = ProjectManagerAssignment.BuilderCommunityID LEFT OUTER JOIN ProjectManagers ON ProjectManagerAssignment.ProjectManagerID = ProjectManagers.ProjectManagerID LEFT OUTER JOIN SalespersonAssignment ON BuilderCommunity.BuilderCommunityID = SalespersonAssignment.BuilderCommunityID LEFT OUTER JOIN Salesperson ON SalespersonAssignment.SalesPersonID = Salesperson.SalesPersonID LEFT OUTER JOIN JobType ON FieldSuperDataEntry.Type = JobType.jobtypeid Order By Builder, Community

Open in new window

Sample-Data.xlsx
0
Comment
Question by:Sim1980
2 Comments
 
LVL 65

Accepted Solution

by:
Jim Horn earned 500 total points
ID: 39627957
Give this a whirl <air code>..
SELECT Builder, Community, Super, ProjectManager, SalesPerson, 
   SUM(CASE WHEN JobType='Slab' THEN 1 ELSE 0 END) as Slabs, 
   SUM(CASE WHEN JobType='DriedIn' THEN 1 ELSE 0 END) as DriedIn
   COUNT(JOBType) as TotalCombined
FROM YourTableNameGoesHere
GROUP BY Builder, Community, Super, ProjectManager, SalesPerson
ORDER BY whatever

Open in new window

Also I have an article out there on SQL Server GROUP BY Solutions, with a couple of similar examples.

Hope this helps.
Jim
0
 

Author Closing Comment

by:Sim1980
ID: 39628024
Thank you!
0

Featured Post

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

895 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

13 Experts available now in Live!

Get 1:1 Help Now