Total Pageviews

2014/05/12

[Oracle] LISTAGG function

Requirement
The following SQL statement will retrieve multiple row of data
SELECT PDATE,
       ACC || F_SYS_FIND_ACC_NM(:SCODE, :FYR, ACC, '000') AS ACC_NM
FROM CF0006
WHERE ACC LIKE '24%'
      AND LENGTH(TRIM(ACC))=11


But my requirement is to turn these multiple row of data into a single row per group.

Solution
Oracle provided LISTAGG function to fulfill this kind of requirement.

LISTAGG is an aggregate function that can optionally be used as an analytic (i.e. the optional OVER() clause). The following elements are mandatory:
  • the column or expression to be aggregated;
  • the WITHIN GROUP keywords;
  • the ORDER BY clause within the grouping.
The arguments to the function are subject to the following rules:
  • The measure_expr can be any expression. Null values in the measure column are ignored.
  • The delimiter_expr designates the string that is to separate the measure values. This clause is optional and defaults to NULL.
  • The order_by_clause determines the order in which the concatenated values are returned. The function is deterministic only if the ORDER BY column list achieved unique ordering.
For example. The following group-set aggregate example lists, for each pdate in the CF0006 table, the ACC_NM in that CF0006 in order of their pdate (separate with comma):

SELECT PDATE,
       LISTAGG(ACC || F_SYS_FIND_ACC_NM(:SCODE, :FYR, ACC, '000'), ', ') WITHIN
       GROUP (ORDER BY PDATE) "ACC_NM_LIST"
FROM CF0006
WHERE ACC LIKE '24%'
      AND LENGTH(TRIM(ACC))=11
GROUP BY PDATE;


Reference
[1] http://docs.oracle.com/cd/E11882_01/server.112/e10592/functions089.htm#SQLRF55593
[2] http://www.oracle-developer.net/display.php?id=515


2014/05/09

java.lang.NullPointerException: Cannot find parameter "json" from request.


Problem
I found out I fail to execute print functions in every function in my system.


And the console throw this exception message:
 java.lang.NullPointerException: Cannot find parameter "json" from request.  
   at java.util.Objects.requireNonNull(Objects.java:226) ~[na:1.7.0_25]  
   at com.cht.commons.web.resolver.JsonParamResolver.resolveArgument(JsonParamResolver.java:34) ~[cht-commons-web-0.1.0-SNAPSHOT.jar:0.1.0-SNAPSHOT]  

Root Cause
As I check its request header, it submit nothing



It result from my request had been blocked by AdBlock (Chrome Plug-in), which I installed yesterday. That's why I failed to execute every print function today.

As I disabled this Chrome plug-in, AdBlock, and check its request header again. We can see request header had submitted data.

Finally, my report function had recovered.





2014/05/02

ORA-01422 Error From Oracle Function

Problem
I wrote an Oracle function on my own as bellows:
CREATE OR REPLACE FUNCTION F_FMS420R_GET_ACCUM_AMTA (S_YYY_MM IN VARCHAR2 --起始年月
 ,E_YYY_MM IN VARCHAR2 --結束年月
 ,ASP IN VARCHAR2 --性質別
) RETURN NUMBER IS AMT_A NUMBER(20, 2) := 0;

 BEGIN
SELECT AMT_A INTO AMT_A FROM
  (SELECT YEAR, ASP, SUM(AMT_A) AS AMT_A
   FROM FMS420FA
   WHERE (YYY_MM BETWEEN S_YYY_MM AND E_YYY_MM)
     AND ASP = ASP
   GROUP BY YEAR, ASP);

 RETURN (AMT_A);

 END F_FMS420R_GET_ACCUM_AMTA;

After I test it, it show this error message as bellows:
 ORA-01422: exact fetch returns more than requested number of rows (精確擷取傳回的列數超過所要求的列數) 
 ORA-06512: 在 "AP_PSR.F_FMS420R_GET_ACCUM_AMTA", line 9  
 ORA-06512: 在 line 11  

But this select SQL statement must return one record, if I provide all search criteria
SELECT YEAR,
       ASP,
       SUM(B.AMT_A) AS AMT_A
FROM FMS420FA B
WHERE B.YYY_MM BETWEEN :S_YYY_MM AND :E_YYY_MM
  AND B.ASP = :ASP
GROUP BY YEAR,
         ASP

Root cause

This error result from the parameter name cannot be equal to database column name. If yes, Oracle will not know what I am doing and ignore this search criteria. That's why it will have ORA-01422 error.

