# Processing postgres table

**URL:** <https://www.ruby-forum.com/t/processing-postgres-table/151537>\
**Category:** Ruby\
**Created:** [October 22, 2008, 4:46pm UTC](https://www.ruby-forum.com/t/processing-postgres-table/151537 "2008-10-22T16:46:27Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![dan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/eada6e/32.png) [@dan](https://www.ruby-forum.com/u/dan)\
**Post date:** [October 22, 2008, 4:46pm UTC](https://www.ruby-forum.com/t/processing-postgres-table/151537/1 "2008-10-22T16:46:27Z")

</div>

I’m using the ruby-postgres gem as ruby-pg isn’t installing & as there  
no longer seems to be any tutorials or how-tos I thought I’d ask here.

Basically I’m trying to iterate through each row of data and assign the  
values to some variables then print them out to XML (XML output’s sorted  
thanks to builder)  
This is one set of that data, one question with 5 answers. How can I go  
about finding out the number of rows that contain the same first column?

“A new question by mbr - option 3 is the correct  
option”;“afsdfsd”;“mbr”;"";FALSE;"-0.25";“1”;"’’"

“A new question by mbr - option 3 is the correct  
option”;“sdfsdf”;“mbr”;"";FALSE;"-0.25";“2”;"’’"

“A new question by mbr - option 3 is the correct  
option”;“sdfsdf”;“mbr”;"";TRUE;“1.0”;“3”;"’’"

“A new question by mbr - option 3 is the correct  
option”;“sdfsdf”;“mbr”;"";FALSE;"-0.25";“4”;"’’"

“A new question by mbr - option 3 is the correct  
option”;“sdfsdaf”;“mbr”;"";FALSE;"-0.25";“5”;"’’"

This is the current code I’m using for postgres.  
db = PGconn.connect(pghost, pgport,’’,’’,pgtbl,dbname,’’)  
results = db.exec(‘SELECT question\_stem, option\_text, author\_id,  
module\_code, correct, weighting, option\_id, feedback FROM RESULTS\_JOIN’)  
results.each do |row|  
puts "by position: #{row[0]} #{row[1]} #{row[2]} #{row[3]} #{row[4]}  
#{row[5]} #{row[6]} #{row[7]} "  
end

any pointers would be great.  
Kind Regards,  
Dan

---

<div class="post-metadata">

**Author:** ![dan](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/eada6e/32.png) [@dan](https://www.ruby-forum.com/u/dan)\
**Post date:** [October 23, 2008, 2:06pm UTC](https://www.ruby-forum.com/t/processing-postgres-table/151537/2 "2008-10-23T14:06:29Z")

</div>

2008/10/22 Dan W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid):

> I’m using the ruby-postgres gem as ruby-pg isn’t installing & as there  
> no longer seems to be any tutorials or how-tos I thought I’d ask here.
> 
> Basically I’m trying to iterate through each row of data and assign the  
> values to some variables then print them out to XML (XML output’s sorted  
> thanks to builder)  
> This is one set of that data, one question with 5 answers. How can I go  
> about finding out the number of rows that contain the same first column?

SELECT question\_stem, count(\*)  
FROM RESULTS\_JOIN  
group by question\_stem

Cheers

robert
