如何从表中获取最大行数
您好,
我有以下代码和一个大问题:
WITH CALC1 AS (
SELECT OTQUOT, OTIT01 AS ITEMS, ROUND(OQCQ01 * OVRC01,2) AS COST
FROM @[email protected] WHERE OTIT01 <> ''
UNION ALL
SELECT OTQUOT, OTIT02 AS ITEMS, ROUND(OQCQ02 * OVRC02,2) AS COST
FROM @[email protected] WHERE OTIT02 <> ''
UNION ALL
SELECT OTQUOT, OTIT03 AS ITEMS, ROUND(OQCQ03 * OVRC03,2) AS COST
FROM @[email protected] WHERE OTIT03 <> ''
UNION ALL
SELECT OTQUOT, OTIT04 AS ITEMS, ROUND(OQCQ04 * OVRC04,2) AS COST
FROM @[email protected] WHERE OTIT04 <> ''
UNION ALL
SELECT OTQUOT, OTIT05 AS ITEMS, ROUND(OQCQ05 * OVRC05,2) AS COST
FROM @[email protected] WHERE OTIT05 <> ''
ORDER BY OTQUOT ASC
)
SELECT OTQUOT, ITEMS, MAX(COST)
FROM CALC1
WHERE OTQUOT = '04886471'
GROUP BY OTQUOT, ITEMS
结果:
04886471 FEPO5050WCGA24 13.21
04886471 GFRK1650SGL 36.21
04886471 FRA7500GA 12.6
04886471 CGIFESHAZ 11.02
04886471 CGIFESHPDPR 11.79
04886471 GFRK1350DBL 68.23
04886471 RET1.63825GP 32.55
04886471 FRSA 0.12
04886471 GFRK1350SGL 55.94
04886471 GFRK1650DBL 71.89
04886471 FEPO6565WCGA24 16.6
04886471 PCAP5050GA 0.28
04886471 FEPO6565NCPAG24 0.000000
如何获得具有最高值的 Itemcode 的行的结果? 在这种情况下我需要结果: 04886471 GFRK1650DBL 71.89 但我不知道如何更改我的代码来实现这一点 - 有人可以帮助我吗?
HI
I have the following code and a massive problem:
WITH CALC1 AS (
SELECT OTQUOT, OTIT01 AS ITEMS, ROUND(OQCQ01 * OVRC01,2) AS COST
FROM @[email protected] WHERE OTIT01 <> ''
UNION ALL
SELECT OTQUOT, OTIT02 AS ITEMS, ROUND(OQCQ02 * OVRC02,2) AS COST
FROM @[email protected] WHERE OTIT02 <> ''
UNION ALL
SELECT OTQUOT, OTIT03 AS ITEMS, ROUND(OQCQ03 * OVRC03,2) AS COST
FROM @[email protected] WHERE OTIT03 <> ''
UNION ALL
SELECT OTQUOT, OTIT04 AS ITEMS, ROUND(OQCQ04 * OVRC04,2) AS COST
FROM @[email protected] WHERE OTIT04 <> ''
UNION ALL
SELECT OTQUOT, OTIT05 AS ITEMS, ROUND(OQCQ05 * OVRC05,2) AS COST
FROM @[email protected] WHERE OTIT05 <> ''
ORDER BY OTQUOT ASC
)
SELECT OTQUOT, ITEMS, MAX(COST)
FROM CALC1
WHERE OTQUOT = '04886471'
GROUP BY OTQUOT, ITEMS
result:
04886471 FEPO5050WCGA24 13.21
04886471 GFRK1650SGL 36.21
04886471 FRA7500GA 12.6
04886471 CGIFESHAZ 11.02
04886471 CGIFESHPDPR 11.79
04886471 GFRK1350DBL 68.23
04886471 RET1.63825GP 32.55
04886471 FRSA 0.12
04886471 GFRK1350SGL 55.94
04886471 GFRK1650DBL 71.89
04886471 FEPO6565WCGA24 16.6
04886471 PCAP5050GA 0.28
04886471 FEPO6565NCPAG24 0.000000
How can I get the result of the row with the Itemcode that has the highest value?
In this case I need the result:
04886471 GFRK1650DBL 71.89
but i dont know how to change my code to get that - can anybody please help me?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用
ROW_NUMBER()
窗口函数提供选择顶行的方法。请注意,您需要将其放入派生子查询中,因为WHERE
子句无法引用查询自己的行号。您必须使用外部查询来做到这一点:此外,我不会为 UNION 查询中的
ORDER BY
烦恼。Use the
ROW_NUMBER()
windowing function to provide a means to select the top row. Note that you need to put it into a derived subquery because aWHERE
clause cannot reference the query's own row number. You have to do that with an outside query:Also I wouldn't bother with the
ORDER BY
inside your UNION query.如果我正确理解你的问题,添加
ORDER BY COST DESC LIMIT 1
应该可以解决问题。编辑:如果评论表明您的 SQL 引擎不支持
LIMIT
,请参阅 这篇文章介绍如何使用 Microsoft SQL Server 模拟LIMIT
(它也适用于其他兼容 SQL 的引擎,例如 Oracle、PostgreSQL 或 DB2,因为它仅使用 SQL 标准构造)。Adding
ORDER BY COST DESC LIMIT 1
should do the trick, if I correctly understand your question.Edit: If as a comment suggests your SQL engine does not support
LIMIT
, see this post on how to emulateLIMIT
with Microsoft SQL Server (it will also work in other SQL-compliant engines such as Oracle, PostgreSQL or DB2 since it only uses SQL-standard constructs).