使用 SQL 服务器中不同记录的开始日期和结束日期,创建一个具有数据范围的临时 table
Create a temp table with data range using startdate and enddate from different records in SQL Server
我必须在 Crystal 报告中编写一个命令,它应该创建某种临时 table 以用于报告本身。
我基本需要:
VATDates
:
+------------+------------+----------+---------+
| StartDate | EndDate | VATCode | Rate |
+------------+------------+----------+---------+
| 01/12/2008 | 31/12/2009 | GB001 | 15 |
| 01/01/2010 | 31/12/2011 | GB001 | 17.5 |
| 01/01/2012 |GETDATE()+1 | GB001 | 20 |
+----------------------------------------------+
我有:
VATDates
:
+------------+----------+---------+
| Date | VATCode | Rate |
+------------+----------+---------+
| 01/12/2008 | GB001 | 15 |
| 01/01/2010 | GB001 | 17.5 |
| 01/01/2012 | GB001 | 20 |
+---------------------------------+
如何从第 2 个 SQL 语句中的数据中获取第 1 个 table 的结果?
您可以使用子选择来获取 EndDate。
select
v1.StartDate,
isnull((select top 1 v2.StartDate - 1 from VATDates as v2 where v2.StartDate > v1.StartDate order by v2.StartDate), '9999-12-31') as EndDate,
v1.VATCode,
v1.Rate
from
VATDates as v1
我必须在 Crystal 报告中编写一个命令,它应该创建某种临时 table 以用于报告本身。
我基本需要:
VATDates
:
+------------+------------+----------+---------+
| StartDate | EndDate | VATCode | Rate |
+------------+------------+----------+---------+
| 01/12/2008 | 31/12/2009 | GB001 | 15 |
| 01/01/2010 | 31/12/2011 | GB001 | 17.5 |
| 01/01/2012 |GETDATE()+1 | GB001 | 20 |
+----------------------------------------------+
我有:
VATDates
:
+------------+----------+---------+
| Date | VATCode | Rate |
+------------+----------+---------+
| 01/12/2008 | GB001 | 15 |
| 01/01/2010 | GB001 | 17.5 |
| 01/01/2012 | GB001 | 20 |
+---------------------------------+
如何从第 2 个 SQL 语句中的数据中获取第 1 个 table 的结果?
您可以使用子选择来获取 EndDate。
select
v1.StartDate,
isnull((select top 1 v2.StartDate - 1 from VATDates as v2 where v2.StartDate > v1.StartDate order by v2.StartDate), '9999-12-31') as EndDate,
v1.VATCode,
v1.Rate
from
VATDates as v1