Solution
Just rename input parameter name from ASP to INPUT_ASP, then this problem can be resolved.
CREATE OR REPLACE FUNCTION F_FMS420R_GET_ACCUM_AMTA (S_YYY_MM IN VARCHAR2 --起始年月
 ,E_YYY_MM IN VARCHAR2 --結束年月
 ,INPUT_ASP IN VARCHAR2 --性質別
) RETURN NUMBER IS AMT_A NUMBER(20, 2) := 0;

 BEGIN
SELECT AMT_A INTO AMT_A FROM
  (SELECT YEAR, ASP, SUM(AMT_A) AS AMT_A
   FROM FMS420FA
   WHERE (YYY_MM BETWEEN S_YYY_MM AND E_YYY_MM)
     AND ASP = INPUT_ASP
   GROUP BY YEAR, ASP);

 RETURN (AMT_A);

 END F_FMS420R_GET_ACCUM_AMTA;


2014/04/29

[Apache Maven Problem] org/apache/maven/shared/ filtering/MavenFilteringException

Problem
As I build my JavaEE project with Apache Maven in Eclipse, it show this error message in my Eclipse
 Multiple annotations found at this line:  
 Execution default-resources of goal org.apache.maven.plugins:maven-resources-plugin:2.6:resources failed: A required class was missing while executing org.apache.maven.plugins:maven-resources-plugin:2.6:resources: org/apache/maven/shared/filtering/MavenFilteringException   
 -----------------------------------------------------   
 realm =      plugin>org.apache.maven.plugins:maven-resources-plugin:2.6   
 strategy = org.codehaus.plexus.classworlds.strategy.SelfFirstStrategy   
 urls[0] = file:/C:/Users/javis-msi/.m2/repository/org/apache/maven/     plugins/maven-resources-plugin/2.6/maven-resources-plugin-2.6.jar   
 urls[1] = file:/C:/Users/javis-msi/.m2/repository/org/apache/maven/reporting/maven-reporting-api/2.0.6/maven-reporting-api-2.0.6.jar   
 urls[2] = file:/C:/Users/javis-msi/.m2/     repository/org/apache/maven/doxia/doxia-sink-api/1.0-alpha-7/doxia-sink-api-1.0-alpha-7.jar   
 urls[3] = file:/C:/Users/javis-     msi/.m2/repository/commons-cli/commons-cli/1.0/commons-cli-1.0.jar   
 urls[4] = file:/C:/Users/javis-msi/.m2/repository/org/codehaus/plexus/plexus-interactivity-api/1.0-alpha-4/plexus-interactivity-api-1.0-alpha-4.jar   
 urls[5] = file:/C:/Users/javis-msi/.m2/     repository/junit/junit/3.8.1/junit-3.8.1.jar   
 urls[6] = file:/C:/Users/javis-msi/.m2/repository/org/codehaus/plexus/plexus-utils/2.0.5/plexus-utils-2.0.5.jar   
 urls[7] = file:/C:/Users/javis-msi/.m2/repository/org/apache/maven/shared/maven-filtering/1.1/maven-     filtering-1.1.jar   
 urls[8] = file:/C:/Users/javis-msi/.m2/repository/org/codehaus/plexus/plexus-interpolation/1.13/plexus-interpolation-1.13.jar   
 Number of foreign imports: 4  

I try to do mvn clean install again, again, and again. But still in vain.

Solution
This problem may result from you have broken jar files. Try to retrieve jar files again. The steps are as follows:

Step1. Go to you .m2 folder, and delete everything.

Step2. Retrieve all jar files again. 

2014/04/27

201404 Travel

烏來






2014/04/25

201404 Kyoto

京都某處櫻花


金閣寺

彥根城

平等院


奈良


平野神社

平安神宮




2014/04/16

CronMaker - a utility which helps you to build cron expressions

It's a good utility to build cron expression as I use Quartz to write batch program.
http://www.cronmaker.com/

2014/04/02

SVN error on Eclipse: org.tigris.subversion.javahl.ClientException: Attempted to lock an already-locked dir

Problem
As I commit my source code to SVN in eclipse, but Eclipse shut down unexpectedly. After I launch Eclipse again, and try to commit again, it show this error message:




 org.apache.subversion.javahl.ClientException: Attempted to lock an already-locked dir  
 svn: Commit failed (details follow):  
 svn: Working copy 'D:\workspace\fms\fms-webapp\src\main\java\gov\nta\fms\web\controller' locked.  
 svn: 'D:\workspace\fms\fms-webapp\src\main\java\gov\nta\fms\web\controller' is already locked. 

