groupby_computeby

download groupby_computeby

of 6

Transcript of groupby_computeby

  • 8/12/2019 groupby_computeby

    1/6

    create table orderdet(

    ordid int constraint oid primary key ,price money ,

    name varchar ( 10 ))

    insert into orderdet values ( 6 , 1000 , 'jensen' )

    alter table dbo . orderdet add orddate datetime

    update dbo . orderdet set orddate ='2008-09-02 00:00:00.000' where ordid =4

    select * from dbo . orderdetselect name , sum ( price ) from dbo . orderdet group by nameselect name , sum ( price ) from dbo . orderdet group by name , price

    select name , sum ( price ) as 'sum_price' , orddate from dbo . orderdet group by name , orddate having orderdate ='2008-09-02 00:00:00.000'

    SELECT name , SUM( Price ) FROM OrderdetWHERE name ='Hansen' OR name ='Jensen'GROUP BY nameHAVING SUM( Price )> 1500

    select name , price from dbo . orderdet where name in( 'hansen' , 'jensen' ) order by namecompute sum ( price )

    select name , price from dbo . orderdet order by namecompute sum ( price )select name , sum ( price ) from dbo . orderdet group by name

    select name , price from dbo . orderdet where name in( 'hansen' , 'jensen' ) order by namecompute sum ( price ) by name

    PIVOT********

    select * from Purchasing . PurchaseOrderHeader order by VendorID , EmployeeID

    select VendorID , [164] as emp1 , [198] as emp2 , [223] as emp3 from ( select PurchaseOrderID , EmployeeID , VendorID from Purchasing . PurchaseOrderHeader ) ppivot(count ( PurchaseOrderID ) for EmployeeID in ( [164] , [198] , [223] ))as

  • 8/12/2019 groupby_computeby

    2/6

  • 8/12/2019 groupby_computeby

    3/6

    The result-set will look like this:

    Customer SUM(OrderPrice)

    Hansen 2000

    Nilsen 1700

    Jensen 2000

    Nice! Isn't it? :)

    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:

    Customer SUM(OrderPrice)

    Hansen 5700

    Nilsen 5700

    Hansen 5700

    Hansen 5700

    Jensen 5700

    Nilsen 5700

    The result-set above is not what we wanted.

    Explanation of why the above SELECT statement cannot be used: The SELECT statement above hastwo columns specified (Customer and SUM(OrderPrice). The "SUM(OrderPrice)" returns a single value (thatis the total sum of the "OrderPrice" column), while "Customer" returns 6 values (one value for each row inthe "Orders" table). This will therefore not give us the correct result. However, you have seen that theGROUP BY statement solves this problem.

    GROUP BY More Than One Column

    We can also use the GROUP BY statement on more than one column, like this:

    SELECT Customer,OrderDate,SUM(OrderPrice) FROM OrdersGROUP BY Customer,OrderDate

    SQL HAVING Clause

  • 8/12/2019 groupby_computeby

    4/6

    The HAVING Clause

    The HAVING clause was added to SQL because the WHERE keyword could not be used with aggregatefunctions.

    SQL HAVING Syntax

    SELECT column_name, aggregate_function(column_name)FROM table_nameWHERE column_name operator valueGROUP BY column_nameHAVING aggregate_function(column_name) operator value

    SQL HAVING Example

    We have the following "Orders" table:

    O_Id OrderDate OrderPrice Customer

    1 2008/11/12 1000 Hansen

    2 2008/10/23 1600 Nilsen

    3 2008/09/02 700 Hansen

    4 2008/09/03 300 Hansen

    5 2008/08/30 2000 Jensen

    6 2008/10/04 100 Nilsen

    Now we want to find if any of the customers have a total order of less than 2000.

    We use the following SQL statement:

    SELECT Customer,SUM(OrderPrice) FROM OrdersGROUP BY CustomerHAVING SUM(OrderPrice)

  • 8/12/2019 groupby_computeby

    5/6

    GROUP BY CustomerHAVING SUM(OrderPrice)>1500

    The result-set will look like this:

    Customer SUM(OrderPrice)Hansen 2000

    Jensen 2000

  • 8/12/2019 groupby_computeby

    6/6

    The rules regarding the use of the COMPUTE and COMPUTE BY clausesare listed below:

    1>The DISTINCT keyword cannot be used with the aggregate functions.

    2>All columns referred to in the COMPUTE clause must appear in theSELECT column list.

    3>The ORDER BY clause must be used whenever the COMPUTE BY clauseis used.

    4>The ORDER BY clause can be eliminated only when the COMPUTE

    clause is used.

    5>The columns listed in the COMPUTE BY clause must match thecolumns used in the ORDER BY clause.

    6>More than one COMPUTE clause can be used in the SELECT statementto produce a result with subtotals and a grand total.

    7>The different aggregate functions can be used on more than onecolumn with the COMPUTE BY clause.