Showing posts with label aggregate. Show all posts
Showing posts with label aggregate. Show all posts

Tuesday, March 27, 2012

Can I GROUP BY aggregate Function (Like SUM)

Hello,
I column that calculated at run time in insert , can i gruop by this column,the new one that not exist yetyes, with a derived table, but why would you want to?

please show an example, using sample data to illustrate|||this is my case:

i have a table with a column datetime (I use to record time of calls) , i want to create table that hold data for each hour, so i round the column and want to group by this column in the same time|||you can round a datetime? please show your query|||This is the Query of round datetime

DATEADD(Hour, DATEDIFF(Hour, 0, cdrCallDate), 0)

You can change [hour] to day , minutes , second , etc...|||yes, you can GROUP BY that expression :)

and you cannot use second, it causes an overflow

:)|||thanks man for this information

Friday, February 24, 2012

Can aggregate functions be used in a check-constraint or a computed-column?

Hi,

Can aggregate functions (e.g. Sum, Avg) be used in a check contraint or in a table's computed column?

Thanks!

Green:

I don't think you can use subqueries for either circumstance; therefore, I am not sure that you can apply either one to a table. I think that the best you can do for a function is a scalar function. Can somebody else verify this?


Dave

|||evn i dont think its possible...as they r the values that can be entered for a particular column , while aggregate function will need a group....still will check it out soon and confirm..|||

You cannot do it directly, but as Dave suggested, you can do it by creating a function. Here's a simple example

create function SumTestOne()
returns int
as
begin
declare @.retVal int
select @.retVal = sum(one) from Test
return @.retVal
end

create table Test
(one int,
two int,
check (two > dbo.SumTestOne()))

insert Test values (100, 200)

select * from Test

|||Thanks for the suggestion to use a function. Tried it and it works well!