我通过引用两个同义词创建了一个视图。我要收获查看表的当前查询如下
SELECT dbo.synonym1.JobNo
, dbo.synonym1.Customer
, dbo.synonym2.PostalService
, dbo.synonym2.FirstDate
, dbo.synonym2.SecondDate
FROM dbo.synonym1
INNER JOIN dbo.synonym2 ON dbo.synonym1.JobNo = sbo.synonym2.JobNo
WHERE dbo.synonym2.PostalService = 'UPS'
现在,我想在每一行中找到dbo.synonym2.FirstDate
和dbo.synonym2.SecondDate
之间的区别,并在同一行中再次INSERT
与查看表中的差异,因为存在差异。< / p>
例如,如果FirstDate
为2012-06-03 00:00:00.000
且SecondDate
为2012-06-05 00:00:00.000
,那么它应该插入相同的行两次。
我正在使用SQL Server 2008 R2,而且我是SQL的新手。请帮帮我
目前在视图中使用上述查询填充的示例数据如下所示
JobNo Customer PostalService FirstDate SecondDate
1 ABC UPS 2012-06-03 00:00:00.000 2012-06-03 00:00:00.000
2 DEF UPS 2012-06-03 00:00:00.000 2012-06-06 00:00:00.000
3 XYZ UPS 2012-06-03 00:00:00.000 2012-06-05 00:00:00.000
我需要一个结果如下
JobNo Customer PostalService FirstDate SecondDate
1 ABC UPS 2012-06-03 00:00:00.000 2012-06-03 00:00:00.000
2 DEF UPS 2012-06-03 00:00:00.000 2012-06-05 00:00:00.000
2 DEF UPS 2012-06-03 00:00:00.000 2012-06-05 00:00:00.000
2 XYZ UPS 2012-06-03 00:00:00.000 2012-06-05 00:00:00.000
3 XYZ UPS 2012-06-03 00:00:00.000 2012-06-06 00:00:00.000
3 XYZ UPS 2012-06-03 00:00:00.000 2012-06-06 00:00:00.000
如果您看到JobNo 2的日期之间的差异为3,那么数据将重复三次。并且JobNo 3为2,因此数据重复两次。
答案 0 :(得分:1)
您的请求有点令人困惑,因为通常不会插入视图。
但是,以下内容会将行插入表
insert into ViewTable(JobNo, Customer, PostalService, FirstDate, SecondDate)
SELECT dbo.synonym1.JobNo, dbo.synonym1.Customer, dbo.synonym2.PostalService,
dbo.synonym2.FirstDate, dbo.synonym2.SecondDate
FROM dbo.synonym1 INNER JOIN
dbo.synonym2
ON dbo.synonym1.JobNo = sbo.synonym2.JobNo
WHERE dbo.synonym2.PostalService = 'UPS' and
datediff(d, FirstDate, SecondDate) <> 0
您需要将ViewTable定义为实际表格。
答案 1 :(得分:0)
听起来你想要的是下面的union语句来填充这个临时表。 除非我误解了你要求的东西。
DECLARE @MyTable as TABLE(JobNo VARHCAR(50), Customer VARCHAR(50),
Postalservice VARCHAR(50), MyDate DateTime)
INSERT into @MyTable
SELECT dbo.synonym1.JobNo
, dbo.synonym1.Customer
, dbo.synonym2.PostalService
, dbo.synonym2.FirstDate
FROM dbo.synonym1
INNER JOIN dbo.synonym2 ON dbo.synonym1.JobNo = sbo.synonym2.JobNo
WHERE dbo.synonym2.PostalService = 'UPS'
UNION
SELECT dbo.synonym1.JobNo
, dbo.synonym1.Customer
, dbo.synonym2.PostalService
, dbo.synonym2.SecondDate
FROM dbo.synonym1
INNER JOIN dbo.synonym2 ON dbo.synonym1.JobNo = sbo.synonym2.JobNo
WHERE dbo.synonym2.PostalService = 'UPS'
从@MyTable
插入您的视图答案 2 :(得分:0)
Create Table #temp
(
StartDate DateTime,
EndDate DateTime
)
Insert into #temp(StartDate, EndDate)
Values('2012-06-03 00:00:00.000', '2012-06-03 00:00:00.000');
Insert into #temp(StartDate, EndDate)
Values('2012-06-03 00:00:00.000', '2012-06-06 00:00:00.000');
Insert into #temp(StartDate, EndDate)
Values('2012-06-03 00:00:00.000', '2012-06-05 00:00:00.000');
With DateRange As
(
select Convert(DateTime, '2012-06-02 00:00:00.000') as StartDate,
DATEADD(dd, 1, Convert(DateTime, '2012-06-02 00:00:00.000')) as EndDate
Union All
select EndDate, DATEADD(dd, 1, EndDate) From DateRange
Where EndDate <= Convert(DateTime, '2012-06-10 00:00:00.000')
)
Select T.* from #temp T
Left Join DateRange R on ((T.StartDate < R.EndDate and T.EndDate > R.StartDate)
or (T.StartDate = R.EndDate and T.EndDate = R.StartDate))
order by T.EndDate
Drop table #temp