101. Examine the data in the PROMO_BEGIN_DATE column of the PROMOTIONS table:
PROMO_BEGIN _DATE
04-jan-00
10-jan-00
15-dec-99
18-oct-98
22-aug-99
You want to display the number of promotions started in 1999 and 2000.
Which query gives the correct output?
A. SELECT SUM(DECODE(SUBSTR(promo_begin_date,8),'00',1,0)) "2000",
SUM(DECODE(SUBSTR(promo_begin_date,8),'99',1,0)) "1999"
FROM promotions;
B. SELECT SUM(CASE TO_CHAR(promo_begin_date,'yyyy') WHEN '99' THEN 1
ELSE 0 END) "1999",SUM(CASE TO_CHAR(promo_begin_date,'yyyy') WHEN '00' THEN 1
ELSE 0 END) "2000"
FROM promotions;
C. SELECT COUNT(CASE TO_CHAR(promo_begin_date,'yyyy') WHEN '99' THEN 1
ELSE 0 END) "1999",COUNT(CASE TO_CHAR(promo_begin_date,'yyyy') WHEN '00' THEN 1
ELSE 0 END) "2000"
FROM promotions;
D. SELECT COUNT(DECODE(SUBSTR(TO_CHAR(promo_begin_date,'yyyy'), 8), '1999', 1, 0)) "1999",
COUNT(DECODE(SUBSTR(TO_CHAR(promo_begin_date,'yyyy'), 8),'2000', 1,
0)) "2000"
FROM promotions;
Answer: A
答案解析:
分析A答案
SUBSTR(promo_begin_date,8),即是取日期的最后两个值。
(DECODE(SUBSTR(promo_begin_date,8),'00',1,0)如果最后两个值是00,则值为1,否则为0
(DECODE(SUBSTR(promo_begin_date,8),'99',1,0)如果最后两个值是99,则值为1,否则为0
SUM(DECODE(SUBSTR(promo_begin_date,8),'00',1,0)) 加起来有多少个1,即有多少个2000年的
SUM(DECODE(SUBSTR(promo_begin_date,8),'99',1,0))加起来有多少个1,即有多少个1999年的
B,条件不对,应该 TO_CHAR(promo_begin_date,'yyyy') 改成 TO_CHAR(promo_begin_date,'yy')
C,与B同样错误
D,截取不对,SUBSTR(TO_CHAR(promo_begin_date,'yyyy'), 8)改成SUBSTR(TO_CHAR(promo_begin_date,'yyyy'), 1)