aggregate-functions

Oracle SQL - Sum and group data by week

陌路散爱 提交于 2019-11-29 03:07:06
I have records related to dates: DATE AMOUNT 16.03.2013 3 16.03.2013 4 16.03.2013 1 16.03.2013 3 17.03.2013 4 17.03.2014 3 I know how to sum them up for each day, but how could I sum them up by week?` Vignesh Kumar A Try this SELECT to_char(DATE - 7/24,'IYYY'), to_char(DATE - 7/24,'IW'),SUM(AMOUNT) FROM YourTable GROUP BY to_char(DATE - 7/24,'IYYY'), to_char(DATE - 7/24,'IW') FIDDLE DEMO Output would be: +-----+-------+--------+ |YEAR | WEEK | AMOUNT | +-----+-------+--------+ |2013 | 11 | 18 | |2013 | 13 | 3 | +-----+-------+--------+ You can use TRUNC function to truncate date to the first

How to set correct attribute names to a json aggregated result with GROUP BY clause?

半腔热情 提交于 2019-11-29 02:55:15
问题 I have a table temp defined like this: id | name | body | group_id ------------------------------- 1 | test_1 | body_1 | 1 2 | test_2 | body_2 | 1 3 | test_3 | body_3 | 2 4 | test_4 | body_4 | 2 I would like to produce a result grouped by group_id and aggregated to json. However, query like this: SELECT group_id, json_agg(ROW(id, name, body)) FROM temp GROUP BY group_id; Produces this result: 1;[{"f1":1,"f2":"test_1","f3":"body_1"}, {"f1":2,"f2":"test_2","f3":"body_2"}] 2;[{"f1":3,"f2":"test

Bigquery SQL for sliding window aggregate

旧街凉风 提交于 2019-11-29 02:20:15
Hi I have a table that looks like this Date Customer Pageviews 2014/03/01 abc 5 2014/03/02 xyz 8 2014/03/03 abc 6 I want to get page view aggregates grouped by week but showing aggregates for past 30 days - (sliding window aggregates with window-size of 30 days for every week) I am using google bigquery EDIT: Gordon - re your comment about "Customer", Actually what I need is slightly more complicated thats why I included customer in the table above. I am looking to get the number of customers who had >n pageviews in a 30day window every week. something like this Date Customers>10 pageviews in

How to do a count on a union query

╄→гoц情女王★ 提交于 2019-11-29 01:33:29
问题 I have the following query: select distinct profile_id from userprofile_... union select distinct profile_id from productions_... How would I get the count of the total number of results? 回答1: If you want a total count for all records, then you would do this: SELECT COUNT(*) FROM ( select distinct profile_id from userprofile_... union all select distinct profile_id from productions_... ) x 回答2: you should use Union All if there are equals rows in both tables, because Union makes a distinct

How to avoid error “aggregate functions are not allowed in WHERE”

一笑奈何 提交于 2019-11-28 21:03:09
This sql code throws an aggregate functions are not allowed in WHERE SELECT o.ID , count(p.CAT) FROM Orders o INNER JOIN Products p ON o.P_ID = p.P_ID WHERE count(p.CAT) > 3 GROUP BY o.ID; How can I avoid this error? Replace WHERE clause with HAVING , like this: SELECT o.ID , count(p.CAT) FROM Orders o INNER JOIN Products p ON o.P_ID = p.P_ID GROUP BY o.ID HAVING count(p.CAT) > 3; HAVING is similar to WHERE , that is both are used to filter the resulting records but HAVING is used to filter on aggregated data (when GROUP BY is used). Use HAVING clause instead of WHERE Try this: SELECT o.ID,

aggregating time series in R

↘锁芯ラ 提交于 2019-11-28 20:58:43
I have the following OHLC data (by 3-minute intervals) library(tseries) library(xts) library(quantmod) > str(tickmin) An ‘xts’ object from 2010-06-30 15:47:00 to 2010-09-08 15:14:00 containing: Data: num [1:8776, 1:5] 9215 9220 9205 9195 9195 ... - attr(*, "dimnames")=List of 2 ..$ : NULL ..$ : chr [1:5] "zv.Open" "zv.High" "zv.Low" "zv.Close" ... Indexed by objects of class: [POSIXct,POSIXt] TZ: xts Attributes: NULL >tickmin 2010-09-08 15:02:00 20 2010-09-08 15:04:00 77 2010-09-08 15:08:00 86 2010-09-08 15:11:00 7 2010-09-08 15:14:00 43 > start(tickmin) [1] "2010-06-30 15:47:00 EDT" > end

MySQL: How to SUM() a TIMEDIFF() on a group?

。_饼干妹妹 提交于 2019-11-28 19:40:21
So I've got a set of results that looks something like this: SELECT User_ID, StartTime, EndTime, TIMEDIFF(EndTime, StartTime) AS TimeDiff FROM MyTable ------------------------------------------------------------------ | User_ID | StartTime | EndTime | TimeDiff | ------------------------------------------------------------------ | 1 | 2010-11-05 08:00:00 | 2010-11-05 09:00:00 | 01:00:00 | ------------------------------------------------------------------ | 1 | 2010-11-05 09:00:00 | 2010-11-05 10:00:00 | 01:00:00 | ------------------------------------------------------------------ | 2 | 2010-11

Extra Fields with SQL MIN() & GROUP BY

孤街醉人 提交于 2019-11-28 19:18:14
When using the SQL MIN() function, along with GROUP BY, will any additional columns (not the MIN column, or one of the GROUP BY columns) match the data in the matching MIN row? For example, given a table with department names, employee names, and salary: SELECT MIN(e.salary), e.* FROM employee e GROUP BY department Obviously I'll get two good columns, the minimum salary and the department. Will the employee name (and any other employee fields) be from the same row? Namely the row with the MIN(salary)? I know there could very possibly be two employees with the same (and lowest) salary, but all

count without group

我怕爱的太早我们不能终老 提交于 2019-11-28 19:16:17
I have one table named GUYS(ID,NAME,PHONE) and i need to add a count of how many guys have the same name and at the same time show all of them so i can't group them. example: ID NAME PHONE 1 John 335 2 Harry 444 3 James 367 4 John 742 5 John 654 the wanted output should be ID NAME PHONE COUNT 1 John 335 3 2 Harry 444 1 3 James 367 1 4 John 742 3 5 John 654 3 how could i do that? i only manage to get lot of guys with different counts. thanks Since MySQL doesn't have analytical functions like Oracle , you'll have to resort to a sub-query. Don't use GROUP BY , use a sub-select to count the number

GROUP BY and COUNT in PostgreSQL

纵然是瞬间 提交于 2019-11-28 19:10:27
The query: SELECT COUNT(*) as count_all, posts.id as post_id FROM posts INNER JOIN votes ON votes.post_id = posts.id GROUP BY posts.id; Returns n records in Postgresql: count_all | post_id -----------+--------- 1 | 6 3 | 4 3 | 5 3 | 1 1 | 9 1 | 10 (6 rows) I just want to retrieve the number of records returned: 6 . I used a subquery to achieve what I want, but this doesn't seem optimum: SELECT COUNT(*) FROM ( SELECT COUNT(*) as count_all, posts.id as post_id FROM posts INNER JOIN votes ON votes.post_id = posts.id GROUP BY posts.id ) as x; How would I get the number of records in this context