SQL having,group by 与order by 的几点注意
2011-03-21 23:20
330 查看
当有聚合函数时(聚合函数为avg, sum等函数)可以用group by 与having
例子
SELECT c.CompanyName, COUNT(o.OrderID) AS account
FROM Northwind.dbo.Customers AS c, Northwind.dbo.Orders AS o, Northwind.dbo.[Order Details] AS od
WHERE c.CustomerID=o.CustomerID AND o.OrderID=od.OrderID
GROUP BY c.CompanyName HAVING COUNT(od.orderID)<=15
order by COUNT(o.OrderID) DESC;
如果没有聚合函数,可直接把限定条件放进where中进行限定,否则会提示错误像这样
olumn 'Northwind.dbo.Orders.ShippedDate' is invalid in the HAVING clause because it is not contained in either an aggregate function or the GROUP BY clause.
错误的范例
select o.OrderID, o.OrderDate, o.RequiredDate,o.ShippedDate, c.CompanyName, e.LastName+','+e.FirstName
FROM Northwind.dbo.Customers AS c, Northwind.dbo.Orders AS o, Northwind.dbo.Employees AS e
where c.CustomerID=o.CustomerID AND o.EmployeeID= e.EmployeeID
GROUP BY c.CompanyName HAVING o.ShippedDate IS NOT null;
正确的范例
SELECT o.OrderID,
o.OrderDate,
o.RequiredDate,
o.ShippedDate,
s.CompanyName AS shippersCompanyName,
c.CompanyName AS customersCompanyName,
employees = e.LastName + ',' + e.FirstName
FROM Northwind.dbo.Customers AS c,
Northwind.dbo.Orders AS o,
Northwind.dbo.Employees AS e,
Northwind.dbo.shippers AS s
WHERE c.CustomerID = o.CustomerID
AND o.EmployeeID = e.EmployeeID
AND o.ShipVia = s.ShipperID
AND o.ShippedDate IS NOT NULL
ORDER BY C.CompanyName;
PS 前面没有聚合,这里 GROUP BY 需要前面的所有字段
HAVING
感谢论坛上的wwwwgou和fredrickhu
例子
SELECT c.CompanyName, COUNT(o.OrderID) AS account
FROM Northwind.dbo.Customers AS c, Northwind.dbo.Orders AS o, Northwind.dbo.[Order Details] AS od
WHERE c.CustomerID=o.CustomerID AND o.OrderID=od.OrderID
GROUP BY c.CompanyName HAVING COUNT(od.orderID)<=15
order by COUNT(o.OrderID) DESC;
如果没有聚合函数,可直接把限定条件放进where中进行限定,否则会提示错误像这样
olumn 'Northwind.dbo.Orders.ShippedDate' is invalid in the HAVING clause because it is not contained in either an aggregate function or the GROUP BY clause.
错误的范例
select o.OrderID, o.OrderDate, o.RequiredDate,o.ShippedDate, c.CompanyName, e.LastName+','+e.FirstName
FROM Northwind.dbo.Customers AS c, Northwind.dbo.Orders AS o, Northwind.dbo.Employees AS e
where c.CustomerID=o.CustomerID AND o.EmployeeID= e.EmployeeID
GROUP BY c.CompanyName HAVING o.ShippedDate IS NOT null;
正确的范例
SELECT o.OrderID,
o.OrderDate,
o.RequiredDate,
o.ShippedDate,
s.CompanyName AS shippersCompanyName,
c.CompanyName AS customersCompanyName,
employees = e.LastName + ',' + e.FirstName
FROM Northwind.dbo.Customers AS c,
Northwind.dbo.Orders AS o,
Northwind.dbo.Employees AS e,
Northwind.dbo.shippers AS s
WHERE c.CustomerID = o.CustomerID
AND o.EmployeeID = e.EmployeeID
AND o.ShipVia = s.ShipperID
AND o.ShippedDate IS NOT NULL
ORDER BY C.CompanyName;
PS 前面没有聚合,这里 GROUP BY 需要前面的所有字段
HAVING
感谢论坛上的wwwwgou和fredrickhu
相关文章推荐
- sql中Distinct、Group by、having、order by使用注意事项
- [转]sql中Distinct、Group by、having、order by使用注意事项
- sql中Distinct、Group by、having、order by使用注意事项
- SQL中 where, group by,having,order by 的重点
- SqlServer 中 Group by、having、order by、Distinct 使用注意事项
- 【sql之区分Order by、group by、where、having】
- sql语句中where、group by、having、order by 是否可以使用别名
- Group by、having、order by、Distinct 使用注意事项
- SQL(三)查询(一) 简单查询(order by、group by、having、as)
- 基本SQL语句练习(order by,group by,having)
- SQL语句Group By和Having需要注意的地方
- SQL中的Where,Group By,Order By和Having
- SQL中order by 、group by 、having的用法区别
- SQL中的Where,Group By,Order By和Having
- 当一个SQL语句同时出现了where,group by,having,order by的时候,执行顺序和编写顺序
- SQL中的Where,Group By,Order By和Having的用法/区别
- SQL 语句:order by 和group by 语句的注意点:
- 关于sql的UNION ,order by 、group by 、having等的用法
- 用到 count; group by ; order by ;having 的一个SQL语句
- SQL中的Where,Group By,Order By和Having