SQL获取日期列表以及前后天数,没有重复 [英] SQL to get list of dates as well as days before and after without duplicates

查看:46
本文介绍了SQL获取日期列表以及前后天数,没有重复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在表格中显示日期列表

SELECT mydate AS MyDate, 1 AS DateType从我的表哪里 myTable.fkId = @MyFkId;

<块引用>

2010 年 1 月 1 日 - 1
2010 年 1 月 2 日 - 1
2010 年 1 月 10 日 - 1

没问题.但是,我现在需要使用不同的 DateType 显示之前的日期和之后的日期.

<块引用>

2009 年 12 月 31 日 - 2
2010 年 1 月 1 日 - 1
2010 年 1 月 2 日 - 1
2010 年 1 月 3 日 - 2
2010 年 1 月 9 日 - 2
2010 年 1 月 10 日 - 1
2010 年 1 月 11 日 - 2

我以为我可以使用联合

SELECT MyDate, DateType从 (SELECT mydate - 1 AS MyDate, 2 AS DateType从我的表哪里 myTable.fkId = @MyFkId;联盟SELECT mydate + 1 AS MyDate, 2 AS DateType从我的表哪里 myTable.fkId = @MyFkId;联盟SELECT mydate AS MyDate, 1 AS DateType从我的表哪里 myTable.fkId = @MyFkId;) 作为 myCombinedDateTable

但这包括原始日期的副本.

<块引用>

2009 年 12 月 31 日 - 2
2010 年 1 月 1 日 - 2
2010 年 1 月 1 日 - 1
2010 年 1 月 2 日 - 2
2010 年 1 月 2 日 - 1
2010 年 1 月 3 日 - 2
2010 年 1 月 9 日 - 2
2010 年 1 月 10 日 - 1
2010 年 1 月 11 日 - 2

我怎样才能最好地删除这些重复项?我正在考虑临时表,但不确定这是否是最好的方法.

在我看来,这也可能会导致性能问题,因为我在三个不同的时间运行相同的查询.

处理此请求的最佳方式是什么?

解决方案

虽然你已经接受了解决方案,我还是把这个解决方案给大家参考:

SELECT MyDate, Min(DateType)从(SELECT MyDate + T1.RecordType AS MyDate, T1.DateType从(选择 1 AS RecordType, 2 AS DateType联合所有选择 0 AS RecordType, 1 AS DateType联合所有选择 -1 AS RecordType, 2 AS DateType) 作为 T1交叉加入 myTable其中 myTable.fkId = @MyFkId) AS 组合表按我的日期分组

这个解决方案的优点,myTable 只被查询一次,当前情况下我们在 fkID 上有一个过滤器,所以现在性能并不重要,但是如果我们必须评估复杂的查询,那么这种技术可以很好地用于 Union.

I need to display a list of dates, which I have in a table

SELECT mydate AS MyDate, 1 AS DateType
FROM myTable
WHERE myTable.fkId = @MyFkId;

Jan 1, 2010 - 1
Jan 2, 2010 - 1
Jan 10, 2010 - 1

No problem. However, I now need to display the date before and the date after as well with a different DateType.

Dec 31, 2009 - 2
Jan 1, 2010 - 1
Jan 2, 2010 - 1
Jan 3, 2010 - 2
Jan 9, 2010 - 2
Jan 10, 2010 - 1
Jan 11, 2010 - 2

I thought I could use a union

SELECT MyDate, DateType
FROM (
    SELECT mydate - 1 AS MyDate, 2 AS DateType
    FROM myTable
    WHERE myTable.fkId = @MyFkId;

    UNION

    SELECT mydate + 1 AS MyDate, 2 AS DateType
    FROM myTable
    WHERE myTable.fkId = @MyFkId;

    UNION

    SELECT mydate AS MyDate, 1 AS DateType
    FROM myTable
    WHERE myTable.fkId = @MyFkId;
) AS myCombinedDateTable

This however includes duplicates of the original dates.

Dec 31, 2009 - 2
Jan 1, 2010 - 2
Jan 1, 2010 - 1
Jan 2, 2010 - 2
Jan 2, 2010 - 1
Jan 3, 2010 - 2
Jan 9, 2010 - 2
Jan 10, 2010 - 1
Jan 11, 2010 - 2

How can I best remove these duplicates? I am considering a temporary table, but am unsure if that is the best way to do it.

This also appears to me that it may provide performance issues as I am running the same query three separate times.

What would be the best way to handle this request?

解决方案

Although you have accepted the solution, let me give this solution for the reference:

SELECT MyDate, Min(DateType)
From
(
  SELECT MyDate + T1.RecordType AS MyDate, T1.DateType
  FROM
  (
    Select 1 AS RecordType, 2 AS DateType
    Union ALL
    Select 0 AS RecordType, 1 AS DateType
    Union ALL
    Select -1 AS RecordType, 2 AS DateType
  ) AS T1
  CROSS JOIN myTable
  Where myTable.fkId = @MyFkId
) AS CombinedTable
Group By MyDate

Advantage of this solution, myTable is queried only once, current case we are having a filter on fkID so right now performance will not matter, but if we have to evaluate complex query then this technique can work fine with respect to Union.

这篇关于SQL获取日期列表以及前后天数,没有重复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