aggregate-functions

Mysql: Group by Hour, 0 if no data

試著忘記壹切 提交于 2019-12-06 06:08:57
I have the following query: SELECT count(*) as 'totalCalls', HOUR(`end`) as 'Hour' FROM callsDataTable WHERE company IN ( SELECT number FROM products WHERE products.id IN (@_PRODUCTS)) AND YEAR(`end`) = @_YEAR AND MONTH(`end`) = @_MONTH group by HOUR(`end`) Above query returns only the hours in which there where calls made: totalCalls Hour 2 0 1 2 4 7 98 8 325 9 629 10 824 13 665 15 678 16 665 17 606 18 89 22 5 23 The desired output should be all the hours, and where there are no calls it should be 0 calls for that hour, like below: totalCalls Hour 0 0 0 1 1 2 0 3 0 4 0 5 0 6 4 7 98 8 325 9

How to create a MySQL stored aggregate function?

爷,独闯天下 提交于 2019-12-06 05:08:44
I need to write a stored function that does an aggregate operation on a column. Let's say a median (but actually, there are many different summary functions I want to implement). Ideally, I would like to have something like medBy( col1 col2 col3 ) which then for each row returns a median of the col1 values of all the rows that have the same col2 and col3 value as this one. That way, I wouldn't have to do GROUP BY on the whole query, just have a repeating value in that one column. This question ( How to write the quantile aggregate function? ) asked something similar, but it got answered with a

Left Join Lateral and array aggregates

你说的曾经没有我的故事 提交于 2019-12-06 04:59:42
I'm using Postgres 9.3. I have two tables T1 and T2 and a n:m relation T1_T2_rel between them. Now I'd like to create a view that in addition to the columns of T1 provides a column that, for each record in T1, contains an array with the primary key ids of all related records of T2. If there are no related entries in T2, corresponding fields of this column shall contain null-values. An abstracted version of my schema would look like this: CREATE TABLE T1 ( t1_id serial primary key, t1_data int ); CREATE TABLE T2 ( t2_id serial primary key ); CREATE TABLE T1_T2_rel ( t1_id int references T1( t1

SQL Server 2005 Computed Column Result From Aggregate Of Another Table Field's Value

一世执手 提交于 2019-12-06 04:51:50
问题 Sorry for the long question title. I guess I'm on to a loser on this one but on the off chance. Is it possible to make the calculation of a calculated field in a table the result of an aggregate function applied to a field in another table. i.e. You have a table called 'mug', this has a child called 'color' (which makes my UK head hurt but the vendor is from the US, what you going to do?) and this, in turn, has a child called 'size'. Each table has a field called sold. The size.sold

Pyspark Spark DataFrame - Aggregate and filter columns in map type column

时光总嘲笑我的痴心妄想 提交于 2019-12-06 04:51:27
My DataFrame looks like: | c1 | c2| c3 | |----+---+------- | A | b | 22:00| | A | b | 23:00| | A | b | 09:00| | A | c | 22:00| | B | c | 09:30| I would like to perform some aggregations and create a second DataFrame with 3 columns: c1 : is the column that I want to group by. map_category_room_date : map type, key the c2 and value the lower/min value in c3 . cnt_orig : is the count on how many rows the original group had. Result | c1 | map_category_room_date | cnt_orig | |----------+-------------------------+----------| | 'A' |{'b': 09:00, 'C': 22:00} | 4 | | 'B' |{'c': 09:30} | 1 | What

MySQL ORDER BY AVG() DESC not working when certain columns are included

好久不见. 提交于 2019-12-06 04:08:07
I'm doing a query to return all the rows in table1, along with their average rating from table2: SELECT `table1`.`description`, AVG( `table2`.`rating` ) AS avg_rating FROM `table1` LEFT JOIN `table2` ON ( `table2`.`botid` = `table1`.`id` ) GROUP BY `table1`.`id` ORDER BY avg_rating DESC The problem is that even though I specify DESC , the results are being returned ASC : +-------------+------------+ | description | avg_rating | +-------------+------------+ | test2 | 1.0000 | | test3 | 3.0000 | | test4 | 3.0000 | | saasdf | 4.0000 | +-------------+------------+ Why isn't MySQL honoring ORDER BY

Combined aggregated and non-aggregate query in SQL

蹲街弑〆低调 提交于 2019-12-06 02:30:25
问题 Not sure how to phrase this question, but I want an aggregate query applied to multiple rows. Hopefully an example should make this easier. Assuming I have the following data: player | year | games ------------------------- ausmubr01 | 2006 | 139 ausmubr01 | 2007 | 117 bondsba01 | 2006 | 130 bondsba01 | 2007 | 126 stairma01 | 2006 | 26 stairma01 | 2006 | 77 stairma01 | 2006 | 14 stairma01 | 2007 | 125 And for each player in each year, I want to calculate their "career year", i.e. the number

Daily average for the month (needs number of days in month)

回眸只為那壹抹淺笑 提交于 2019-12-06 02:22:52
I have a table as follow: CREATE TABLE counts ( T TIMESTAMP NOT NULL, C INTEGER NOT NULL ); I create the following views from it: CREATE VIEW micounts AS SELECT DATE_TRUNC('minute',t) AS t,SUM(c) AS c FROM counts GROUP BY 1; CREATE VIEW hrcounts AS SELECT DATE_TRUNC('hour',t) AS t,SUM(c) AS c,SUM(c)/60 AS a FROM micounts GROUP BY 1; CREATE VIEW dycounts AS SELECT DATE_TRUNC('day',t) AS t,SUM(c) AS c,SUM(c)/24 AS a FROM hrcounts GROUP BY 1; The problem now comes in when I want to create the monthly counts to know what to divide the daily sums by to get the average column a i.e. the number of

Create two aggregate columns by Group By Pandas

谁都会走 提交于 2019-12-06 01:37:15
I'm new to DataFrames and I want to group multiple columns and then sum and keep a count on the last column. e.g. s = pd.DataFrame(np.matrix([[1, 2,3,4], [3, 4,7,6],[3,4,5,6],[1,2,3,7]]), columns=['a', 'b', 'c', 'd']) a b c d 0 1 2 3 4 1 3 4 7 6 2 3 4 5 6 3 1 2 3 7 I want to group on a , b and c but then sum on d and count the elements within the group. I can count by s = s.groupby(by=["a", "b", "c"])["d"].count() a b c 1 2 3 2 3 4 5 1 7 1 And I can sum by s = s.groupby(by=["a", "b", "c"])["d"].sum() a b c 1 2 3 11 3 4 5 6 7 6 However I want to combine it such that The resulting dataframe has

Count the number of rows that contain a letter/number

此生再无相见时 提交于 2019-12-06 01:35:31
What I am trying to achieve is straightforward, however it is a little difficult to explain and I don't know if it is actually even possible in postgres. I am at a fairly basic level. SELECT, FROM, WHERE, LEFT JOIN ON, HAVING , e.t.c the basic stuff. I am trying to count the number of rows that contain a particular letter/number and display that count against the letter/number. i.e How many rows have entries that contain an "a/A" (Case insensitive) The table I'm querying is a list of film names. All I want to do is group and count 'a-z' and '0-9' and output the totals. I could run 36 queries