Solution
Right click on the offending project, click Team and then select Refresh/Cleanup. SVN gets the offending .lock files and deletes them. 

cleanup D:/workspace/fms/fms-webapp/src/main/java  

After Refresh/Cleanup, I can commit source code successfully
commit -m "add 4 spaces for rptInfo parameter" D:/workspace/fms/fms-webapp/src/main/java/gov/nta/fms/web/controller/Fms406rReportController.java  
   Sending    D:/workspace/fms/fms-webapp/src/main/java/gov/nta/fms/web/controller/Fms406rReportController.java  
   Transmitting file data ...  
   Committed revision 1264.  

Reference

2014/03/31

How to set Static IP Address from command line

Requirement
Owing I need to move workplace from office to customer site lately, I need to change my laptop's IP address as I change workplace. So I edit a batch file to set my static IP address automatically.

Syntax
Let’s say I want to give my Windows 7 system an IP address of 10.144.38.195, a subnet mask of 255.255.255.0, a default gateway of 10.144.38.254. Type the following command and save as a batch file with encoding with ANSI
 netsh interface ipv4 set address name="區域連線" source=static address=10.144.38.195 mask=255.255.255.0 gateway=10.144.38.254  

The address will be found via netsh interface ipv4 show interfaces command.
Idx   Met     MTU     狀態         名稱  
 --- ---------- ---------- ------------ ---------------------------  
  1     50 4294967295 connected   Loopback Pseudo-Interface 1  
  13     50    1500 disconnected 無線網路連線  
  12      1    1500 disconnected 區域連線  
  19     30    1500 disconnected 區域連線 2  
  16     40    1500 disconnected Bluetooth 網路連線  
  20     20    1500 connected   區域連線 3  

In this case, I would like to configure 區域連線. So the address name="區域連線"

Step by step
Step1. open command prompt.
Step2. execute the batch script file, i.e.SetIP4Office.bat
 C:\Users\albert\Desktop>SetIP4Office.bat  
 C:\Users\albert\Desktop>netsh interface ipv4 set address name="區域連線" source=static address=10.14  
 4.38.195 mask=255.255.255.0 gateway=10.144.38.254  

Well Done!

So you can edit different batch files to set different IP address for different workplace.

Reference
[1] http://www.windowsreference.com/networking/how-to-set-staticdhcp-ip-address-from-command-line/

Bat(Batch) script 執行出現出現中文亂碼問題

Problem
I edit a batch script which be used to set IP address
netsh interface ipv4 set address name="區域連線" source=static address=10.144.38.195 mask=255.255.255.0 gateway=10.144.38.254 

But when I execute this batch script in command prompt, it seems that the Traditional Chinese characters are broken.
C:\Users\albert\Desktop>netsh interface ipv4 set address name="?€?€??" source=static address=10.14  
 4.38.195 mask=255.255.255.0 gateway=10.144.38.254  
 檔案名稱、目錄名稱或磁碟區標籤語法錯誤。  

Solution
This problem results from the encoding of the batch script file. You need to change its encoding from UTF-8 to ANSI.

Reference
[1] http://www.bathome.net/viewthread.php?tid=24088

2014/03/27

投資的最高境界是「不交易」!

這篇文章不錯

[quote 1]「掌握波段,比殺進殺出要賺得多。」

[quote 2]大盤K值從高處滑落到20附近,就開始買,一路往下買,回彈到80以上,就一路往上賣。

[quote 3]「停損」當然是最重要的一招。只要持股跌了10%到15%,就該執行停損,認賠出場。


[quote 4]如果你忘了當初買進的價格,或許你在面對它反彈到相對高價時,你會捨得去賣它。什麼叫「相對高價」?就是技術指標呈現過熱訊號,或是股價來到重要壓力區。

[quote 5]股市還有一句名言:「會買股票不稀奇,會賣股票才是師父。」就算是手上的股票處於獲利狀態,也要懂得「停利」。KD值都來到95以上,這時你若還奢望持股會繼續漲不停,就很可能只是一場「紙上富貴」



http://www.businesstoday.com.tw/article-content-80494-106484?page=1


2014/03/25

How do I insert a record if it does not exist in Oracle?

Requirement
Here has two report items as following:
Each year has its own report items, ex. 102年度--未滿一年之短期融資, 103年度--未滿一年之短期融資, 102年度--公債及賒借收入, 103年度--公債及賒借收入, etc.

This sort of information had been stored in this table.

