Академический Документы
Профессиональный Документы
Культура Документы
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.orderdet select name,sum(price) from dbo.orderdet group by name select 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 Orderdet WHERE name='Hansen' OR name='Jensen' GROUP BY name HAVING SUM(Price)>1500
select name, price from dbo.orderdet where name in('hansen','jensen') order by name compute sum(price) select name, price from dbo.orderdet order by name compute 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 name compute 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)p pivot ( count(PurchaseOrderID) for EmployeeID in ([164],[198],[223]) ) as
O_Id 1 2 3 4 5 6
Now we want to find the total sum (total order) of each customer. We will have to use the GROUP BY statement to group the customers. We use the following SQL statement:
The result-set will look like this: Customer Hansen Nilsen Jensen Nice! Isn't it? :) Let's see what happens if we omit the GROUP BY statement: SUM(OrderPrice) 2000 1700 2000
The result-set will look like this: Customer Hansen Nilsen Hansen Hansen Jensen Nilsen SUM(OrderPrice) 5700 5700 5700 5700 5700 5700
The result-set above is not what we wanted. Explanation of why the above SELECT statement cannot be used: The SELECT statement above has two columns specified (Customer and SUM(OrderPrice). The "SUM(OrderPrice)" returns a single value (that is the total sum of the "OrderPrice" column), while "Customer" returns 6 values (one value for each row in the "Orders" table). This will therefore not give us the correct result. However, you have seen that the GROUP BY statement solves this problem.
Now we want to find if any of the customers have a total order of less than 2000. We use the following SQL statement:
Now we want to find if the customers "Hansen" or "Jensen" have a total order of more than 1500. We add an ordinary WHERE clause to the SQL statement:
The rules regarding the use of the COMPUTE and COMPUTE BY clauses are 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 the SELECT column list. 3>The ORDER BY clause must be used whenever the COMPUTE BY clause is 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 the columns used in the ORDER BY clause. 6>More than one COMPUTE clause can be used in the SELECT statement to produce a result with subtotals and a grand total. 7>The different aggregate functions can be used on more than one column with the COMPUTE BY clause.