官术网_书友最值得收藏!

Generating constants with SELECT statements

During the development of a BI project, sometimes we need to use values which do not necessarily come from tables; these can be constants such as Data refreshed at or Today. The logical tables come in very handy for this purpose.

Getting ready

You can find the SELECT statement for this recipe in the companion code file.

How to do it...

To create a logical table that returns the current date as a constant, do the following:

  1. Go to the Schema Objects | Tables folder and right-click on an empty space in the right pane. From the context menu, choose New | Logical Table.
  2. In the SQL statement text area, paste this sentence:
    select DATEADD(day, DATEDIFF(day, 0, GETDATE()), 0) DateValue
    
  3. Add one Column Object and name it DateValue, and select Date from Data Type.
  4. Click on Save and Close and name it Today.
  5. Update the schema.

How it works...

We created a very simple table with one row and one column. This table will always return the current date at midnight; it is useful to filter records based on moving dates, or simply to display it in a report header. Moreover, since the table has only one row, we can join it anywhere without creating dangerous Cartesian products. Even with a CROSS JOIN, it will always multiply the results by one.

There's more...

In SQL Server constants are generated with a SELECT statement without the FROM clause. If you want to display Eat at Joe's, it's as simple as:

select "Eat at Joe's"

(Sorry, blinking font not included)

In other RDBMS, you may find a different syntax: in Oracle, for example, the current date at midnight can be obtained with the sentence:

SELECT TRUNC(SYSDATE) DateValue FROM DUAL;

And the logical table would work just the same.

See also

  • The Attribute forms – ID and DESC recipe in Chapter 3, Schema Objects – Attributes
主站蜘蛛池模板: 南乐县| 鹿泉市| 玉环县| 浪卡子县| 昌黎县| 望都县| 富宁县| 平顺县| 阜新市| 安化县| 巴南区| 威远县| 哈巴河县| 克山县| 成都市| 拉萨市| 仁怀市| 赤峰市| 柳河县| 招远市| 黔西| 青阳县| 防城港市| 长岛县| 贺兰县| 繁峙县| 突泉县| 光泽县| 五台县| 易门县| 杨浦区| 西乡县| 武陟县| 马公市| 保康县| 辉南县| 沂水县| 剑阁县| 远安县| 五莲县| 泸水县|