Solved

Need help with a mysql select statement

Posted on 2012-03-13
6
525 Views
Last Modified: 2012-06-27
Hi Experts
I have written a query and I am not sure where I have gone wrong.
What we need to achieve is:
1) we need to see how much Time_used id assigned to each project our team is working on.

however in my query I am unable to get the sum of hours assigned to each project.
I am ending up with the sum of all hours assigned to the user.

Sum(theme_keywords_pyramidpromo_assigned.TaskTimeSingle) is the column that holds all the hours assigned per project.

project = theme_keywords_pyramidpromo_assigned.profileid

user  =  usrprofiles.usrid

basically all i need to have the "Time_used" per project, per user for the month and any other work not finished in previous months.

in the attached txt file you will see that in the Time_used column rows 1,2 the values are the same

see column profile Name
credit repair
sarahdacruz.com

both are = 86

they should be
credit repair = 83
sarahdacruz.com = 3

Any suggestions will be appreciated

Query
======
SELECT
      usrprofiles.Department AS Department,
      usrprofiles.Email AS Email,
      usrprofiles.Title AS Title,
      usrprofiles.FirstName AS FirstName,
      usrprofiles.Surname AS Surname,
      projectprofile.profileName AS profileName,
      usrprofiles.AvailabilityDaysInMonth AS AvailabilityDaysInMonth,
      usrprofiles.AvailabilityHoursInDay AS AvailabilityHoursInDay,
      (usrprofiles.AvailabilityDaysInMonth * usrprofiles.AvailabilityHoursInDay)AS TotalHours,
      (SELECT Sum(theme_keywords_pyramidpromo_assigned.TaskTimeSingle)FROM theme_keywords_pyramidpromo_assigned WHERE theme_keywords_pyramidpromo_assigned.usrid = usrprofiles.usrid and theme_keywords_pyramidpromo_assigned.profileid = theme_keywords_pyramidpromo_assigned.profileid) AS Time_used,
      round(((usrprofiles.AvailabilityDaysInMonth * usrprofiles.AvailabilityHoursInDay) - sum(theme_keywords_pyramidpromo_assigned.TaskTimeSingle)), 2)AS Hour_Left
FROM
      usrprofiles
JOIN theme_keywords_pyramidpromo_assigned ON theme_keywords_pyramidpromo_assigned.usrid = usrprofiles.usrid
JOIN projectprofile ON projectprofile.profileid = theme_keywords_pyramidpromo_assigned.profileid
WHERE
usrprofiles.CompanyID = usrprofiles.CompanyID
AND theme_keywords_pyramidpromo_assigned.completed = 0
AND(MONTH(theme_keywords_pyramidpromo_assigned.dateDue) <= MONTH(now()))
AND(YEAR(theme_keywords_pyramidpromo_assigned.dateDue) = YEAR(now()))
GROUP BY
usrprofiles.usrid,
theme_keywords_pyramidpromo_assigned.profileid,
projectprofile.CompanyID
team-time-management.txt
0
Comment
Question by:matthewdacruz
  • 4
  • 2
6 Comments
 

Author Comment

by:matthewdacruz
Comment Utility
No one know how to do this type of query?
0
 
LVL 34

Expert Comment

by:Beverley Portlock
Comment Utility
I am not really into complex SQL queries, I would do this as a series of simple queries from PHP. Having said that, your GROUP BY has user as the primary grouping, should it not have the project as the primary grouping?

GROUP BY
projectprofile.CompanyID,
usrprofiles.usrid,
theme_keywords_pyramidpromo_assigned.profileid

rather than

GROUP BY
usrprofiles.usrid,
theme_keywords_pyramidpromo_assigned.profileid,
projectprofile.CompanyID
0
 

Author Comment

by:matthewdacruz
Comment Utility
Hi bportlock
We need the GROUP BY usrprofiles.usrid, as there are multiple people working on a particular project. I f we group by just theme_keywords_pyramidpromo_assigned.profileid,
projectprofile.CompanyID then all of the team get lumped under a project.

We wanting to manage the teams workload and see what they currently working on.
Maybe there is a better way of writing the query I have?
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Accepted Solution

by:
matthewdacruz earned 0 total points
Comment Utility
Got it figured out, it was the nested select that caused the issues.
should have just been  

Sum(theme_keywords_pyramidpromo_assigned.TaskTimeSingle)

instead of

 (SELECT Sum(theme_keywords_pyramidpromo_assigned.TaskTimeSingle)FROM theme_keywords_pyramidpromo_assigned WHERE theme_keywords_pyramidpromo_assigned.usrid = usrprofiles.usrid and theme_keywords_pyramidpromo_assigned.profileid = theme_keywords_pyramidpromo_assigned.profileid) AS Time_used,
0
 
LVL 34

Expert Comment

by:Beverley Portlock
Comment Utility
Glad you are sorted out.
0
 

Author Closing Comment

by:matthewdacruz
Comment Utility
I am closing this request as I figured out the issue.
The nsested select statement resulted in the wrong totals being found
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
Read about achieving the basic levels of HRIS security in the workplace.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

771 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

11 Experts available now in Live!

Get 1:1 Help Now