Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

Help with GROUP BY with SUM

$
0
0

Hello all:

I have always struggled with getting this right, so hopefully someone can assist with this. 

Basically I have two tables:

CREATE TABLE [dbo].[Placement](
	[PlacementID] [int] IDENTITY(1,1) NOT NULL,
	[TravelerID] [int] NOT NULL,
	[ActivityTypeID] [int] NULL,
	[ActivityStatusID] [int] NULL,
	[StartDate] [datetime] NULL,
	[EndDate] [datetime] NULL,
	[NumberOfWeeks] [int] NULL,
	[BookingCompanyID] [int] NOT NULL
) 



CREATE TABLE [dbo].[TravelerCompanyData](
	[TravelerID] [int] NOT NULL,
	[CompanyID] [int] NOT NULL,
	[StatusID] [int] NOT NULL
) 


I am trying to join the placement table to the TravelerCompanyData table so that I only get one row per TravelerID. The point of this query is to sum up the number of weeks. So I want to sum the number of weeks of Placements that have been completed (if any), and add them to however many week the person has been currently on assignment. I cannot use the NumberOfWeeks column if a placement is currently "On Assignment" because it counts the number of weeks from the start date to the end date, and the Traveler will not be all the way to the end date yet.

However, I think with my SUMs in there and all the columns I am referencing in the CASE statement, I am not getting the grouping right. 

The query below will yield four columns, one for each sum because there are four separate rows in the placement table that fit the criteria of the JOIN/WHERE

Some notes:

ActivityStatusID = 23 means the placement status is "on assignment"

= 24 means the placement in "complete"

CompanyID 10 and 12 are the only companies that I want to fit this summing criteria. 

I am using only one travelerID to simplify the result set. 

SELECT
			 trav.TravelerID
			,CASE
					WHEN p2.EndDate IS NULL -- Not on assignment, can sum number of weeks
						THEN SUM(p.NumberOfWeeks)
				ELSE -- multiple assignments, count weeks
					CASE
						WHEN (DATEDIFF(day, p2.StartDate, GETDATE()) + 1) % 7 < 3 
							THEN (DATEDIFF(day, p2.StartDate, GETDATE()) + 1) / 7+
								 ISNULL(p.NumberOfWeeks,0) 
						WHEN (DATEDIFF(day, p2.StartDate, GETDATE()) + 1) % 7 >= 3 
							THEN (DATEDIFF(day, p2.StartDate, GETDATE()) + 1) / 7 + 1+
							     ISNULL(p.NumberOfWeeks,0) 
					END
			 END AS WksOnAssignment
FROM TravelerCompanyData trav
-- Completed placements ------------------
LEFT JOIN dbo.Placement p  ON p.TravelerID = trav.TravelerID
		AND p.ActivityStatusID IN (24)
		AND trav.CompanyID = p.BookingCompanyID
-- Placement if on assignment  ------------
LEFT JOIN dbo.Placement p2 ON p2.TravelerID = trav.TravelerID
		AND p2.ActivityStatusID IN (23)
		AND trav.CompanyID = p2.BookingCompanyID
WHERE (		trav.CompanyID IN (10,12))
		AND 
	  (		p.PlacementID IS NOT NULL 
		OR
			p2.PlacementID IS NOT NULL )
AND trav.TravelerID = 31788
GROUP BY trav.TravelerID
		,p.NumberOfWeeks
		,p2.StartDate
		,p2.EndDate

If I add the NumberOfWeeks column in, it is adding that number to the number of weeks the current On Assignment placement currently has for each row (56 weeks), but I need to add that 56 weeks to the sum of the NumberOfWeeks column. Here is the result set:

TravelerID	NumberOfWeeks	WksOnAssignment
31788	             8	              64
31788	             12	              68
31788	             25	              81
31788	             33	              89

So am I joining wrong, grouping wrong, or summing wrong? 

Thanks so much for any pointers!!!





Viewing all articles
Browse latest Browse all 23857

Trending Articles