Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Grouping Not Working
Message
Information générale
Forum:
Microsoft SQL Server
Catégorie:
Syntaxe SQL
Divers
Thread ID:
01488401
Message ID:
01488404
Vues:
51
>Here's a sample of the data:
>
>
>ImportbatchId DepartmentID CreateDate              Void  Accepted
>------------- ------------ ----------------------- ----- -----------
>321761        114          2010-09-10 09:02:51.190 0     4
>321762        114          2010-09-10 09:03:27.073 0     4
>321764        114          2010-09-10 09:06:21.803 0     4
>321765        114          2010-09-10 09:06:39.700 0     4
>321766        114          2010-09-10 09:08:49.383 0     4
>321767        114          2010-09-10 09:09:00.120 0     4
>321783        114          2010-09-10 11:11:15.770 0     4
>321784        114          2010-09-10 11:17:24.303 0     4
>321816        120          2010-09-10 12:27:38.020 0     6
>321835        120          2010-09-10 13:22:22.720 0     6
>321836        120          2010-09-10 13:26:37.423 0     12
>321837        120          2010-09-10 13:29:27.607 0     12
>321838        120          2010-09-10 13:40:15.103 0     12
>321840        120          2010-09-10 14:23:03.047 0     6
>321842        114          2010-09-10 14:29:13.723 0     4
>321843        114          2010-09-10 14:32:55.427 0     4
>321845        107          2010-09-10 14:47:46.697 0     2
>321846        107          2010-09-10 14:47:53.020 0     2
>
>
>I'm trying to group by the Department, Date only (not time), and accepted. Here's my query:
>
>
>CREATE PROCEDURE usp_Get_Daily_Volume_Data
>	@DepartmentID	INT = NULL,
>	@StartDate		DATETIME = NULL,
>	@EndDate		DATETIME = NULL,
>	@Variance		FLOAT = NULL
>
>AS
>	-- Set up parameter defaults
>	IF @StartDate IS NULL
>		SET @StartDate = CURRENT_TIMESTAMP
>	
>	IF @EndDate IS NULL
>		SET @EndDate = DATEADD(wk, -1, @StartDate)
>
>	IF @Variance IS NULL
>		SET @Variance = .15
>
>	-- Get the average for the last 6 months and the boundry values
>	DECLARE @Average INT
>	DECLARE @MinAccepted INT
>	DECLARE @MaxAccepted INT
>
>	SELECT @Average = AVG(Accepted)
>		FROM tblImportBatch 
>		WHERE Void = 0 AND
>			  CreateDate > DATEADD(m, -6, CURRENT_TIMESTAMP)
>	SET @MinAccepted = @Average - (@Average * @Variance)
>	SET @MaxAccepted = @Average + (@Average * @Variance)
>
>	-- Pull the data
>	SELECT	ib.DepartmentID, 
>			d.Description AS DeptDescription,
>			DATEADD(DAY, DATEDIFF(DAY, 0, ib.CreateDate), 0) AS CreateDate,
>			ib.Accepted
>		FROM tblImportBatch ib
>		JOIN tblDepartment d ON ib.DepartmentID = d.DepartmentID
>		WHERE ib.Void = 0 AND
>			  ib.DepartmentID = ISNULL(ib.DepartmentID, @DepartmentID) AND
>			  ib.CreateDate BETWEEN @StartDate AND @EndDate AND
>			  ib.Accepted NOT BETWEEN @MinAccepted AND @MaxAccepted
>		GROUP BY ib.DepartmentID, d.Description, DATEADD(DAY, DATEDIFF(DAY, 0, ib.CreateDate), 0), ib.Accepted
>		ORDER BY ib.DepartmentID, d.Description, DATEADD(DAY, DATEDIFF(DAY, 0, ib.CreateDate), 0), ib.Accepted
>
>
>And here's the results I'm getting:
>
>
>DepartmentID CreateDate              Accepted
>------------ ----------------------- -----------
>2            2010-10-12 00:00:00.000 1
>2            2010-10-12 00:00:00.000 4
>2            2010-10-12 00:00:00.000 6
>6            2010-10-12 00:00:00.000 5
>6            2010-10-12 00:00:00.000 6
>6            2010-10-12 00:00:00.000 20
>6            2010-10-13 00:00:00.000 20
>6            2010-10-21 00:00:00.000 5
>6            2010-10-21 00:00:00.000 6
>6            2010-10-21 00:00:00.000 7
>12           2010-10-22 00:00:00.000 1
>15           2010-09-30 00:00:00.000 1
>15           2010-09-30 00:00:00.000 3
>
>
>You can see it's not summing the Accepted by department.
>
>Anyone see what's wrong?

Your ISNULL condition is written backwards. It should be ISNULL(@DepartmentID, DepartmentID)
BTW, take also a look at this blog post
Do you use ISNULL(...). Don't, it does not perform

See better solution for nullable integer parameter discussed here
Relevant MSDN forum's thread and even better one Discussion about NULL integer parameters
If it's not broken, fix it until it is.


My Blog
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform