473,699 Members | 2,181 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

accessing last element of an array

38 New Member
Dear all,

i have a column with data type array with variable size ...... e.g
number = [1,3,4,,7,54,98, 255]

i want to access the last element of this array.......

i accessed the first element by following statement....
select * from student where number =[1]
this worked fine....

BUT i am interested to access the last element using SQL command..... any suggestion plz.....

thnx
artist
Dec 29 '06 #1
7 21819
michaelb
534 Recognized Expert Contributor
You may be able to do something like this:
Expand|Select|Wrap|Line Numbers
  1.  
  2. select * from student where array_upper(number)  =  some-value; 
  3.  
I did not test this code, search Postgres manual for Arrays if it does not execute as expected.
Dec 29 '06 #2
artistlikeu
38 New Member
You may be able to do something like this:
Expand|Select|Wrap|Line Numbers
  1.  
  2. select * from student where array_upper(number)  =  some-value; 
  3.  
I did not test this code, search Postgres manual for Arrays if it does not execute as expected.

This does not works.......??
may be there is a tricky query that can give the required result.??
any one can help in this regards????

thnx @rtist
Jan 2 '07 #3
michaelb
534 Recognized Expert Contributor
There's nothing tricky here aside of the fact that you'd be much better off if you don't always expect to get the complete answer, but instead be willing to take a hint, a direction so to speak, and then go and do some homework, which often involves reading a manual and spending some time trying to figure out how it works.

Let's go over this one more time. First create some table that has a string and integer array for fields.
Expand|Select|Wrap|Line Numbers
  1. test_db=> create table foo ( f1 varchar(20), f2 integer[] );
  2.  CREATE TABLE
  3.  
  4. test_db=> insert into foo values ('this is a string', '{1,2,3,100,101}');
  5.  INSERT 75356 1
  6.  
Now try to use the array_upper function the way I first suggested:
Expand|Select|Wrap|Line Numbers
  1. test_db=> select * from foo where array_upper(f2) = 5;
  2.  ERROR:  function array_upper(integer[]) does not exist
  3.  HINT:  No function matches the given name and argument types. 
  4.         You may need to add explicit type casts.
  5.  
Oh, well, this is definitely not working, let's go online and read the manual at http://www.postgresql. org/docs/7.4/static/arrays.html
Just five minutes later we got some fresh ideas, let's try it again, now with correct arg list:
Expand|Select|Wrap|Line Numbers
  1. test_db=> select array_upper(f2, 1) from foo;
  2.   array_upper
  3.  -------------
  4.             5
  5.  
  6. -- that's correct, there are indeed 5 elements in this array: {1,2,3,100,101}
  7. -- let's try couple more things
  8.  
  9. test_db=> select * from foo where array_upper(f2, 1) = 5;
  10.          f1        |       f2
  11.  ------------------+-----------------
  12.   this is a string | {1,2,3,100,101}
  13.  
  14.  (1 row)
  15.  
  16. -- this worked just fine, let's see if we can query by the value of 
  17. -- the upper_bound array's element 
  18.  
  19. test_db=> select * from foo where f2[5] = 101;
  20.          f1        |       f2
  21.  ------------------+-----------------
  22.   this is a string | {1,2,3,100,101}
  23. (1 row)
  24.  
  25. -- we half way there, finally let's pretend we don't know the size of array
  26.  
  27. test_db=> select * from foo where f2[array_upper(f2,1)] = 101;
  28.         f1        |       f2
  29. ------------------+-----------------
  30.   this is a string | {1,2,3,100,101}
  31. (1 row)
  32.  
Jan 2 '07 #4
artistlikeu
38 New Member
There's nothing tricky here aside of the fact that you'd be much better off if you don't always expect to get the complete answer, but instead be willing to take a hint, a direction so to speak, and then go and do some homework, which often involves reading a manual and spending some time trying to figure out how it works.

Let's go over this one more time. First create some table that has a string and integer array for fields.
Expand|Select|Wrap|Line Numbers
  1. test_db=> create table foo ( f1 varchar(20), f2 integer[] );
  2.  CREATE TABLE
  3.  
  4. test_db=> insert into foo values ('this is a string', '{1,2,3,100,101}');
  5.  INSERT 75356 1
  6.  

Now try to use the array_upper function the way I first suggested:
Expand|Select|Wrap|Line Numbers
  1. test_db=> select * from foo where array_upper(f2) = 5;
  2.  ERROR:  function array_upper(integer[]) does not exist
  3.  HINT:  No function matches the given name and argument types. 
  4.         You may need to add explicit type casts.
  5.  
Oh, well, this is definitely not working, let's go online and read the manual at http://www.postgresql. org/docs/7.4/static/arrays.html
Just five minutes later we got some fresh ideas, let's try it again, now with correct arg list:
Expand|Select|Wrap|Line Numbers
  1. test_db=> select array_upper(f2, 1) from foo;
  2.   array_upper
  3.  -------------
  4.             5
  5.  
  6. -- that's correct, there are indeed 5 elements in this array: {1,2,3,100,101}
  7. -- let's try couple more things
  8.  
  9. test_db=> select * from foo where array_upper(f2, 1) = 5;
  10.          f1        |       f2
  11.  ------------------+-----------------
  12.   this is a string | {1,2,3,100,101}
  13.  
  14.  (1 row)
  15.  
  16. -- this worked just fine, let's see if we can query by the value of 
  17. -- the upper_bound array's element 
  18.  
  19. test_db=> select * from foo where f2[5] = 101;
  20.          f1        |       f2
  21.  ------------------+-----------------
  22.   this is a string | {1,2,3,100,101}
  23. (1 row)
  24.  
  25. -- we half way there, finally let's pretend we don't know the size of array
  26.  
  27. test_db=> select * from foo where f2[array_upper(f2,1)] = 101;
  28.         f1        |       f2
  29. ------------------+-----------------
  30.   this is a string | {1,2,3,100,101}
  31. (1 row)
  32.  
