0

Query being used at this time:

update metrics.time_created 
set cumu_count = (
    select count(*) 
    from perf_stats.time 
    where date(insert_datetime)='2015-12-18'
) 
where id=max(id);

I get an "invalid use of group function" here -- how can I rewrite this to keep the same logic? I have to update the most recent row in the metrics.time_created table.

Hotdin Gurning
  • 1,821
  • 3
  • 15
  • 24
user3299633
  • 2,971
  • 3
  • 24
  • 38

3 Answers3

1

Try this:

update metrics.time_created a
set cumu_count = (
  select count(*) 
  from perf_stats.time 
  where date(insert_datetime)='2015-12-18'
) 
where exists (
  select 1
  from (select max(id) as maxid from metrics.time_created) t
  where maxid = a.id
);

Example demo: http://sqlfiddle.com/#!9/6bc3cd/1

EDIT:

Based on comment, here's the change

update metrics.time_created a

set cumu_count = 
(
  select count(*) 
  from perf_stats.time pt
  where exists
  (
    select 1
    from (select max(curr_date) as mcd from metrics.time_created) x
    where mcd = date(insert_datetime)
  )
) 

where exists 
(
  select 1
  from (select max(id) as maxid from metrics.time_created) t
  where maxid = a.id
);

Example demo: http://sqlfiddle.com/#!9/fcc91a/1

zedfoxus
  • 35,121
  • 5
  • 64
  • 63
0

You can use self join something like this :

update metrics.time_created as t1
inner join (
    select max(id) as id 
    from metrics.time_created
) as t2 on t1.id = t2.id
set cumu_count = (
  select count(*) 
  from perf_stats.time 
  where date(insert_datetime)='2015-12-18'
)
Hotdin Gurning
  • 1,821
  • 3
  • 15
  • 24
0

If you are trying to update one row with the maximum id, then you can use order by and limit:

update metrics.time_created
    set cumu_count = (select count(*)
                      from perf_stats.time
                      where date(insert_datetime) = '2015-12-18'
                     )
    order by id desc
    limit 1;
Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786