Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
Meltdown
Starting Member
37 Posts |
Posted - 2006-01-28 : 19:01:36
|
Hi everyone, I have the following T-SQL codeCREATE PROCEDURE dbo.OrdersGrouped ASBEGIN SET NOCOUNT ON;SELECT tblReps.Name, tblReps.Active, Sum(tblOrders.Prod_Price) AS 'Total Order Value', Sum(tblOrders.AST_Value) AS 'Total AST', Sum(tblOrders.Design_Time) AS 'Total Design Time' Count(tblOrders.OrderNo) AS 'Number of Orders', FROM tblReps INNER JOIN tblOrders ON tblReps.Code = tblOrders.Rep_CodeGROUP BY tblReps.Name, tblReps.Active;ENDGO I need to have another column of data called Page Rate which is calculated by dividing 'Total Order Value'/'Number of Orders' AS Page Rate. Can anyone tell me how to achieve this in a SP?Thanks for any help |
|
|
Meltdown
Starting Member
37 Posts |
Posted - 2006-01-28 : 19:50:08
|
| sorry,ignore |
 |
|
|
khtan
In (Som, Ni, Yak)
17689 Posts |
Posted - 2006-01-28 : 20:05:53
|
| >> RETURN @TotalOrdersuse OUTPUT parameter to return the value----------------------------------'KH'I come I saw I post |
 |
|
|
Meltdown
Starting Member
37 Posts |
Posted - 2006-01-28 : 20:09:33
|
| Sorry khtan, I confused the issue with my second post, which I've now deleted, I still need a solution, if possible.Thanks |
 |
|
|
Michael Valentine Jones
Yak DBA Kernel (pronounced Colonel)
7020 Posts |
Posted - 2006-01-28 : 23:23:08
|
Calculate it this way.case -- Prevent divide by zero errorwhen Count(tblOrders.OrderNo) = 0 -- Return null if divisor is zerothen nullelse -- Convert to decimal to before dividing -- to be able to calculate to 2 decimal places -- Round result to 2 decimal places convert(decimal(10,2),round( convert(decimal(15,5),Sum(tblOrders.Prod_Price)) / convert(decimal(15,5),Count(tblOrders.OrderNo)) ,2))end as [Page Rate] CODO ERGO SUM |
 |
|
|
Kristen
Test
22859 Posts |
Posted - 2006-01-29 : 05:00:18
|
Or wrap it into a subselect:CREATE PROCEDURE dbo.OrdersGrouped ASBEGIN SET NOCOUNT ONSELECT *, [Total Order Value] / NullIf(Number of Orders, 0) as [Page Rate]FROM( SELECT tblReps.Name, tblReps.Active, Sum(tblOrders.Prod_Price) AS 'Total Order Value', Sum(tblOrders.AST_Value) AS 'Total AST', Sum(tblOrders.Design_Time) AS 'Total Design Time' Count(tblOrders.OrderNo) AS 'Number of Orders', FROM tblReps INNER JOIN tblOrders ON tblReps.Code = tblOrders.Rep_Code GROUP BY tblReps.Name, tblReps.Active;) AS XENDGO Couple of observations:I would suggest using [MyAliasName] instead of 'MyAliasName' to avoid SQL Server accidentally interpreting the AliasName as a string if you miss, say, a comma in the syntax somewhere.I would prefix the tables etc. with "dbo." so that SQL doesn't have to check if a version of the table exists owned-by the current user.And you ought not to use "SELECT *" as I have done in this example!!!Kristen |
 |
|
|
Meltdown
Starting Member
37 Posts |
Posted - 2006-01-29 : 07:33:37
|
Thanks Michael and Kristen for the replies and tips. I have it working now, but I have another question. I would now like to add a percentage column beside the 'Total Order Value' column. This column would indicate what percentage of the overall total that 'Total Order Value' represents.My Code:ALTER PROCEDURE dbo.OrdersGrouped ASBEGIN SET NOCOUNT ONSELECT [Rep Name],[Active Rep],[Total Order Value],[Total AST],[Total Design Time],[Number of Orders],[Total Order Value] / NullIf([Number of Orders], 0) as [Page Rate]FROM( SELECT dbo.tblReps.Name AS [Rep Name], dbo.tblReps.Active AS [Active Rep], Sum(dbo.tblOrders.Prod_Price) AS [Total Order Value], Sum(dbo.tblOrders.AST_Value) AS [Total AST], Sum(dbo.tblOrders.Design_Time) AS [Total Design Time], Count(dbo.tblOrders.OrderNo) AS [Number of Orders] FROM dbo.tblReps INNER JOIN dbo.tblOrders ON dbo.tblReps.Code = dbo.tblOrders.Rep_Code GROUP BY dbo.tblReps.Name, dbo.tblReps.Active) AS XEND Thanks for any help |
 |
