Thank you.
GROUP BY In SQL Server
I want to know why we use GROUP BY In SQL server ?
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Akkiraju IvaturiPosted Aug 6, 2012, 7:35 PM
Satyapriya NayakPosted Aug 6, 2012, 8:09 AM
Rahul BhattPosted Aug 6, 2012, 5:04 AM
Using Group By, you have sum, min, max, count based on value
Below query return sum of all quantity based on CustomerID
select CustomerID ,SUM(Qty) from ProductDetailTable group by CustomerID
IF you want to take greater then 100 Qty based on CustmerID then user Having Clause
select CustomerID ,SUM(Qty) from ProductDetailTable group by CustomerID having SUM(Qty)>100
Having Clasue used with Group By Clasuse
Jignesh TrivediPosted Aug 5, 2012, 11:56 PM
Grouping records is the process of combining like SELECT columns together. Once combined, Aggregate functions such as SUM and AVERAGE can be performed on the set.
GROUP BY should be used to apply aggregate operators to each group.
SELECT Employee, Rank
FROM Employees
GROUP BY Employee, Rank
http://msdn.microsoft.com/en-us/library/ms177673.aspx
hope this will help you.
Hemant SrivastavaPosted Aug 5, 2012, 11:40 PM
Following example will show why we use "GROUP BY"
We have the following "Orders" table:SQL GROUP BY Example
We will have to use the GROUP BY statement to group the customers.
We use the following SQL statement:
SELECT Customer,SUM(OrderPrice) FROM Orders
GROUP BY Customer
The result-set will look like this:
Let's see what happens if we omit the GROUP BY statement:
SELECT Customer,SUM(OrderPrice) FROM Orders
The result-set will look like this:
If it helps you, pls make it accepted.
Thanks