SQL查询,不包括同一列的特定值

时间:2012-12-17 12:56:15

标签: sql ms-access

我想生成一个项目进度报告,其中包括几乎检查给定时间范围内已完成里程碑的百分比。 (例如:A公司今天有10个到期里程碑,但只有8个已完成,因此它们的进度比计划慢20%。)

这一部分很简单,而且效果很好,但在生成之前,我确定项目计划是否获得批准。已批准的项目计划是里程碑1和2 = 100的项目计划。

下面你可以看到我必须使用的表结构。

ID          Company     Project     Milestone   Date        Completion
170825240   Company A   Project 1   Milestone 1 29.10.12    100
170825311   Company A   Project 1   Milestone 2 29.10.12    0
170825242   Company A   Project 1   Milestone 3 05.11.12    100
170825173   Company A   Project 1   Milestone 4 17.12.12    0
170825104   Company B   Project 1   Milestone 1 29.10.12    100
170825035   Company B   Project 1   Milestone 2 29.10.12    100
170824966   Company B   Project 1   Milestone 3 05.11.12    100
170824897   Company B   Project 1   Milestone 4 17.12.12    0
170824828   Company C   Project 1   Milestone 1 29.10.12    100
170824759   Company C   Project 1   Milestone 2 29.10.12    100
170824690   Company C   Project 1   Milestone 3 05.11.12    0
170824621   Company C   Project 1   Milestone 4 17.12.12    0

在此快照中,项目1被批准用于公司B和C.

我面临的问题是如何在单个查询中合并仅过滤已批准的计划的进度。我不想手动编写例外,因为我们正在谈论公司和项目的1600种组合。

任何人都知道该怎么做?

感谢!!!!

已更新

SELECT S1.[Company], COUNT(S1.[Milestone]) AS ShouldBeMilestones

FROM Sheet1 AS S1

INNER JOIN Sheet1 S2
ON S2.[Company] = S1.[Company]
AND S2.[Project] = S1.[Project]
AND S2.[Milestone] = 'Milestone 1'
AND S2.[%compl#] = 100

INNER JOIN Sheet1 S3
ON S3.[Company] = S1.[Company]
AND S3.[Project] = S1.[Project]
AND S3.[Milestone] = 'Milestone 2'
AND S3.[%compl#] = 100

WHERE ((S1.[Task class] <> 'A') AND (S1.[Task class] <> 'B') AND (S1.[Task class] <> ''))
AND S1.[Milestone] NOT LIKE '0.*' AND S1.[Milestone] NOT LIKE '1.*'
AND S1.[Start] <= Now()

GROUP BY S1.[Company];

2 个答案:

答案 0 :(得分:1)

添加两个where条件,每个条件都会检查里程碑的完成情况:

from    Milestones m
where   exists
        (
        select  *
        from    Milestones m1
        where   m1.Company = m.Company
                and m1.Project = m.Project
                and m1.Milestone = 'Milestone 1'
                and m1.Completion = 100
        )
        and exists
        (
        select  *
        from    Milestones m2
        where   m2.Company = m.Company
                and m2.Project = m.Project
                and m2.Milestone = 'Milestone 2'
                and m2.Completion = 100
        )

答案 1 :(得分:1)

这也可以从前一个答案中借用联接;)

select m.* 
(from    Milestones m
inner join Milesones as m1 
      on m1.Company = m.Company
      and m1.Project = m.Project
      and m1.Milestone = 'Milestone 1'
      and m1.Completion = 100)
inner join Milestones as m2
        on m2.Company = m.Company
                and m2.Project = m.Project
                and m2.Milestone = 'Milestone 2'
                and m2.Completion = 100

编辑应该解决一些ms访问问题,但是无法在访问时对其进行测试,我没有看到()围绕第一次加入的好处