|
|
Kristen
Test
22859 Posts |
Posted - 2006-01-29 : 08:23:07
|
In that case I think I'd pull the inner select into a temporary table first, use that to work out the "grand total", and then report from the temporary table and calculate the Percentage using the stored "grand total" value.DECLARE @MyGrandTotal moneySELECT dbo.tblReps.Name AS [Rep Name], dbo.tblReps.Active AS [Active Rep], Sum(dbo.tblOrders.Prod_Price) AS [Total Order Value], Sum(dbo.tblOrders.AST_Value) AS [Total AST], Sum(dbo.tblOrders.Design_Time) AS [Total Design Time], Count(dbo.tblOrders.OrderNo) AS [Number of Orders] INTO #MyTempTableFROM dbo.tblReps INNER JOIN dbo.tblOrders ON dbo.tblReps.Code = dbo.tblOrders.Rep_CodeGROUP BY dbo.tblReps.Name, dbo.tblReps.ActiveSELECT @MyGrandTotal = SUM([Total Order Value])SELECT [Rep Name],[Active Rep],[Total Order Value],[Total AST],[Total Design Time],[Number of Orders],[Total Order Value] / NullIf([Number of Orders], 0) as [Page Rate],([Total Order Value] * 100.0) / @MyGrandTotal as [Overall Percentage]FROM #MyTempTableORDER BY [Rep Name], [Active Rep] Note that you should use CREATE TABLE #MyTempTable ... (rather than SELECT ... INTO #MyTempTable) for efficiency - but I am too lazy for this example!Kristen |
 |
|
|
Meltdown
Starting Member
37 Posts |
Posted - 2006-01-29 : 11:07:11
|
Hi Kristen, thanks a lot for your help, a couple of issues...Your line SELECT @MyGrandTotal = SUM([Total Order Value])..is returning a Not a Valid Column error, I dont know why that is?Also, is it possible to return the [Overall Percentage] column formatted as a percentage, to 2 decimal places?ThanksALTER PROCEDURE dbo.OrdersGrouped2ASBEGIN SET NOCOUNT ON;DECLARE @MyGrandTotal moneyCreate Table #TempTable([Rep Name] nvarchar(30),[Active Rep] bit,[Total Order Value] money,[Total AST] smallint,[Total Design Time] smallint,[Number of Orders] smallint)INSERT #TempTableSELECT dbo.tblReps.Name AS [Rep Name], dbo.tblReps.Active AS [Active Rep], Sum(dbo.tblOrders.Prod_Price) AS [Total Order Value], Sum(dbo.tblOrders.AST_Value) AS [Total AST], Sum(dbo.tblOrders.Design_Time) AS [Total Design Time], Count(dbo.tblOrders.OrderNo) AS [Number of Orders] FROM dbo.tblReps INNER JOIN dbo.tblOrders ON dbo.tblReps.Code = dbo.tblOrders.Rep_CodeGROUP BY dbo.tblReps.Name, dbo.tblReps.ActiveSELECT @MyGrandTotal = SUM([Total Order Value])SELECT [Rep Name],[Active Rep],[Total Order Value],[Total AST],[Total Design Time],[Number of Orders],[Total Order Value] / NullIf([Number of Orders], 0) as [Page Rate],([Total Order Value] * 100.0) / @MyGrandTotal as [Overall Percentage]FROM #TempTableORDER BY [Rep Name], [Active Rep]END |
 |
|
|
Kristen
Test
22859 Posts |
Posted - 2006-01-29 : 11:11:30
|
| Sorry should have beenSELECT @MyGrandTotal = SUM([Total Order Value])FROM #TempTable"Also, is it possible to return the [Overall Percentage] column formatted as a percentage, to 2 decimal places?"You would be better off formatting it in your application layer. Alternatively CAST it to an appropriate datatype - 2d.p. decimal, or the CONVERT function has a couple of formatting options - see Books on Line for detailsKristen |
 |
|
|
Meltdown
Starting Member
37 Posts |
Posted - 2006-01-30 : 05:59:14
|
Thanks a lot Kristen. got it sorted now |
 |
|
|
|
|
|
|
|