Dear Michael,
Thank you for sparing your time. But this already have done. This is not my problem. I tell you my problem in your example.
I have to find the first element of array i.e 1. I will use following query
"select f2[1] from foo;"
this will result i row and one element
f2
-----------------------
1
1 row
Now If i have 2000 rows i can find the first element of any array without knowing the exact value of elemnt of array.
Now i am interested to find last element of array. You suppose that i do not have 1 or 2 rows. I have 69000 records for which i have to find this. Your suggestion works for few rows by mentioning element of array. For 69000 records i have to find a function or a smart query. Now if u can helop i will be grateful to you.
OR
If u tell me a query or function of postgresql that inverts the position of elements of array, it will also help me. i e if i have 30 elements in an array, function should invert elements such as 30 as 1, 29 as 2 28 as 3 and so on....

waiting kind response from any one.......
@rtist
Jan 3 '07 #5
michaelb
534 Recognized Expert Contributor
>> Now If i have 2000 rows i can find the first element of any array
>> without knowing the exact value of element of array.
>> Now i am interested to find last element of array.
Actually, the number of rows is not important here.
The code I suggested should work regardless of how many rows you have in your table.
This is the last line from my previous post, it is only slighly modified to fit your case. You may add other fields to the select list if you need them.

Select the last element of field f2 (which is a one-dimentional array of integers) from all records in table foo.

Expand|Select|Wrap|Line Numbers
  1.  
  2. select f2[array_upper(f2,1)] from foo;
  3.  
Let me know if we still have some misunderstandin g here and your requirement are different.

michael.
Jan 3 '07 #6
artistlikeu
38 New Member
Actually, the number of rows is not important here.
The code I suggested should work regardless of how many rows you have in your table.
This is the last line from my previous post, it is only slighly modified to fit your case. You may add other fields to the select list if you need them.

Select the last element of field f2 (which is a one-dimentional array of integers) from all records in table foo.

Expand|Select|Wrap|Line Numbers
  1.  
  2. select f2[array_upper(f2,1)] from foo;
  3.  
Let me know if we still have some misunderstandin g here and your requirement are different.

michael.
Dear Michael,
Thank you very much... it is solved.... and i got central point.
Cheers,,,,,,
2rtist
Jan 3 '07 #7
michaelb
534 Recognized Expert Contributor
You're welcome!
Jan 4 '07 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

6
2743
by: Chris Styles | last post by:
Dear All, I've been using some code to verify form data quite happily, but i've recently changed the way my form is structured, and I can't get it to work now. Originally : The form is called "form1", and I have selects called "PORTA", "PORTB" ... etc...
5
9285
by: junky_fellow | last post by:
Consider a 3-dimensional array: char a = { "abcd", "efgh", "ijkl" , "mnop", "qrst", "uvwx" } ; when i print the addresses for a,a and a they are same. but when i access element a+1, a+1 and a+1 they are different. why is it so?
13
14725
by: Joseph Garvin | last post by:
When I first came to Python I did a lot of C style loops like this: for i in range(len(myarray)): print myarray Obviously the more pythonic way is: for i in my array: print i
10
1632
by: Christian Christmann | last post by:
Hi, I'm wondering if my small example is not "dangerous": #define SIZE 10 char global; char* globalPtr = global; int main()
16
8020
by: quantumred | last post by:
Can I use pointer arithmetic on the members of a structure in the following way? Should I be worried about structure padding? This works in my debugger but I wonder if I'm bending some rule here. struct char_struct { unsigned char a; unsigned char b; unsigned char c; unsigned char d; };
7
3809
by: Chuck Anderson | last post by:
I'm pretty much a JavaScript novice. I'm good at learning by example and changing those examples to suit my needs. That said .... ..... I have some select fields in a form I created for a database search that I am unable to figure out how to access. (The search is implemented in Php/MySQL.) The user enters search values for: name, address1, city, .... etc., ..... and for each of these they also select whether the search should...
5
3687
by: junky_fellow | last post by:
Hi, I discussed about this earlier as well but I never got any satisfactory answer. So, I am initiating this again. Page 84, WG14/N869 "If both the pointer operand and the result point to elements of the same array object, or one past the last element of the array object, the evaluation shall not produce an overflow; otherwise, the
5
2876
by: Paul Brettschneider | last post by:
Hello, I have a global static array of structs and want to access a given element using an identifier. I don't want to use the element subscript, because it will change if I insert elements before the element I want to access. In assembler I would simply add a label in front of the element, but this doesn't work in C++. The following programm snippet might explain what I want to do:
2
2285
by: ...vagrahb | last post by:
I am having accessing individual rows from a multidimensional array pass to a function as reference CODE: function Declaration int Part_Buffer(char (*buffer),int Low, int High)
1
8945
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8902
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7787
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6550
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5889
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4392
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4641
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3075
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2016
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.