自定义 SQL GROUP BY 子句
我有一个非常定制的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
此处的
NOT EXISTS
将排除任何具有零件编号的行,而收据已填充在表中的任何位置。The
NOT EXISTS
here will exclude any row that has a partnumber for which a receipt is populated anywhere in the table.这是反加入选项
Here's the Anti-Join option
编辑:根据评论中的澄清,这简化为:
如果我理解正确,应该这样做:
HAVING
子句将消除任何PartNumber
,其中甚至有一个具有该PartNumber
的任何行的非零Receipt
条目。Edit: Based on clarification in comments, this simplifies to:
If I understand you correctly, this should do it:
The
HAVING
clause will eliminate anyPartNumber
where there is even a single non-zeroReceipt
entry for any rows with thatPartNumber
.