aggregate-functions

How to get definition/source code of an aggregate in PostgreSQL?

随声附和 提交于 2019-12-05 13:10:31
I found this related answer useful: Export "Create Aggregate" functions from PostgreSQL But how do I get the CREATE AGGREGATE statement without a GUI client (e.g. with psql command line)? a_horse_with_no_name Something like this, but I'm not sure if this covers all possible ways of creating an aggregate (it definitely does not take the need for quoted identifiers into account) SELECT 'create aggregate '||n.nspname||'.'||p.proname||'('||format_type(a.aggtranstype, null)||') (sfunc = '||a.aggtransfn ||', stype = '||format_type(a.aggtranstype, null) ||case when op.oprname is null then '' else ',

TSQL: Cannot perform an aggregate function AVG on COUNT(*) to find busiest hours of day

谁都会走 提交于 2019-12-05 11:36:55
Consider a SQL Server table that holds log data. The important parts are: CREATE TABLE [dbo].[CustomerLog]( [ID] [int] IDENTITY(1,1) NOT NULL, [CustID] [int] NOT NULL, [VisitDate] [datetime] NOT NULL, CONSTRAINT [PK_CustomerLog] PRIMARY KEY CLUSTERED ([ID] ASC)) ON [PRIMARY] The query here is around finding the distribution of visits BY HOUR of the day. We're interested in seeing the distribution of the average number of visits for the hour in a given date range. The query results would be something like this: HourOfDay Avg.Visits.In.Hour 0 24 1 16 5 32 6 89 7 823 etc.etc. The intention is to

Is there any MySQL Aggregate Function for “CONTAINS”?

旧城冷巷雨未停 提交于 2019-12-05 11:28:54
Say I have this data set user | group --------+------- a@a.com | A a@a.com | B b@b.com | A c@c.com | B d@d.com | A d@d.com | B d@d.com | C I want to convert this into a table like this: user | IN_A | IN_B | IN_C --------+-------+-------+------- a@a.com | TRUE | TRUE | FALSE b@b.com | TRUE | FALSE | FALSE c@c.com | FALSE | TRUE | FALSE d@d.com | TRUE | TRUE | TRUE I've got: SELECT user, IF(LOCATE('A', GROUP_CONCAT(group)) > 0, TRUE, FALSE) AS IN_A, IF(LOCATE('B', GROUP_CONCAT(group)) > 0, TRUE, FALSE) AS IN_B, IF(LOCATE('C', GROUP_CONCAT(group)) > 0, TRUE, FALSE) AS IN_C FROM users GROUP BY

Terms Aggregation for nested field in Elastic Search

让人想犯罪 __ 提交于 2019-12-05 09:58:33
I have next mapping for field in Elastic Search (definition in YML): my_analyzer: type: custom tokenizer: keyword filter: lowercase products_filter: type: "nested" properties: filter_name: {"type" : "string", analyzer: "my_analyzer"} filter_value: {"type" : "string" , analyzer: "my_analyzer"} Each document has a lot of filters and it looks like: "products_filter": [ { "filter_name": "Rahmengröße", "filter_value": "33,5 cm" } , { "filter_name": "color", "filter_value": "gelb" } , { "filter_name": "Rahmengröße", "filter_value": "39,5 cm" } , { "filter_name": "Rahmengröße", "filter_value": "45,5

Cumulative sum of values by month, filling in for missing months

僤鯓⒐⒋嵵緔 提交于 2019-12-05 09:51:51
I have this data table and I'm wondering if is possible create a query that get a cumulative sum by month considering all months until the current month . date_added | qty ------------------------------------ 2015-08-04 22:28:24.633784-03 | 1 2015-05-20 20:22:29.458541-03 | 1 2015-04-08 14:16:09.844229-03 | 1 2015-04-07 23:10:42.325081-03 | 1 2015-07-06 18:50:30.164932-03 | 1 2015-08-22 15:01:54.03697-03 | 1 2015-08-06 18:25:07.57763-03 | 1 2015-04-07 23:12:20.850783-03 | 1 2015-07-23 17:45:29.456034-03 | 1 2015-04-28 20:12:48.110922-03 | 1 2015-04-28 13:26:04.770365-03 | 1 2015-05-19 13:30:08

Create nested json from sql query postgres 9.4

a 夏天 提交于 2019-12-05 07:41:17
I need to get as a result from query fully structured JSON. I can see in postgres that there are some built in functions that may be useful. As an example I created a structure as follows: -- Table: person -- DROP TABLE person; CREATE TABLE person ( id integer NOT NULL, name character varying(30), CONSTRAINT person_pk PRIMARY KEY (id) ) WITH ( OIDS=FALSE ); ALTER TABLE person OWNER TO postgres; -- Table: car -- DROP TABLE car; CREATE TABLE car ( id integer NOT NULL, type character varying(30), personid integer, CONSTRAINT car_pk PRIMARY KEY (id) ) WITH ( OIDS=FALSE ); ALTER TABLE car OWNER TO

I am getting: "You tried to execute a query that does not include the specified expression 'OrdID' as part of an aggregate function. How do I bypass?

久未见 提交于 2019-12-05 07:04:51
问题 My code is as follows: SELECT Last, OrderLine.OrdID, OrdDate, SUM(Price*Qty) AS total_price FROM ((Cus INNER JOIN Orders ON Cus.CID=Orders.CID) INNER JOIN OrderLine ON Orders.OrdID=OrderLine.OrdID) INNER JOIN ProdFabric ON OrderLine.PrID=ProdFabric.PrID AND OrderLine.Fabric=ProdFabric.Fabric GROUP BY Last ORDER BY Last DESC, OrderLine.OrdID DESC; This code has been answered before, but vaguely. I was wondering where I am going wrong. You tried to execute a query that does not include the

creating a pseudo linked list in sql

不羁岁月 提交于 2019-12-05 02:57:27
问题 I have a table that has the following columns table: route columns: id, location, order_id and it has values such as id, location, order_id 1, London, 12 2, Amsterdam, 102 3, Berlin, 90 5, Paris, 19 Is it possible to do a sql select statement in postgres that will return each row along with the id with the next highest order_id? So I want something like... id, location, order_id, next_id 1, London, 12, 5 2, Amsterdam, 102, NULL 3, Berlin, 90, 2 5, Paris, 19, 3 Thanks 回答1: select id, location,

Aggregate strings in descending order in a PostgreSQL query

a 夏天 提交于 2019-12-05 00:09:13
In addition to the question How to concatenate strings of a string field in a PostgreSQL 'group by' query? How can I sort employee in descending order? I am using PostgreSQL 8.4 which doesn't support string_agg() . I've tried to use the following, but it isn't supported: array_to_string(array_agg(employee ORDER BY employee DESC), ',') I'd appreciate any hint to the right answer. In PostgreSQL 9.0 or later you can order elements inside aggregate functions : SELECT company_id, array_agg(employee ORDER BY company_id DESC)::text FROM tbl GROUP BY 1; That's not available for PostgreSQL 8.4 . You

Oracle aggregation function to allocate amount

江枫思渺然 提交于 2019-12-04 19:43:10
问题 Suppose I have 2 tables T1 and T2 as follows T1 : bag_id bag_type capacity ------|--------|-------- 1 A 500 2 A 300 3 A 100 4 B 200 5 B 100 T2 : item_type item_amount ---------|----------- A 850 B 300 Each record in table T1 represents a bag and its capacity, here I have 5 bags. I want to write an SQL that allocate items in table T2 into each bag with the same type, i.e. the result should be like this bag_id bag_type capacity allocated_amount ------|--------|--------|---------------- 1 A 500