6

这些查询中的任何一个都可以在 SQL 中完成吗?

SELECT dates FROM system 
WHERE dates > 'January 5, 2010' AND dates < 'January 30, 2010'

SELECT number FROM system 
WHERE number > 10 AND number < 20

我想创建一个generate_series,这就是我要问的原因。

4

8 回答 8

12

我假设您想要根据系列中的第一个和最后一个值生成任意数量的值的记录集。

PostgreSQL

SELECT  num
FROM    generate_series (11, 19) num

SQL Server

WITH    q (num) AS
        (
        SELECT  11
        UNION ALL
        SELECT  num + 1
        FROM    q
        WHERE   num < 19
        )
SELECT  num
FROM    q
OPTION (MAXRECURSION 0)

Oracle

SELECT  level + 10 AS num
FROM    dual
CONNECT BY
        level < 10

MySQL

对不起。

于 2010-02-19T17:31:12.843 回答
1

日期排序... SQL 团队的 Michael Valentine Jones 有一个很棒的日期函数

在这里查看:

http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=61519

于 2010-02-19T17:31:21.350 回答
1

在甲骨文中

WITH
START_DATE AS
(
    SELECT TO_CHAR(TO_DATE('JANUARY 5 2010','MONTH DD YYYY'),'J') 
    JULIAN FROM DUAL
),
END_DATE AS
(
    SELECT TO_CHAR(TO_DATE('JANUARY 30 2010','MONTH DD YYYY'),'J') 
    JULIAN FROM DUAL
),
DAYS AS
(
    SELECT END_DATE.JULIAN - START_DATE.JULIAN DIFF
    FROM START_DATE, END_DATE
)
SELECT  TO_CHAR(TO_DATE(N + START_DATE.JULIAN, 'J'), 'MONTH DD YYYY') 
        DESIRED_DATES
FROM 
START_DATE,
(
    SELECT LEVEL N 
    FROM DUAL, DAYS
    CONNECT BY LEVEL < DAYS.DIFF
)
于 2010-02-19T18:07:13.540 回答
1

如果你想得到天的列表,使用类似的 SQL

选择 ... 作为date“2010-01-20”和“2010-01-24”之间的天数

并返回如下数据:

days 
---------- 
2010-01-20
2010-01-21
2010-01-22
2010-01-23
2010-01-24 

此解决方案不使用循环、过程或临时表。子查询生成最近一千天的日期,并且可以根据您的需要扩展为向后或向前。

select a.Date 
from (
    select curdate() - INTERVAL (a.a + (10 * b.a) + (100 * c.a)) DAY as Date
    from (select 0 as a union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) as a
    cross join (select 0 as a union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) as b
    cross join (select 0 as a union all select 1 union all select 2 union all select 3 union all select 4 union all select 5 union all select 6 union all select 7 union all select 8 union all select 9) as c
) a
where a.Date between '2010-01-20' and '2010-01-24' 

输出:

Date
----------
2010-01-24
2010-01-23
2010-01-22
2010-01-21
2010-01-20

性能说明

在这里测试一下,性能出奇的好:上面的查询需要 0.0009 秒。

如果我们扩展子查询以生成大约。100,000 个数字(因此大约有 274 年的日期),它在 0.0458 秒内运行。

顺便说一句,这是一种非常便携的技术,只需稍作调整即可与大多数数据库一起使用。

于 2010-02-19T18:50:38.243 回答
0

不确定这是否是您要问的,但如果您想选择不是从表中选择的东西,您可以使用“DUAL”

select 1, 2, 3 from dual;

将返回一行 3 列,包含这三个数字。

从 dual 中选择对于运行函数很有用。可以通过手动输入运行函数,而不是在其中选择其他内容。例如:

select some_func('First Parameter', 'Second parameter') from dual;

将返回 some_func 的结果。

于 2010-02-19T17:34:31.410 回答
0

在 SQL Server 中,您可以使用BETWEEN关键字。

链接: http: //msdn.microsoft.com/nl-be/library/ms187922 (en-us).aspx

于 2010-02-19T17:35:32.373 回答
0

您可以使用WHERE和选择范围AND WHERE。我不能谈论性能,但它可能。

于 2010-02-19T17:38:54.873 回答
0

这个问题最简单的解决方案是 Tally 或 Numbers 表。这是一个简单地存储整数和/或日期序列的表

Create Table dbo.Tally ( 
                        NumericValue int not null Primary Key Clustered
                        , DateValue datetime NOT NULL 
                        , Constraint UK_Tally_DateValue Unique ( DateValue )
                        )
GO

;With TallyItems
As (
    Select 0 As Num
    Union All
    Select ROW_NUMBER() OVER ( Order By C1.object_id ) As Num
    From sys.columns as c1
        cross join sys.columns as c2
    )
Insert dbo.Tally(NumericValue, DateValue)
Select Num, DateAdd(d, Num, '19000101')
From TallyItems 
Where Num 

Once you have that table populated, you never need touch it unless you want to expand it. I combined the dates and numbers into a single table but if you needed more numbers than dates, then you could break it into two tables. In addition, I arbitrarily filled the table with 100K rows but you could obviously add more. Every day between 1900-01-01 to 9999-12-31 takes about 434K rows. You probably won't need that many but even if you did, the storage is tiny.

Regardless, this is a common technique to solving many gaps and sequences problems. For example, your original queries all ran in less than tenth of a second. You can also use this sort of table to solve gaps problems like:

Select NumericValue
From dbo.Tally
    Left Join MyTable
        On Tally.NumericValue = MyTable.IdentityColumn
Where Tally.NumericValue Between SomeLowValue And SomeHighValue
于 2010-02-20T07:17:31.530 回答