我想知道你们是否可以帮助我在单一查询中简化以下查询。
我有10个查询,就像下面的查询一样。所有这些之间的唯一区别是字段“Outcoming Task”,以1开头,直到10。
SELECT
S1.[Main Project] AS Company,
S1.[Main Link] AS Project,
S1.[Name] AS Milestone,
S1.[Start] AS MilestoneDueDate,
S2.[Name] AS Successor,
S2.[Start] AS SuccessorDueDate
FROM Sheet1 AS S1 INNER JOIN Sheet1 AS S2 ON S1.[Outcoming Task 1] = S2.[ID]
WHERE
S1.Start >S2.[Start] AND
S1.[%compl#] <> 100 AND
(S1.[Task class] <> '***Sammelvorgang' AND S1.[Task class] <> '' AND S1.[Task class] <> 'M D-Muster') AND
(S2.[Task class] <> '***Sammelvorgang' AND S2.[Task class] <> '' AND S2.[Task class] <> 'M D-Muster')
ORDER BY
S1.[Main Project],
S1.[Main Link],
S1.[Name];
我创建它的原因是因为DBA而不是为任务之间的连接创建表,创建了10个字段,如Outcoming Task 1,Outcoming Task 2和Outcoming Task 3 ...
是否可以将所有查询合并为一个? 你们能给我一个关于如何做的小费吗?
信息: MS-ACCESS 2010
谢谢,
答案 0 :(得分:0)
尝试类似下面的查询。我还认为,列名中的空格不是最佳实践。
SELECT
S1.[Main Project] AS Company,
S1.[Main Link] AS Project,
S1.[Name] AS Milestone,
S1.[Start] AS MilestoneDueDate,
S2.[Name] AS Successor_Task1,
S2.[Start] AS SuccessorDueDate_Task1,
S3.[Name] AS Successor_Task2,
S3.[Start] AS SuccessorDueDate_Task2,
... 3, 4, 5, 6, 7, 8, 9, 10 ...
FROM
Sheet1 AS S1
LEFT JOIN Sheet1 AS S2
ON S1.[Outcoming Task 1] = S2.[ID]
LEFT JOIN Sheet1 AS S3
ON S1.[Outcoming Task 2] = S3.[ID]
... 3, 4, 5, 6, 7, 8, 9, 10 ...
WHERE
S1.Start >S2.[Start] AND
S1.[%compl#] <> 100 AND
(S1.[Task class] <> '***Sammelvorgang' AND S1.[Task class] <> '' AND S1.[Task class] <> 'M D-Muster') AND
(S2.[Task class] <> '***Sammelvorgang' AND S2.[Task class] <> '' AND S2.[Task class] <> 'M D-Muster') AND
(S3.[Task class] <> '***Sammelvorgang' AND S3.[Task class] <> '' AND S3.[Task class] <> 'M D-Muster')
... 3, 4, 5, 6, 7, 8, 9, 10 ...
ORDER BY
S1.[Main Project],
S1.[Main Link],
S1.[Name];
答案 1 :(得分:0)
听起来好像你想要一个UNION查询。这是一个简单的例子:
假设你有两张桌子
<强> [表1] 强>
[ID]自动编号
[输出任务1]数字(长整数)
[Outcoming Task 2] Numeric(Long Integer)
带数据
ID Outcoming Task 1 Outcoming Task 2
-- ---------------- ----------------
1 1 2
2 1 3
<强> [任务] 强>
[ID]自动编号
[TaskName]文字(255)
带数据
ID TaskName
-- --------
1 Task1
2 Task2
3 Task3
查询
SELECT t1.[ID], 1 AS [Outcoming Task], t.[TaskName]
FROM [Table1] t1 INNER JOIN [Tasks] t ON t.[ID] = t1.[Outcoming Task 1]
UNION
SELECT t1.[ID], 2 AS [Outcoming Task], t.[TaskName]
FROM [Table1] t1 INNER JOIN [Tasks] t ON t.[ID] = t1.[Outcoming Task 2]
将产生
ID Outcoming Task TaskName
-- -------------- --------
1 1 Task1
1 2 Task2
2 1 Task1
2 2 Task3