?
Solved

Change query to sum in MySQL database

Posted on 2013-11-06
2
Medium Priority
?
271 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

Percona Live Europe 2017 | Sep 25 - 27, 2017

The Percona Live Open Source Database Conference Europe 2017 is the premier event for the diverse and active European open source database community, as well as businesses that develop and use open source database software.

Question has a verified solution.

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

A Stored Procedure in Microsoft SQL Server is a powerful feature that it can be used to execute the Data Manipulation Language (DML) or Data Definition Language (DDL). Depending on business requirements, a single Stored Procedure can return differe…
It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties

752 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