怎样在Form block中实现Group by---一篇不错的文章

怎样在Form block中实现Group by---一篇不错的文章

Problem Description:
====================

In Oracle Forms, you define a GROUP BY clause for a block
by setting the WHERE Clause or ORDER BY Clause block property.

You run the form.
When you try to execute a query, the following error occurs:

FRM-40505: ORACLE error: unable to perform query.

When you select Help-->Display error, one of the following errors displays:

Error 1: without setting WHERE or ORDER BY clause
-------------------------------------------------
SELECT <column1, column2, etc.>,ROWID FROM TABLE
WHERE GROUP BY <column1, column2, etc.>
or
SELECT <column1, column2, etc.>,ROWID FROM TABLE
WHERE GROUP BY <*subset* of column1, column2, etc.>
or
SELECT <column1, column2, etc.>,ROWID FROM TABLE
ORDER BY GROUP BY <column1, column2, etc.>
or
SELECT <column1, column2, etc.>,ROWID FROM TABLE
ORDER BY GROUP BY <*subset* of column1, column2, etc.>
and
ORA-00936: missing expression


Error 2: ORDER BY Clause lists a column
---------------------------------------
SELECT <column1, column2, etc.>,ROWID FROM TABLE
ORDER BY <column> GROUP BY <column1, column2, etc.>
or
SELECT <column1, column2, etc.>,ROWID FROM TABLE
ORDER BY <column> GROUP BY <*subset* of column1, column2, etc.>
and
ORA-00933: SQL command not properly ended


Error 3: WHERE Clause has a condition and
you GROUP BY a subset of columns in the SELECT list
------------------------------------------------------------
SELECT <column1, column2, etc.>,ROWID FROM TABLE
WHERE <condition> GROUP BY <*subset* of column1, column2, etc.>
and
ORA-00979: not a GROUP BY expression


How do you specify a GROUP BY clause for a block in Oracle Forms?

Solution Description:
=====================

To query a block with a GROUP BY clause, do the following:

1. Specify the GROUP BY clause in the WHERE Clause block property.

Error 1
-------
ORA-00936 occurs when you specify only a GROUP BY clause
in either the WHERE Clause or ORDER BY Clause block property.
Oracle Forms automatically inserts the word WHERE into the
SQL statement when you set the WHERE Clause block property and
the word ORDER BY when you set the ORDER BY Clause block property.
The construction of the SQL statement is invalid with the presence of
WHERE GROUP BY or ORDER BY GROUP BY.

Workaround:
In the WHERE Clause block property, precede the GROUP BY clause
with a dummy WHERE condition (e.g. 1=1):
1=1 GROUP BY <column1, column2, etc.>


Error 2
-------
ORA-00933 occurs because the GROUP BY clause must precede the
ORDER BY clause. Hence, you cannot set the GROUP BY clause in the
ORDER BY Clause block property.


Error 3
-------
ORA-00979 occurs when you specify the GROUP BY clause in the
WHERE Clause block property and do not reference all columns
in the SELECT list in the GROUP BY clause. All columns in the
SELECT list that are not in group functions must be in the
GROUP BY clause.


2. Every SELECT statement issued by Oracle Forms contains a
pseudocolumn ROWID. Because all columns in the SELECT list
that are not in group functions must be in the GROUP BY clause,
ROWID must also be in the GROUP BY clause. However, because ROWID
is unique for every row, grouping by ROWID does not yield the
desired results.

Workaround:
To avoid grouping by ROWID, turn off the selection of ROWID:
a. Set the following block properties:
Key Mode: Non-Updateable
Primary Key: True (On)

Setting the Key Mode block property to Non-Updateable
eliminates the need for Oracle to use the ROWID as the
unique identifier for each row.

b. For at least one item in the block, set the following item property:
Primary Key: True (On)

Note: Oracle Forms 4.5.7.11.0 lets you get away with not
setting the above properties when specifying a dummy
WHERE condition and GROUP BY clause in the
WHERE Clause block property. ROWID does not end up in
the SELECT list despite the Key Mode property setting.
Other versions (e.g. 4.5.6.5.5, 4.5.7.1.6, 4.5.8.1.2)
require you to set the above properties.

3. When you use a GROUP BY clause in the WHERE Clause block property,
do not allow users to insert or update data in the block.

Run the form, and query the block.
This displays the grouped records.


Solution Explanation:
=====================

Oracle does not support modification of the WHERE Clause and ORDER BY Clause
block properties in non-standard ways. Refer to the WHERE/ORDER BY Clause
documentation in the Oracle Forms 4.X Reference Manual:

WHERE Clause and ORDER BY Clause are standard SQL clauses,
and must follow the rules for such constructs as specified
in the SQL Language Reference Manual.

Enhancement request 565914 for a GROUP BY Clause block property
in Forms has been filed.


Additional Information:
=======================

Oracle Documentation:
---------------------
Oracle Forms 4.X Reference Manual
Chapter 5, Properties
Key Mode, Primary Key (Block), Primary Key (Item), and
WHERE Clause/ORDER BY Clause

Oracle7 Server SQL Reference
Chapter 4, Commands
SELECT

Enhancement Request:
--------------------
565914
CREATE A GROUP BY CLAUSE BLOCK PROPERTY IN ORACLE FORMS

你可能感兴趣的:(oracle,sql,SQL Server,UP)