Solved

Change query to sum in MySQL database

Posted on 2013-11-06
2
260 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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

744 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

15 Experts available now in Live!

Get 1:1 Help Now