Our requirement is to generate the two report items automatically next year.

Solution
We can use merge statement to accomplish this requirement.
Step1: Find record(s) in FMS405FA based on our defined search criteria
Step2: Result
2.1: If does not find any record, then insert data into FMS405FA
2.2: If found record(s), then skip it.
MERGE INTO FMS405FA T1 
USING DUAL ON(T1.ACCYR =
     (SELECT TRIM(TO_CHAR(EXTRACT(YEAR FROM SYSDATE)-1911, '000'))  FROM DUAL)
      AND T1.RPT_TYPE='FMS434R'
      AND T1.RPT_NO='0001') 
WHEN NOT MATCHED THEN 
  INSERT(T1.ACCYR, T1.RPT_TYPE, T1.RPT_NO, T1.RPT_NM)
  VALUES(
         (SELECT TRIM(TO_CHAR(EXTRACT(YEAR FROM SYSDATE)-1911, '000'))
          FROM DUAL), 'FMS434R', '0001',

         (SELECT TRIM(TO_CHAR(EXTRACT(YEAR FROM SYSDATE)-1911, '000'))
          FROM DUAL)||'年度--未滿一年之短期融資')


MERGE INTO FMS405FA T1 
USING DUAL 
ON(T1.ACCYR =
   (SELECT TRIM(TO_CHAR(EXTRACT(YEAR FROM SYSDATE)-1911, '000')) FROM DUAL)
   AND T1.RPT_TYPE='FMS434R'
   AND T1.RPT_NO='0002') 
WHEN NOT MATCHED THEN 
  INSERT(T1.ACCYR, T1.RPT_TYPE, T1.RPT_NO, T1.RPT_NM)
  VALUES(
         (SELECT TRIM(TO_CHAR(EXTRACT(YEAR FROM SYSDATE)-1911, '000'))
          FROM DUAL), 'FMS434R', '0002',

         (SELECT TRIM(TO_CHAR(EXTRACT(YEAR FROM SYSDATE)-1911, '000'))
          FROM DUAL)||'年度--公債及賒借收入')

Reference
[1] http://docs.oracle.com/cd/B28359_01/server.111/b28286/statements_9016.htm#SQLRF01606

2014/03/20

How to create a Boolean field in Oracle

Problem
If we would like to create a field, it will store the information if this datum had create by external system or be added by manual. 
Originally, I would like to crate a field with Boolean data type. But it seems that Oracle does not support Boolean data type.

Solution
We can use the check constraint to limit its value, i.e. 'Y' means adding by manual, 'N' means adding by external system.
ex.
ALTER TABLE FMS435FB ADD CONSTRAINT SELF_ADDED_CHECK CHECK (SELF_ADDED IN ('N',
                                                                           'Y'));


Reference
[1] http://www.techonthenet.com/oracle/check.php

How to set the width of modal dialog

Problem
User claimed they do not want to see the scroll bar in the modal dialog.
How do I set the width of dialog manually?

Solution
We can set the modal dialog thru style.

Step1.  Defined a css style for the modal dialog in the head section of an HTML page, and using the style tag.
 <style type="text/css">  
 .fms435rTab3Dialog {  
   width: 900px;  
   left: 40%;  
 }  
 </style>  

Step2. Assign css into windowClass which is one of modal dialog's parameter
1:  $scope.create = function() {  
2:        openDialog();  
3:      };  
4:      var openDialog = function(){  
5:        var options = {  
6:            templateUrl: 'fms435rTab3Dialog.html',  
7:            controller: 'fms435rTab3Controller',  
8:            windowClass:"fms435rTab3Dialog",  
9:            backdrop: true,  
10:            keyboard: true,  
11:            backdropClick: true  
12:        };  
13:        $modal.open(options);  
14:      };  

See...the width had been adjusted.


Reference

2014/03/13

Error: ng:areq Bad Argument

Problem
I have a html page with 2 tabs.

As I entered this page, it occurred javascript errors with "Error: ng:areq" and link to this page: http://docs.angularjs.org/error/ng/areq?p0=fms435rTab1Controller&p1=not%20a%20function,%20got%20undefined


Trace the Problem
There are three html files to consist of this page. Each html file have its own JavaScript file, and will be imported by mail page.

fms435r.js
 (function() {  
   var app = angular.module("fms435rApp", [ 'ntaCommonModule' ]);  
 })();  

fms435rTab1.js
(function() {  
   var app = angular.module("fms435rApp");  
   app.factory('fms435rService', function(cResource) {  
     return {};  
   });  
   app.controller('fms435rTab1Controller', function($scope, fms435rService,  
       stateManager, alerter, userHolder) {      
   });  
 })(); 

