将联合结果与重复行组合

时间:2017-07-07 12:58:33

标签: sql-server union

我有以下SQL查询:

SELECT qryRef_Routes_tbl.Ref_Route_Name, qryRef_Routes_tbl.Ref_Route_ID, tblFMS_Data.FM_FinYear, tblFMS_Data.FM_Period,
 tblPeriod_Lookup.PL_ID, Sum(IIf([FM_TrustNo]=0 And [FM_Incident]=25 And [Ref_Fault_Valid_Combo]=-1,1,0)) AS Success,
 Sum(IIf([FM_Incident]=25 And [Ref_Fault_Valid_Combo]=-1,1,0)) AS CountPrevent
FROM 
(
 ( 
  ( tblFMS_Data INNER JOIN qryRef_Routes_tbl ON tblFMS_Data.FM_Route = qryRef_Routes_tbl.Ref_Route_ID )
    INNER JOIN tblRCM_II_Assets ON tblFMS_Data.FM_EllipseNo = tblRCM_II_Assets.RI_Number 
  )
  INNER JOIN tblPeriod_Lookup ON tblFMS_Data.FM_FinYear = tblPeriod_Lookup.PL_FinYear
   AND tblFMS_Data.FM_Period = tblPeriod_Lookup.PL_PeriodNo
)
INNER JOIN qryRef_Detectable_Failure_Valid_Combo ON tblFMS_Data.FM_Component2 = qryRef_Detectable_Failure_Valid_Combo.Ref_Fault_Comp2_ID
 AND tblFMS_Data.FM_Component = qryRef_Detectable_Failure_Valid_Combo.Ref_Fault_Comp1_ID
GROUP BY qryRef_Routes_tbl.Ref_Route_Name, qryRef_Routes_tbl.Ref_Route_ID, tblFMS_Data.FM_FinYear, tblFMS_Data.FM_Period, tblPeriod_Lookup.PL_ID
HAVING
(
 (
  tblPeriod_Lookup.PL_ID Between [Forms]![frmMainMenu]![PriorPeriodID] And
  ([Forms]![frmMainMenu]![PriorPeriodID]-12)
  )
)
UNION
SELECT qryRef_Routes_tbl.Ref_Route_Name, qryRef_Routes_tbl.Ref_Route_ID, tblFMS_Data.FM_FinYear, tblFMS_Data.FM_Period,
 tblPeriod_Lookup.PL_ID, Sum(IIf([FM_TrustNo]=0 And [FM_Incident]=25 And [Ref_Fault_Valid_Combo]=-1,1,0)) AS Success,
 Sum(IIf([FM_Incident]=25 And [Ref_Fault_Valid_Combo]=-1,1,0)) AS CountPrevent
FROM
(
 (
  ( tblFMS_Data INNER JOIN qryRef_Routes_tbl ON tblFMS_Data.FM_Route = qryRef_Routes_tbl.Ref_Route_ID)
    INNER JOIN tblRCM_II_Assets ON tblFMS_Data.FM_EllipseNo = tblRCM_II_Assets.RI_Pway_Number
  )
  INNER JOIN tblPeriod_Lookup ON tblFMS_Data.FM_FinYear = tblPeriod_Lookup.PL_FinYear
   AND tblFMS_Data.FM_Period = tblPeriod_Lookup.PL_PeriodNo
)
INNER JOIN qryRef_Detectable_Failure_Valid_Combo ON tblFMS_Data.FM_Component2 = qryRef_Detectable_Failure_Valid_Combo.Ref_Fault_Comp2_ID
 AND tblFMS_Data.FM_Component = qryRef_Detectable_Failure_Valid_Combo.Ref_Fault_Comp1_ID
GROUP BY qryRef_Routes_tbl.Ref_Route_Name, qryRef_Routes_tbl.Ref_Route_ID, tblFMS_Data.FM_FinYear, tblFMS_Data.FM_Period, tblPeriod_Lookup.PL_ID
HAVING
(
 (
   tblPeriod_Lookup.PL_ID Between [Forms]![frmMainMenu]![PriorPeriodID] And [Forms]![frmMainMenu]![PriorPeriodID]-12
  )
);

此结果是重复的。我希望组合重复行的结果。

请注意,在Union'

的2个查询中,Join查询略有不同

2 个答案:

答案 0 :(得分:0)

