Hi folks,
I need to change the sum(decode()) expressions that are like
SUM(Decode(vcon.WAGON_TYPE_CODE,'MS',1,0))
to something that counts rows with vcon.WAGON-TYPE-CODE = 'MS' but only when wag.ACI-TAG-NO is distinct.
So if two columns look like this
vcon.WAGON_TYPE_CODE wag.ACI_TAG_NO
MS HI1111
SS C99999
MS HI1111
MS HI7777
SS HI8888
MS HI6666
The expression needs to return the number 3 rather than 4 as SUM(Decode(vcon.WAGON_TYPE_CODE,'MS',1,0)) currently does.
Any suggestions?
querySELECT = "SELECT "
querySELECT = querySELECT & "trn.WID_DATE, "
querySELECT = querySELECT & "trn.MINE_CODE, "
querySELECT = querySELECT & "trn.TRAIN_CONTROL_ID, "
querySELECT = querySELECT & "trn.NUM_CARS as HBD_Car_Count, "
querySELECT = querySELECT & "SUM(Decode(vcon.WAGON_TYPE_CODE,'MS',1,0)) M_Series, "
querySELECT = querySELECT & "(SUM(Decode(vcon.WAGON_TYPE_CODE,'SS',1,0))-SUM(Decode(wag.ACI_TAG_NO,'HI0000',1,0))) S_Series, "
querySELECT = querySELECT & "SUM(Decode(vcon.WAGON_TYPE_CODE,'CS',1,0)) C_Series, "
querySELECT = querySELECT & "SUM(Decode(wag.ACI_TAG_NO,'HI0000',1,0)) as No_Tag, "
querySELECT = querySELECT & "(COUNT(1) - trn.NUM_CARS) DB_Mismatch "
queryFROM = "FROM widsys.consist con, widsys.train trn, widsys.wagon wag, widsys.v_consist_ore_detail vcon "
queryWHERE = "WHERE trn.TRAIN_RECORD_ID = con.TRAIN_RECORD_ID "
queryWHERE = queryWHERE & "AND con.WAGON_ID = wag.WAGON_ID "
queryWHERE = queryWHERE & "AND ((vcon.CONSIST_ID=con.CONSIST_ID) "
queryWHERE = queryWHERE & "AND trn.MINE_CODE In (" & mine & ") "
queryWHERE = queryWHERE & "AND (trn.DIRECTION='N') "
queryWHERE = queryWHERE & "AND (wag.ACI_TAG_TYPE In ('CONTROL','SLAVE','ORE')) "
queryWHERE = queryWHERE & "AND (trn.WID_DATE>={ts '" & startDate & "'} "
queryWHERE = queryWHERE & "AND trn.WID_DATE<={ts '" & endDate & "'})) "
queryGROUPBY = "GROUP BY trn.WID_DATE, trn.MINE_CODE, trn.TRAIN_CONTROL_ID, trn.NUM_CARS "
queryORDERBY = "ORDER BY trn.WID_DATE DESC"