fms435rTab2.js
 (function() {  
   var app = angular.module("fms435rApp", [ 'ntaCommonModule' ]);  
   app.factory('fms435rService', function(cResource) {  
     return {};  
   });  
   app.controller('fms435rTab2Controller', function($scope, fms435rService,  
       stateManager, alerter, userHolder) {  
   });  
 })();  

Root Cause
The problem result from fms435rTab2.js. 
It misuse angular.module, because it create a new module, not to retrieve.
Therefore, you should fix it as bellows:
 (function() {  
   var app = angular.module("fms435rApp");  
   app.factory('fms435rService', function(cResource) {  
     return {};  
   });  
   app.controller('fms435rTab2Controller', function($scope, fms435rService,  
       stateManager, alerter, userHolder) {  
   });  
 })(); 

Here is the syntax
ParamTypeDetails
namestring
The name of the module to create or retrieve.
requires
(optional)
Array.=
If specified then new module is being created.
If unspecified then the module is being retrieved for
further configuration.
configFnFunction
Optional configuration function for the module.
 Same as Module#config().

Reference
[1] http://docs.angularjs.org/api/ng/function/angular.module

2014/03/12

ORACLE/PLSQL: ADD_MONTHS FUNCTION

Requirement
If we would like to do month calculation in SQL, how to do it?

Solution
Oracle provided a build-in ADD_MONTHs function for us, the syntax is as bellow:



For example, if I would like to get last month and in Chinese calendar system format, the SQL statement will be looking like this:


SELECT TRIM(TO_CHAR(EXTRACT(YEAR
                            FROM ADD_MONTHS(SYSDATE, -1))-1911, '000'))|| TRIM(TO_CHAR(EXTRACT(MONTH
                                                                                             FROM ADD_MONTHS(SYSDATE, -1)), '00')) AS LAST_MONTH
FROM DUAL

Reference
[1] http://www.techonthenet.com/oracle/functions/add_months.php
[2] http://docs.oracle.com/cd/B19306_01/server.102/b14200/functions004.htm

2014/03/05

How to resolve ORA-01790

Problem
When I use UNION ALL syntax to union two select SQL statement, Oracle throw "ORA-01790 expression must have same datatype as corresponding expression" error message.

This error result from I would like to union two result with different data type.

Solution
You can make good of CAST function to cast into the same data type and do union all.

For example. 
SELECT CAST(NVL(FMS406FB.DAY_RSLT, 0) AS NVARCHAR2(30)) DAY_RSLT --每日結存數
FROM .....

Reference
[1] http://docs.oracle.com/cd/B19306_01/server.102/b14200/functions016.htm

2014/02/28

2014/02 Travel


自由廣場




2014/02/27

Oracle: how to do insert if data doesn't exist

Here is my database schema:

Requirement
If we cannot find data via ROC_YM, primary key, in FMS415FA, then it should insert specific ROC_YM data into this table

How to do to fulfill this requirement
INSERT INTO FMS451FA(ROC_YM)
SELECT :ROC_YM
FROM dual
WHERE NOT EXISTS
    (SELECT ROC_YM
     FROM FMS451FA
     WHERE ROC_YM = :ROC_YM)

2014/02/26

Oracle: how to UPSERT (Update or Insert)

Assume we have a requirement to read text file which sent by external system, and the UPSERT operation either updates or inserts a row in a table, depending if the table already has a row that matches the data.

Oracle 9i introduced the MERGE statement. The MERGE statement takes a list of records which are usually in a staging table, and adds them to a master table. If the record exists in the master table, it should be updated with the new values in the staging table, otherwise insert the record from the staging table.

MERGE statement doesn't always need "multiple tables", but it does need a query as the source. Something like this should work:

MERGE INTO FMS420FB T1 USING DUAL ON (T1.YEAR = :YEAR
                                      AND T1.MONTH= :MONTH
                                      AND T1.SUBJECT_ID=:SUBJECT_ID) WHEN MATCHED THEN
UPDATE
SET T1.SUBJECT=:SUBJECT,
               T1.AMT=:AMT WHEN NOT MATCHED THEN
INSERT (T1.YEAR,
        T1.MONTH,
        T1.SUBJECT_ID,
        T1.SUBJECT,
        T1.AMT)
VALUES(:YEAR,
       :MONTH,
       :SUBJECT_ID,
       :SUBJECT,
       :AMT)