2018年7月6日 星期五

布林函數的基本公理


布林函數的基本公理

在說明之前要先說最基本的觀念.....

數位邏輯最基本的概念講的是電路的連接...

串聯、並聯知道吧~假如說有兩個開關,串聯的話一定要兩個接通才能形成通路;而並聯只要有一個接通就能成為通路。

而在數位邏輯上,1通常表示為開,0表示為關

而在數位邏輯上最基本的邏輯閘~有著所謂的及閘(AND gate)跟或閘(OR gate),分別表示串連跟並聯。

所以在及閘(AND gate)上有著下面的關係式(等於串聯)

0‧0=0(2個都關→斷路)
0‧1=0(11開→斷路)
1‧0=0(11關→斷路)
1‧1=1(2個都開→通路)

在或閘(OR gate)上有著下面的關係式(等於並聯)

0+0=0(2個都關→斷路)
0+1=1(11開→通路)
1+0=1(11關→通路)
1=1=1(2個都開→通路)

藉由串、並聯的關係式,又可以衍生如下的基本定理

X+0=X(在並聯電路裡,X1or0跟斷路並聯,若X1(通路)結果就會是通路;若X0(斷路)結果就是斷路,所以X是什麼,結果就是什麼)

如果是X+1??當然結果必然是1(在並聯裡,只要有一個通路,整個電路一定是通路X+1=1,所以有著一種記法,1加任何數都是1)

X‧1=X(在串聯電路裡,X1or0跟斷路串聯,若X1(通路)結果就會是通路;若X0(斷路)結果就是斷路,所以X是什麼,結果就是什麼)

如果是X‧0??當然結果必然是0(在串聯裡,只要有一個斷路,整個電路一定是斷路X‧0=0)

還有X',上面的一撇是代表著相反的意思,就是如果是1,撇一撇就會變成0,如果是0,撇一撇就會變成1

有著這些觀念,再給你一些進階的觀念:

