0

我需要更改类似的 sum(decode()) 表达式

SUM(Decode(vcon.WAGON_TYPE_CODE,'MS',1,0)) 

使用 vcon.WAGON-TYPE-CODE = 'MS' 但仅当 wag.ACI-TAG-NO 不同时才计算行数。

所以如果两列看起来像这样

vcon.WAGON_TYPE_CODE   wag.ACI_TAG_NO 
MS                     HI1111 
SS                     C99999 
MS                     HI1111
MS                     HI7777
SS                     HI8888
MS                     HI6666

该表达式需要返回数字 3 而不是 SUM(Decode(vcon.WAGON_TYPE_CODE,'MS',1,0)) 当前所做的那样返回数字 4。

有什么建议么?

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"
4

2 回答 2

2

我认为以下应该有效(这wag.ACI_TAG_NO仅在以下情况下才算不同vcon.WAGON_TYPE_CODE='MS'):

COUNT(DISTINCT Decode(vcon.WAGON_TYPE_CODE,'MS',wag.ACI_TAG_NO,NULL))

例如:

SQL> WITH data AS (
  2     SELECT 'MS' WAGON_TYPE_CODE, 'HI1111' ACI_TAG_NO FROM DUAL UNION ALL
  3     SELECT 'SS', 'C99999' FROM DUAL UNION ALL
  4     SELECT 'MS', 'HI1111' FROM DUAL UNION ALL
  5     SELECT 'MS', 'HI7777' FROM DUAL UNION ALL
  6     SELECT 'SS', 'HI8888' FROM DUAL UNION ALL
  7     SELECT 'MS', 'HI6666' FROM DUAL
  8  )
  9  SELECT COUNT(DISTINCT decode(WAGON_TYPE_CODE,'MS',ACI_TAG_NO,NULL))
 10    FROM DATA
 11  ;

COUNT(DISTINCTDECODE(WAGON_TYP
------------------------------
                             3

干杯,

——
文森特

于 2009-06-11T08:05:15.873 回答
0

您可以使用过滤重复标签的子查询。您的查询非常复杂,但这只会计算您要查找的总和:

SELECT SUM(Decode(sub.WAGON_TYPE_CODE,'MS',1,0))
FROM (
    SELECT DISTINCT vcon.WAGON_TYPE_CODE, wag.ACI_TAG_NO
    FROM widsys.wagon wag
    INNER JOIN widsys.consist con 
        ON con.wagon_id = wag.wagon_id
    INNER JOIN widsys.v_consist_ore_detail vcon
        ON vcon.CONSIST_ID = con.CONSIST_ID
) sub

DISTINCT 确保每个 (wagon_type,aci_tag_no) 组合只有一行。

于 2009-06-11T10:36:46.140 回答