SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
IF OBJECT_ID('dbo.usp_GetTransactionsByDept') IS NOT NULL
   DROP PROCEDURE dbo.usp_GetTransactionsByDept
GO
-- =============================================
-- Author:		Eric Miles
-- Create date: 8/22/2008
-- Description:	
-- =============================================
CREATE PROCEDURE usp_GetTransactionsByDept
	-- Add the parameters for the stored procedure here
	@startDT datetime = 0, 
	@endDT datetime = 0
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

    -- Insert statements for procedure here
	SELECT		av.[Value] as 'AttributeValuesDeptNo', u.[Name] as 'UsersName', p.PartNum as 'ProductsPartNum',
				p.Description as 'ProductsDescription', p.PackageQty as 'ProductsPackageQty',
				p.Price as 'ProductsPrice', t.TransactionDT as 'TransactionsTransactionDT'
	FROM		Transactions t
					JOIN Products p on p.ProductNo = t.ProductNo
					JOIN Cards c on c.CardNo = t.CardNo
					JOIN Users u on u.CardNo = c.CardNo
					JOIN AttributeValues av on av.AttributeValueNo = u.DeptNo
	WHERE		t.TransactionDT >= @startDT AND t.TransactionDT <= @endDT
	GROUP BY	av.[Value], u.[Name], p.PartNum, p.Description,
				p.PackageQty, p.Price, t.TransactionDT
	ORDER BY	av.[Value], u.[Name], t.TransactionDT
END
GO
