我在Visual Studio 2015和SQL Server 2014中使用了SSRS / SSDT。有一个错误已经出现在>中。 8年you can't select multiple columns from different tables that have the same name。为了解决这个问题,我需要使用子查询。我找到的每个答案都会重写给定查询以删除子查询,这通常很好,但在这种情况下不适用。如何将参数传递给SQL Server中的子查询?
列别名不适用于此错误 - 使用AS
会在"复制"上返回未知的列错误尽管列与其他所有列一起使用。 SELECT
子句中的最后两行有效,因为正在查询这些值,因此报表可以使用它们,但实际查询的其余部分不会使用它们。
这是我当前的代码(由于子查询返回多行而无法正常工作)。
SELECT t.[Description],
t.RequestedCompletionDate,
t.CommitDate,
t.StatusId,
t.PriorityId,
p.ProjectNumber,
s.Name AS StatusDescription,
pr.Name AS PriorityDescription
FROM ProjectTask t
inner join Project p
on p.Id = t.ProjectId
inner join Project_TaskStatus s
on s.Id = t.StatusId
inner join Project_Priority pr
on pr.Id = t.PriorityId
WHERE t.Type = 'ET'
AND t.StatusId NOT IN (4,7)
AND
(
SELECT StatusId FROM Project
-- WHERE ?
)
NOT IN (3, 4)
ORDER BY t.PriorityId,
t.CommitDate,
t.RequestedCompletionDate
这是注释中请求的带别名的代码。它抛出一个错误:
SELECT t.[Description],
t.RequestedCompletionDate,
t.CommitDate,
t.StatusId AS TaskStatusId,
t.PriorityId,
p.ProjectNumber,
p.StatusId AS ProjectStatusId,
s.Name AS StatusDescription,
pr.Name AS PriorityDescription
FROM ProjectTask t
inner join Project p
on p.Id = t.ProjectId
inner join Project_TaskStatus s
on s.Id = TaskStatusId
inner join Project_Priority pr
on pr.Id = t.PriorityId
WHERE t.Type = 'ET'
AND TaskStatusId NOT IN (4,7)
AND ProjectStatusId NOT IN (3,4)
ORDER BY t.PriorityId,
t.CommitDate,
t.RequestedCompletionDate
-- Invalid column name 'TaskStatusId'.
-- Invalid column name 'TaskStatusId'.
-- Invalid column name 'TaskStatusId'.
-- Invalid column name 'ProjectStatusId'.
-- Invalid column name 'ProjectStatusId'.
理想代码如下所示,但它会抛出错误An item with the same key has already been added
,这是SSRS / SSDT在尝试返回多个同名列时抛出的错误。
SELECT t.[Description],
t.RequestedCompletionDate,
t.CommitDate,
t.StatusId,
t.PriorityId,
p.ProjectNumber,
p.StatusId,
s.Name AS StatusDescription,
pr.Name AS PriorityDescription
FROM ProjectTask t
inner join Project p
on p.Id = t.ProjectId
inner join Project_TaskStatus s
on s.Id = t.StatusId
inner join Project_Priority pr
on pr.Id = t.PriorityId
WHERE t.Type = 'ET'
AND t.StatusId NOT IN (4,7)
AND p.StatusId NOT IN (3,4)
ORDER BY t.PriorityId,
t.CommitDate,
t.RequestedCompletionDate
答案 0 :(得分:1)
当前代码(不起作用,因为子查询返回多个 行)。
所以不是这个
AND
(
SELECT StatusId FROM Project
-- WHERE ?
)
NOT IN (3, 4)
你可以做到
AND
EXISTS (
SELECT 1 FROM Project p2
WHERE p2.StatusId IN (3, 4) AND p2.Id = p.Id
)
答案 1 :(得分:1)
试试这个:
SELECT t.[Description],
t.RequestedCompletionDate,
t.CommitDate,
t.StatusId AS TaskStatusId,
t.PriorityId,
p.ProjectNumber,
p.StatusId AS ProjectStatusId,
s.Name AS StatusDescription,
pr.Name AS PriorityDescription
FROM ProjectTask t
inner join Project p
on p.Id = t.ProjectId
inner join Project_TaskStatus s
on s.Id = t.StatusId
inner join Project_Priority pr
on pr.Id = t.PriorityId
WHERE t.Type = 'ET'
AND t.StatusId NOT IN (4,7)
AND p.StatusId NOT IN (3,4)
ORDER BY t.PriorityId,
t.CommitDate,
t.RequestedCompletionDate
答案 2 :(得分:0)
修复很简单,重命名第二列,即b.Field1 AS Field01或者只是省略字段
答案 3 :(得分:0)
我觉得这很愚蠢,但显然只能在子查询中调用UIView
。它知道我引用父查询的值,即使它在子查询中。谢谢大家的帮助。
p.Id