`

《Pro Oracle SQL》CHAPTER 9 The Model Clause -- 9.8 Lookup Tables

阅读更多

Lookup Tables     查找表 (page 290)
    You can define a lookup table and refer to that lookup table in the rules section. Such a lookup table is
sometimes termed a  reference table . Reference tables are defined in the initial section of the SQL
statement and then referred in the rules section of the SQL statement.

    你可以定义一查找表且在规则段引用该查找表。该查找表有时被称之为引用表。引用表在SQL语句的初始化段定义而在SQL语句的规格段引用。
    In Listing 9-19, lines 5 to 9 define a lookup table ref_prod  using a Reference  clause. Line 5
REFERENCE ref_prod is specifying ref_prod as a lookup table. Column Prod_name is a dimension
column as specified in line 8 and column Prod_list_price is a measures column. Note that the
reference table must be unique on dimension column and should retrieve exactly one row per
dimension column’s value. 
    在列表9-19,5到9行使用Reference子句定义lookup表ref_prod。 行5 REFERENCE ref_prod定义ref_prod为一查找表。在第8行定义列Prod_name是维度列且列Prod_list_price是度量列。注意引用表必须是在维度列上唯一且每一维度列值应当准确的检索一行。
    Line 10 specifies the main model section starting with the keyword MAIN. This section is named as
main_section for ease of understanding, although any name can be used. In the line 15, a rule for the
column Prod_list_price is specified and populated from the lookup table ref_prod. Line 16 shows that
the reference table that measures columns is accessed using the clause  ref_prod.prod_list_price
[cv(product)] . The current value of the Product column is passed as a lookup key in the lookup table
using the clause  cv(product) .
    行10定义主model段由关键字MAIN开头。 虽然可以使用任意命名,但为容易理解该段命名为main_section。在15行指定了对列 Prod_list的规则且由来自查找表ref_prod。行16展示度量列访问了查找表,使用子句ref_prod.prod_list_price[cv(product)]。Product列的当前值作为查找表中的查找键传入,使用子句 cv(product)。
    In summary, you define a lookup table using a  REFERENCE clause, and then access that lookup table
using the syntax  look_table_name.measures column .
For example, the syntax in this example is
ref_prod.prod_list_price [cv(product)]. To access a specific row in the lookup table, you pass the
current value of the dimension column from the left hand side of the rule, in this example, using the
cv(product) clause. You might be able to understand better if you imagine ref_prod as a table,
cv(product)  as primary key in to that table, and prod_list_price as a column to fetch from that lookup
table.
    总结一下,你用REFERENCE 子句定义一个查找表,而用句法“查找表名.度量列”访问查找表。 例如,在本例中的句法是ref_prod.prod_list_price [cv(product)]。为了访问查找表中的特定行,你规则的左手边传入维度列的当前值,在本例中,用cv(product) 子句。如果你想象ref_prod 是一张表可能能更好的理解,cv(product) 作为那个表的主键,而prod_list_price 作为一列取自查找表。
Listing 9-19.  Reference Model
 1     select year, week,sale, prod_list_price
 2        from sales_fact
 3        where country in ('Australia') and product ='Xtend Memory'
 4        model return updated rows
 5        REFERENCE ref_prod on
 6          (select prod_name, max(prod_list_price) prod_list_price from products
 7             group by prod_name)
 8           dimension by (prod_name)
 9           measures (prod_list_price)
10        MAIN main_section
11          partition by (product, country)
12          dimension by (year, week)
13          measures ( sale, receipts, 0 prod_list_price )
14          rules   (
15              prod_list_price[year,week] order by year, week =
16                                  ref_prod.prod_list_price [ cv(product) ]
17        )
18*   order by year, week;
 
 YEAR WEEK       SALE PROD_LIST_PRICE
----- ---- ---------- ---------------
 2000   31      44.78           20.99
 2000   33     134.11           20.99
 2000   34     178.52           20.99
...
 
    More lookup tables can be added if needed. Suppose you also need to retrieve the
country_iso_code column values from another table. You achieved that by adding a lookup table
ref_country   as shown in Listing 9-20 lines 10 to 13. Column Country_name is the dimension column
and Country_iso_code is a measures column. Lines 22 and 23 refer to the lookup table using a new
rule Iso_code.  This rule is accessing the lookup table ref_country using the Current Value of the
Country column as the lookup key. 
    如果需要可以加入多个查找表。 假设你也需要从其它表检索country_iso_code列值。你可以加入另一个查找表ref_country达到目的, 如列表9-20的行10到13所示。列Country_name是维度列而Country_iso_code是度量列。行22和23用新规则Iso_code引用lookup表。这个规则访问查找表ref_country,使用Country列的当前值作为查找键。
Listing 9-20.  More Lookup Tables
 1     select year, week,sale,  prod_list_price, iso_code
 2        from sales_fact
 3        where country in ('Australia') and product ='Xtend Memory'
 4        model return updated rows
 5        REFERENCE ref_prod on
 6          (select prod_name, max(prod_list_price) prod_list_price from
 7             products group by prod_name)
 8           dimension by (prod_name)
 9           measures (prod_list_price)
10        REFERENCE ref_country on
11          (select country_name, country_iso_code from countries)
12           dimension by (country_name )
13           measures (country_iso_code)
14        MAIN main_section
15          partition by (product, country)
16          dimension by (year, week)
17          measures (  sale, receipts, 0 prod_list_price ,
18                              cast(' ' as varchar2(5)) iso_code)
19          rules   (
20              prod_list_price[year,week] order by year, week =
21                             ref_prod.prod_list_price [ cv(product) ],
22              iso_code [year, week] order by year, week =
23                             ref_country.country_iso_code [ cv(country)]
24        )
25*   order by year, week
 
YEAR WEEK       SALE        PROD_LIST_PRICE     ISO_C
----   ----    ----------     ---------------       -----
2000   31      44.78           20.99                     AU
2000   33     134.11           20.99                    AU
2000   34     178.52           20.99                    AU
2000   35      78.82           20.99                     AU
2000   36     118.41           20.99                     AU
 ...

0
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics