473,394 Members | 1,932 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

inserting values into array column

i have query result (25) rows. can i insert them into an column (array) in a table.

thnx
Jan 11 '07 #1
7 4788
michaelb
534 Expert 512MB
Yes, you can...
but at some point you really have to start reading the manual!

http://www.postgresql.org/docs/8.0/static/arrays.html
http://www.postgresql.org/docs/8.0/static/functions-array.html
Jan 11 '07 #2
Yes, you can...
but at some point you really have to start reading the manual!

http://www.postgresql.org/docs/8.0/static/arrays.html
http://www.postgresql.org/docs/8.0/static/functions-array.html
Dear Michael

I m having another problem regarding (1-many relation). If u guide me.
i wrote a query which results to count the multiple values of an entry using count i.e.
select course_id, count(student_id)
from school
GROUP BY course_id
having count(student_id) > 5

this query gives me some results. i am interested to find which student id is against which course....1-many relationship. my result should look like

course_id + student_id
4500 + 1501
4500 + 1508
4500 + 1541
4500 + 1551
4500 + 1532
4500 + 1510
4700 + 1589
4700 + 1518
4700 + 1546
4700 + 1538
4700 + 1522
4700 + 1533
.
.
.
.
and then i want to insert student_id column into a column(array).


thnx
regards
@rtist
Jan 12 '07 #3
michaelb
534 Expert 512MB
Can you post the table schema, please?
Jan 12 '07 #4
Can you post the table schema, please?
Dear Michael, i do not know how to put schema or tablhe. But my problem is simple. i once again explain it here. If u kindly have a look, i will be grateful 2 u.

i have a table school with two fields
course_id (int4).....
student_id (int4)

1) now many students share same course. i want to write a query that gives me course_id which has more than 5 students in a course.

my result should be like this....

course_id + student_id
4500 + 1501
4500 + 1508
4500 + 1541
4500 + 1551
4500 + 1532
4500 + 1510
4700 + 1589
4700 + 1518
4700 + 1546
4700 + 1538
4700 + 1522
4700 + 1533
4700 + 1564
.
.
.
2) i have an other table which has two field

course_id (int4)
student_id (int4[])..... this field is one dimensional array

and now i want to insert student_id column into this column(array) against course_id. My expected results should be like this

course_id + student_id
4500 + {1501, 1508, 1541, 1551, 1532, 1510}
4700 + {1589, 1518, 1546, 1538, 1522, 1533, 1564}
.
.
.
this problem is urgent... if someone gives me hint plz....

thnx n regards
@rtist
Jan 13 '07 #5
michaelb
534 Expert 512MB
The first query seems quite simple:

Expand|Select|Wrap|Line Numbers
  1.  
  2. SELECT course_id, student_id 
  3. FROM    school WHERE course_id in 
  4. (
  5.     select course_id from school group by (course_id) 
  6.     having count(course_id) > 5 
  7. );
  8.  
From your previous posting I assume that the second table already has
columns with the course_id, but it needs to have the second field populated - the array of student_id for each course.

This query is little more complex... I think something like this
should work for you:

Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3. UPDATE courses set student_id = 
  4. (
  5.     SELECT ARRAY (select student_id from school 
  6.         where courses.course_id = school.course_id)
  7. )
  8.  
  9.  
Both topics: subqueries and arrays are well documented in the Postgres manual, you may want to look at the code examples there.
Jan 15 '07 #6
The first query seems quite simple:

Expand|Select|Wrap|Line Numbers
  1.  
  2. SELECT course_id, student_id 
  3. FROM    school WHERE course_id in 
  4. (
  5.     select course_id from school group by (course_id) 
  6.     having count(course_id) > 5 
  7. );
  8.  
From your previous posting I assume that the second table already has
columns with the course_id, but it needs to have the second field populated - the array of student_id for each course.

This query is little more complex... I think something like this
should work for you:

Expand|Select|Wrap|Line Numbers
  1.  
  2.  
  3. UPDATE courses set student_id = 
  4. (
  5.     SELECT ARRAY (select student_id from school 
  6.         where courses.course_id = school.course_id)
  7. )
  8.  
  9.  
Both topics: subqueries and arrays are well documented in the Postgres manual, you may want to look at the code examples there.

Dear Michael,

this results all value in one array. i want values respectively as i indicate din my expected result. If u can kindly give hint....
thnx
Jan 16 '07 #7
michaelb
534 Expert 512MB
I'm not sure I understand.


expected results should be like this

course_id + student_id

4500 + {1501, 1508, 1541, 1551, 1532, 1510}
4700 + {1589, 1518, 1546, 1538, 1522, 1533, 1564}
Isn't this what you're getting with this query?
Jan 16 '07 #8

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

Similar topics

3
by: Tyler Hudson | last post by:
/*Code below raises following errors: Server: Msg 245, Level 16, State 1, Line 6 Syntax error converting the varchar value 'a' to a column of data type int. */ create table #x (i integer, c...
3
by: Joachim Klassen | last post by:
Hi all, first apologies if this question looks the same as another one I recently posted - its a different thing but for the same szenario:-). We are having performance problems when...
2
by: a | last post by:
NEW Post Here's my best guess at how to insert this dataset.... the code runs, but no new records are added to the sql table. I've read and split a delimited text file into a dataset. It...
15
by: Jaraba | last post by:
I am working in a project that I need to parse an arrayt an select records based upon the values parsed. I used the functions developed by Knut Stolze in his article 'Parsing Strings'. I am...
6
by: fniles | last post by:
I am using VB.NET 2003 and SQL Server 2000. I have a table with a datetime column type. When inserting into the table for that column, I set it to Date.Now.ToString("T") , which is something like...
4
by: Manikandan | last post by:
Hi, I'm inserting a datetime values into sql server 2000 from c# SQL server table details Table name:date_test columnname datatype No int date_t DateTime ...
10
by: Rahul Babbar | last post by:
Hi, I am getting the following error, while executing the simple insert script on a few tables. INSERT INTO <table_name>(<col1>) VALUES (1); DB2 SQL error: SQLCODE: -440, SQLSTATE: 42884,...
5
by: dos360 | last post by:
Hello, I have two tables, one is a list of activities, the other a list of participants. I want to insert one record in the activities table and then using its identity column as foreign key, I...
2
by: AlexanderDeLarge | last post by:
Hi! I got a problem that's driving me crazy and I'm desperately in need of help. I'll explain my scenario: I'm doing a database driven site for a band, I got these tables for their discography...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
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...

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.