0

昨天我在这个 SQL 语句上得到了帮助,它工作得非常好......现在我正在尝试使用这个查询中的数据来更新不同表上的两列。

这是我的查询:

SELECT x.TechID,
       Count(*) AS cnt,
       tblEmployeeData.LName,
       tblEmployeeData.Pernr,
       tblEmployeeData.Occurrences,
       tblEmployeeData.Standing
FROM   tblEmployeeData
       INNER JOIN tblOccurrence AS x
         ON tblEmployeeData.TechID = x.TechID
WHERE  ( ( ( x.OccurrenceDate ) BETWEEN Dateadd("m", -6, Date()) AND Date() )
         AND ( ( EXISTS (SELECT *
                         FROM   tblOccurrence AS y
                         WHERE  y.TechID = x.TechID
                                AND Dateadd ("d", -1, x.[OccurrenceDate]) = y.[OccurrenceDate]) ) = False ) )
GROUP  BY x.TechID,
          tblEmployeeData.LName,
          tblEmployeeData.Pernr;

我想要做的是获取结果并更新 tblEmployeeData 两列。一列 (tblEmployeeData.Occorrences) 将是由 'cnt' 表示的值......然后是困难的部分...... tblEmployeeData.Standing 列将使用来自 'cnt' 的值进行更新,如下所示:

0-3 = "Good"
4-5 = "Verbal Warning"
6-7 = "Written Warning"
8 = "Final Written Warning"
9+ = "Termination" 

这已经是一个很大的 SQL 语句了,这超出了我的想象!

4

1 回答 1

0

您可以将查询包装在另一个查询中:

SELECT TechID, Rank FROM Rank,
(SELECT x.TechID, Count(*) AS cnt, tblEmployeeData.LName, 
    tblEmployeeData.Pernr, tblEmployeeData.Occurrences, tblEmployeeData.Standing
FROM tblEmployeeData
INNER JOIN tblOccurrence AS x ON tblEmployeeData.TechID = x.TechID
WHERE (((x.OccurrenceDate) Between DateAdd("m",-6,Date()) And Date())
  AND ((Exists     
    (SELECT * FROM tblOccurrence AS y  WHERE y.TechID = x.TechID AND DATEADD 
    ("d", -1, x.[OccurrenceDate]) = y.[OccurrenceDate]))=False))
GROUP BY x.TechID, tblEmployeeData.LName, tblEmployeeData.Pernr) a
WHERE a.Cnt BETWEEN Rank.Low And rank.High

这个想法是您将查询与Rank表一起使用,如下所示:

Low High    Rank
0   3       Good
4   5       Verbal Warning
6   7       Written Warning
8   8       Final Written Warning
9   99      Termination

编辑重新评论

这对我来说是一个粗略的模型

SELECT a.TechID, tblRank.Rank FROM tblRank, (SELECT x.TechID, Count(*) AS cnt, tblEmployeeData.LName, 
    tblEmployeeData.Pernr, tblEmployeeData.Occurrences, tblEmployeeData.Standing
FROM tblEmployeeData
INNER JOIN tblOccurrence AS x ON tblEmployeeData.TechID = x.TechID
WHERE (((x.OccurrenceDate) Between DateAdd("m",-6,Date()) And Date()) AND ((Exists     
    (SELECT * FROM tblOccurrence AS y  WHERE y.TechID = x.TechID AND DATEADD 
    ("d", -1, x.[OccurrenceDate]) = y.[OccurrenceDate]))=False))
GROUP BY x.TechID, tblEmployeeData.LName, tblEmployeeData.Pernr, tblEmployeeData.Occurrences, tblEmployeeData.Standing) a
WHERE a.Cnt BETWEEN tblRank.Low And tblrank.High
于 2012-08-14T20:46:03.343 回答