?
Solved

Change query to sum in MySQL database

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In this blog, we’ll look at how improvements to Percona XtraDB Cluster improved IST performance.
Ready to get certified? Check out some courses that help you prepare for third-party exams.
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.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
Suggested Courses

862 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