views:

489

answers:

2

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"
+2  A: 

Hi Thomas,

I think the following should work (this count the distinct wag.ACI_TAG_NO only when vcon.WAGON_TYPE_CODE='MS') :

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

e-g:

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

Cheers,

--
Vincent

Vincent Malgrat
+1 As much as I like the SUM(DECODE(x,y,1,0)) construct, Vincent has the right approach here,
spencer7593
A: 

You could use a subquery that filters duplicate tags. Your query is pretty complex, but this would calculate just the sum you're looking for:

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

The DISTINCT makes sure there is only one row per (wagon_type,aci_tag_no) combination.

Andomar