[DB2] order by sum()

Dev/DB 2012. 7. 20. 14:59

order by sort-key 부분에 들어가는 값

sum()이 들어가면 안됨!!!(ms-sql)은 정상적으로 원하는 결과를 가져옴. 


alias를 사용해서 가져와야 함.


select sum(a) as a

...

order by a 


------------------


sort-key
column-nameinteger, or sort-key-expression that specifies the value that is to be used to order the rows of the result of the subselect.

If a single sort-key is identified, the rows are ordered by the values of that sort-key. If more than one sort-key is identified, the rows are ordered by the values of the first sort-key, then by the values of the second sort-key, and so on. sort-key cannot be a LOB or XML expression.

The result table can be ordered by a named column in the select list by specifying a sort-key that is an integer or the column name. The result table can be ordered by an unnamed column in the select list by specifying a sort-key that is an integer or, in some cases, by a sort-key-expression that matches the expression in the select list.

column-name
An identifier that usually identifies a column of the result table. In this case, column-name must be the name of a named column in the select list. If the fullselect includes a set operator, the column name cannot be qualified.

If the query is a subselect, the column-name can also identify a column name of a table, view, or nested table expression identified in the FROM clause, including a column that is defined as implicitly hidden. The subselect must not include any of the following:

  • DISTINCT in the select list
  • Aggregate functions in the select list
  • GROUP BY clause
integer
Start of changeAn unsigned integer that must be greater than 0 and not greater than the number of columns in the result table. The integer n identifies the nth column of the result table.End of change
sort-key-expression
Start of changeAn expression that is not simply a column-name or unsigned integer constant. The query to which ordering is applied must be a subselect to use this form of the sort-key.End of change

The sort-key-expression cannot include an expression that is not deterministic or a function that is defined to have an external action except for the RID built-in function and the ROW CHANGE expression. Any column name in the expression must conform to the rules described Column names in sort keys. If sort-key-expression includes an aggregate function, the input arguments to that function must not reference a named column in the select list that is derived from an aggregate function. An expression cannot be specified if DISTINCT is used in the select list of the subselect.

If the subselect is grouped, the sort-key-expression might or might not be in the select list of the subselect. When sort-key-expression is not in the select list the following rules apply:

  • Each expression in the ORDER BY clause must either:
    • Use one or more grouping expressions
    • Use a column name that either unambiguously identifies a grouping column of R or is specified within a aggregate function.
  • Each expression in the ORDER BY clause must not contain a scalar fullselect. 


출처 : http://publib.boulder.ibm.com/infocenter/dzichelp/v2r2/topic/com.ibm.db2z10.doc.sqlref/src/tpc/db2z_sql_orderbyclause.htm


Posted by pwsunf
,

[DB2] fetch-first-clause

Dev/DB 2012. 7. 20. 13:54

The FETCH FIRST clause limits the number of rows that can be fetched. It

improves the performance of queries with potentially large result tables when only

a limited number of rows are needed.


출처 : DB2® Version 9.1 for z/OS® SQL Reference (SC18-9854-12)


---------------

How to Limit Query Results for DB2 Databases

Many times users are only interested in a subset of the results returned from an SQL query. DB2 provides a mechanism for limiting the records returned from an SQL query with the FETCH FIRST command. Using the FETCH FIRST syntax improves the performance of queries when not all results are required. Listed below are some examples explaining how to use FETCH FIRST.

Example 1: Returning the first 100 rows from a table called employee:

select * from employee fetch first 100 rows only

Example 2: Using the fetch first syntax with an order by clause

select * from employee order by salary desc fetch first 10 rows only 




자세한 정보는 아래 문서에서 확인.

출처 : http://www.razorsql.com/articles/db2_limit_query.html


Posted by pwsunf
,

-613

THE PRIMARY KEY OR A UNIQUE CONSTRAINT IS TOO LONG OR HAS TOO MANY COLUMNS

Explanation

The number of columns that are defined for a PRIMARY KEY or UNIQUE constraint is greater than 64, or the sum of the column length attributes is greater than the number allowed for the type of index.

If the failing statement was an ALTER TABLE statement that included the SET DATA TYPE clause to alter the definition of an existing column, a column being changed is part of a unique or primary constraint. The new sum of the lengths of the columns of the keys exceeds 255-n, where n is the number of columns allowing null values. This sum currently exceeds the maximum number that is allowed.

