选择表格中多行中两个日期列之间的年份和季度。

huangapple go评论64阅读模式
英文:

Select year and quarters between two date columns in multiple rows in a table

问题

以下是已翻译的代码部分:

SELECT [YEAR], [Quarter], ([YEAR] + CAST([Quarter] AS DECIMAL)/10) A FROM QuarterYear QY 
WHERE ([YEAR] + CAST([Quarter] AS DECIMAL)/10) 
between 
(SELECT top 1 YEAR(Acq_Date) + CAST(DATEPART(qq, Acq_Date) AS DECIMAL)/10 as ACQ FROM 
QuarterIssue) 
and 
(SELECT top 1 YEAR(Dis_Date) + CAST(DATEPART(qq, Dis_Date) AS DECIMAL)/10 as DIS FROM 
QuarterIssue)

如果需要进一步的帮助,请随时提出。

英文:

I have the following two tables

Table QuarterIssue

id Acq_Date Dis_Date
1 2015-08-07 2016-12-31
2 2018-01-05 2022-12-31
3 2019-06-21 2022-12-31

Table QuarterYear

Year Quarter
2014 1
2014 2
2014 3
2014 4
2015 1
2015 2
2015 3
2015 4
2016 1

The QuarterYear table is a master table which has all the years and quarters.

I have a custom requirement to write a query to get the quarters between two dates from these following two tables. I have to select all the years and quarters between Acq_Date and Dis_Date for all "ids" in the QuarterIssue table. Basically my result has to be,

YEAR Quarter
2015 3
2015 4
2016 1
2016 2
2016 3
2016 4
2018 1
2018 2
2018 3
2018 4
2019 1
2019 2
2019 3
2019 4
2020 1
2020 2
2020 3
2020 4
2021 1
2021 2
2021 3
2021 4
2022 1
2022 2
2022 3
2022 4

This is what I have so far,

SELECT [YEAR], [Quarter], ([YEAR] + CAST([Quarter] AS DECIMAL)/10) A FROM QuarterYear QY 
WHERE ([YEAR] + CAST([Quarter] AS DECIMAL)/10) 
between 
(SELECT top 1 YEAR(Acq_Date) + CAST(DATEPART(qq, Acq_Date) AS DECIMAL)/10 as ACQ FROM 
QuarterIssue) 
and 
(SELECT top 1 YEAR(Dis_Date) + CAST(DATEPART(qq, Dis_Date) AS DECIMAL)/10 as DIS FROM 
QuarterIssue)

As you can see right now I'm able to get the desired result of one of the "id" at a time. Is there any way we can use join or subquery to return the results I'm expecting?

答案1

得分: 1

以下是翻译好的部分:

这是一种在没有DISTINCT的情况下完成任务的方法,如果你对这种方法感兴趣的话(通常使用DISTINCT是一种非常低效的方法,通过不同的排序方式来隐藏连接返回太多行的事实,因为多行匹配了)。

基本上,我们只需将主表中的每个季度转换为开始日期和结束日期,然后检查问题表中重叠的行(我在这里描述了重叠模式)。

;WITH QuarterRange AS
(
  SELECT Year, Quarter, 
    RangeStart = DATEADD(MONTH, 3*(Quarter-1), 
                 DATEFROMPARTS(Year, 1, 1))
  FROM dbo.QuarterYear
)
SELECT Year, Quarter 
FROM QuarterRange AS qr
WHERE EXISTS
(
  SELECT 1 FROM dbo.QuarterIssue AS qi
    WHERE qi.Dis_Date >= qr.RangeStart
      AND qi.Acq_Date <  DATEADD(MONTH, 3, qr.RangeStart)
)
ORDER BY Year, Quarter;
英文:

Here's a way to do it without the DISTINCT, if you're into that kind of thing (DISTINCT is often a very inefficient way - by means of a distinct sort - to hide the fact that a join returned too many rows because multiple rows matched).

Basically, we just convert each quarter from your master table into a start and end date, and then check for rows in the issue table that overlap (and I describe the overlap pattern here).

;WITH QuarterRange AS
(
  SELECT Year, Quarter, 
    RangeStart = DATEADD(MONTH, 3*(Quarter-1), 
                 DATEFROMPARTS(Year, 1, 1))
  FROM dbo.QuarterYear
)
SELECT Year, Quarter 
FROM QuarterRange AS qr
WHERE EXISTS
(
  SELECT 1 FROM dbo.QuarterIssue AS qi
    WHERE qi.Dis_Date &gt;= qr.RangeStart
      AND qi.Acq_Date &lt;  DATEADD(MONTH, 3, qr.RangeStart)
)
ORDER BY Year, Quarter;

huangapple
  • 本文由 发表于 2023年2月24日 05:05:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/75550321.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定