0

我想知道你们是否可以帮助我在单个查询中简化以下查询

我有 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

谢谢,

4

2 回答 2

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];
于 2013-03-21T10:49:54.970 回答
0

听起来您可能想要一个 UNION 查询。这是一个简单的例子:

假设你有两张桌子

[Table1]
[ID] AutoNumber
[Outcoming Task 1] Numeric(Long Integer)
[Outcoming Task 2] Numeric(Long Integer)

有数据

ID  Outcoming Task 1  Outcoming Task 2  
--  ----------------  ----------------  
1   1                 2  
2   1                 3

[任务]
[ID] 自动编号
[任务名称] 文本 (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
于 2013-03-21T10:56:15.233 回答