dbTalk Databases Forums  

Help for grouping query

comp.databases.postgresql comp.databases.postgresql


Discuss Help for grouping query in the comp.databases.postgresql forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
jmb@nospam.com
 
Posts: n/a

Default Help for grouping query - 11-29-2010 , 01:20 PM






Hello,

I have a table with columns id and start_time:

id | start_time
-----+---------------------
96 | 2010-11-24 09:30:00
97 | 2010-11-24 10:30:00
95 | 2010-11-25 08:00:00

I would like to write a query that will group for each *date* in the
start_time column the row count... something like this:

start_time | count
--------------------+-------
2010-11-24 09:30:00 | 2
2010-11-25 08:00:00 | 1

Of course, there is no date column in the table, otherwise it would be
too easy

Any help will be appreciated!

Thanks

Jim

Reply With Quote
  #2  
Old   
danfan46
 
Posts: n/a

Default Re: Help for grouping query - 11-29-2010 , 02:08 PM






On 2010-11-29 20:20, jmb (AT) nospam (DOT) com wrote:
Quote:
Hello,

I have a table with columns id and start_time:

id | start_time
-----+---------------------
96 | 2010-11-24 09:30:00
97 | 2010-11-24 10:30:00
95 | 2010-11-25 08:00:00

I would like to write a query that will group for each *date* in the
start_time column the row count... something like this:

start_time | count
--------------------+-------
2010-11-24 09:30:00 | 2
2010-11-25 08:00:00 | 1

Of course, there is no date column in the table, otherwise it would be
too easy

Any help will be appreciated!

Thanks

Jim
with T1 (id, TS)
as (values (96,'2010-11-24 09:30:00')
,(97,'2010-11-24 10:30:00')
,(95,'2010-11-25 08:00:00')
)
Select count(*)
,cast(TS as date)
from T1
group by cast(TS as date)
;
-----------------------------
count ts
1 2010-11-25
2 2010-11-24

/dg

Reply With Quote
  #3  
Old   
Thomas Kellerer
 
Posts: n/a

Default Re: Help for grouping query - 11-29-2010 , 02:08 PM



jmb (AT) nospam (DOT) com wrote on 29.11.2010 20:20:
Quote:
Hello,

I have a table with columns id and start_time:

id | start_time
-----+---------------------
96 | 2010-11-24 09:30:00
97 | 2010-11-24 10:30:00
95 | 2010-11-25 08:00:00

I would like to write a query that will group for each *date* in the
start_time column the row count... something like this:

start_time | count
--------------------+-------
2010-11-24 09:30:00 | 2
2010-11-25 08:00:00 | 1

Of course, there is no date column in the table, otherwise it would be
too easy

Any help will be appreciated!

If you can live with the fact that the hour is removed, this is pretty simple:

SELECT cast(start_time as date), count(*)
FROM your_table
GROUP BY cast(start_time as date)

Thomas

Reply With Quote
  #4  
Old   
Lennart Jonsson
 
Posts: n/a

Default Re: Help for grouping query - 11-29-2010 , 02:59 PM



On 2010-11-29 20:20, jmb (AT) nospam (DOT) com wrote:
Quote:
Hello,

I have a table with columns id and start_time:

id | start_time
-----+---------------------
96 | 2010-11-24 09:30:00
97 | 2010-11-24 10:30:00
95 | 2010-11-25 08:00:00

I would like to write a query that will group for each *date* in the
start_time column the row count... something like this:

start_time | count
--------------------+-------
2010-11-24 09:30:00 | 2
2010-11-25 08:00:00 | 1

Of course, there is no date column in the table, otherwise it would be
too easy

Any help will be appreciated!
I don't have a psql installation at hand, so I can't verify, but you
could try something like:

select distinct count(*) over (partition by cast(ts as date))
,min(TS) over (partition by cast(ts as date))
from T

For a large table, I don't think it will be very efficient though.

/Lennart

Reply With Quote
  #5  
Old   
Lennart Jonsson
 
Posts: n/a

Default Re: Help for grouping query - 11-29-2010 , 03:06 PM



On 2010-11-29 21:59, Lennart Jonsson wrote:
[...]
Quote:
I don't have a psql installation at hand, so I can't verify, but you
could try something like:

select distinct count(*) over (partition by cast(ts as date))
,min(TS) over (partition by cast(ts as date))
from T

Probably clearer like:

select start_time, count(*) from (
select min(TS) over (partition by cast(ts as date)) as start_time
from T
) as X
group by start_time;

/L

Reply With Quote
  #6  
Old   
Lennart Jonsson
 
Posts: n/a

Default Re: Help for grouping query - 11-29-2010 , 03:17 PM



On 2010-11-29 22:06, Lennart Jonsson wrote:
[...]
Quote:
select start_time, count(*) from (
select min(TS) over (partition by cast(ts as date)) as start_time
from T
) as X
group by start_time;

without OLAP function:

select start_time, count(*) from (
select cast(ts as date) as dt, min(TS) as start_time
from T group by cast(ts as date)
) as X
join T
on dt = cast(ts as date)
group by start_time

/L

Reply With Quote
  #7  
Old   
Robert Klemme
 
Posts: n/a

Default Re: Help for grouping query - 11-29-2010 , 03:43 PM



On 11/29/2010 09:08 PM, Thomas Kellerer wrote:
Quote:
jmb (AT) nospam (DOT) com wrote on 29.11.2010 20:20:
Hello,

I have a table with columns id and start_time:

id | start_time
-----+---------------------
96 | 2010-11-24 09:30:00
97 | 2010-11-24 10:30:00
95 | 2010-11-25 08:00:00

I would like to write a query that will group for each *date* in the
start_time column the row count... something like this:

start_time | count
--------------------+-------
2010-11-24 09:30:00 | 2
2010-11-25 08:00:00 | 1

Of course, there is no date column in the table, otherwise it would be
too easy

Any help will be appreciated!


If you can live with the fact that the hour is removed, this is pretty
simple:

SELECT cast(start_time as date), count(*)
FROM your_table
GROUP BY cast(start_time as date)
The loss of hours is easily fixed. One could do

SELECT min(start_time) as start_time, count(*)
FROM your_table
GROUP BY cast(start_time as date)

Other aggregation functions can be used as well, but other than min()
and max() do not seem very useful to me.

Kind regards

robert

Reply With Quote
  #8  
Old   
Robert Klemme
 
Posts: n/a

Default Re: Help for grouping query - 11-29-2010 , 03:44 PM



On 11/29/2010 10:17 PM, Lennart Jonsson wrote:
Quote:
On 2010-11-29 22:06, Lennart Jonsson wrote:
[...]

select start_time, count(*) from (
select min(TS) over (partition by cast(ts as date)) as start_time
from T
) as X
group by start_time;


without OLAP function:

select start_time, count(*) from (
select cast(ts as date) as dt, min(TS) as start_time
from T group by cast(ts as date)
) as X
join T
on dt = cast(ts as date)
group by start_time
Why so complicated? Why not just

SELECT min(start_time) as start_time, count(*)
FROM your_table
GROUP BY cast(start_time as date)

?

Kind regards

robert

Reply With Quote
  #9  
Old   
Lennart Jonsson
 
Posts: n/a

Default Re: Help for grouping query - 11-29-2010 , 04:09 PM



On 2010-11-29 22:44, Robert Klemme wrote:
[...]
Quote:
SELECT min(start_time) as start_time, count(*)
FROM your_table
GROUP BY cast(start_time as date)

clearly better. I claim my right to ignore the power of the group by
operator ;-)

/Lennart

Reply With Quote
  #10  
Old   
jmb@nospam.com
 
Posts: n/a

Default Re: Help for grouping query - 11-29-2010 , 04:29 PM



Thomas Kellerer <OTPXDAJCSJVU (AT) spammotel (DOT) com> wrote:

Quote:
SELECT cast(start_time as date), count(*)
FROM your_table
GROUP BY cast(start_time as date)
that's it!
Thank you very much Thomas, and thanks to the other guys who spent some
time to help and answered to my question.

Jim

--

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 - 2012, Jelsoft Enterprises Ltd.