简体   繁体   English

Oracle Group by NULL中的SQL返回多行

[英]SQL in Oracle Group by NULL returns multiple rows

I have a sql statement which is trying to retrieve the rows having a count greater than 1 for all the similar rows. 我有一条sql语句正在尝试检索所有类似行的计数大于1的行。 In all those rows, one field appears to be holding null value. 在所有这些行中,一个字段似乎保持空值。

Case 1 : 情况1 :

Let's say the table name is ABC 假设表格名称是ABC


BUSINESS_UNIT, INVOICE, FLAG BUSINESS_UNIT,发票,标志

A 1 (Null) A 1(空)

A 1 (Null) A 1(空)


SQL Statement : SELECT BUSINESS_UNIT, INVOICE, FLAG from TABLE ABC group by BUSINESS_UNIT, INVOICE, FLAG having COUNT(*) > 1 SQL语句:按COUNT(*)> 1的BUSINESS_UNIT,INVOICE,FLAG从TABLE ABC组中选择SELECT BUSINESS_UNIT,INVOICE,FLAG

I am expecting it to return no rows as both the flags are basically blank. 我期望它不返回任何行,因为两个标志基本上都是空白的。 But it returns the 但它返回


BUSINESS_UNIT, INVOICE, FLAG BUSINESS_UNIT,发票,标志

A 1 (Null) A 1(空)


Case 2 : 情况2:

TABLE ABC 表ABC


BUSINESS_UNIT, INVOICE, FLAG BUSINESS_UNIT,发票,标志

A 1 (Null) A 1(空)

A 1 (Null) A 1(空)

B 1 1 B 1 1

B 1 (Null) B 1(无)


SQL Statement : SELECT BUSINESS_UNIT, INVOICE, FLAG from TABLE ABC group by BUSINESS_UNIT, INVOICE, FLAG having COUNT(*) >1 SQL语句:按COUNT(*)> 1的BUSINESS_UNIT,INVOICE,FLAG从TABLE ABC组中选择SELECT BUSINESS_UNIT,INVOICE,FLAG

I am expecting the result to be 我期望结果是


BUSINESS_UNIT, INVOICE, FLAG BUSINESS_UNIT,发票,标志

B 1 1 B 1 1


This is for Oracle 11g. 这是针对Oracle 11g的。 Can someone please help on this and let me know if we can use any of the delivered functions in Oracle? 有人可以帮忙吗,让我知道我们是否可以使用Oracle中提供的任何功能?


After editing the original sql statements. 编辑原始sql语句后。

I should actually have removed flag from the Group by and select and added to the count in order to identify multiple flags for similar rows. 实际上,我应该从Group by中删除标志,然后选择并添加到计数中,以便为相似的行标识多个标志。


Thanks! 谢谢!

well, null is also a value, so if you group by a nullable field, null values will be distinct from non null values. 好吧,null也是一个值,因此,如果按可为空的字段分组,则空值将不同于非空值。

If you don't wanna null values, just add a where clause 如果您不想使用空值,只需添加一个where子句

SELECT INVOICE 
from TABLE ABC 
where flag is not null
group by BUSINESS_UNIT, INVOICE, FLAG 
having COUNT(*) >1

This is your first query: 这是您的第一个查询:

SELECT BUSINESS_UNIT, INVOICE, FLAG
from TABLE ABC
group by BUSINESS_UNIT, INVOICE, FLAG
having COUNT(*) > 1;

The count(*) is returning the number of rows in each group. count(*)返回每个组中的行数。 One of the groups is flag and the NULL values are all grouped together. 组之一是flag ,并且NULL值全部分组在一起。 If you want to count non-NULL values of flag , then you can use: 如果要计算flag非NULL值,则可以使用:

SELECT BUSINESS_UNIT, INVOICE, FLAG
from TABLE ABC
group by BUSINESS_UNIT, INVOICE, FLAG
having COUNT(flag) > 1;

Or filter as Raphaël Althaus suggests. 或按照RaphaëlAlthaus的建议进行过滤。

Your second query is: 您的第二个查询是:

SELECT INVOICE
from TABLE ABC
group by BUSINESS_UNIT, INVOICE, FLAG
having COUNT(*) > 1;

You seem to magically want three columns in the output when there is only one column in the select . select只有一列时,您似乎神奇地希望输出中有三列。 Yes, you can have more columns in the group by than in the select , if you really want. 是的,如果您确实想要的话,您可以在group by比列select更多的列。

Your final question is: 您的最后一个问题是:

Can someone please help on this and let me know if we can use any of the delivered functions in Oracle? 有人可以帮忙吗,让我知道我们是否可以使用Oracle中提供的任何功能?

Of course you can. 当然可以。 Oracle has a very powerful set of functions. Oracle具有非常强大的功能集。 Most of them are consistent with the ANSI standard and behave the same way on Oracle and on other databases. 它们中的大多数与ANSI标准一致,并且在Oracle和其他数据库上的行为相同。 You just need to learn how to use them correctly. 您只需要学习如何正确使用它们。

Besides adding a WHERE-clause as suggested by Raphaël Althaus you might also change count(*) to count(flag): 除了添加RaphaëlAlthaus建议的WHERE条款外,您还可以将count(*)更改为count(flag):

SELECT INVOICE 
from TABLE ABC 
group by BUSINESS_UNIT, INVOICE, FLAG 
having COUNT(flag) >1

I think I have found what I was intending to look for. 我想我已经找到了想要的东西。

SELECT BUSINESS_UNIT, INVOICE from TABLE ABC group by BUSINESS_UNIT, INVOICE having COUNT(DISTINCT Flag) >1 SELECT BUSINESS_UNIT,由BUSINESS_UNIT从TABLE ABC组中获得INVOICE,且COUNT(DISTINCT标志)> 1的INVOICE

Case 1: It returns no rows 情况1:不返回任何行

Case 2 : It returns 1 row 情况2:返回1行


BUSINESS_UNIT, INVOICE BUSINESS_UNIT,发票

B 1 B 1


Thanks Raphael, Dnoeth and Gordon for your help on this! 感谢Raphael,Dnoeth和Gordon在此方面的帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM