English 中文(简体)
2. 在另一日期范围内选择有日期范围的分行
原标题:Select rows with date range within another date range

我有一个账户表,有专栏,用于起始日期和结束日期。 我希望用户提供起始日期和结束日期,如果账户在规定的开端日期和结束日期之间随时活跃,则所有数据必须退还。

如果账户目前仍在运行,最终日期可能无效。

The account must be selected if it was open at any time between the user specified range which means that even accounts that were opened or closed between the range must be included.

这里是我想要的情景。

https://i.stack.imgur.com/Gtqu5.gif” alt=“alt text”/>

黄色部分是用户进入的日期范围。 绿色部分是我所希望选择的内容。 小规模红 part是我不想选择的东西。 G、H和I的末端arrow指这些账户没有结束日期。 没有任何arrow子的所有其他人都有结束日期。

最佳回答

因此,这笔数额用于核实账户的起算日期是在用户提供截止日期之前,账户的结束日期是在用户提供起算日期之后(或无效,在这种情况下,我们甚至不需要核对):

where
    account.start_date < @user_end_date and
    (account.end_date >= @user_start_date or account.end_date is null)
问题回答
SELECT
    *
FROM
    accounts
WHERE
    (start_date >= :user_supplied_start AND start_date <= :user_supplied_end)
OR
    (end_date >= :user_supplied_end AND end_date <= :user_supplied_start)
OR
    (start_date <= :user_supplied_start AND end_date IS NULL) 
SELECT  *
FROM    accounts
WHERE   start_date <= @input_end_date
        AND (end_date >= @input_start_date OR (end_date IS NULL AND NOW() >= @input_start_date))




相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

难以执行 REGEXP_SUBSTR

I m 查询Oracle 10g。 我有两张表格(样本数据见下文)。 i m 试图提取一些领域

SQL Query Shortcuts

What are some cool SQL shorthands that you know of? For example, something I learned today is you can specify to group by an index: SELECT col1, col2 FROM table GROUP BY 2 This will group by col2

PHP array callback functions for cleaning output

I have an array of output from a database. I am wondering what the cleanest way to filter the values is example array Array ( [0] => Array ( [title] => title 1 ...

OracleParameter and DBNull.Value

we have a table in an Oracle Database which contains a column with the type Char(3 Byte). Now we use a parameterized sql to select some rows with a DBNull.Value and it doesn t work: OracleCommand ...

Running numbers in SQL

I have a SQL-statement like this: SELECT name FROM users WHERE deleted = 0; How can i create a result set with a running number in the first row? So the result would look like this: 1 Name_1 2 ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...

热门标签