将NAME查询合并到匹配ID号的第二个查询中

时间:2012-08-01 00:29:19

标签: sql sql-server-2008

以下是两个简单的查询:

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

enter image description here 第一个给出了一个小的名称表,我希望将第二个查询放入ModifiedByEntityID字段,显示名称而不是数字。

3 个答案:

答案 0 :(得分: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 

答案 1 :(得分:1)

您可以将JOIN两个查询合并为一个:

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

答案 2 :(得分:1)

执行此操作的愚蠢方法是加入您的两个查询并获取所需的字段。

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

当然,这将是一个缓慢的查询,但如果你在小桌子上工作会很有帮助。