dbTalk Databases Forums  

[NOVICE] Postgresql-query for absolute beginner

mailing.database.pgsql-novice mailing.database.pgsql-novice


Discuss [NOVICE] Postgresql-query for absolute beginner in the mailing.database.pgsql-novice forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Meike Börder
 
Posts: n/a

Default [NOVICE] Postgresql-query for absolute beginner - 08-20-2012 , 05:55 AM






Hey folks,



as you may have guessed from the title, I’m an absolute beginner using
PostgreSQL and I need some help. I have a table with the columns ‘country’
(text), ‘sample site’ (text) and ‘date’ (timestamp with time zone):

country sample site date

AA a1 01.01.2000

AA a1 12.02.1999

AA a2 01.01.2000

BB b1 23.04.1984

BB b2 05.05.2000

BB b3 01.01.2000

CC c1 15.03.1998

CC c1 24.10.2002

CC c2 15.03.1998



What I want to know now is how often the different sampling sites were
tested. How do I have to write my query? I tried using something like

Select country, sample site, count (date) from samples

But the result was rubbish. I could Imagine that I have to create a loop for
this query (the table has overall more than 1 mio rows) to get a result
table with a list of sampling sites and the number of surveys for each of
them.



Help would be greatly appreciated!



Yours, Meike

Reply With Quote
  #2  
Old   
Thom Brown
 
Posts: n/a

Default Re: [NOVICE] Postgresql-query for absolute beginner - 08-20-2012 , 06:35 AM






On 20 August 2012 11:55, Meike Börder <boerder (AT) uni-landau (DOT) de> wrote:
Quote:
Hey folks,



as you may have guessed from the title, I’m an absolute beginner using
PostgreSQL and I need some help. I have a table with the columns ‘country’
(text), ‘sample site’ (text) and ‘date’ (timestamp with time zone):

country sample site date

AA a1 01.01.2000

AA a1 12.02.1999

AA a2 01.01.2000

BB b1 23.04.1984

BB b2 05.05.2000

BB b3 01.01.2000

CC c1 15.03.1998

CC c1 24.10.2002

CC c2 15.03.1998



What I want to know now is how often the different sampling sites were
tested. How do I have to write my query? I tried using something like

Select country, sample site, count (date) from samples

But the result was rubbish. I could Imagine that I have to create a loop for
this query (the table has overall more than 1 mio rows) to get a result
table with a list of sampling sites and the number of surveys for each of
them.
Did you use a GROUP BY?

SELECT country, sample_site, count(date)
FROM samples
GROUP BY country, sample_site;

A GROUP BY is required when using aggregate functions, of which "count" is one.

--
Thom


