0

I am new to Oracle.

I tried a query in MySQL and it works fine

select 
    segment1, description, organization_id
from
    apps.mtl_system_items_b
where
    organization_id IN (110 , 268, 448)
GROUP BY segment1
HAVING COUNT(DISTINCT organization_id) = 3

But in Oracle, it says not a group by expression. What's wrong?

EDIT:-

I want the result like this

organisation_id   segment1
110               306145244
268               306145244
448               306145244
110               444444444
268               444444444
448               444444444
4

3 回答 3

1

You have ungrouped an unaggregated expressions in your SELECT list.

MySQL allows them (without specifying which of many possible values will it return), but Oracle does not.

By definition, your query will return only segments with 3 different organizations. So which of those organizations you want to return?

If you only want to return segment, just use this:

SELECT  segment1
FROM    apps.mtl_system_items_b
WHERE   organization_id IN (110 , 268, 448)
GROUP BY
        segment1
HAVING  COUNT(DISTINCT organization_id) = 3

If you want all items with segments belonging to three organizations, use this:

SELECT  segment1, description, organization_id
FROM    (
        SELECT  i.*,
                COUNT(DISTINCT organization_id) OVER (PARTITION BY segment1) cnt
        FROM    apps.mtl_system_items_b
        WHERE   organization_id IN (110, 268, 448)
        )
WHERE   cnt = 3
于 2013-04-25T06:25:23.173 回答
0

Sorry for the quick answer

select 
segment1, description, organization_id from
apps.mtl_system_items_b where
organization_id IN (110 , 268, 448) 
GROUP BY segment1, description, organization_id 
HAVING COUNT(DISTINCT organization_id) =3

Maybe this explanation could help you to gain some understanding in terms of GROUP BY

The SQL GROUP BY clause can be used in an SQL SELECT statement to collect data across multiple records and group the results by one or more columns.

The syntax for the SQL GROUP BY clause is:

SELECT column1, column2, ... column_n, aggregate_function (expression)
FROM tables
WHERE predicates
GROUP BY column1, column2, ... column_n;

aggregate_function can be a function such as SQL SUM function, SQL COUNT function, SQL MIN function, or SQL MAX function.

Since you have the aggregate_function you need to group by each of the field you display or select.

Please refer to the depth-explanation below

http://docs.oracle.com/javadb/10.6.2.1/ref/rrefsqlj32654.html

于 2013-04-25T06:24:42.140 回答
0

Here's the reason why the query work on MySQL: MySQL Extensions to GROUP BY.

In Oracle and any other RDBMS, this won't work because you need to explicitly list all the non-aggregated columns on the group by clause. To make it work, add them on the GROUP BY clause

GROUP BY segment1, description, organization_id
于 2013-04-25T06:25:33.040 回答