Total Pageviews

2014/02/13

How to use UNPIVOT to Convert Columns into Rows

Here is our original data structure:

We would like to turn columns into rows:

Here is our original SQL statement:
 SELECT ROC_YM,   
       CUM_INCM_TAX,   --當月收入(2)-稅課收入  
       CUM_NON_INCM_TAX, --當月收入(2)-非稅課收入(含其他)  
       CUM_INCM_LOAN,  --當月收入(2)-融資財源  
       CUM_TTL_INCM,   --當月收入(2)-合計  
       CUM_TTL_PAY_AMT  --當月支出(3)  
 FROM FMS406VE   
 WHERE roc_ym BETWEEN 09701 AND 10212  
 ORDER BY ROC_YM;  

If we would like to turn columns to rows, we need to utilize UNPIVOT as bellows:
 SELECT ROC_YM,   
       AMOUNT_TYPE,   
       AMOUNT  
 FROM FMS406VE   
 UNPIVOT INCLUDE NULLS(AMOUNT FOR AMOUNT_TYPE IN  
             (CUM_INCM_TAX AS '01',   --當月收入(2)-稅課收入  
                      CUM_NON_INCM_TAX AS '02', --當月收入(2)-非稅課收入(含其他)  
                       CUM_INCM_LOAN AS '03',  --當月收入(2)-融資財源  
                       CUM_TTL_INCM AS '04',   --當月收入(2)-合計  
                       CUM_TTL_PAY_AMT AS '05')) --當月支出(3)  
 WHERE ROC_YM BETWEEN 09701 AND 10212 --年月區間  
 ORDER BY ROC_YM, AMOUNT_TYPE; 

2014/02/11

Oracle Analytic functions

The general syntax of analytic function is:
Function(arg1,..., argn) OVER ( [PARTITION BY <...>] [ORDER BY <....>] [] )

  •  The OVER clause defines a window or user-specified set of rows within a query result set. A window function then computes a value for each row in the window. You can use the OVER clause with functions to compute aggregated values such as moving averages, cumulative aggregates, running totals, or a top N per group results
  • PARTITION BY divides the query result set into partitions. The window function is applied to each partition separately and computation restarts for each partition.
  • ORDER BY clause Defines the logical order of the rows within each partition of the result set. 
According to this SQL statement:
1:  select FYR, --會計年度  
2:        ROC_YM, --年月  
3:        INCM_LOAN --融資財源收入  
4:  from FMS406VC   
5:  where fyr=102 and ROC_YM=10201;  

We can get this result, and we would like to accumulate the value of INCM_LOAD:

We can use partition by clause to accumulate the value of INCM_LOAD
1:  select FYR, --會計年度  
2:        ROC_YM, --年月  
3:        SUM(SUM(INCM_LOAN)) OVER (PARTITION BY FYR ORDER BY ROC_YM) AS CUM_INCM_LOAD --累積融資財源收入  
4:  from FMS406VC   
5:  where fyr=102 and ROC_YM=10201  
6:  GROUP BY FYR ,ROC_YM;  

And we can get the expected result:

2014/02/10

How to convert year from Western to Chinese in Oracle? (在Oracle中如何將西元年轉民國年)

Requirement
Assume we had a table which contains all work days, and recorded in CALENDAR_DATE column with date data type.

According customers' requirement, they would to show Chinese year instead of Western year.

Solution
We can make good use of EXTRACT function which provide by Oracle.

SQL statement:
1:  SELECT CALENDAR_DATE,   
2:      TRIM(TO_CHAR(EXTRACT(YEAR FROM CALENDAR_DATE)-1911, '000'))||  
3:      TRIM(TO_CHAR(EXTRACT(MONTH FROM CALENDAR_DATE), '00'))||  
4:      TRIM(TO_CHAR(EXTRACT(DAY FROM CALENDAR_DATE), '00'))   
5:      AS CHINESE_CALENDAR  
6:   FROM SYS111FC  
7:  WHERE NO_WORK_IND = '0' AND ROC_YR='102'  

After executing the SQL as above, we can see 2013 had been convert to 102.


2014/02/07

Quick way to build prototypes - Moqups

Official site - https://moqups.com/home/

Moqups is a nifty HTML5 App used to create wireframes, mockups or UI concepts, prototypes depending on how you like to call them.


You can use Moqups to build prototype quickly to confirm user requirement as bellowing:



2014/01/15

2013-14 日本八天七夜

橫濱夜景


東京鐵塔


原宿街頭


皇居


吾妻橋夜景


東京天空樹


東京車站夜景


台場夜景


淺草寺


六本木夜景


六本木大蜘蛛

2014/01/09

Solution to ORA-00911(invalid character, 字元無效)

Problem
As I would like to execute an insert SQL statement, it throws ORA-00911 error. After googling, it recommend to "remove the invalid character from the statement or enclose the object name in double quotation marks". 
But the insert SQL statement is working fine in SQL Developer, only failed in java program.

Solution
Remove the semi-colon in the end of SQL statement.

2013/12/24

Cannot Import Java Source Code via Astah Pro

Problem
When I used Astah Pro to do import java code

