Sql server 如何比较两个没有关系的表(一个表有40k条记录,另一个表有7k条记录)?
我试过使用交叉连接,但运行需要5分钟,还有其他方法吗? 注: 我比较了日期和Int字段,表1中有来自系统的记录,表2存储了工作日历日期。 SQL ServerSql server 如何比较两个没有关系的表(一个表有40k条记录,另一个表有7k条记录)?,sql-server,date,ssis,case-when,cross-join,Sql Server,Date,Ssis,Case When,Cross Join,我试过使用交叉连接,但运行需要5分钟,还有其他方法吗? 注: 我比较了日期和Int字段,表1中有来自系统的记录,表2存储了工作日历日期。 SQL Server 您并没有让它变得简单,但这里有一个可行的解决方案。将来,在您的问题中尝试使用这种类型的代码,这样我们就可以专注于查询和解决方案。我已经编辑了您的一些日期,以使示例生效 编辑:新代码 declare @cal table ( calID int not null , date_ date not null , isWe
您并没有让它变得简单,但这里有一个可行的解决方案。将来,在您的问题中尝试使用这种类型的代码,这样我们就可以专注于查询和解决方案。我已经编辑了您的一些日期,以使示例生效 编辑:新代码
declare @cal table (
calID int not null
, date_ date not null
, isWeekday bit not null
, isHoliday bit not null
, year_ int not null
);
insert into @cal (calID, date_, isWeekday, isHoliday, year_)
select 1, '1-Jan-2010', 1, 1, 2010 union all
select 2, '2-Jan-2010', 0, 0, 2010 union all
select 3, '3-Jan-2010', 0, 0, 2010 union all
select 4, '4-Jan-2010', 1, 0, 2010 union all
select 5, '5-Jan-2010', 1, 0, 2010 union all
select 6, '6-Jan-2010', 1, 0, 2010 union all
select 7, '7-Jan-2010', 1, 0, 2010 union all
select 8, '8-Jan-2010', 1, 0, 2010 union all
select 9, '9-Jan-2010', 0, 0, 2010 union all
select 10, '10-Jan-2010', 0, 0, 2010 union all
select 11, '11-Jan-2010', 1, 0, 2010 union all
select 12, '12-Jan-2010', 1, 0, 2010 union all
select 13, '13-Jan-2010', 1, 0, 2010 union all
select 14, '14-Jan-2010', 1, 0, 2010 union all
select 15, '15-Jan-2010', 1, 0, 2010 union all
select 16, '16-Jan-2010', 0, 0, 2010 union all
select 17, '17-Jan-2010', 0, 0, 2010 union all
select 18, '18-Jan-2010', 1, 1, 2010 union all
select 19, '19-Jan-2010', 1, 0, 2010 union all
select 20, '20-Jan-2010', 1, 0, 2010 union all
select 21, '21-Jan-2010', 1, 0, 2010 union all
select 22, '22-Jan-2010', 1, 0, 2010 union all
select 23, '23-Jan-2010', 0, 0, 2010 union all
select 24, '24-Jan-2010', 0, 0, 2010 union all
select 25, '25-Jan-2010', 1, 0, 2010 union all
select 26, '26-Jan-2010', 1, 0, 2010;
declare @date table(
dateID int identity(1,1) not null
, date2 date null
, date3 date null
, date4 date null
, date5 date null
);
insert into @date (date2, date3, date4, date5)
select '6/20/2009', NULL, NULL, '7/19/2009' union all
select '1/2/2010', NULL, NULL, '1/19/2010' union all
select '1/4/2010', NULL, NULL, '1/15/2010' union all
select '1/2/2010', NULL, NULL, '1/22/2010' union all
select '9/17/2009', NULL, NULL, '10/26/2009' union all
select '6/4/2009', NULL, NULL, '6/24/2009';
;with cte as (
select dateid
, b.date_
from @date
cross apply (
Select Top (DateDiff(DAY,date2,IsNull(date5,date2))+1) DateAdd(DAY, -1+Row_Number() Over (Order By 1/0),date2) date_
from master..spt_values n1
) b
)
select distinct b.dateID
, c.date2
, c.date5
, count(*) over(order by b.dateid) cnt
from @cal a
join cte b
on a.date_ = b.date_
join @date c
on b.dateid = c.dateid
where isWeekday = 1
and isHoliday = 0
您可以从master..spt_值n1中更改
对于这样的事情:
;with E00(n) as (select 1 union all select 1)
, E02(n) as (select 1 from E00 a, E00 b)
, E04(n) as (select 1 from E02 a, E02 b)
, E08(n) as (select 1 from E04 a, E04 b)
, E16(n) as (select 1 from E08 a, E08 b)
, E32(n) as (select 1 from E16 a, E16 b)
, cteTally(d) as (select row_number() over (order by n) from E32)
, cte as (
select dateid
, b.date_
from @date
cross apply (
select top (datediff(day,date2,isnull(date5,date2))+1) dateadd(day, -1+row_number() over(order by 1/0),date2) date_
from cteTally
) b
)
select distinct b.dateID
, c.date2
, c.date5
, count(*) over(order by b.dateid) cnt
from @cal a
join cte b
on a.date_ = b.date_
join @date c
on b.dateid = c.dateid
where isWeekday = 1
and isHoliday = 0
请创建一个非常简单的示例数据集,并将其添加到帖子中,以说明您的问题以及您希望实现的目标。比较的是什么?关系不存在并不意味着您不能对您认为应该匹配的列执行完全外部联接。。。它只会更慢。。。如果有关系的话。。。您也可以使用诸如except或intersect。。。目前的问题太模糊,无法提供任何真正的帮助。jcaceres,请设置一些示例数据并解释整个问题。昨天,您发布了一些查询,但没有更多的范围和对总体目标的理解以及数据的外观,我们只是猜测您试图实现的目标,给我们一些数据和表定义-int和date列之间的关系是什么?好的,请稍等片刻…取决于您所说的“比较”是什么意思我试图计算日历C和从系统B中提取的日期之间的日期,结果应该是数字,正如我在尝试实现文件中所说的。我使用的代码在sampleCode链接中。好的,那么你想计算两个日期之间的天数吗?我看到你只想从另一篇文章中计算工作日的天数了吗?没错,这就是为什么我使用sum。根据结果,我的代码运行良好,但执行时间很长,这就是为什么我在寻找解决方案来解决这个问题。我编辑了示例代码,这并不是完整的解决方案,但您应该能够从中找到答案,我重点讨论了date2和date5之间的区别。此外,您的日历数据需要更新,您的日期为2009年,您提供的示例从2010年开始