postgresql - postgres query error for GROUP BY clause -


i have table login_logout time

id  user_id     start               end                      total(sec) -------------------------------------------------------------------------------------- 1   1   "2014-04-14 12:17:54.772"   "2014-04-14 12:17:55.772"   1 2   1   "2014-04-14 12:22:41.763"   "2014-04-14 12:31:14.295"   512 3   2   "2014-04-14 12:43:51.823"   "2014-04-14 12:48:40.706"   288 4   2   "2014-04-14 13:22:51.656"   "2014-04-14 13:22:52.656"   1 

i want data in format

    user : 1 ------------------------------------------------------------------         start_time      end_time    total (seconds)         "12:17"         "12:17"         1         "12:22"         "12:31"         512                                     total:  513      user : 2 ------------------------------------------------------------------         "12:43"         "12:48"         288         "13:22"         "13:22"         1                                     total   289 

but getting date in format

user : 1 ------------------------------------------------ start_time  end_time    total (seconds) "12:17"     "12:17"     1                 total : 1 user : 1 ------------------------------------------------ "12:22"     "12:31"     512                 total : 512  user : 2  ------------------------------------------------ "12:43"     "12:48"     288                 total : 288  user : 2  ------------------------------------------------ "13:22"      "13:22"    1                 total : 1 

the query :

select l.user_id,        substr(cast (l.start::time text), 1,5) starttime,       substr(cast (l::time text), 1,5) endtime,        sum(total)   login_logout l  group  l.user_id,            starttime,            endtime 

i understand happening because of starttime,endtime added in group clause.

when remove it, famous 'must appear in group clause or used in aggregate function' error.

can please in solving this?

try this

select  l.user_id,   substr(cast (min(l.stime)::time text), 1,5) starttime, substr(cast (max(l.etime)::time text), 1,5) endtime, sum(total) login_logout l group l.user_id 

it give following ouptut:

   uid  starttime endtime  total      1   12:17    12:31    513     2   12:43     13:22    289 

or

select cast(uid text) || '2' ord, null uid,''  starttime,'' endtime, sum(tse) tt group uid  union  select cast(uid text) || '1' ord, l.uid,        substr(cast (min(l.stime)::time text), 1,5) starttime,       substr(cast (max(l.etime)::time text), 1,5) endtime,        sum(tse)   tt l  group  l.uid,            l.stime,            l.etime order ord 

the ord column used order by

this output show this

ord uid  starttime endtime  total 11   1     12:22     12:31   512 11   1     12:17     12:17     1 12                           513 21   2     13:22     13:22     1 21   2     12:43     12:48   288 22                           289 

Comments

Popular posts from this blog

windows - Single EXE to Install Python Standalone Executable for Easy Distribution -

c# - Access objects in UserControl from MainWindow in WPF -

javascript - How to name a jQuery function to make a browser's back button work? -