group-by

RoR Postgresql timezone group by not working on Heroku

我的未来我决定 提交于 2019-12-14 03:47:00
问题 I would like my reports to display in CST while my database is in UTC. For this to work I need to be able to group by a time column and have the db do timezone conversions. This works on my dev postgresql box: offset = -5 result = ActiveRecord::Base.connection.execute("select date(srl.created_at AT TIME ZONE '#{offset.to_s}') AS date, count(*) from server_request_logs srl where srl.created_at between '#{@from.to_s(:db)}' and '#{@to.to_s(:db)}' group by date(srl.created_at AT TIME ZONE '#

Is there a performance difference in using a GROUP BY with MAX() as the aggregate vs ROW_NUMBER over partition by?

£可爱£侵袭症+ 提交于 2019-12-14 03:45:43
问题 Is there a performance difference between the following 2 queries, and if so, then which one is better?: select q.id, q.name from( select id, name, row_number over (partition by name order by id desc) as row_num from table ) q where q.row_num = 1 versus select max(id) , name from table group by name (The result set should be the same) This is assuming that no indexes are set. UPDATE: I tested this, and the group by was faster. 回答1: The group by should be faster. The row number has to assign a

how to groupBY using spring data

别说谁变了你拦得住时间么 提交于 2019-12-14 03:28:22
问题 hi i'm using spring data in My project and I'm trying group by two fields, heres the request: @Query( "SELECT obj from Agence obj GROUP BY obj.secteur.nomSecteur,obj.nomAgence" ) Iterable<Agence> getSecteurAgenceByPc(); but it doesnt work for me..what i want is this result: -Safi -CTM CZC1448YZN 2UA13817KT -Rabat -CTM CZC1349G1B 2UA0490SVR -Agdal G3M4NOJ -Essaouira -CTM CZC1221B85 -Gare Routiere Municipale CZC145YL3 What I get is { "status": 0, "data": [ { "secteur": "Safi", "agence": "CTM" }

How to get the frequency from grouped data with dplyr?

99封情书 提交于 2019-12-14 03:07:06
问题 This certainly is a basic question but I cannot figure it out by myself. Please consider the following: In a large dataset with patient characteristics in long format I want to summarise some variables. I would prefer to use dplyr for that. For the example data set: db <- data.frame(ID = c(rep(1, 3), rep(2,4), rep(3, 2)), Gender = factor(c(rep("woman", 7), rep("man", 2))), Grade = c(rep(3, 3), rep(1, 4), rep(2, 2))) db # ID Gender Grade # 1 1 woman 3 # 2 1 woman 3 # 3 1 woman 3 # 4 2 woman 1

MySQL - Joining the same table twice but grabbing different results

a 夏天 提交于 2019-12-14 03:06:25
问题 I previously asked a question in regards to the same situation here but since I was so vague about my query, the provided solution didn't work for my specific case. Here's my second attempt at resolving this: I have a series of tables that are connected in strange ways... Here's the end result I'm trying to achieve with my SELECT query: |----|---------|----------|----------|---------------|---------------|------------|---------------| | id | company | city | province | manager_name | manager

MySQL - Conditional MIN MAX to return distinct record

∥☆過路亽.° 提交于 2019-12-14 03:05:57
问题 I have a database dump from the geonames website for Great Britain. It consists of approx 60000 records. example data is as follows: id | name | admin1 | admin2 | admin3 | feature_class | feature_code ------------------------------------------------------------------------------------------- 2652355 | Cornwall | ENG | C6 | | A | ADM2 11609029 | Cornwall | ENG | | | L | RGN 6269131 | England | ENG | | | A | ADM1 The first record with feature code ADM2 means it is administrative level 2 The

Selecting the best score per player

Deadly 提交于 2019-12-14 02:57:36
问题 I have a table called Scores which contains columns: id , player_id , value1 , value2 , value3 and date . The table has next following content: +------+-----------+--------+--------+--------+------------+ | id | player_id | value1 | value2 | value3 | date | +------+-----------+--------+--------+--------+------------+ | 1 | 1 | 10 | 0 | 0 | 2012-08-02 | +------+-----------+--------+--------+--------+------------+ | 2 | 2 | 15 | 1 | 0 | 2012-08-03 | +------+-----------+--------+--------+-------

How to use hbase coprocessor to implement groupby?

本小妞迷上赌 提交于 2019-12-14 01:28:50
问题 Recently I learned hbase coprocessor, I used endpoint to accumulate one column of hbase table.For example, the hbase table named "pendings",its family is "asset", I accumulate all the value of "asset:amount". The table has other columns,such as "asset:customer_name". The first thing I want to do is accumulate the the value of "asset:amount" group by "asset:customer_name". But I found there is not API for groupby, or I did not find it. Do you know how to implement GROUPBY or how to use the API

How to order by count desc in each group in a hive?

▼魔方 西西 提交于 2019-12-14 00:19:03
问题 Here's the HQL: select A, B, count(*) as cnt from test_table group by A, B order by cnt desc; The sample output is as follows: a1 | b1 | 5 a2 | b1 | 3 a1 | b2 | 2 a2 | b2 | 1 But what I want is to do the order by in each group of A, and the intended output is like: a1 | b1 | 5 a1 | b2 | 2 a2 | b1 | 3 a2 | b2 | 1 Could anyone can give me some idea how to resolve this problem in just one HQL? Thanks a lot! 回答1: select A, B, count(*) as cnt from test_table group by A, B order by A, cnt desc; 回答2

c# Linq remove duplicate items and calculate average values

可紊 提交于 2019-12-14 00:09:52
问题 For example I have a list of objects (properties: Name and value) item1 20; item2 30; item1 50; I want the result: item1 35 (20+50)/2 item2 30 How can I do this? thanks sorry guys, duplicate is based on item.Name. 回答1: var results = from kvp in source group kvp by kvp.Key.ToUpper() into g select new { Key= g.Key, Value= g.Average(kvp => kvp.Value) } or var results = source.GroupBy(c=>c.Name) .Select(c => new (c.Key, c.Average(d=>d.Value))); 回答2: You could do it using average and group by: