索引匹配以匹配两个日期之间的日期

时间:2020-01-13 22:09:08

标签: excel excel-formula

我有一个日期列B,它是日期。我的会计年度从4月1日开始,到3月31日结束,我的日期范围如下:

enter image description here

enter image description here

这是我的公式,包括命名范围。我遇到N / A错误,并且不确定自己在做什么错。

{=INDEX(fiscalyear,MATCH(1,(startdate>=B2)*(enddate<=B2),0))}

3 个答案:

答案 0 :(得分:3)

您可以使用以下方法摆脱查找:

SET NOCOUNT ON
DECLARE @table TABLE(
RowId INT PRIMARY KEY IDENTITY(1, 1),
ForeignKeyConstraintName NVARCHAR(200),
ForeignKeyConstraintTableSchema NVARCHAR(200),
ForeignKeyConstraintTableName NVARCHAR(200),
ForeignKeyConstraintColumnName NVARCHAR(200),
PrimaryKeyConstraintName NVARCHAR(200),
PrimaryKeyConstraintTableSchema NVARCHAR(200),
PrimaryKeyConstraintTableName NVARCHAR(200),
PrimaryKeyConstraintColumnName NVARCHAR(200)
)
INSERT INTO @table(ForeignKeyConstraintName, ForeignKeyConstraintTableSchema, ForeignKeyConstraintTableName, ForeignKeyConstraintColumnName)
SELECT
U.CONSTRAINT_NAME,
U.TABLE_SCHEMA,
U.TABLE_NAME,
U.COLUMN_NAME
FROM
INFORMATION_SCHEMA.KEY_COLUMN_USAGE U
INNER JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS C
ON U.CONSTRAINT_NAME = C.CONSTRAINT_NAME
WHERE
C.CONSTRAINT_TYPE = 'FOREIGN KEY'
UPDATE @table SET
PrimaryKeyConstraintName = UNIQUE_CONSTRAINT_NAME
FROM
@table T
INNER JOIN INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS R
ON T.ForeignKeyConstraintName = R.CONSTRAINT_NAME
UPDATE @table SET
PrimaryKeyConstraintTableSchema = TABLE_SCHEMA,
PrimaryKeyConstraintTableName = TABLE_NAME
FROM @table T
INNER JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS C
ON T.PrimaryKeyConstraintName = C.CONSTRAINT_NAME
UPDATE @table SET
PrimaryKeyConstraintColumnName = COLUMN_NAME
FROM @table T
INNER JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE U
ON T.PrimaryKeyConstraintName = U.CONSTRAINT_NAME
--SELECT * FROM @table
--DROP CONSTRAINT:
SELECT
'
ALTER TABLE [' + ForeignKeyConstraintTableSchema + '].[' + ForeignKeyConstraintTableName + ']
DROP CONSTRAINT [' + ForeignKeyConstraintName + ']

GO'
FROM
@table

答案 1 :(得分:2)

由于任何会计年度在会计年度结束后立即开始,因此您可以使用:

=INDEX(fiscalyear,MATCH(B2,startdate,1))

答案 2 :(得分:2)

尽管前面的两个答案都是正确的,并且公式比原始答案更简单,但我想指出原始问题在哪里-比较运算符在那里交换了。

是:

{=INDEX(fiscalyear,MATCH(1,(startdate>=B2)*(enddate<=B2),0))}

必须是:

{=INDEX(fiscalyear,MATCH(1,(startdate<=B2)*(enddate>=B2),0))}