Monday, October 3, 2011

MSSQL GROUP BY Clause WITH ROLLUP

Бүлэглэгдсэн мэдээллийн нэгдсэн дүнг доор нь бодон харуулахад хэрэглэгдэх функц

USE test
go
create table Orders
(
    OrderID int primary key,
    Customer varchar(10),
    OrderDate datetime,
    ShippingCost money
)

create table OrderDetails
(
    DetailID int primary key,
    OrderID int references Orders(OrderID),
    Item varchar(10),
    Amount money
)

go

insert into Orders
select 1,'ABC', '2007-01-01', 40 union all
select 2,'ABC', '2007-01-02', 30 union all
select 3,'ABC', '2007-01-03', 25 union all
select 4,'DEF', '2007-01-02', 10

insert into OrderDetails
select 1, 1, 'Item A', 100 union all
select 2, 1, 'Item B', 150 union all
select 3, 2, 'Item C', 125 union all
select 4, 2, 'Item B', 50 union all
select 5, 2, 'Item H', 200 union all
select 6, 3, 'Item X', 100 union all
select 7, 4, 'Item Y', 50 union all
select 8, 4, 'Item Z', 300

select * from Orders
select Customer, MAX(OrderDate) as OrderDate, SUM(ShippingCost) as ShippingCost
from Orders GROUP BY Customer WITH ROLLUP

No comments: