Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

get record number added to query 3

Status
Not open for further replies.

kkitt

Programmer
Dec 1, 2002
122
US
I am looking for a way to add a record number count to a selection query like below:

Code:
select field1, field2, field3
  from table
 where field1 = 'data'

output is:

  field1        field2     field3
  ------        ------     ------
   data          data       data
   ...           ...         ...

what I am looking for is:

Code:
select recnum?, field1, field2, field3
  from table
 where field1 = 'data'

output to be:

   recnum?      field1    field2    field2
   -------      ------    ------    ------
      1.         data      data     data
      2.          ...      ....      ...
etc....

Thanks in advance
 
Consider OLAP function Row_Number():

Code:
SELECT ROWNUMBER() OVER(ORDER BY FIELD1), FIELD1,FIELD2,FIELD3 FROM TABLE

Ties Blom
Information analyst
 
you just need a counter or what is the purpose?

Be aware, that when re-running the query the rownumber() will not be the same when data physically changed.

And the function is not available on z/OS.

Juliane
 

This is for DB2 V8 running under z/OS. I am just looking to add a counter to the query.

thanks in advance
 
I am getting close what I have coded is:

Code:
SELECT PEIM_ID, ROW#                      
  FROM T819PEIM TB1,                      
 TABLE ( SELECT COUNT(*) AS ROW#          
           FROM T819PEIM TB2              
          WHERE TB2.PEIM_ID < TB1.PEIM_ID 
        ) AS TEMP_TAB                     
 WHERE PEIM_YEAR = '2005'                 
   AND PEIM_ID  LIKE '46581%%%%'          

---------+---------+---
PEIM_ID           ROW# 
---------+---------+---
465810276       217810 
465810301       217811 
465810405       217812 
465810425       217813 
465810735       217814 
465810875       217815 
465810886       217816 
465810910       217817 
465810911       217818 
465810921       217819 
465811193       217820

but what I am looking for is

Code:
---------+---------+---
PEIM_ID           ROW# 
---------+---------+---
465810276          1
465810301          2 
465810405          3
465810425          4
465810735          5
465810875          6
465810886          7
465810910          8
465810911          9
465810921          10
465811193          11

Any thoughts

Thanks in advance.
 
kkitt,
Try adding the outer where clauses into the inner table:
SELECT PEIM_ID, ROW#
FROM T819PEIM TB1,
TABLE ( SELECT COUNT(*) AS ROW#
FROM T819PEIM TB2
WHERE TB2.PEIM_ID < TB1.PEIM_ID
AND TB2.PEIM_YEAR = '2005'
AND TB2.PEIM_ID LIKE '46581%%%%'
) AS TEMP_TAB
WHERE PEIM_YEAR = '2005'
AND PEIM_ID LIKE '46581%%%%'

I think that might make the difference you are looking for.

Juliane, many thanks for the star
[thumbsup]
 
thanks, this works great....

I just needed to change the 'count(*)' to 'count(*) + 1' to get it starting from 1 rather then zero.

I have included a sample of one of the queries that I needed to add it to. Get messy when joining tables and such, LOL

Code:
SELECT HIST_PEIMS, ROW#                               
  FROM ( SELECT HSTU_PEIMS    AS HIST_PEIMS,          
                'H'           AS HIST_TYPE            
           FROM T819HSTU                              
          WHERE HSTU_PEIMS LIKE '46581%%%%'           
         UNION ALL                                    
         SELECT HSST_PEIMS      AS HIST_PEIMS,        
                'S'             AS HIST_TYPE          
           FROM T819HSST                              
          WHERE HSST_PEIMS LIKE '46581%%%%'           
       ) AS A,                                        
   TABLE(SELECT COUNT(*) + 1 AS ROW#                  
           FROM ( SELECT HSTU_PEIMS AS HIST_PEIMS
                    FROM T819HSTU                     
                   WHERE HSTU_PEIMS LIKE '46581%%%%'  
                  UNION ALL                           
                  SELECT HSST_PEIMS AS HIST_PEIMS
                    FROM T819HSST                       
                   WHERE HSST_PEIMS LIKE '46581%%%%'    
                )AS B                                   
           WHERE B.HIST_PEIMS < A.HIST_PEIMS) AS TEM_TAB
 WHERE HIST_TYPE IN ('H',' ')                           
 ORDER BY 2 DESC

and this is one of the simpler ones, OH WOE is ME.......

Another Star for the help
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top