聚合函数和分组问题
如果我们从以下有效的简单 SQL 语句开始。
SELECT sor.FPARTNO, sum(sor.FUNETPRICE)
FROM sorels sor
GROUP BY sor.FPARTNO
FPartNo 是零件编号,Funetprice 显然是净价。用户还想要描述,这会导致问题。如果我跟进这一点:
SELECT sor.FPARTNO, sor.fdesc, sum(sor.FUNETPRICE)
FROM sorels sor
GROUP BY sor.FPARTNO, sor.fdesc
如果该零件号的描述有多种变化,通常文本中的变化非常小,那么我实际上不会聚合零件号。有道理吗?
我确信这一定很简单。如何返回与零件号对应的第一个 fdesc?任何描述变化就足够了,因为它们几乎完全相同。
编辑:描述是一个文本字段。
If we start with the following simple SQL statement which works.
SELECT sor.FPARTNO, sum(sor.FUNETPRICE)
FROM sorels sor
GROUP BY sor.FPARTNO
FPartNo is the part number and the Funetprice is obviously the net price. The user also wants the description and this causes a problem. If I follow up with this:
SELECT sor.FPARTNO, sor.fdesc, sum(sor.FUNETPRICE)
FROM sorels sor
GROUP BY sor.FPARTNO, sor.fdesc
If there are multiple variations of the description for that part number, typically very small variations in the text, then I don't actually aggregate on the part number. Make sense?
I'm sure this must be simple. How can I return the first fdesc that corresponds to the part number? Any of the description variations would suffice as they are almost entirely identical.
Edit: The description is a text field.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您无法升级到(最大)类型的 SQL Server 2005 :-)
请尝试此操作。 SUBSTRING 适用于 blob,但返回 varchar文本。所以聚合应该可以工作,但你会丢失一些数据
If you can't upgrade to SQL Server 2005 for a (max) type :-)
Try this. SUBSTRING works on blobs, but returns varchar for text. So the aggregate should work and you lose some data
你尝试过吗
?甚至MAX也能做到。
尝试转换 NTEXT 字段
Have you tried
Or even MAX can do.
Try casting the NTEXT field
还没有机会对此进行测试,但应该很接近。如果您使用的是 SQL Server 2005,那么使用 CTE 会更加干净。
Have not had a chance to test this, but it should be close. If you were using SQL Server 2005, it would be a lot cleaner with CTEs.