Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Change query to sum in MySQL database

Posted on 2013-11-06
2
Medium Priority
?
272 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
[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
2 Comments
 
LVL 66

Accepted Solution

by:
Jim Horn earned 2000 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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
In this article, I’ll talk about multi-threaded slave statistics printed in MySQL error log file.
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.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

618 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