這是我該怎麼做的。計劃是獲取每個結果集,然後將其轉換爲XML並比較兩個XML結果。更容易(在我看來)。
CREATE TABLE MyTable
(
[id] integer identity,
[Title] varchar(1024),
[Point] int,
[Date] datetime
);
insert into MyTable([Title], [Point], [Date])
values('T1', 1, '2012-04-26 07:14:34.000'),
('T1', 2, '2012-07-26 07:14:34.000'),
('T1', 3, '2012-06-26 07:14:34.000'),
('T1', 4, '2012-05-26 07:14:34.000'),
('T2', 1, '2012-04-26 07:14:34.000'),
('T2', 2, '2012-07-26 07:14:34.000'),
('T2', 3, '2012-06-26 07:14:34.000'),
('T2', 4, '2012-05-26 07:14:34.000'),
('T3', 4, '2012-05-26 07:14:34.000'),
('T3', 3, '2012-06-26 07:14:34.000'),
('T4', 1, '2012-04-26 07:14:34.000'),
('T4', 2, '2012-07-26 07:14:34.000'),
('T4', 3, '2012-06-26 07:14:34.000'),
('T4', 4, '2012-05-27 07:14:34.000'),
('T5', 2, '2012-12-27 07:14:34.000'),
('T5', 6, '2012-05-27 07:14:34.000'),
('T5', 3, '2012-07-26 07:14:34.000');
-- your original queries
Select Point, Date From MyTable Where Title = 'T1';
Select Point, Date From MyTable Where Title = 'T2';
-- first I am going to just get each one as XML
SELECT [Point], [Date]
FROM MyTable
WHERE Title = 'T1'
FOR XML PATH('');
SELECT [Point], [Date]
FROM MyTable
WHERE Title = 'T2'
FOR XML PATH('');
-- first just get the two results into one result set
select t.FirstCheck, t.SecondCheck
from (
select (SELECT [Point], [Date]
FROM MyTable
WHERE Title = 'T1'
FOR XML PATH('')) as FirstCheck,
(SELECT [Point], [Date]
FROM MyTable
WHERE Title = 'T2'
FOR XML PATH('')) as SecondCheck
) as t;
-- now for the real check.
declare @flag int;
select @flag = case when t.FirstCheck = t.SecondCheck then 1 else 0 end
from (
select (SELECT [Point], [Date]
FROM MyTable
WHERE Title = 'T1'
FOR XML PATH('')) as FirstCheck,
(SELECT [Point], [Date]
FROM MyTable
WHERE Title = 'T2'
FOR XML PATH('')) as SecondCheck
) as t;
-- this should return 1
select @flag as Flag;
select @flag = case when t.FirstCheck = t.SecondCheck then 1 else 0 end
from (
select (SELECT [Point], [Date]
FROM MyTable
WHERE Title = 'T1'
FOR XML PATH('')) as FirstCheck,
(SELECT [Point], [Date]
FROM MyTable
WHERE Title = 'T3'
FOR XML PATH('')) as SecondCheck
) as t;
-- this should return 0
select @flag as Flag;
如果我理解你attemptig做什麼,這應該這樣做。
你在找什麼輸出?你如何篩選查詢?你是否希望看到t4.4與t1.4,t2.4,t3.4有不同的日期。是否缺少t5.1和t5.4顯着(反過來說是t1.6等)如果您知道有5個標題,通過/ count的簡單分組將有助於查找空位;並且只有4點1出現在26.4上,例如 – u07ch 2012-04-26 09:13:48