But it show the error which complain encoding issues:


Solution
As we do import java code, we need to set the property of "Charset of imported files" to UTF-8.

Then we can import Java source code successfully.




An internal error occurred during: "Initializing Java Tooling" from Eclipse

Problem
One day as I startup my eclipse it show the error as bellows:

Solution
Step1. Close Eclipse
Step2. Go to your eclipse workspace, and go to this directory
 %workspace%/.metadata/.plugins/org.eclipse.jdt.core/  
ex.

Step3. delete variablesAndContainers.dat
Step4. Start Eclipse







2013/12/05

How to pad a string with spaces when size of data is unknown in Microsoft SQL Server

Requirement
When I select a character data with varchar(6) from Microsoft SQL Server....

And these data should have fixed length in report.
If the data is 01, it should right pad with 4 spaces.
If the data is 01234, it should right pad with 1 space.

Solution
If we would like to meet this requirement in SQL Server, we can use 'CAST' function which provided by SQL Server
select cast(repno as char(6)) from NSS313FA  

Utilizing cast function in SQL statement, you can get fixed length in report no matter how long the data is.
   


2013/12/04

How to display column footer immediately without any spaces after details band

Problem
This is my report design in iReport:

Here is the Detail property and Column Footer property setting:

But...as I generate pdf, there has unnecessary spaces between Detail band and Column Footer band.

Solution
Step1. Right click --> property

Step2. Check the "Float column footer" property

Then this problem will be solved. There will not have any spaces between Detail band and Column Footer band.








2013/11/29

Why I can't print dashed lines?

Problem
This is my setting in iReport. The line had been set as dashed line.

And you can see the dashed line in chrome pdf viewer


But as I print this pdf file, I cannot see the dashed line!



Root Cause
It may result from some problem in Chrome PDF Viewer.

Solution
Go to chrome://plugins/
 Disable Chrome PDF Viewer and Enable Adboe Reader

Then click the print button.

See...the dashed line had shown in my paper





Why can't I save PDF in Chrome?

Problem
My chrome version is up-to-date.

But why I can't save PDF after viewing it within Chrome, the save button does not work.


Root Cause
It may result from some problem in Chrome PDF Viewer.

Solution
Go to chrome://plugins/
 Disable Chrome PDF Viewer and Enable Adboe Reader

Then the save button is working now!



2013/11/27

How to convert the amount from Arabic numerals to Chinese number (如何將阿拉伯數字金額轉大寫國字)

Requirement
In our payment slip, we need to convert the amount from Arabic numerals to Chinese number. ex. 325250 will be converted to 參拾貳萬伍仟貳佰伍拾.


Solution
Here has a utility method to do conversion.
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
   /**  
    * Convert to Chinese amount  
    *   
    * @param amount  
    *      the amount  
    * @return the string  
    */  
   public static String convertToChineseAmt(Long amount) {  
     String num[] = { "零", "壹", "貳", "參", "肆", "伍", "陸", "柒", "捌", "玖" };  
      String xx[] = { "拾", "佰", "仟" };  
      String yy[] = { "萬", "億", "兆", "京" };  
      String chineseAmt = "";  
      int i;  
      int temp;  
      boolean first = true;  
      boolean zero = false;  
      for (i = 0; (int) (amount / (long) Math.pow(10, i)) != 0; i++) {  
        ;// 計算位數i  
      }  
      if (i == 0) {  
        return num[0];  
      }  
      for (int k = (i - 1) / 4; k >= 0; k--) {  
        temp = (int) ((amount / (long) Math.pow(10, 4 * k)) % (int) Math.pow(10, 4));  
        if (temp == 0) {  
          zero = true;  
          continue;  
        }  
        for (int x = 3; x >= 0; x--) {  
          if (first) {  
            x = (i - 1) % 4;  
            first = false;  
          }  
          if (temp / (int) Math.pow(10, x) % 10 == 0) {  
            zero = true;  
          } else {  
            if (zero) {  
              chineseAmt += num[0];  
              zero = false;  
            }  
            chineseAmt += num[temp / (int) Math.pow(10, x) % 10];  
            if (x > 0) {  
              chineseAmt += xx[x - 1];  
            }  
          }  
        }  
        if (k > 0) {  
          chineseAmt = chineseAmt + yy[k - 1];  
        }  
      }  
      return chineseAmt;  
    }  

Test
We inputted three amount into this method to do conversion
1
2
3
4
5
    public static void main(String args[]) {  
      System.out.println(ReportUtils.convertToChineseAmt(2500L));  
      System.out.println(ReportUtils.convertToChineseAmt(50100L));  
      System.out.println(ReportUtils.convertToChineseAmt(325250L));  
    }  



The result is as bellows:
1
2
3
  貳仟伍佰  
  伍萬零壹佰  
  參拾貳萬伍仟貳佰伍拾  

2013/11/26

How to clear an array in AngularJS?

Question
If we have an array in AngularJS, how do I clear its content?

Answer
If we have an array, ex.
 $scope.itemarray = ['A', 'B', 'C'];  

If we would like to clear its content in specific situation, what we need to do is as bellowing:
 $scope.itemarray.length = 0;