Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can use WINDOW functions of DB2 to achieve this.</p> <p>You can rewrite your existing query as:</p> <pre><code>SELECT T1.REQUEST_ID, COUNT(1) OVER(PARTITION BY T1.REQUEST_ID) AS TRANS_ABC FROM TABLE1 AS T1, TABLE2 AS T2 WHERE T1.REQUEST_ID=1234 AND T1.COMPONENTID = T2.PARENTCOMPID AND T2.TRANS_TYPE='ABC' </code></pre> <p>You can use any column in the select list when you use a window function. Check the link below for more information:</p> <p><a href="http://publib.boulder.ibm.com/infocenter/db2luw/v9/index.jsp?topic=/com.ibm.db2.udb.admin.doc/doc/r0023461.htm" rel="nofollow">http://publib.boulder.ibm.com/infocenter/db2luw/v9/index.jsp?topic=/com.ibm.db2.udb.admin.doc/doc/r0023461.htm</a></p> <p>If you wanted the data to be pivoted am not sure if DB2 inherently supports PIVOT/UNPIVOT. This is the best I can construct (not sure about performance though and haven't testedt the query):</p> <p>NOTE: This query considers 6 different types of trans_types. If the Trans_Type is not finite then am not sure if there would be any easy solution.</p> <pre><code>SELECT * FROM ( SELECT t1.request_id, t1.req_user, t1.req_acct, SUM(DECODE(trans_type, 'AA',1, 0)) OVER(PARTITION BY t1.request_id) AS trans_aa, SUM(DECODE(trans_type, 'BB',1, 0)) OVER(PARTITION BY t1.request_id) AS trans_bb, SUM(DECODE(trans_type, 'CC',1, 0)) OVER(PARTITION BY t1.request_id) AS trans_cc, SUM(DECODE(trans_type, 'DD',1, 0)) OVER(PARTITION BY t1.request_id) AS trans_dd, SUM(DECODE(trans_type, 'EE',1, 0)) OVER(PARTITION BY t1.request_id) AS trans_ee, SUM(DECODE(trans_type, 'FF',1, 0)) OVER(PARTITION BY t1.request_id) AS trans_ff, SUM(1) AS total_trans ROW_NUMBER() OVER(PARTITION BY t1.request_id) AS rn FROM TABLE1 AS t1, TABLE2 AS t2 WHERE t1.request_id=1234 AND t1.componentid = t2.parentcompid ) a WHERE rn = 1 </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload