自定义 SQL GROUP BY 子句

发布于 2024-12-29 07:41:27 字数 306 浏览 1 评论 0原文

我有一个非常定制的 SQL 查询,但在实现时遇到问题。我正在使用 SQL-Server-2008。

我在此查询中只有一张表,但我正在寻找非常具体的数据。此查询的要求是:

对于每个 DISTINCT PartNumber(列),我需要选择要选择的 NEWEST(最大)PO(列)。但是,还有另一列名为“Receipt”,如果它包含值,则应将 PartNumber 一起排除。

我对 GROUP BY 子句和选择的 CASES 有点熟悉,但我不确定如何将我所知道的所有内容结合到一个工作查询中......

非常感谢任何帮助!预先感谢=)。

I have a very customized SQL query that I am having problems implementing. I am using SQL-Server-2008.

I have only one table in this query, but I am looking for very specific data. The requirements for this query are:

For each DISTINCT PartNumber (column), I need to select the NEWEST (max) PO (column) to be selected. However, there is another column named "Receipt" where if it contains a value at all, then the PartNumber should be excluded all together.

I am somewhat familiar with GROUP BY clauses and CASES for selections, but I'm not sure how to tie all I know together into one working query...

Any help is greatly appreciated! Thanks in advance =).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

来日方长 2025-01-05 07:41:27
SELECT Partnumber, MAX(PO)
FROM MyTable t1
WHERE NOT EXISTS (SELECT 1
                  FROM MyTable
                  WHERE (Receipt <> '0'
                         OR Receipt <> '')
                  AND Partnumber = t1.partnumber)
GROUP BY PartNumber

此处的 NOT EXISTS 将排除任何具有零件编号的行,而收据已填充在表中的任何位置。

SELECT Partnumber, MAX(PO)
FROM MyTable t1
WHERE NOT EXISTS (SELECT 1
                  FROM MyTable
                  WHERE (Receipt <> '0'
                         OR Receipt <> '')
                  AND Partnumber = t1.partnumber)
GROUP BY PartNumber

The NOT EXISTS here will exclude any row that has a partnumber for which a receipt is populated anywhere in the table.

又爬满兰若 2025-01-05 07:41:27

这是反加入选项

SELECT t1.Partnumber, MAX(t1.PO)
FROM MyTable t1
     LEFT JOIN
      (SELECT DISTINCT PartNumber From MyTable
       WHERE  COALESCE(Receipt, '') = '') t2
     ON t1.Partnumber = t2.Partnumber
WHERE
     t2.Partnumber is null
GROUP BY t1.PartNumber

Here's the Anti-Join option

SELECT t1.Partnumber, MAX(t1.PO)
FROM MyTable t1
     LEFT JOIN
      (SELECT DISTINCT PartNumber From MyTable
       WHERE  COALESCE(Receipt, '') = '') t2
     ON t1.Partnumber = t2.Partnumber
WHERE
     t2.Partnumber is null
GROUP BY t1.PartNumber
无风消散 2025-01-05 07:41:27
SELECT MAX(PO)
FROM aTable
WHERE PartNumber NOT IN (
   SELECT PartNumber
   FROM aTable
   WHERE Receipt IS NULL
   GROUP BY PartNumber
   HAVING PartNumber IS NOT NULL /* fix */
)
GROUP BY PartNumber
SELECT MAX(PO)
FROM aTable
WHERE PartNumber NOT IN (
   SELECT PartNumber
   FROM aTable
   WHERE Receipt IS NULL
   GROUP BY PartNumber
   HAVING PartNumber IS NOT NULL /* fix */
)
GROUP BY PartNumber
小耗子 2025-01-05 07:41:27

编辑:根据评论中的澄清,这简化为:

如果我理解正确,应该这样做:

SELECT MAX(PO)
FROM Table
GROUP BY PartNumber
HAVING MAX(Receipt) = 0

HAVING子句将消除任何PartNumber,其中甚至有一个具有该 PartNumber 的任何行的非零 Receipt 条目。

Edit: Based on clarification in comments, this simplifies to:

If I understand you correctly, this should do it:

SELECT MAX(PO)
FROM Table
GROUP BY PartNumber
HAVING MAX(Receipt) = 0

The HAVING clause will eliminate any PartNumber where there is even a single non-zero Receipt entry for any rows with that PartNumber.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文