从 select 语句中的 14 个可能列返回日期
Returning a date from 14 possible columns within a select statement
我有一个 table 叫做 F0008 Schema。它有 14 列,我可以在其中找到一个开始日期(JDE 日期格式),它可以从列名告诉我我们财政年度的月份数。
这是一个示例,说明我的 120314 测试日期 return 4 决定为 2020 年 11 月 9 日:
DECLARE @TestDate INT
SET @TestDate = 120314
SELECT CASE
WHEN F0008.CDD01J >= @TestDate THEN 1
WHEN F0008.CDD02J >= @TestDate THEN 2
WHEN F0008.CDD03J >= @TestDate THEN 3
WHEN F0008.CDD04J >= @TestDate THEN 4
WHEN F0008.CDD05J >= @TestDate THEN 5
WHEN F0008.CDD06J >= @TestDate THEN 6
WHEN F0008.CDD07J >= @TestDate THEN 7
WHEN F0008.CDD08J >= @TestDate THEN 8
WHEN F0008.CDD09J >= @TestDate THEN 9
WHEN F0008.CDD10J >= @TestDate THEN 10
WHEN F0008.CDD11J >= @TestDate THEN 11
WHEN F0008.CDD12J >= @TestDate THEN 12
WHEN F0008.CDD13J >= @TestDate THEN 13
WHEN F0008.CDD14J >= @TestDate THEN 14
END AS int_num
FROM [ODS].[PRODDTA].[F0008] F0008
where F0008.CDDTPN = 'B'
and F0008.CDFY = 20
然后我有一个名为 F42119 Schema 的订单 table。这个 table 中有一个名为 SDIVD 的列,它是 JDE 格式的更新日期。我可以从这个 table 中获取一系列日期,如下所示:
SELECT distinct F42119.SDIVD
FROM [ODS].[PRODDTA].[F42119] F42119
WHERE F42119.SDIVD BETWEEN 120314 AND 120334
order by 1
我需要做的是合并这两个查询的结果,这样当我从 F42119 中提取日期列表时,我还可以从 F0008 中提取 case 语句的对应结果。
简而言之,我需要知道 F42119 上任何交易的财务月份编号。
除了看起来很可怕之外,还有什么能阻止您将其放入子查询吗?
SELECT distinct F42119.SDIVD,
(
SELECT CASE
WHEN F0008.CDD01J >= F42119.SDIVD THEN 1
WHEN F0008.CDD02J >= F42119.SDIVD THEN 2
WHEN F0008.CDD03J >= F42119.SDIVD THEN 3
WHEN F0008.CDD04J >= F42119.SDIVD THEN 4
WHEN F0008.CDD05J >= F42119.SDIVD THEN 5
WHEN F0008.CDD06J >= F42119.SDIVD THEN 6
WHEN F0008.CDD07J >= F42119.SDIVD THEN 7
WHEN F0008.CDD08J >= F42119.SDIVD THEN 8
WHEN F0008.CDD09J >= F42119.SDIVD THEN 9
WHEN F0008.CDD10J >= F42119.SDIVD THEN 10
WHEN F0008.CDD11J >= F42119.SDIVD THEN 11
WHEN F0008.CDD12J >= F42119.SDIVD THEN 12
WHEN F0008.CDD13J >= F42119.SDIVD THEN 13
WHEN F0008.CDD14J >= F42119.SDIVD THEN 14
END AS int_num
FROM [ODS].[PRODDTA].[F0008] F0008
where F0008.CDDTPN = 'B'
and F0008.CDFY = 20
-- NOTE: you could also hide this in a function to make your life simpler
) as mySubqueryResult
FROM [ODS].[PRODDTA].[F42119] F42119
WHERE F42119.SDIVD BETWEEN 120314 AND 120334
order by 1
我有一个 table 叫做 F0008 Schema。它有 14 列,我可以在其中找到一个开始日期(JDE 日期格式),它可以从列名告诉我我们财政年度的月份数。
这是一个示例,说明我的 120314 测试日期 return 4 决定为 2020 年 11 月 9 日:
DECLARE @TestDate INT
SET @TestDate = 120314
SELECT CASE
WHEN F0008.CDD01J >= @TestDate THEN 1
WHEN F0008.CDD02J >= @TestDate THEN 2
WHEN F0008.CDD03J >= @TestDate THEN 3
WHEN F0008.CDD04J >= @TestDate THEN 4
WHEN F0008.CDD05J >= @TestDate THEN 5
WHEN F0008.CDD06J >= @TestDate THEN 6
WHEN F0008.CDD07J >= @TestDate THEN 7
WHEN F0008.CDD08J >= @TestDate THEN 8
WHEN F0008.CDD09J >= @TestDate THEN 9
WHEN F0008.CDD10J >= @TestDate THEN 10
WHEN F0008.CDD11J >= @TestDate THEN 11
WHEN F0008.CDD12J >= @TestDate THEN 12
WHEN F0008.CDD13J >= @TestDate THEN 13
WHEN F0008.CDD14J >= @TestDate THEN 14
END AS int_num
FROM [ODS].[PRODDTA].[F0008] F0008
where F0008.CDDTPN = 'B'
and F0008.CDFY = 20
然后我有一个名为 F42119 Schema 的订单 table。这个 table 中有一个名为 SDIVD 的列,它是 JDE 格式的更新日期。我可以从这个 table 中获取一系列日期,如下所示:
SELECT distinct F42119.SDIVD
FROM [ODS].[PRODDTA].[F42119] F42119
WHERE F42119.SDIVD BETWEEN 120314 AND 120334
order by 1
我需要做的是合并这两个查询的结果,这样当我从 F42119 中提取日期列表时,我还可以从 F0008 中提取 case 语句的对应结果。
简而言之,我需要知道 F42119 上任何交易的财务月份编号。
除了看起来很可怕之外,还有什么能阻止您将其放入子查询吗?
SELECT distinct F42119.SDIVD,
(
SELECT CASE
WHEN F0008.CDD01J >= F42119.SDIVD THEN 1
WHEN F0008.CDD02J >= F42119.SDIVD THEN 2
WHEN F0008.CDD03J >= F42119.SDIVD THEN 3
WHEN F0008.CDD04J >= F42119.SDIVD THEN 4
WHEN F0008.CDD05J >= F42119.SDIVD THEN 5
WHEN F0008.CDD06J >= F42119.SDIVD THEN 6
WHEN F0008.CDD07J >= F42119.SDIVD THEN 7
WHEN F0008.CDD08J >= F42119.SDIVD THEN 8
WHEN F0008.CDD09J >= F42119.SDIVD THEN 9
WHEN F0008.CDD10J >= F42119.SDIVD THEN 10
WHEN F0008.CDD11J >= F42119.SDIVD THEN 11
WHEN F0008.CDD12J >= F42119.SDIVD THEN 12
WHEN F0008.CDD13J >= F42119.SDIVD THEN 13
WHEN F0008.CDD14J >= F42119.SDIVD THEN 14
END AS int_num
FROM [ODS].[PRODDTA].[F0008] F0008
where F0008.CDDTPN = 'B'
and F0008.CDFY = 20
-- NOTE: you could also hide this in a function to make your life simpler
) as mySubqueryResult
FROM [ODS].[PRODDTA].[F42119] F42119
WHERE F42119.SDIVD BETWEEN 120314 AND 120334
order by 1