如果您不想复制值,请使用public static void main(String[] args) { Scanner userinput = new Scanner(System.in); String guess = ""; String scramble = ""; String[] questions = {"animal", "duck", "dinosaur", "butterfly", "dog", "horse", "cow", "cat", "elephant", "crocodile", "alligator"}; char charArr[] = null; String wordToGuess = questions[new Random().nextInt(questions.length)];// questions[0] for (int a = 0; a < wordToGuess.length(); a++) { charArr = wordToGuess.toCharArray(); //0a 1n 2i 3m 4a 5l 6s //generating a random number from the length of word to guess. Example: Animal, generated no = "1" int rdm = new Random().nextInt(charArr.length); //shuffling the words char temp = charArr[a]; //charArr[0] is the letter "A" which is moved to temp charArr[a] = charArr[rdm]; //A is then moved to charArr[rdm] which is index 1, so the word is currently "AAimals' charArr[rdm] = temp; //charArr[1] contains n which is then moved to temmp, which is 0, becoming nAimal } scramble = new String(charArr); System.out.println("Your word is: " + scramble); do { while (!guess.equals(wordToGuess)) { System.out.print(">>> "); guess = userinput.nextLine(); if (!guess.equals(wordToGuess)) { System.out.print(">>> "); System.out.println("Please try again!"); } else { System.out.println(">>> Great Job getting the answer!"); } } }while (!guess.equalsIgnoreCase("end")); } }

答案 1 :(得分:0)

也许你的意思是在聚合上结合工会的结果......

SELECT sub.Ref_Route_Name
     , sub.Ref_Route_ID
     , sub.FM_FinYear
     , sub.FM_Period
     , sub.PL_ID
     , Sum(sub.Success) as Success
     , Sum(sub.CountPrevent) as countPrevent
FROM (
SELECT qryRef_Routes_tbl.Ref_Route_Name
     , qryRef_Routes_tbl.Ref_Route_ID
     , tblFMS_Data.FM_FinYear
     , tblFMS_Data.FM_Period
     , tblPeriod_Lookup.PL_ID
     , Sum(IIf([FM_TrustNo]=0 And [FM_Incident]=25 And [Ref_Fault_Valid_Combo]=-1,1,0)) AS Success
     , Sum(IIf([FM_Incident]=25 And [Ref_Fault_Valid_Combo]=-1,1,0)) AS CountPrevent
FROM tblFMS_Data 
INNER JOIN qryRef_Routes_tbl 
  ON tblFMS_Data.FM_Route = qryRef_Routes_tbl.Ref_Route_ID 
INNER JOIN tblRCM_II_Assets 
  ON tblFMS_Data.FM_EllipseNo = tblRCM_II_Assets.RI_Number 
INNER JOIN tblPeriod_Lookup PL
  ON tblFMS_Data.FM_FinYear = tblPeriod_Lookup.PL_FinYear
 AND tblFMS_Data.FM_Period = tblPeriod_Lookup.PL_PeriodNo
INNER JOIN qryRef_Detectable_Failure_Valid_Combo  rdfvc
  ON tblFMS_Data.FM_Component2 = rdfvc.Ref_Fault_Comp2_ID
 AND tblFMS_Data.FM_Component = rdfvc.Ref_Fault_Comp1_ID
WHERE PL.PL_ID Between [Forms]![frmMainMenu]![PriorPeriodID] And[Forms]![frmMainMenu]![PriorPeriodID]-12
GROUP BY qryRef_Routes_tbl.Ref_Route_Name
    , qryRef_Routes_tbl.Ref_Route_ID
    , tblFMS_Data.FM_FinYear
    , tblFMS_Data.FM_Period, tblPeriod_Lookup.PL_ID
UNION ALL
SELECT qryRef_Routes_tbl.Ref_Route_Name
     , qryRef_Routes_tbl.Ref_Route_ID
     , tblFMS_Data.FM_FinYear
     , tblFMS_Data.FM_Period
     , tblPeriod_Lookup.PL_ID
     , Sum(IIf([FM_TrustNo]=0 And [FM_Incident]=25 And [Ref_Fault_Valid_Combo]=-1,1,0)) AS Success
     , Sum(IIf([FM_Incident]=25 And [Ref_Fault_Valid_Combo]=-1,1,0)) AS CountPrevent
FROM tblFMS_Data 
INNER JOIN qryRef_Routes_tbl 
  ON tblFMS_Data.FM_Route = qryRef_Routes_tbl.Ref_Route_ID
INNER JOIN tblRCM_II_Assets 
  ON tblFMS_Data.FM_EllipseNo = tblRCM_II_Assets.RI_Pway_Number
INNER JOIN tblPeriod_Lookup PL 
  ON tblFMS_Data.FM_FinYear = tblPeriod_Lookup.PL_FinYear
 AND tblFMS_Data.FM_Period = tblPeriod_Lookup.PL_PeriodNo
INNER JOIN qryRef_Detectable_Failure_Valid_Combo rdfvc
  ON tblFMS_Data.FM_Component2 = rdfvc.Ref_Fault_Comp2_ID
 AND tblFMS_Data.FM_Component = rdfvc.Ref_Fault_Comp1_ID
WHERE  PL.PL_ID Between [Forms]![frmMainMenu]![PriorPeriodID] And [Forms]![frmMainMenu]![PriorPeriodID]-12
GROUP BY qryRef_Routes_tbl.Ref_Route_Name
       , qryRef_Routes_tbl.Ref_Route_ID
       , tblFMS_Data.FM_FinYear
       , tblFMS_Data.FM_Period
       , tblPeriod_Lookup.PL_ID

) Sub
GROUP BY Sub.Ref_Route_Name
       , sub.Ref_Route_ID
       , sub.FM_FinYear
       , sub.FM_Period
       , sub.PL_ID

不确定查询的重复结果是否应“合并”或作为重复删除,因此添加了一个联合。

相关问题