Compute By clause in SQL Server

Posted By  youngmurukan On 20 Apr 2010 10:04:45
emailbookmarkadd commentsprint
No of Views:1390
Bookmarked:0 times
Votes:0 times

Introduction

We can use this clause to sum/count/avg/max/min so on. This clause will give you the output as detail and summary which is based on the fields you want to summarize.

select * from #temp
order by student
compute sum(marks) by student

 

in above compute by clause, you must specify the field you want to sum in Compute clause and specify the field in By clause based on which field you need to compute.
Very important thing is you must specify the Order By clause in which specify the fileds whatever you specify in By clause in Compute clause.
The output of above query is, 

Image Loading

When we try with max,min,avg, the query and output would be as shown below,

Using MAX()

select * from #temp
order by student
compute max(marks) by student
Image Loading

Using Min ()

select * from #temp
order by student
compute min(marks) by student
Image Loading

Using Avg()

select * from #temp
order by student
compute avg(marks) by student
Image Loading

I hope this helpto you.

Sign Up to vote for this article
Other popular Tips/Tricks
    In this tips, I will explain how to ALTER or ADD two or more columns in to table by t-sql script.
    Published Date : 25/Mar/2011
    A very important feature of SQL Server 2008 is that we can enable CDC(Change Data capture) on database or table.We can track the database had CDC enabled by querying IS_CDC_ENABLED column
    Published Date : 17/Jan/2011
    A very important feature of SQL Server 2008 is that we can enable CDC(Change Data capture) on database or table.We can track the database had CDC enabled by querying IS_CDC_ENABLED column
    Published Date : 17/Jan/2011
    This is the third tips date related function in sql server. In this I going to show you how to use DATEDIFF() function in sql server. The DATEDIFF () is useful to get the specified date part between two dates
    Published Date : 16/Dec/2010
    The DATEADD function is a powerful built-in function sql server to add dates in different way and types. So I would like to explore in this tip how we can use the DATEADD function for different purpose.
    Published Date : 12/Dec/2010
Comments
By:Rahul kambojDate Of Posted:2/27/2012 3:49:18 AM
good article
thanks for the article
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top