aggregate-functions

How to Perform Consecutive Counts of Column by Group Conditionally Upon Another Column

风格不统一 提交于 2019-12-11 06:08:11
问题 I'm trying to get consecutive counts from the Noshow column grouped by the PatientID column. The below code that I am using is very close to the results that I wish to attain. However, using the sum function returns the sum of the whole group. I would like the sum function to only sum the current row and only the rows that have a '1' above it. Basically, I'm trying to count the consecutive amount of times a patient noshows their appointment for each row and then reset to 0 when they do show.

pandas groupby with two key

点点圈 提交于 2019-12-11 05:55:04
问题 I took a whole afternoon trying to implement this task but failed ,I've got a pandas data frame like this columns=[ka,kb_1,kb_2,timeofEvent,timeInterval] 0:'3M' '2345' '2345' '2014-10-5',3000 1:'3M' '2958' '2152' '2015-3-22',5000 2:'GE' '2183' '2183' '2012-12-31',515 3:'3M' '2958' '2958' '2015-3-10',395 4:'GE' '2183' '2285' '2015-4-19',1925 5:'GE' '2598' '2598' '2015-3-17',1915 What is to be implemented is a new data frame grouped by "ka and kb_1" below columns=[ka,kb,errorNum,errorRate

Mysql - How to create column that automatically sums rows?

懵懂的女人 提交于 2019-12-11 05:37:34
问题 I have a mysql database table called "character" with columns like name, strength, intelligence, skill. I want to create a column that sums up strength, intelligence and skill AUTOMATICALLY. Is this possible? I have seen loads of pages showing how to do queries. I did a query just fine, such as select (str+intel+skl) as sum from character; It returns a sum just fine. But what I'm missing (don't understand) is how to: either AUTOMATE that query into my mysql db (for example, when I do "select

Fetch Max from a date column grouped by a particular field

寵の児 提交于 2019-12-11 05:09:51
问题 I have a table similar to this: LogId RefId Entered ================================== 1 1 2010-12-01 2 1 2010-12-04 3 2 2010-12-01 4 2 2010-12-06 5 3 2010-12-01 6 1 2010-12-10 7 3 2010-12-05 8 4 2010-12-01 Here, LogId is unique; For each RefId, there are multiple entries with timestamp. What I want to extract is LogId for each latest RefId. I tried solutions from this link:http://stackoverflow.com/questions/121387/sql-fetch-the-row-which-has-the-max-value-for-a-column. But, it returns

Average aggregation on temporal DB based on measurment with 10-minute intervals in PostgreSQL

 ̄綄美尐妖づ 提交于 2019-12-11 04:37:18
问题 I have a temporal database and I want to perform the temporal aggregation via average function on time series data by using data in time sequence of following: Hourly average of HH= (HH-1):51, HH:01, HH:11, HH:21, HH:31, HH:41. Moreover, aforementioned values are instantaneous values. Sample data: ambtemp ddate_ttime 1 -1.42 2007-09-28 23:39:09 2 -1.24 2007-09-28 23:41:09 3 -1.28 2007-09-28 23:43:09 4 -1.28 2007-09-28 23:45:09 5 -1.24 2007-09-28 23:47:09 6 -1.42 2007-09-28 23:49:09 7 -1.68

Mapping result of aggregate query to hibernate object

筅森魡賤 提交于 2019-12-11 04:05:33
问题 Is it possible to map the result of an aggregate query to a field in a hibernate-backed domain object? For example: If I have a Car object that looks like the following -- @Entity public class Car { @Id private int id; @Column private String carName; private int carCount; ---Getters/Setters--- } I would like the carCount field/property to be the total count of all the cars in my persistence store, is this possible? I've looked at the Hibernate documentation, I can run the query, but I don't

Subselect on array_agg in postgresql

霸气de小男生 提交于 2019-12-11 03:47:11
问题 Is there a way to use a value from an aggregate function in a having clause in Postgresql 9.2+? For example, I would like to get each monkey_id with a 2nd highest number > 123, as well as the second highest number. In the example below, I'd like to get (monkey_id 1, number 222). monkey_id | number ------------------ 1 | 222 1 | 333 2 | 0 2 | 444 SELECT monkey_id, (array_agg(number ORDER BY number desc))[2] as second FROM monkey_numbers GROUP BY monkey_id HAVING second > 123 I get column

mysql use group by column in where condition

試著忘記壹切 提交于 2019-12-11 03:29:14
问题 How can I make this query work : SELECT column1.....,SUM(Hits) AS Hits FROM table WHERE SUM(Hits) > 100 GROUP BY column1..... The problem is the where clause, mysql display error : Error Code : 1111 Invalid use of group function I try to change the query to : SELECT column1.....,SUM(Hits) AS Hits FROM table WHERE Hits > 100 GROUP BY column1..... It did not help. thanks 回答1: SELECT column1.....,SUM(Hits) AS HitsSum FROM table GROUP BY column1..... HAVING HitsSum > 100 回答2: The reason for the

Can get an average of values in a json array using postgres?

浪子不回头ぞ 提交于 2019-12-11 03:23:14
问题 One of the great things about postgres is that it allows indexing into a json object. I have a column of data formatted a little bit like this: {"Items": [ {"RetailPrice":6.1,"EffectivePrice":0,"Multiplier":1,"ItemId":"53636"}, {"RetailPrice":0.47,"EffectivePrice":0,"Multiplier":1,"ItemId":"53404"} ] } What I'd like to do is find the average RetailPrice of each row with these data. Something like select avg(json_extract_path_text(item_json, 'RetailPrice')) but really I need to do this for

How to get id of the row which was selected by aggregate function? [duplicate]

泪湿孤枕 提交于 2019-12-11 03:16:15
问题 This question already has answers here : Select first row in each GROUP BY group? (16 answers) Closed last year . I have next data: id | name | amount | datefrom --------------------------- 3 | a | 8 | 2018-01-01 4 | a | 3 | 2018-01-15 10:00 5 | b | 1 | 2018-02-20 I can group result with the next query: select name, max(amount) from table group by name But I need the id of selected row too. Thus I have tried: select max(id), name, max(amount) from table group by name And as it was expected it