0

2つの簡単なクエリを次に示します。

SELECT EntityID, EntityName FROM EntityRelations.Entities 
 WHERE EntityID IN (SELECT ModifiedByEntityID FROM EntityRelations.AssistanceFlags af WITH (nolock)
  JOIN EntityRelations.Entities et WITH (nolock) ON af.EntityId = et.EntityId
WHERE ModifiedDate BETWEEN '6/1/12 3:00 AM'
                       AND '8/1/12 2:59 AM')

SELECT EntityName, Note, ModifiedByEntityID, ModifiedDate FROM EntityRelations.AssistanceFlags af WITH (nolock)
  JOIN EntityRelations.Entities et WITH (nolock) ON af.EntityId = et.EntityId
 WHERE ModifiedDate BETWEEN '6/1/12 3:00 AM'
                        AND '8/1/12 2:59 AM'
 ORDER BY ModifiedDate ASC

ここに画像の説明を入力してください 1つ目は、ModifiedByEntityIDフィールドへの2つ目のクエリに挿入したい名前の小さなテーブルを示し、番号の代わりに名前が表示されます。

4

3 に答える 3

1

JOIN2つのクエリを1つにまとめることができます。

SELECT EntityName, Note, ModifiedByEntityID, e.EntityName, ModifiedDate 
FROM EntityRelations.AssistanceFlags af WITH (nolock)
JOIN EntityRelations.Entities et WITH (nolock) 
  ON af.EntityId = et.EntityId
LEFT JOIN 
(
  SELECT EntityID, EntityName 
  FROM EntityRelations.Entities 
  WHERE EntityID IN (SELECT ModifiedByEntityID 
                     FROM EntityRelations.AssistanceFlags af WITH (nolock)
                     JOIN EntityRelations.Entities et WITH (nolock) 
                        ON af.EntityId = et.EntityId
                     WHERE ModifiedDate BETWEEN '6/1/12 3:00 AM' AND '8/1/12 2:59 AM')
) e
  ON ModifiedByEntityID = e.EntityID
WHERE ModifiedDate BETWEEN '6/1/12 3:00 AM' AND '8/1/12 2:59 AM'
ORDER BY ModifiedDate ASC
于 2012-08-01T00:39:27.153 に答える
1
SELECT EntityName, Note, X.EntityName , ModifiedByEntityID, ModifiedDate FROM EntityRelations.AssistanceFlags af WITH (nolock)
  JOIN EntityRelations.Entities et WITH (nolock) ON af.EntityId = et.EntityId
LEFT OUTER JOIN (SELECT EntityID, EntityName FROM EntityRelations.Entities 
         WHERE EntityID IN (SELECT ModifiedByEntityID FROM EntityRelations.AssistanceFlags af WITH (nolock)
         JOIN EntityRelations.Entities et WITH (nolock) ON af.EntityId = et.EntityId
         WHERE ModifiedDate BETWEEN '6/1/12 3:00 AM'
                                AND '8/1/12 2:59 AM'))X ON X.EntityID = ModifiedByEntityID 
 WHERE ModifiedDate BETWEEN '6/1/12 3:00 AM'
                        AND '8/1/12 2:59 AM'
 ORDER BY ModifiedDate ASC

JerryBによる編集:あいまいなEntityNameメッセージを受け取っていたので、これに適合させました。これは完全に機能しています。ありがとうございます。

DECLARE @FROM DATETIME, @TO DATETIME

    SET @FROM = '6/1/12 3:00 AM'
    SET @TO   = '8/1/12 3:00 AM'

SELECT et.EntityName, Note, X.EntityName "CSR", ModifiedDate FROM EntityRelations.AssistanceFlags af WITH (nolock) 
  JOIN EntityRelations.Entities et WITH (nolock) ON af.EntityId = et.EntityId 
LEFT OUTER JOIN (SELECT EntityID, EntityName FROM EntityRelations.Entities  
         WHERE EntityID IN (SELECT ModifiedByEntityID FROM EntityRelations.AssistanceFlags af WITH (nolock) 
         JOIN EntityRelations.Entities et WITH (nolock) ON af.EntityId = et.EntityId 
         WHERE ModifiedDate BETWEEN @FROM AND @TO))X ON X.EntityID = af.ModifiedByEntityID  
 WHERE ModifiedDate BETWEEN @FROM AND @TO
 ORDER BY ModifiedDate ASC 
于 2012-08-01T00:37:23.290 に答える
1

これを行うためのばかげた方法は、2つのクエリを結合して、目的のフィールドを取得することです。

select a.EntityName,a.Note,b.EntityID,a.ModifiedDate
from
(
SELECT EntityID, EntityName FROM EntityRelations.Entities 
 WHERE EntityID IN (SELECT ModifiedByEntityID FROM EntityRelations.AssistanceFlags af WITH (nolock)
  JOIN EntityRelations.Entities et WITH (nolock) ON af.EntityId = et.EntityId
WHERE ModifiedDate BETWEEN '6/1/12 3:00 AM'
                       AND '8/1/12 2:59 AM')
) as b
inner join 
(
SELECT EntityName, Note, ModifiedByEntityID, ModifiedDate FROM EntityRelations.AssistanceFlags af WITH (nolock)
  JOIN EntityRelations.Entities et WITH (nolock) ON af.EntityId = et.EntityId
 WHERE ModifiedDate BETWEEN '6/1/12 3:00 AM'
                        AND '8/1/12 2:59 AM'

) as a 
on b.EntityID = a.ModifiedByEntityID
 ORDER BY a. ModifiedDate ASC

もちろん、これは遅いクエリになりますが、小さなテーブルで作業する場合に役立ちます。

于 2012-08-01T00:45:12.543 に答える