我在SQL中有这个:
SELECT DISTINCT
O.custid,
P.productname,
TO_CHAR(MAX((quantity) * D.unitprice) AS "Revenue"
FROM corp.Orders O
LEFT JOIN corp.Order_Details D ON O.orderid = D.orderid
LEFT JOIN corp.Products P ON D.productid = P.productid
GROUP BY O.custid, P.productname
HAVING P.productname = 'Ctte de Blaye'
ORDER BY MAX(quantity) * D.unitprice DESC;
它给出了:
CUSTID Productname revenue
QUICK Ctte de Blaye 15810
HANAR Ctte de Blaye 15810
PICCO Ctte de Blaye 10540
RATTC Ctte de Blaye 10540
SIMOB Ctte de Blaye 10540
MEREP Ctte de Blaye 10329.2
QUEEN Ctte de Blaye 8432
KOENE Ctte de Blaye 7905
GREAL Ctte de Blaye 7905
WHITC Ctte de Blaye 6587.5
SPLIR Ctte de Blaye 4216
ERNSH Ctte de Blaye 4216
BERGS Ctte de Blaye 3952.5
TORTU Ctte de Blaye 3952.5
THEBI Ctte de Blaye 2635
SANTG Ctte de Blaye 2108
BLONP Ctte de Blaye 2108
SPECD Ctte de Blaye 1317.5
RANCH Ctte de Blaye 527
如何让它只返回前两行?
答案 0 :(得分:2)
将查询的开头更改为SELECT TOP 2 DISTINCT
答案 1 :(得分:2)
如果使用SQL Server 2000及更高版本,则可以在查询中添加TOP子句:
SELECT TOP 2 DISTINCT O.custid, P.productname, to_char(Max((quantity)*(D.unitprice))) AS "Revenue"
FROM (corp.Orders O LEFT JOIN corp.Order_Details D ON O.orderid = D.orderid) LEFT JOIN corp.Products P ON D.productid = P.productid GROUP BY O.custid, P.productname
HAVING (((P.productname)='Ctte de Blaye'))
ORDER BY Max((quantity)*(D.unitprice)) DESC;
在不同的数据库中没有标准的支持方式,所以知道你正在使用哪一个是必不可少的。
有关许多不同的
,请参阅this页面SELECT * FROM T LIMIT 10 --PostgreSQL, MySQL, SQLite, H2
SELECT * from T WHERE ROWNUM <= 10 --Oracle (also supports the standard, since Oracle8i)
SELECT FIRST 10 * from T --Ingres
SELECT FIRST 10 * FROM T order by a --Informix
SELECT SKIP 20 FIRST 10 * FROM T order by c, d --Informix (row numbers are filtered after order by is evaluated. SKIP clause was introduced in a v10.00.xC4 fixpack)
SELECT * FROM T FETCH FIRST 10 ROWS ONLY --DB2 (also supports the standard, since DB2 v8)
SELECT TOP 10 * FROM T --MS SQL Server (also supports the standard, since SQL Server 2005), Sybase ASE, MS Access
SELECT TOP 10 START AT 20 * FROM T --Sybase SQL Anywhere (also supports the standard, since version 9.0.1)
答案 2 :(得分:1)
我们假设你在PostgreSQL上运行它。
尝试使用LIMIT:
SELECT DISTINCT
O.custid,
P.productname,
TO_CHAR(MAX((quantity) * D.unitprice) AS "Revenue"
FROM corp.Orders O
LEFT JOIN corp.Order_Details D ON O.orderid = D.orderid
LEFT JOIN corp.Products P ON D.productid = P.productid
GROUP BY O.custid, P.productname
HAVING P.productname = 'Ctte de Blaye'
ORDER BY MAX(quantity) * D.unitprice DESC
LIMIT 2;
答案 3 :(得分:0)
SELECT DISTINCT
O.custid
, P.productname
, TO_CHAR(MAX((quantity) * D.unitprice) AS "Revenue"
FROM corp.Orders O
LEFT JOIN corp.Order_Details D ON O.orderid = D.orderid
LEFT JOIN corp.Products P ON D.productid = P.productid
GROUP BY O.custid
, P.productname
HAVING P.productname = 'Ctte de Blaye'
ORDER BY MAX(quantity) * D.unitprice DESC
FETCH FIRST 2 ROWS ONLY
;
SELECT * FROM (
SELECT DISTINCT
O.custid
, P.productname
, TO_CHAR(MAX((quantity) * D.unitprice) AS "Revenue"
FROM corp.Orders O
LEFT JOIN corp.Order_Details D ON O.orderid = D.orderid
LEFT JOIN corp.Products P ON D.productid = P.productid
GROUP BY O.custid
, P.productname
HAVING P.productname = 'Ctte de Blaye'
ORDER BY MAX(quantity) * D.unitprice DESC
)
WHERE ROWNUM < 3;
SELECT DISTINCT
O.custid
, P.productname
, TO_CHAR(MAX((quantity) * D.unitprice) AS "Revenue"
FROM corp.Orders O
LEFT JOIN corp.Order_Details D ON O.orderid = D.orderid
LEFT JOIN corp.Products P ON D.productid = P.productid
GROUP BY O.custid
, P.productname
HAVING P.productname = 'Ctte de Blaye'
ORDER BY MAX(quantity) * D.unitprice DESC
LIMIT 2;
SELECT TOP 2 DISTINCT
O.custid
, P.productname
, TO_CHAR(MAX((quantity) * D.unitprice) AS "Revenue"
FROM corp.Orders O
LEFT JOIN corp.Order_Details D ON O.orderid = D.orderid
LEFT JOIN corp.Products P ON D.productid = P.productid
GROUP BY O.custid
, P.productname
HAVING P.productname = 'Ctte de Blaye'
ORDER BY MAX(quantity) * D.unitprice DESC
;
答案 4 :(得分:0)
正如我在this article中所述,限制结果集大小的语法取决于您使用的数据库。
SQL标准
SQL:2008标准定义了以下语法,用于限制SQL查询结果集:
SELECT
title
FROM
post
ORDER BY
id DESC
FETCH FIRST 50 ROWS ONLY
Oracle从12c开始支持SQL:2008 Top-N记录子句,从2012年开始支持SQL Server,从8.4开始支持PostgreSQL。
SQL Server
虽然SQL Server支持SQL:2008 Top-N标准语法,但您还需要提供OFFSET子句:
SELECT
title
FROM
post
ORDER BY
id DESC
OFFSET 0 ROWS
FETCH FIRST 50 ROWS ONLY
在旧版SQL Server上,您可以使用TOP:
SELECT TOP 50
title
FROM
post
ORDER BY
id DESC
Oracle 11g 和更低版本
在版本12c之前,要获取Top-N记录,您必须使用派生表和ROWNUM伪列:
SELECT *
FROM (
SELECT
title
FROM
post
ORDER BY
id DESC
)
WHERE ROWNUM <= 50
MySQL和PostgreSQL 8.3或更早版本
通常,MySQL和PostgreSQL使用LIMIT子句将结果集限制为前N个记录:
SELECT
title
FROM
post
ORDER BY
id DESC
LIMIT 50