聚合函数和分组问题

发布于 2024-08-16 13:52:00 字数 461 浏览 3 评论 0原文

如果我们从以下有效的简单 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

心头的小情儿 2024-08-23 13:52:00

如果您无法升级到(最大)类型的 SQL Server 2005 :-)

请尝试此操作。 SUBSTRING 适用于 blob,但返回 varchar文本。所以聚合应该可以工作,但你会丢失一些数据

SELECT
   sor.FPARTNO, MIN(SUBSTRING(sor.fdesc, 1, 8000)), sum(sor.FUNETPRICE)
FROM
    sorels sor
GROUP BY
    sor.FPARTNO, SUBSTRING(sor.fdesc, 1, 8000)

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

SELECT
   sor.FPARTNO, MIN(SUBSTRING(sor.fdesc, 1, 8000)), sum(sor.FUNETPRICE)
FROM
    sorels sor
GROUP BY
    sor.FPARTNO, SUBSTRING(sor.fdesc, 1, 8000)
七七 2024-08-23 13:52:00

你尝试过吗

SELECT sor.FPARTNO, MIN(sor.fdesc), sum(sor.FUNETPRICE) 
FROM sorels sor 
GROUP BY sor.FPARTNO

?甚至MAX也能做到。

尝试转换 NTEXT 字段

DECLARE @sorels TABLE(
        FPARTNO INT,
        fdesc NTEXT,
        FUNETPRICE FLOAT
)

SELECT sor.FPARTNO, MIN(CAST(sor.fdesc AS VARCHAR(4000))), sum(sor.FUNETPRICE)  
FROM @sorels sor  
GROUP BY sor.FPARTNO

Have you tried

SELECT sor.FPARTNO, MIN(sor.fdesc), sum(sor.FUNETPRICE) 
FROM sorels sor 
GROUP BY sor.FPARTNO

Or even MAX can do.

Try casting the NTEXT field

DECLARE @sorels TABLE(
        FPARTNO INT,
        fdesc NTEXT,
        FUNETPRICE FLOAT
)

SELECT sor.FPARTNO, MIN(CAST(sor.fdesc AS VARCHAR(4000))), sum(sor.FUNETPRICE)  
FROM @sorels sor  
GROUP BY sor.FPARTNO
轮廓§ 2024-08-23 13:52:00

还没有机会对此进行测试,但应该很接近。如果您使用的是 SQL Server 2005,那么使用 CTE 会更加干净。

SELECT agg.FPARTNO,
       (SELECT TOP 1 inner.FDESC FROM sorels inner WHERE inner.FPARTNO = agg.FPARTNO) FDESC,
       agg.FUNETPRICESUM
FROM   (SELECT sor.FPARTNO,
               sum(sor.FUNETPRICE) FUNETPRICESUM
        FROM sorels sor 
        GROUP BY sor.FPARTNO) agg

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.

SELECT agg.FPARTNO,
       (SELECT TOP 1 inner.FDESC FROM sorels inner WHERE inner.FPARTNO = agg.FPARTNO) FDESC,
       agg.FUNETPRICESUM
FROM   (SELECT sor.FPARTNO,
               sum(sor.FUNETPRICE) FUNETPRICESUM
        FROM sorels sor 
        GROUP BY sor.FPARTNO) agg
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文