0

Functionality I'm trying to add to my DB2 stored procedure:

    Select a MIN() date from a joined table column.
    IF there was more than one row in this joined table, append a " * " to the date.

Thanks, any help or guidance is much appreciated.

4

2 回答 2

1

尚不清楚需要哪种 DB2 风格,也不清楚任何建议是否有效。这适用于 DB2 for i:

SELECT
 T1.joinCol1,
 max( T2.somedateColumn ),
 count( t2.somedateColumn ),
 char(max( T2.somedateColumn )) concat case when count( T2.somedateColumn )>1 then '*' else '' end 
 FROM joinFile1 t1 join joinFile2 t2
           on joinCol1 = joinCol2
 GROUP BY T1.joinCol1
 ORDER BY T1.joinCol1

SQL 相当通用,因此它应该转换为许多环境和版本。

根据需要替换表名和列名。这里的 COUNT() 实际上是从 JOIN 计算行数,而不是具体日期出现的次数。如果需要重复日期的计数,则还需要对此示例进行一些更改。

于 2014-03-25T03:14:44.063 回答
0

Hope this helps

Say I have result coming as


1   Jeff    1
2   Jeff    333
3   Jeff    77
4   Jeff    1
5   Jeff    14
6   Bob     22
7   Bob     4
8   Bob     5
9   Bob     6

Here the value 1 is repeated twice(in 3 column)

So, this query gets the count as 2 along with the * concatenated along with it

SELECT A.USER_VAL, DECODE(A.CNT, '1', A.CNT, '0', A.CNT, CONCAT(A.CNT, '*')) AS CNT FROM (SELECT DISTINCT BT.USER_VAL, CAST(COUNT(*) AS VARCHAR2(2)) AS CNT FROM SO_BUFFER_TABLE_8 BT GROUP BY BT.USER_VAL) A

于 2012-05-12T06:32:37.760 回答