aggregate-functions

MySQL only within the current month?

你说的曾经没有我的故事 提交于 2019-11-30 18:46:57
I have the following MySQL query, and I'm trying to adjust it so it only fetches results which are within the current month (of the current year), I'm guessing you may require more info about my MySQL structure so here it goes - I have a UNIX timestamp generated by PHP's time() stored within the time column (under the referrals table), so with the below setup it would be t2.time . So my problem is I'm unsure how to proceed, I'm guessing it would be something like adding the following to end of the WHERE clause? => AND t2.time IS WITHIN THE CURRENT MONTH (caps are just on to distinguish problem

How to update fields that is the aggregate result of another table in MySQL?

北城余情 提交于 2019-11-30 17:29:11
UPDATE a JOIN b ON a.app_id=b.app_id GROUP BY a.app_id SET remark_avg=AVG(b.score),remark_count=COUNT(b.id); The above is basically what I want to do,but it's not a valid MySQL statement,how to write it correctly? UPDATE a INNER JOIN (SELECT AVG(b.score) avg_score, COUNT(b.id) cnt_id, b.app_id FROM b GROUP BY b.app_id) x ON x.app_id = a.app_id SET remark_avg = x.avg_score, remark_count = x.cnt_id; 来源: https://stackoverflow.com/questions/5752075/how-to-update-fields-that-is-the-aggregate-result-of-another-table-in-mysql

Select running total until specific SUM is reached

坚强是说给别人听的谎言 提交于 2019-11-30 15:31:46
I am trying to select the first n rowid values from the following table variable that will get me as close to a sum(itemcount) of 200,000 without crossing that threshhold. If I was looking at this manually, I would just take the top 3 rows. I do not want to use a cursor unless there is no pure-set-based way. What is a good set-based way to get all of the rowid values "sum while/until" I get to a running total of 200,000? I looked at "running totals" at http://www.1keydata.com/sql/sql-running-totals.html but that did not seem like it would work out because the real table has around 500k rows.

Nesting Aggregate Functions - SQL

若如初见. 提交于 2019-11-30 14:56:52
问题 I want to make a SQL query which finds the catagory of awards for movies which has the highest average rating, so for a group of movies which have won a particular award, if they have a higher average rating than any other awards group of movies then it will be returned. I tried something like this: SELECT MAX(AVG(m."Rating")) FROM awards a, movies m WHERE a."Title" = m."Title" GROUP BY a."Award" but it seems that aggregate functions cannot be nested. How can I call the max function on the

Multiple array_agg() calls in a single query

杀马特。学长 韩版系。学妹 提交于 2019-11-30 14:11:19
问题 I'm trying to accomplish something with my query but it's not really working. My application used to have a mongo db so the application is used to get arrays in a field, now we had to change to Postgres and I don't want to change my applications code to keep v1 working. In order to get arrays in 1 field within Postgres I used array_agg() function. And this worked fine so far. However, I'm at a point where I need another array in a field from another different table. For example: I have my

Does GQL support commonly available SQL Style aggregation?

蓝咒 提交于 2019-11-30 14:07:40
What I'm looking for a simple Aggregate Functions that are widely available in versions of SQL. Simple things like Select Count(*) from table1 to the more complex. If these are available, is there some documentation you could point me to? Thanks - Giggy The SQL aggregate functions are not available. What you want to do is follow patterns like the sharded counters example: http://code.google.com/appengine/articles/sharding_counters.html which explain that instead of aggregating the values on queries, you want to keep the counters up to date when the values are inserted, updated, or deleted. The

SQL Server: collect values in an aggregation temporarily and re-use in the same query

℡╲_俬逩灬. 提交于 2019-11-30 14:04:13
How do I accumulate values in T-SQL? AFAIK there is no ARRAY type. I want to re-use the values in the same query like demonstrated in this PostgreSQL example using array_agg() . SELECT a[1] || a[i] AS foo ,a[2] || a[5] AS bar -- assuming we have >= 5 rows for simplicity FROM ( SELECT array_agg(text_col ORDER BY text_col) AS a ,count(*)::int4 AS i FROM tbl WHERE id between 10 AND 100 ) x How would I best solve this with T-SQL ? Best I could come up with are two CTE and subselects: ;WITH x AS ( SELECT row_number() OVER (ORDER BY name) AS rn ,name AS a FROM #t WHERE id between 10 AND 100 ), i AS

Applying a custom groupby aggregate function to output a binary outcome in pandas python

删除回忆录丶 提交于 2019-11-30 13:39:55
I have a dataset of trader transactions where the variable of interest is Buy/Sell which is binary and takes on the value of 1 f the transaction was a buy and 0 if it is a sell. An example looks as follows: Trader Buy/Sell A 1 A 0 B 1 B 1 B 0 C 1 C 0 C 0 I would like to calculate the net Buy/Sell for each trader such that if the trader had more than 50% of trades as a buy, he would have a Buy/Sell of 1, if he had less than 50% buy then he would have a Buy/Sell of 0 and if it were exactly 50% he would have NA (and would be disregarded in future calculations). So for trader A, the buy proportion

Sum totals of two queries

℡╲_俬逩灬. 提交于 2019-11-30 12:45:47
I have two basic queries which I need to sum the totals of: Select hours, sum(hours) FROM table WHERE name='xxx' and Description='Worked' Select hours2, sum(hours2) FROM table WHERE name='xxx' and Description2='Worked' I've tried UNION and it will get me the totals of each query but it will not combine them. Table setup is: ID name hours description hours2 description2 I need to correlate hours to description and hours2 to description2 which is why I have the two different queries. I need to sum the totals of hours and hours2. First of all, you missed group by , so even though mysql doesn't

Nesting Aggregate Functions - SQL

妖精的绣舞 提交于 2019-11-30 12:00:21
I want to make a SQL query which finds the catagory of awards for movies which has the highest average rating, so for a group of movies which have won a particular award, if they have a higher average rating than any other awards group of movies then it will be returned. I tried something like this: SELECT MAX(AVG(m."Rating")) FROM awards a, movies m WHERE a."Title" = m."Title" GROUP BY a."Award" but it seems that aggregate functions cannot be nested. How can I call the max function on the average ratings for each catagory? If you are only interested in the value itself, the following should