| View previous topic :: View next topic | 
	
	
		| Author | Message | 
	
		| vani Beginner
 
 
 Joined: 28 May 2003
 Posts: 51
 Topics: 30
 
 
 | 
			
				|  Posted: Mon Nov 10, 2003 7:43 am    Post subject: SQL QUERY |   |  
				| 
 |  
				| I have a table INASBU with definition as : 
  	  | Code: |  	  | Column Name        Col No Col Type Length
 *                  *      *        *
 ------------------ ------ -------- ------
 NLBKZ                   1         CHAR          1
 GDAT                    2         CHAR         10
 NLBID                   3         CHAR          4
 NLBBEZ                  4        CHAR         60
 
 | 
 There is no primary index on this table.Now I want to select all the distinct values. ie most recent GDAT with unique NLBKZ, NLBID and NLBBEZ
 Now  i have records as :
 GDAT        NLBID  NLBBEZ
 ----------  -----  ---------------------------------------------------
 20030531    0002
 |  | 
	
		| Back to top |  | 
	
		|  | 
	
		| kolusu Site Admin
 
  
 
 Joined: 26 Nov 2002
 Posts: 12394
 Topics: 75
 Location: San Jose
 
 | 
			
				|  Posted: Mon Nov 10, 2003 8:42 am    Post subject: |   |  
				| 
 |  
				| Vani, 
 Try this
 
 
  	  | Code: |  	  | SELECT MAX(GDAT) FROM TABLE
 WHERE NLBKZ  = (SELECT DISTINCT NLBKZ FROM TABLE)
 AND NLBID  = (SELECT DISTINCT NLBID FROM TABLE)
 AND NLBEZ  = (SELECT DISTINCT NLBEZ FROM TABLE)
 ;
 
 | 
 
 Hope this helps...
 
 cheers
 
 kolusu
 |  | 
	
		| Back to top |  | 
	
		|  | 
	
		| vani Beginner
 
 
 Joined: 28 May 2003
 Posts: 51
 Topics: 30
 
 
 | 
			
				|  Posted: Mon Nov 10, 2003 9:44 pm    Post subject: |   |  
				| 
 |  
				| Did not work Distinct can be used only once
 
 Any other solution?
 |  | 
	
		| Back to top |  | 
	
		|  | 
	
		| sri_naveen Beginner
 
  
 Joined: 29 Oct 2003
 Posts: 10
 Topics: 0
 Location: Indore, India
 
 | 
			
				|  Posted: Mon Nov 10, 2003 11:29 pm    Post subject: |   |  
				| 
 |  
				| Hi vani...try this.. 
 SELECT MAX(GDAT) FROM <your table T1>, <same table T2>
 WHERE T1.NLBKZ  <> T2.NLBKZ
 AND <same for other two>
 
 this is a bit cumbursome...since its a full scan checking if its not equal..LI have no idea why multiple distinct dont work..which db are you using ?
 _________________
 Regards,
 Naveen Srinivasan
 Computer Sciences Corporation
 
 --To err is human, to err again is more human--
 |  | 
	
		| Back to top |  | 
	
		|  | 
	
		| vani Beginner
 
 
 Joined: 28 May 2003
 Posts: 51
 Topics: 30
 
 
 | 
			
				|  Posted: Tue Nov 11, 2003 7:48 am    Post subject: |   |  
				| 
 |  
				| The below query worked. Thanks for all. 
 SELECT DISTINCT T.NLBID,T.NLBKZ,T.NLBBEZ,T.GDAT
 FROM SAS1T01.INASBU T
 WHERE T.GDAT = (SELECT MAX(GDAT)
 FROM SAS1T01.INASBU
 WHERE NLBID = T.NLBID);
 |  | 
	
		| Back to top |  | 
	
		|  | 
	
		|  | 
	
		|  |