0

昨日、この SQL ステートメントの助けを借りて、うまく機能しています...今、このクエリのデータを使用して、別のテーブルの 2 つの列を更新しようとしています。

私が持っているクエリは次のとおりです。

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 の 2 つの列を更新することです。1 つの列 (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 に答える