aggregate-functions

MySQL COUNT function not performing as I would like in multiple joined query

不问归期 提交于 2020-01-04 07:31:06
问题 I have been altering a query for a while now, reading many posts on this wonderful site to get to where I am with this so far. But, alas, now I am stuck :( This query and relevant part of the database I have designed is similar to the youtube comment liking system. Relevant tables and fields are: USRS usr_id int (PK) COMMENTS comment_id int (PK) usr_id int (FK) references usrs(usr_id) topic_id int (FK) references topics(topic_id) descr varchar created varchar COMMENT_LIKERS comment_id int (PK

How to include missing data for multiple groupings within the time span?

China☆狼群 提交于 2020-01-04 05:33:33
问题 I have below referenced query which groups studies counts by teacher, study year-month, and room for the past 12 months (including current month). The result I get is correct, however, I would like to include rows with zero counts for when the data is missing. I looked at several other related posts but could not get desired output: Postgres - how to return rows with 0 count for missing data? Postgresql group month wise with missing values Best way to count records by arbitrary time intervals

Aggregate boolean values to true if any of the source columns is true

半城伤御伤魂 提交于 2020-01-04 03:56:07
问题 Let say I have the following table: id column_a column_b column_c 1 t f t 2 t f f 3 f t f From the above table, I want to: select rows from id = 1,2; The result should be: column_a column_b column_c t f t If any of the rows among the defined id has a true for a particular column we assume the result to be true. 回答1: Use the aggregate function bool_or(). SELECT bool_or(column_a) AS column_a , bool_or(column_b) AS column_b , bool_or(column_c) AS column_c FROM tbl WHERE id IN (1,2); The manual:

MySQL: Select MAX() from sub-query with COUNT()

馋奶兔 提交于 2020-01-04 02:06:25
问题 Before you mark this as duplicate please take a look at this SQLFiddle. I have this schema: CREATE TABLE book(book_id int, book_name varchar(100), author_id int, editor_id varchar(100), isbn varchar(100)); INSERT INTO book VALUES (1 , 'Book1 Title' , 12 , 'Editor1' , '8000-9000' ), (2 , 'Book2 Title' , 98 , 'Editor1' , '8000-9001' ), (1 , 'Book1 Title' , 12 , 'Editor1' , '8000-9002' ), (3 , 'Book3 Title' , 3 , 'Editor1' , '8000-9003' ); CREATE TABLE author(author_id int, fn varchar(100), ln

Aggregating in R

时光怂恿深爱的人放手 提交于 2020-01-03 11:33:33
问题 I have a data frame with two columns. I want to add an additional two columns to the data set with counts based on aggregates. df <- structure(list(ID = c(1045937900, 1045937900), SMS.Type = c("DF1", "WCB14"), SMS.Date = c("12/02/2015 19:51", "13/02/2015 08:38"), Reply.Date = c("", "13/02/2015 09:52") ), row.names = 4286:4287, class = "data.frame") I want to simply count the number of Instances of SMS.Type and Reply.Date where there is no null. So in the toy example below, i will generate the

MySQL - Max() return wrong result

不问归期 提交于 2020-01-03 03:09:06
问题 I tried this query on MySQL server (5.1.41)... SELECT max(volume), dateofclose, symbol, volume, close, market FROM daily group by market I got this result: max(volume) dateofclose symbol volume close market 287031500 2010-07-20 AA.P 500 66.41 AMEX 242233000 2010-07-20 AACC 16200 3.98 NASDAQ 1073538000 2010-07-20 A 4361000 27.52 NYSE 2147483647 2010-07-20 AAAE.OB 400 0.01 OTCBB 437462400 2010-07-20 AAB.TO 31400 0.37 TSX 61106320 2010-07-20 AA.V 0 0.24 TSXV As you can see, the maximum volume is

Redshift - Calculate monthly active users

筅森魡賤 提交于 2020-01-03 01:42:09
问题 I have a table which looks like this: Date | User_ID 2017-1-1 | 1 2017-1-1 | 2 2017-1-1 | 4 2017-1-2 | 3 2017-1-2 | 2 ... | .. ... | .. ... | .. ... | .. 2017-2-1 | 1 2017-2-2 | 2 ... | .. ... | .. ... | .. I'd like to calculate the monthly active users over a rolling 30 day period. I know Redshift does not do COUNT(DISTINCT)) windowing. What can I do to get the following output? Date | MAU 2017-1-1 | 3 2017-1-2 | 4 <- We don't want to count user_id 2 twice. ... | .. ... | .. ... | .. 2017-2

Redshift - Calculate monthly active users

女生的网名这么多〃 提交于 2020-01-03 01:41:43
问题 I have a table which looks like this: Date | User_ID 2017-1-1 | 1 2017-1-1 | 2 2017-1-1 | 4 2017-1-2 | 3 2017-1-2 | 2 ... | .. ... | .. ... | .. ... | .. 2017-2-1 | 1 2017-2-2 | 2 ... | .. ... | .. ... | .. I'd like to calculate the monthly active users over a rolling 30 day period. I know Redshift does not do COUNT(DISTINCT)) windowing. What can I do to get the following output? Date | MAU 2017-1-1 | 3 2017-1-2 | 4 <- We don't want to count user_id 2 twice. ... | .. ... | .. ... | .. 2017-2

Rolling Daily Distinct Counts

旧巷老猫 提交于 2020-01-02 07:07:11
问题 We have a table with the following columns: SESSION_ID USER_ID CONNECT_TS -------------- --------------- --------------- 1 99 2013-01-01 2:23:33 2 101 2013-01-01 2:23:55 3 104 2013-01-01 2:24:41 4 101 2013-01-01 2:24:43 5 233 2013-01-01 2:25:01 We need to get a distinct count of users for each day and a count of "active users" which are defined as users that have used the application in the last 45 days. Here is what we have come up with, but I feel like there has to be a better way: select

Rolling Daily Distinct Counts

拟墨画扇 提交于 2020-01-02 07:07:04
问题 We have a table with the following columns: SESSION_ID USER_ID CONNECT_TS -------------- --------------- --------------- 1 99 2013-01-01 2:23:33 2 101 2013-01-01 2:23:55 3 104 2013-01-01 2:24:41 4 101 2013-01-01 2:24:43 5 233 2013-01-01 2:25:01 We need to get a distinct count of users for each day and a count of "active users" which are defined as users that have used the application in the last 45 days. Here is what we have come up with, but I feel like there has to be a better way: select