You are here: Re: Group By Query Help « MsSQL Server « IT news, forums, messages
Re: Group By Query Help

Posted by Erland Sommarskog on 03/01/06 00:38

Hugo Kornelis (hugo@perFact.REMOVETHIS.info.INVALID) writes:
> More efficient to call the function just once per row:
>
> SELECT Col1, SUM(Col2), MyResult
> FROM (SELECT Col1, Col2, MyFunc(Col3, Col4) AS MyResult
> FROM SomeTable) AS Der
> GROUP BY Col1, MyResult

I tried:

CREATE FUNCTION myfun (@int integer) RETURNS int AS
BEGIN
RETURN ((@int - 10000) % 43)
END
go
SELECT dbo.myfun(OrderID), COUNT(*)
FROM Northwind..Orders
GROUP BY dbo.myfun(OrderID)
go
SELECT myfun, COUNT(*)
FROM (SELECT myfun = dbo.myfun(OrderID)
FROM Northwind..Orders) AS x
GROUP BY myfun
go
DROP FUNCTION myfun

The plans look identical.

However, the derived table saves you from having to repeat a complex
expression, and this is a good thing.

--
Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/prodtechnol/sql/2005/downloads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinfo/previousversions/books.mspx

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация