以下是两个简单的查询:
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
第一个给出了一个小的名称表,我希望将第二个查询放入ModifiedByEntityID字段,显示名称而不是数字。
答案 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
当然,这将是一个缓慢的查询,但如果你在小桌子上工作会很有帮助。