--
Sent via pgsql-novice mailing list (pgsql-novice (AT) postgresql (DOT) org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-novice

Reply With Quote
  #3  
Old   
Oliveiros d'Azevedo Cristina
 
Posts: n/a

Default Re: [NOVICE] Postgresql-query for absolute beginner - 08-20-2012 , 06:38 AM



Howdy, Meike.

Not sure if this is what you want, but
if you write

SELECT "sample site" , COUNT("date")
FROM samples
GROUP BY "sample site"

It will return the number of surveys for each one of the site.

Ain't I right?

Best,
Oliver
----- Original Message -----
From: Meike Börder
To: pgsql-novice (AT) postgresql (DOT) org
Sent: Monday, August 20, 2012 11:55 AM
Subject: [NOVICE] Postgresql-query for absolute beginner


Hey folks,



as you may have guessed from the title, I'm an absolute beginner using PostgreSQL and I need some help. I have a table with the columns 'country' (text), 'sample site' (text) and 'date' (timestamp with time zone):

country sample site date

AA a1 01.01.2000

AA a1 12.02.1999

AA a2 01.01.2000

BB b1 23.04.1984

BB b2 05.05.2000

BB b3 01.01.2000

CC c1 15.03.1998

CC c1 24.10.2002

CC c2 15.03.1998



What I want to know now is how often the different sampling sites were tested. How do I have to write my query? I tried using something like

Select country, sample site, count (date) from samples

But the result was rubbish. I could Imagine that I have to create a loop for this query (the table has overall more than 1 mio rows) to get a result table with a list of sampling sites and the number of surveys for each of them.



Help would be greatly appreciated!



Yours, Meike

Reply With Quote
  #4  
Old   
Meike Börder
 
Posts: n/a

Default Re: [NOVICE] Postgresql-query for absolute beginner - 08-20-2012 , 06:41 AM



thanks a lot, that worked!

cheers, Meike


-----Ursprüngliche Nachricht-----
Von: thombrown (AT) gmail (DOT) com [mailto:thombrown (AT) gmail (DOT) com] Im Auftrag von Thom
Brown
Gesendet: Montag, 20. August 2012 13:35
An: Meike Börder
Cc: pgsql-novice (AT) postgresql (DOT) org
Betreff: Re: [NOVICE] Postgresql-query for absolute beginner

On 20 August 2012 11:55, Meike Börder <boerder (AT) uni-landau (DOT) de> wrote:
Quote:
Hey folks,



as you may have guessed from the title, I’m an absolute beginner using
PostgreSQL and I need some help. I have a table with the columns ‘country’
(text), ‘sample site’ (text) and ‘date’ (timestamp with time zone):

country sample site date

AA a1 01.01.2000

AA a1 12.02.1999

AA a2 01.01.2000

BB b1 23.04.1984

BB b2 05.05.2000

BB b3 01.01.2000

CC c1 15.03.1998

CC c1 24.10.2002

CC c2 15.03.1998



What I want to know now is how often the different sampling sites were
tested. How do I have to write my query? I tried using something like

Select country, sample site, count (date) from samples

But the result was rubbish. I could Imagine that I have to create a
loop for this query (the table has overall more than 1 mio rows) to
get a result table with a list of sampling sites and the number of
surveys for each of them.
Did you use a GROUP BY?

SELECT country, sample_site, count(date) FROM samples GROUP BY country,
sample_site;

A GROUP BY is required when using aggregate functions, of which "count" is
one.

--
Thom



--
Sent via pgsql-novice mailing list (pgsql-novice (AT) postgresql (DOT) org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-novice

Reply With Quote
  #5  
Old   
Gavin Flower
 
Posts: n/a

Default Re: [NOVICE] Postgresql-query for absolute beginner - 08-20-2012 , 06:47 AM



On 20/08/12 23:38, Oliveiros d'Azevedo Cristina wrote:
Quote:
Howdy, Meike.
Not sure if this is what you want, but
if you write
SELECT "sample site" , COUNT("date")
FROM samples
GROUP BY "sample site"
It will return the number of surveys for each one of the site.
Ain't I right?
Best,
Oliver

----- Original Message -----
*From:* Meike Börder <mailto:boerder (AT) uni-landau (DOT) de
*To:* pgsql-novice (AT) postgresql (DOT) org
mailtogsql-novice (AT) postgresql (DOT) org
*Sent:* Monday, August 20, 2012 11:55 AM
*Subject:* [NOVICE] Postgresql-query for absolute beginner

Hey folks,

as you may have guessed from the title, I'm an absolute beginner
using PostgreSQL and I need some help. I have a table with the
columns 'country' (text), 'sample site' (text) and 'date'
(timestamp with time zone):

country sample site date

AA a1 01.01.2000

AA a1 12.02.1999

AA a2 01.01.2000

BB b1 23.04.1984

BB b2 05.05.2000

BB b3 01.01.2000

CC c1 15.03.1998

CC c1 24.10.2002

CC c2 15.03.1998

What I want to know now is how often the different sampling sites
were tested. How do I have to write my query? I tried using
something like

Select country, sample site, count (date) from samples

But the result was rubbish. I could Imagine that I have to create
a loop for this query (the table has overall more than 1 mio rows)
to get a result table with a list of sampling sites and the number
of surveys for each of them.

Help would be greatly appreciated!

Yours, Meike

You're assuming that a 'sample site' value is either unique or that the
count for a site should include all associated countries.

You may, or may not, be right! :-)

Anyhow, please don't top post.


Cheers,
Gavin

Reply With Quote
  #6  
Old   
Oliveiros d'Azevedo Cristina
 
Posts: n/a

Default Re: [NOVICE] Postgresql-query for absolute beginner - 08-20-2012 , 07:09 AM



----- Original Message -----
From: Meike Börder
To: pgsql-novice (AT) postgresql (DOT) org
Sent: Monday, August 20, 2012 11:55 AM
Subject: [NOVICE] Postgresql-query for absolute beginner


Hey folks,



as you may have guessed from the title, I'm an absolute beginner using PostgreSQL and I need some help. I have a table with the columns 'country' (text), 'sample site' (text) and 'date' (timestamp with time zone):

country sample site date

AA a1 01.01.2000

AA a1 12.02.1999

AA a2 01.01.2000

BB b1 23.04.1984

BB b2 05.05.2000

BB b3 01.01.2000

CC c1 15.03.1998

CC c1 24.10.2002

CC c2 15.03.1998



What I want to know now is how often the different sampling sites were tested. How do I have to write my query? I tried using something like

Select country, sample site, count (date) from samples

But the result was rubbish. I could Imagine that I have to create a loop for this query (the table has overall more than 1 mio rows) to get a result table with a list of sampling sites and the number of surveys for each of them.



Help would be greatly appreciated!



Yours, Meike





You're assuming that a 'sample site' value is either unique or that the count for a site should include all associated countries.

You may, or may not, be right! :-)

Anyhow, please don't top post.

* Yeah, that wouldn't work if two sample sites on different countries happen to have the same name, but from the sample data nothing made me think so, as all sample site ID begin with the same letter as the associated country
Any way, thanks for pointin that out.

Greetings from up over,
Oliver



Cheers,
Gavin

Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.