If the failing statement was an ALTER TABLE statement with a PARTITIONING KEY specification, only 64 columns can be specified, and the combined lengths of all columns specified in the PARTITIONING KEY clause exceeds the maximum length of 255-n, where n is the number of columns that can contain null values.

System action

The statement cannot be processed.

Programmer response

Change the table definition to keep within the prescribed limits.

SQLSTATE

54008


---

테이블 설계 변경...

---

출처 : http://publib.boulder.ibm.com/infocenter/dzichelp/v2r2/topic/com.ibm.db2z9.doc.codes/src/tpc/n613.htm

Posted by pwsunf
,

Technote (troubleshooting)


Problem(Abstract)

When you export crawled, analyzed, or searched documents to a relational database, the export fails and “SQL error: SQLCODE=-286, SQLSTATE=42727” is written to the system log in the ES_NODE_ROOT/logs directory.

Cause

Your database mapping file defines a table with more columns than can be contained in the current database table that you created.

Resolving the problem

Delete the database that you created and recreate it with a larger page size. For example, enter the command 

CREATE DB database_name PAGESIZE size.

To determine the page size to specify, check the value of the SQLERRMC parameter in the error message that appears in the export audit log in the ES_NODE_ROOT/logs/audit directory. For example, if the following message appears in the audit log, create a database with a page size of 8192 or higher:

com.ibm.db2.jcc.b.nm: DB2 SQL Error: SQLCODE=-286, SQLSTATE=42727, SQLERRMC=8192 ;DB2ADMIN, DRIVER=3.50.152


출처 : http://www-01.ibm.com/support/docview.wss?uid=swg21414318

------------
pagesize
  • 4K = 4096
  • 8K = 8192
  • 16K = 16384
  • 32K = 32768

Posted by pwsunf
,

Technote (troubleshooting)

Problem(Abstract)

Administrator creates a new DB2 database, which is going to be the Data Mart database. Administrator logs onto the Controller application server, and launches "Controller Configuration". Inside "Database Connections" administrator creates a connection to the new Data Mart target database. Administrator clicks green "Play" button to launch "Database Conversion Utility" (DbConv.exe). Administrator clicks "Connect". User receives error.

Symptom

** ERROR: com.ibm.db2.jcc.am.SqlSyntaxErrorException: DB2 SQL Error: SQLCODE=-204, SQLSTATE=42704, SQLERRMC=FASTNET.USER_TABLES, DRIVER=3.59.81


Cause

There are several possible causes for this error:

Environment

Controller Data Mart database hosted on DB2.

Diagnosing the problem

Scenario #1

Use the command DB2SET to check the DB2 server's settings.

For example, if your DB2 database server is installed on Windows then:

  1. Click "Start - Programs - IBM DB2 - DB2COPY1 (Default) - Command Line Tools - Command Window"
  2. Type "db2set"

On a correctly-configured server, the settings should include the following:
    DB2_DEFERRED_PREPARE_SEMANTICS=YES
    DB2_COMPATIBILITY_VECTOR=ORA

Resolving the problem

Scenario #1

Modify the DB2 database server to be Oracle compatible. Specifically, reconfigure it so that DB2_COMPATIBILITY_VECTOR=ORA.



    Steps:

    Assuming your DB2 database server is hosted on Windows:

    1. Obtain some downtime (nobody using ANY databases hosted on the DB2 server)
    2. Shutdown Controller application server
    3. Logon to DB2 database server as an administrator
    4. Click "Start - Programs - IBM DB2 - DB2COPY1 (Default) - Command Line Tools - Command Window"
    5. Type the following: db2set DB2_COMPATIBILITY_VECTOR=ORA
    6. If necessary, also type: db2set DB2_DEFERRED_PREPARE_SEMANTICS=YES
    7. Type: db2stop
    8. Type: db2start
    9. Restart Controller application server
    10. Test.

Scenario #2
Either:

-----
DB2 테스트 하기 어렵네 휴... DB2 Express-C 10.1은 뭔가 이상한데... 9.7로 작업...
해결책은 Everyone을 DB2USERS에 추가

출처 : http://www-01.ibm.com/support/docview.wss?uid=swg21594790


Posted by pwsunf
,