MS SQL Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsDatabasesMS SQL Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old September 23rd, 2004, 06:05 PM
ianwallis ianwallis is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2004
Posts: 1 ianwallis User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Nested SQL queery ? for timesheet

I have 3 tables:-

(Tasks - list of all the tasks to complete)
Tasks.IMS, = ID of task i.e. 1 (unique)
Tasks.Title, = Title of task i.e. "Build Wall"


(Activities - list of users for each task, i.e. entry for "brick layer, = 24 hours" and entry for "foreman, = 8 hours")
Activities.ID = unique ID i.e. 100
Activities.IMS = links to the Task IMS - i.e. =1
Activities.[Remaining Effort]) = 24 hours

(Timesheet - one entry per activity (i.e. one for brick layer and a second for foreman) per week)
TimeSheet.Activity = links to Activity.ID
TimeSheet.SAT = number of hours booked for Saturday
TimeSheet.SUN
TimeSheet.MON
TimeSheet.TUE
TimeSheet.THU
TimeSheet.WED
TimeSheet.FRI

I simply need a query that will list, each task, the total remaining effort and the total number of hours booked.

My problem is that with as there are multiple time sheets per activity, when I sum the "Remaining Effort", I get a number that is to large.

I.e.

Task = build Wall
Activity = Brick Layer - 24 hoours remaining
Activity = Foreman - 8 hours remainin
TimeSheet.Brick Layer.Week 1 = 30 hours
TimeSheet.Brick Layer.Week 2 = 32 hours
TimeSheet.Foreman.Week 1 = 8 hours
TimeSheet.Foreman.Week 2 = 7 hours

I expect to get
Worked = 30+32+8+7 = 77
SumOfRemaining Effort = 24 +8 = 32

However I get
Worked = 30+32+8+7 = 77 - OK
SumOfRemaining Effort = 24 +8 +24+8= 64 - WRONG

I think I need some type of nested queery ??????? - please help


This is the Queery I have used.

SELECT
Tasks.IMS,
Tasks.Title,
Tasks.[Budget (Hours)],
Sum(Activities.[Remaining Effort]) AS [SumOfRemaining Effort],
Sum(TimeSheet.SAT) AS SumOfSAT,
Sum(TimeSheet.SUN) AS SumOfSUN,
Sum(TimeSheet.MON) AS SumOfMON,
Sum(TimeSheet.TUE) AS SumOfTUE,
Sum(TimeSheet.THU) AS SumOfTHU,
Sum(TimeSheet.WED) AS SumOfWED,
Sum(TimeSheet.FRI) AS SumOfFRI,
Sum([SAT]+[SUN]+[MON]+[TUE]+[WED]+[THU]+[FRI]) AS Worked
FROM (Tasks LEFT JOIN Activities ON Tasks.IMS = Activities.IMS) LEFT JOIN TimeSheet ON Activities.ID = TimeSheet.Activity GROUP BY Tasks.IMS, Tasks.Title;

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesMS SQL Development > Nested SQL queery ? for timesheet


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway
Stay green...Green IT