X+X=X(自己加自己還是自己)X‧X=X(自己乘自己還是自己)(X')'=XX+X'=1X‧X'=0(這些就不再多做解釋了,自己在慢慢推敲吧,上面有括號的部份是記的口訣)

說完這些概念,開始回答你的問題,
X+X‧Y=X,這要牽扯到數學的分配律跟數位邏輯的邏輯概念,先把【‧】當成數學的乘來看:

X+X‧Y=X‧1+X‧Y=X‧(1+Y)=X

能不能了解其中過程?不了解的話這題我先講解一下,其餘公式的話,我就只打出證明過程,文字敘述,自己在慢慢推敲吧,全部的基本概念的都打出來了。

X‧1=X,故X+X‧Y可以變成X‧1+X‧Y,再把共通向提出來變成X‧(1+Y),而【1加任何數都是1】則式子就變成X‧1,而X‧1=X,所以X+X‧Y=X就是這樣出來的。

x+y*z會等於(x+y)*(y+z)嗎?我們先把式子右邊都乘開:
X‧Y+X‧Z+Y‧Y+Y‧Z=X‧Y+X‧Z+Y+Y‧Z
=X‧Z+Y‧(X+1+Z)=X‧Z+Y~~~~~結果是不等於的。

(接下來的運算我會把‧省略,麻煩又看的眼花撩亂= ='')
1. x+x*y=x【證明:X+XY=X(1+Y)=X

2. x*(x+y)=x【證明:X‧(X+Y)=XX+XY=X+XY=X(1+Y)=X

3. x+x'*y=x+y【證明:X+X'‧Y=(X+X')(X+Y)=1‧(X+Y)=X+Y
再這裡補充一個新觀念,是數位邏輯裡獨有的“加法”分配律,數學裡的分配律是指乘法分配律:X(Y+Z)=XY+XZ,數位邏輯則又加上了加法分配律:X+YZ=(X+Y)(X+Z),這議題就是運用了加法分配律觀念。

4. x*(x'+y)=xy【證明:X‧(X'+Y)=XX'+XY=0+XY=XY

一致性:(我們課堂上講的是共通項理論,也就是式子裡如果有XX',中間夾的數如果剛好等於XX'旁邊向的乘積的話,中間項是可以直接被約掉的,就好比下面兩個例子,是不是中間項的數都不見了!)

1.xy+yz+x'z=xy+x'z
【證明:XY+YZ+X'Z=XY+(X+X')YZ+X'Z=XY+XYZ+X'YZ+X'Z=XY(1+Z)+X'Z(1+Y)=XY+X'Z

2.(x+y)(y+z)(x'+z)=(x+y)(x'+z)
這一題要用比較特別的方法,就是把兩邊都乘開後,再比較的手法:

【左證明:(X+Y)(Y+Z)(X'+Z)=(XY+XZ+Y+YZ)(X'+Z)=XYX'+XZX'+x'Y+x'YZ+XYZ+XZZ+YZ+YZZ
=X'Y+XYZ+XYZ+XZ+YZ=X'Y+XZ+YZ(X'+X+1)=
X'Y+XZ+YZ=X'Y+XZ

【右證明:(X+Y)(X'+Z)=XX'+XZ+X'Y+YZ=X'Y+XZ+YZ=X'Y+XZ

其結果是相等的~~大致上就是這樣。

2009-02-18 23:06:43 補充:
看你這樣問,就知道你是剛接觸,而且還蠻想學的,但就我學習的經驗來看的話.....即使給你網站讓你去看,雖然看的懂,卻不知所以然....這門學問還是要人教的阿。

2009-02-18 23:21:55 補充:
(x+y)(y+z)(x'+z)=(x+y)(x'+z)
換個方式
(X+Y)(Y+Z)(X'+Z)=(XY+XZ+Y+YZ)(X'+Z)=XYX'+XZX'+x'Y+x'YZ+XYZ+XZZ+YZ+YZZ
=X'Y+XYZ+XYZ+XZ+YZ=X'Y+XZ+YZ(X'+X+1)
=X'Y+XZ+YZ=X'Y+XZ+YZ+XX'=X(X'+Z)+Y(X'+Z)
=(X+Y)(X'+Z)


ref:https://tw.answers.yahoo.com/question/index?qid=20090217000010KK09528

2017年4月23日 星期日

Server requested LANMAN password (share-level security) but ‘client lanman auth’ is disabled

Q:
Domain=[WORKGROUP] OS=[Unix] Server=[Samba 4.4.4]
Server requested LANMAN password (share-level security) but ‘client lanman auth’ is disabled
tree connect failed: NT_STATUS_ACCESS_DENIED
A: vim smb.conf
lanman auth = yes
client lanman auth = yes
#server端跟client端都使用lanman認證。
———————————————
經驗
client ntlmv2 auth (G)
This parameter determines whether or not smbclient(8) will attempt to authenticate itself to servers using the NTLMv2 encrypted password response.
If enabled, only an NTLMv2 and LMv2 response (both much more secure than earlier versions) will be sent. Many servers (including NT4 < SP4, Win9x and Samba 2.2) are not compatible with NTLMv2. Similarly, if enabled, NTLMv1, client lanman auth and client plaintext auth authentication will be disabled. This also disables share-level authentication. If disabled, an NTLM response (and possibly a LANMAN response) will be sent by the client, depending on the value of client lanman auth. Note that some sites (particularly those following 'best practice' security polices) only allow NTLMv2 responses, and not the weaker LM or NTLM. Default: client ntlmv2 auth = no

2015年6月1日 星期一

Xpages use Java 計算日期

備忘: Xpages use Java  計算日期

var date:java.util.Date = getComponent("dateFrom").getValue();
var cal:java.util.Calendar = java.util.Calendar.getInstance();
cal.setTime(date);
getComponent("inputText1").setValue(cal.get(java.util.Calendar.DAY_OF_MONTH)); 
getComponent("inputText2").setValue(cal.get(java.util.Calendar.MONTH) + 1);  
getComponent("inputText4").setValue(cal.get(java.util.Calendar.DAY_OF_WEEK));

Calling CSJS from within SSJS in XPages

xe:dialog control. You can show and hide that dialog using SSJS.

Here are the SSJS and CSJS commands to show/hide the dialog:
SSJS:
var comp = getComponent("serverSideId");
//To Open the dialog
comp.show();
//To close the dialog
comp.hide();
CSJS
//To Open the dialog
XSP.openDialog("#{id:serverSideId}");
//To close the dialog
XSP.closeDialog("#{id:serverSideId}");
And just for fun, SSJS that executes CSJS:
//To Open the dialog
facesContext.getViewRoot().postScript("XSP.openDialog('#{id:serverSideId}')");
//To Close the dialog
facesContext.getViewRoot().postScript("XSP.closeDialog('#{id:serverSideId}')");
 
資料來源:stackoverflow 

2015年4月30日 星期四

轉帖-安裝Notes 9.0.1 FP3時提示版本不匹配

在运行901FP3安装程序时,先解压到指定路径,如:C:\901FP3
然后修改deploy\hotfix\fix.ini中的ForVersion参数,
将初始值=20131022.0932
改为 =20131015.0605
保存并关闭。

再运行c:\901FP3\setup.exe 即可顺利安装。 

轉自安装Notes 9.0.1 FP3时提示版本不匹配




可以解決IBM Notes 9.0.1 java 處理上的一些問題

Edit your jvm.properties file
Open the {Notes}/framework/rcp/deploy/jvm.properties file in a text editor and add the following lines:
 
vmarg.Djmxremoteport=-Dcom.sun.management.jmxremote.port=8123
vmarg.Djmxremotessl=-Dcom.sun.management.jmxremote.ssl=false
vmarg.Djmxremoteauth=-Dcom.sun.management.jmxremote.authenticate=false
 
The first line tells the Notes client JVM to listen on port 8123 for JMX clients like JConsole (feel free to use a different port if you’d like). 
 
The second line tells it not to use SSL for the connection (which it would do by default).
 
 And the third line tells it not to require a username and password for the connection.
 
 

2015年4月13日 星期一

Full Text Index on separate Drive

Since 8.5.3 (available as of today) we have the option to put the Full Text Index on another drive. We have asked about it many times for many years and finally got this functionality.
The notes.ini parameter e.g. FTBasePath=d:\full_text can be used to switch the starting directory for the full text index from data directory to a different directory or drive.

It makes a lot of sense to separate the FT Index from the NSF data in larger environments.
There are a couple of reasons

- FT Index causes a lot of file-system fragmentation
- At some point even with DAOS on larger environments the file-system for NSF data can be quite big and separating FT and NSF would make sense.
- When using snap-shot backup reducing the size of the file-system containing NSF would make a lot of sense
- From I/O point of view on larger servers with a lot of FT data this can also improve performance

This setting does only make sense for larger environments

To migrate your FT index to a separate drive you need the following steps:

- Set the notes.ini FTBasePath=d:\full_text
- Run updall -f to rebuild all FT indexes.

This will automatically delete the old FT index and re-create it on the new drive.


轉載自:http://blog.nashcom.de/nashcomblog.nsf/dx/full-text-index-on-separate-drive.htm?opendocument&comments

2014年12月2日 星期二

Linux vsftpd變更預設目錄作法

As we know by default vsftpd will configure the anonymous directory to /var/ftp/pub which is great to share files with users 

After trying a lot of different configurations this is what worked for me:


Code:
mkdir /home/pub
mv /var/ftp/pub/* /home/pub/
rm -rf /var/ftp/pub
mount --bind /home/pub/ /var/ftp/pub/
I have a feeling that vsftpd does not lets you browse symlinks but will work with mounted filesystems (bind method).

2013年10月3日 星期四

Dojo 1.8 & Domino 8.5.3

Quick-n-Dirty: Dojo 1.8 & Domino 8.5.3


If you want to use Dojo 1.8 with Domino 8.5.3, you can do the following:
1. Grab the Dojo JAR file from a ND9 installation:
\osgi\shared\eclipse\plugins\com.ibm.xsp.dojo_9.0.0.jar
2. Create a folder “dojo-1.8.0” in your notes data directory
\domino\js\dojo-1.8.0
3. Unpack the JAR file
4. Copy the content of the subfolder \resources\dojo-version of the unpacked JAR file in this folder:

5. Restart HTTP task.
6. Enjoy!

    
    

    


轉自blog@hasselba.ch

2013年8月12日 星期一

Xpage 程式更新 viewPanel 備忘

getComponent('viewPanel9').getData().refresh();

2013年8月1日 星期四

JavaScript search() 方法

String.search

 example

var stringValue = "This is my test string to search";
var result = stringValue.search(/is|test|string/);
//if () {....} 
//true return 6 false return -1
var str = "This is a sample text";
var re = /nothing|text|string/g
if (str.search(re) != -1) {
    midstring = " contains ";
} else {
    midstring = " does not contain ";
}

2013年7月28日 星期日

Learning XPages : The basics of OneUI

Welcome to part 2 in a series of blog entries
on how to create a simple phonebook application in Lotus Domino 8.5 using
XPages. In this part we are going to look at the basics of OneUI and understand
how to use it to provide the standard layout for your application.

By using a combination of DIVs and CSS
it is very easy to create a layout for your web based application. Your
CSS file will contain styles that match up with the divs in the html page
and this is what creates the layout. When we examine the OneUI core.css
file we find a section for structure that looks like this :





/*** S T R U C T U R E ***/
#lotusFrame, .lotusFrame{margin:0 auto;padding:0;min-width:990px;}
#lotusTitleBar, #lotusPlaceBar, .lotusTitleBar, .lotusPlaceBar{margin:0
20px}
#lotusMain, #lotusFooter, .lotusMain, .lotusFooter{border-style:solid;clear:both;margin:0
20px 10px 20px;overflow:hidden}
#lotusMain, .lotusMain{padding:0 0 5px 0;min-height:400px;border-width:0
1px 1px 1px;-moz-border-radius:0 0 4px 4px;-webkit-border-bottom-left-radius:4px;-webkit-border-bottom-right-radius:4px;
background-repeat:no-repeat}
#lotusColLeft, .lotusColLeft{width:180px;padding:20px 10px 10px 10px;}
#lotusColRight, .lotusColRight{width:180px;padding:15px 10px;font-size:.9em}
#lotusColRight h2, .lotusColRight h2{}
#lotusColLeft .lotusFirst, #lotusColRight .lotusFirst, .lotusColLeft .lotusFirst,
.lotusColRight .lotusFirst{margin-top:0;}
#lotusContent, .lotusContent{padding:16px 20px 20px 20px;overflow:hidden;margin-right:auto;margin-left:auto;}
.lotusContentColOne{width:45%;}
.lotusContentColTwo{width:45%;}
This defines parts of the basic layout
however it does not include the OneUI color scheme or graphics. These are
stored in a separate css file called defaulttheme.css.  If you match
these two css files up with some simple html that looks like this :








Banner Contents Here


Titlebar Here


PlaceBar Here




Left Sidebar Here


Main Content Here



Footer Contents Here

You’ll end up with an instant webpage
with a nice look and feel that looks like this :

A picture named M2

( footer and banner contents not shown
)

The core.css and defaulttheme.css and
a couple of other css files are all stored on the domino server in the
datadirectorydominojavaxpsthemeoneui directory. We could reference
the files directly in the XPage application but as you will see in part
3 of this series we are going to use a theme document to include the files


  轉自:Dec's Dom Blog

2013年7月2日 星期二

XPages: Simple function to clear scoped variables

All scope objects (applicationScope, sessionScope, etc.) are maps, so it's quite easy to clear them. This might come in handy during development, if you want to clear the applicationScope.

I've tested it on both applicationScope and sessionScope, and it doesn't seem to do any harm. After the maps are cleared, the server automatically loads the "internal" values used by the system.

function clearMap( map:Map ){
 // Get iterator for the keys
 var iterator = map.keySet().iterator();
 
 // Remove all items
 while( iterator.hasNext() ){
  map.remove( iterator.next() );
 }
}

Usage:
clearMap( applicationScope )

 轉載自DontPanic - a blog about Lotus Notes / Domino

2013年3月12日 星期二

how can I remove brackets from this array?

方法一:

import java.util.*;

public class RandomArray {
    public static void main( String[] args ) {

        Random random = new Random();

        int[][] array = new int[19][19];

        for( int i = 0 ; i < array.length ; i++ ) {
           for ( int j = 0 ; j < array[i].length ; j++ ) {
              array[i][j] = random.nextInt(3-0);
           }
        }

        for( int[] a : array ) {
            System.out.println( Arrays.toString( a ).replace("[", "").replace("]", ""));            
        }


    }
   
}
方法二:

System.out.println(Arrays.toString(a).replaceAll("^\\[|\\]$"));

資料來源:
http://www.dreamincode.net/forums/topic/288259-how-can-i-remove-brackets-from-this-array/

2010年8月18日 星期三

Reference current document in Java Agent

// get the current document
Document currentDoc = agentContext.getDocumentContext();

// get a value of a field named 'testField' from the current document
String str = currentDoc.getItemValueString("testField");

// print the value of the testField of the current document into java console
System.out.println(str);


轉自: http://www-10.lotus.com/ldd/nd6forum.nsf/55c38d716d632d9b8525689b005ba1c0/79bc69e4d4f91db085257372004fb206?OpenDocument

2010年8月11日 星期三

Lotus Notes R6.56 與 JasperReport+iReport 透過Java Agent整合產生PDF 製作流程說明

參考文章

pureXMLでJasperReportsを楽しもう

pureXMLとJasperReportsのXML連携
小川 直人 (naogawa@fsi.co.jp), 主任, 富士ソフト株式会社
概要: JasperReportsは、SQLを発行し、その結果をレポーティングすることができます。しかし、XQueryはサポートしていません。簡単な拡張で、JasperReportsでXQueryが使えるようになります。


http://www.ibm.com/developerworks/jp/data/library/dataserver/j_d-jasperreports/index.html?ca=drs-

2010年7月21日 星期三

iText in Lotus Notes 6.5.X JDK 1.3.6

A Suggest of iText in Lotus Notes 6.5.X
By philipz on November 11, 2007 11:17 PM
Bruno Lowagie, iText Author, give me a tip about "how to use iText in Lotus Notes" in his blog.

There's even an example on how to use iText in Lotus Notes. I would add an extra tip: get the iText code from the 1.3 branch in SVN and compile it with JDK 1.3. That's much better than using iText 1.3.6 and complain on the mailing list about bugs that have been fixed ages ago ;-)

2010年6月21日 星期一

oracle------decode,Case,nvl,nvl2

1.DECODE, NVL, and NVL2简介:
These functions are used to make decisions based on data values within an SQL statement without resorting(求助) to a procedural language like PL/SQL
DECODE和NVL、NVL2函数语法格式
Function syntax
Logic equivalent
DECODE(E1, E2, E3, E4)
IF E1 = E2 THEN E3 ELSE E4
NVL(E1, E2)
IF E1 IS NULL THEN E2 ELSE E1
NVL2(E1, E2, E3)
IF E1 IS NULL THEN E3 ELSE E2
2.DECODE函数:
The DECODE function can be thought of as an inline IF statement. DECODE takes four or more expressions as arguments. Each expression can be a column, a literal, a function, or even a subquery.Since the DECODE function compares two expressions and returns one of two expressions to the caller, it is important that the expression types are identical or that they can at least be translated to be the same type. The same exception would be thrown if the two return expressions (E3 and E4) did not have comparable types.
    SELECT p.part_nbr part_nbr, p.name part_name, s.name supplier,
          DECODE(p.status, 'INSTOCK', 'In Stock',
                            'DISC', 'Discontinued',
                            'BACKORD', 'Backordered',
                            'ENROUTE', 'Arriving Shortly',
                            'UNAVAIL', 'No Shipment Scheduled',
                            'Unknown') part_status
     FROM part p, supplier s
WHERE p.supplier_id = s.supplier_id;

上述DECODE函数将part表status字段的值和前五个表达式进 行比较,如果有匹配的则返回其后紧接着的字符串,如果没有一个匹配得到,则返回字符串’Uunknown’。
3.NVL和NVL2函数:
The NVL and NVL2 functions allow you to test an expression to see whether it is NULL. If an expression is NULL, you can return an alternate, non-NULL value, to use in its place. Since any of the expressions in a DECODE statement can be NULL, the NVL and NVL2 functions are actually specialized versions of DECODE. The following example uses NVL2 to produce the same results as the DECODE example shown in the previous section:
    SELECT lname, NVL2(manager_emp_id, 'NON-MANAGER', 'MANAGER') emp_type
      FROM employee;
注 意:NVL2函数的语法是NVL2(E1, E2, E3) IF E1 IS NULL THEN E3 ELSE E2, 其中空的时候是用E3赋值,而非用E2赋值
4.Case子句简介:
Case子句相对于Decode函数的优势:
● CASE expressions can be used everywhere that DECODE functions are permitted.
● CASE expressions are more readable than DECODE expressions.
● CASE expressions execute faster than DECODE expressions
● CASE expressions handle complex logic more gracefully than DECODE expressions.
● CASE is ANSI-compliant, whereas DECODE is proprietary.
Since CASE is built into Oracle's SQL grammar, there is no need to call a function in order to evaluate the if-then-else logic. While the difference in execution time is miniscule for a single call, the aggregate time savings from not calling a function should become noticeable when working with large result sets.
(由 于Oracle的SQL语法包含了CASE,所以在需要计算if-then-else逻辑时不需要调用函数,在只调用一次时,两者的差别可以 忽略不计,但如果正在处理的是一个大型的记录集,那么每次调用函数加起来的时间总和就值得考虑了。
使 用CASE和使用DECODE函数相比,唯一的劣势就是Oracle 8i PL/SQL不支持CASE函数。)
5.Case子句的语法:
    CASE
       WHEN C1 THEN R1
       WHEN C2 THEN R2
 ...
       WHEN CN THEN RN
       ELSE RD
END
Conditions are evaluated in order. When a condition is found that evaluates to TRUE, the corresponding result is returned, and execution of the CASE logic ends. Therefore, carefully order WHEN clauses to ensure that the desired results are achieved
 
SELECT co.order_nbr, co.cust_nbr,
       CASE WHEN co.expected_ship_dt IS NULL THEN 'NOT YET SCHEDULED'
            WHEN co.expected_ship_dt <= SYSDATE THEN 'SHIPPING DELAYED'
            WHEN co.expected_ship_dt <= SYSDATE + 2 THEN 'SHIPPING SOON'
            ELSE 'BACKORDERED'
      END ship_status
 FROM cust_order co
WHERE co.ship_dt IS NULL AND co.cancelled_dt IS NULL;
 
Similar to DECODE, all results in the CASE expression must have comparable types; otherwise, ORA-932 will be thrown. Each condition in each WHEN clause is independent of the others, however, so your conditions can include various data types, as demonstrated in the next example:
 
SELECT co.order_nbr, co.cust_nbr,
 CASE
    WHEN co.sale_price > 10000 THEN 'BIG ORDER'
    WHEN co.cust_nbr IN (SELECT cust_nbr FROM customer WHERE tot_orders > 100)
   THEN 'ORDER FROM FREQUENT CUSTOMER'
    WHEN co.order_dt < TRUNC(SYSDATE) -- 7 THEN 'OLD ORDER'
    ELSE 'UNINTERESTING ORDER'
  END
 FROM cust_order co
WHERE co.ship_dt IS NULL AND co.cancelled_dt IS NULL;
 
6.简单CASE子句:
Simple CASE expressions are structured differently than searched CASE expressions in that the WHEN clauses contain expressions instead of conditions, and a single expression to be compared to the expressions in each WHEN clause is placed in the CASE clause. Here's the syntax
(简 单CASE表达式在结构上和搜索CASE表达式有着一定的区别:首先WHEN子句包含的是表达式而非条件,其次原来每个WHEN子句中独立的 比较条件被移至CASE子句处,成为唯一的条件。因为简单CASE表达式中每个WHEN的表达式都要和CASE中的条件进行比较,所以它们的类型必须都为 一致的,这使简单CASE表达式的灵活性受到一定的影响)
 
    CASE E0
       WHEN E1 THEN R1
WHEN E2 THEN R2
       ...
       WHEN EN THEN RN
       ELSE RD
END
SELECT p.part_nbr part_nbr, p.name part_name, s.name supplier,
   CASE p.status
       WHEN 'INSTOCK' THEN 'In Stock'
       WHEN 'DISC' THEN 'Discontinued'
       WHEN 'BACKORD' THEN 'Backordered'
       WHEN 'ENROUTE' THEN 'Arriving Shortly'
       WHEN 'UNAVAIL' THEN 'No Shipment Scheduled'
       ELSE 'Unknown'
   END part_status
 FROM part p, supplier s
WHERE p.supplier_id = s.supplier_id;
 
3. DECODE and CASE Examples:
A.行转列:
    You may have run into a situation where you are performing aggregations over a finite set of values, such as days of the week or months of the year, but you want the result set to contain one row with N columns rather than N rows with two columns.
  
    例子:行式统计每个季度的销售额
 
    SELECT TO_CHAR(order_dt, 'Q') sales_quarter, SUM(sale_price) tot_sales
        FROM cust_order
     WHERE order_dt >= TO_DATE('01-JAN-2001','DD-MON-YYYY')
          AND order_dt < TO_DATE('01-JAN-2002','DD-MON-YYYY')
 GROUP BY TO_CHAR(order_dt, 'Q')
 ORDER BY 1;
   
    S TOT_SALES
- ----------
1    9739328
2   10379833
3    9703114
4              9772633
   例子:列式统计每个季度的销售额
 
SELECT SUM(DECODE(TO_CHAR(order_dt, 'Q'), '1', sale_price, 0)) Q_1,
        SUM(DECODE(TO_CHAR (order_dt, 'Q'), '2', sale_price, 0)) Q_2,
          SUM(DECODE(TO_CHAR (order_dt, 'Q'), '3', sale_price, 0)) Q_3,
          SUM(DECODE(TO_CHAR (order_dt, 'Q'), '4', sale_price, 0)) Q_4
     FROM cust_order
WHERE order_dt >= TO_DATE('01-JAN-2001','DD-MON-YYYY')
AND order_dt < TO_DATE('01-JAN-2002','DD-MON-YYYY');

       Q_1        Q_2        Q_3        Q_4
---------- ---------- ---------- ----------
        9739328   10379833    9703114    9772633
Each of the four columns in the previous query are identical, except for the quarter being checked by the DECODE function. For the Q_1 column, for example, a value of 0 is returned unless the order falls in the first quarter, in which case the sale_price column is returned. When the values from all orders in 2001 are summed, only the first quarter orders are added to the total (for Q_1), which has the effect of summing all first quarter orders while ignoring orders for quarters 2, 3, and 4. The same logic is used for Q_2, Q_3, and Q_4 to sum orders for quarters 2, 3, and 4 respectively
    
    SELECT SUM(CASE WHEN TO_CHAR(order_dt, 'Q') = '1' THEN sale_price ELSE 0 END) Q_1,
         SUM(CASE WHEN TO_CHAR(order_dt, 'Q') = '2' THEN sale_price ELSE 0 END) Q_2,
         SUM(CASE WHEN TO_CHAR(order_dt, 'Q') = '3' THEN sale_price ELSE 0 END) Q_3,
         SUM(CASE WHEN TO_CHAR(order_dt, 'Q') = '4' THEN sale_price ELSE 0 END) Q_4
    FROM cust_order
WHERE order_dt >= TO_DATE('01-JAN-2001','DD-MON-YYYY')
      AND order_dt < TO_DATE('01-JAN-2002','DD-MON-YYYY');
    
B. 选择性更新:
In some situations, you may need to modify data only if certain conditions exist. For example, you have a table that records information such as the total number of orders and the largest order booked during the current month. Here's the table definition 
    describe mtd_orders;
Name                                      Null?    Type
----------------------------------------- -------- ------------
TOT_ORDERS                                NOT NULL NUMBER(7)
TOT_SALE_PRICE                            NOT NULL NUMBER(11,2)
MAX_SALE_PRICE                            NOT NULL NUMBER(9,2)
    
    UPDATE mtd_orders mtdo
   SET (mtdo.tot_orders, mtdo.tot_sale_price, mtdo.max_sale_price) =
          (SELECT mtdo.tot_orders + day_tot.tot_orders,
                  mtdo.tot_sale_price + NVL (day_tot.tot_sale_price, 0),
                  DECODE (GREATEST (mtdo.max_sale_price,
                                    NVL (day_tot.max_sale_price, 0)
                                   ),
                          mtdo.max_sale_price, mtdo.max_sale_price,
                          day_tot.max_sale_price
                         ) --判断是否有比 当前最高销售额更高的销售额,有则更新该字段
             FROM (SELECT COUNT (*) tot_orders,                  
                          SUM (sale_price) tot_sale_price,
                          MAX (sale_price) max_sale_price
                     FROM cust_order
                    WHERE cancelled_dt IS NULL AND order_dt >= TRUNC (SYSDATE)) day_tot);

In this statement, the max_sale_price column is set equal to itself unless the value returned from the subquery is greater than the current column value, in which case the column is set to the value returned from the subquery. The next statement uses CASE to perform the same optional update
 
UPDATE mtd_orders mtdo
   SET (mtdo.tot_orders, mtdo.tot_sale_price, mtdo.max_sale_price) =
          (SELECT mtdo.tot_orders + day_tot.tot_orders,
                  mtdo.tot_sale_price + day_tot.tot_sale_price,
                  CASE --判断是否有比当前最高销售额更高的销售额,有则 更新该字段
                     WHEN day_tot.max_sale_price > mtdo.max_sale_price
                        THEN day_tot.max_sale_price
                     ELSE mtdo.max_sale_price
                  END
FROM (
SELECT COUNT (*) tot_orders,
                          SUM (sale_price) tot_sale_price,
                         MAX (sale_price) max_sale_price
                     FROM cust_order
                   WHERE cancelled_dt IS NULL AND order_dt>= TRUNC(SYSDATE)) day_tot);
 
C.除零错误:
As a general rule, you should write your code so unexpected data values are handled gracefully. One of the more common arithmetic errors is ORA-01476: divisor is equal to zero. Whether the value is retrieved from a column, passed in via a bind variable, or returned by a function call, always wrap divisors with DECODE or CASE, as illustrated by the following example
 
SELECT p.part_nbr, SYSDATE + (p.inventory_qty /
 DECODE(my_pkg.get_daily_part_usage(p.part_nbr), NULL, 1,
         0, 1, my_pkg.get_daily_part_usage(p.part_nbr))) anticipated_shortage_dt 
 FROM part p
WHERE p.inventory_qty > 0;
SELECT p.part_nbr, SYSDATE + (p.inventory_qty /
 CASE WHEN my_pkg.get_daily_part_usage(p.part_nbr) > 0
      THEN my_pkg.get_daily_part_usage(p.part_nbr)
ELSE 1 END) anticipated_shortage_dt
FROM part p
WHERE p.inventory_qty > 0;

引 用:
http://bonnyzouming.spaces.live.com/blog/cns!CA8A6A288D8C7AEC!178.trak
引 述這則內容的部落格