2012-04-26 197 views
2

這是我的表:比較兩個選擇值

MyTable的Id, Title, Point, Date

所以我需要檢查是否爲Title = 'member1'所有點和日期等於所有分和Title = 'member2'日期之後Set @flag = 1否則Set @flag = 0的精確兩個選擇是:

Select Point, Date From MyTable Where Title = 'member1' 

而且

Select Point, Date From MyTable Where Title = 'member2' 

我需要檢查所有行是否相等。你有什麼建議?

更新

假定以下示例:

Title Point  Date 

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 -- 26 to 27 
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 

在這個例子中僅僅T1的值和T2的值是相等的,其他的則沒有。

+0

你在找什麼輸出?你如何篩選查詢?你是否希望看到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

回答

1

我猜你需要檢查成員2中沒有member1,反之亦然,所以你需要檢查兩種方式。如果你在一系列日期之間進行這種操作(下面的代碼將會是所有的日期),它可以通過一次性使用整數表來完成,並且效率更高。

IF NOT EXISTS (
SELECT 
    Point, 
    Date 
FROM 
    MyTable aa 
LEFT OUTER JOIN MyTable bb 
ON aa.point = bb.point 
AND aa.date = bb.date 

WHERE 
    aa.title = 'member1' 
AND bb.title = 'member2' 
AND bb.title IS NULL 

UNION 

SELECT 
    Point, 
    Date 
FROM 
    MyTable aa 
LEFT OUTER JOIN MyTable bb 
ON aa.point = bb.point 
AND aa.date = bb.date 

WHERE 
    aa.title = 'member2' 
AND bb.title = 'member1' 
AND bb.title IS NULL 
) 
SELECT @Flag = 1 
+0

這不是我的答案,如果任何行是兩個標題之間的聯合總是返回true,但是我需要如果所有行一致都是真的,並且如果其中只有一個不是,或者任何額外的行將被返回false。我將通過示例更新問題。 – Saeid 2012-04-26 07:30:29

0

這不會贏得任何美容或表演比賽,但它應該足夠。它的要點是

  • 獲取每個標題的計數
  • 加入MyTable本身你喜歡比較
  • 加入兩個各自伯爵表
  • 只選擇結果,其中兩個各領域計數匹配

SQL語句

;WITH cnt (Title, cnt) AS (
    SELECT Title, COUNT(*) 
    FROM MyTable 
    GROUP BY 
      Title 
) 
SELECT DISTINCT mt1.Title 
     , mt2.Title 
FROM MyTable mt1 
     INNER JOIN MyTable mt2 ON mt1.Point = mt2.Point 
            AND mt1.Date = mt2.Date 
            AND mt1.Title < mt2.Title 
     INNER JOIN cnt cnt1 ON cnt1.Title = mt1.Title 
     INNER JOIN cnt cnt2 ON cnt2.Title = mt2.Title   
WHERE cnt1.cnt = cnt2.cnt   
1

這是我該怎麼做的。計劃是獲取每個結果集,然後將其轉換爲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做什麼,這應該這樣做。