aggregate-functions

How to transform sql query results to pivot table using php arrays?

谁说我不能喝 提交于 2019-12-11 03:15:49
问题 I am trying to create some statistics for a library reservation system. The result of my sql query looks like the following structure. total_no_students| department | property | month 241 | Physics | undergraduate | Nov 236 | Physics | undergraduate | Dec 254 | Physics | postgraduate | Nov 210 | Physics | postgraduate | Dec 193 | Architecture| undergraduate | Nov 181 | Architecture| undergraduate | Dec 127 | Architecture| postgraduate | Nov 292 | Architecture| postgraduate | Dec 134 | Biology

mysql fetch sum php

我怕爱的太早我们不能终老 提交于 2019-12-11 02:58:16
问题 sql column - trans_value contain both positive and negative value amount. so i'm trying to figure out how do i set a sum of positive value and negative value aside, so that i can calculate the how much sum of positive and how much is sum of negative. so in the end, i can minus both, positive - negative. edit, i forgot to mention before that there is also column name product id in same table, so product_id | trans_value 1 200 1 250 1 -150 2 500 2 -300 3 200 3 -150 so i need to get sum of

Export “Create Aggregate” functions from PostgreSQL

Deadly 提交于 2019-12-11 02:58:02
问题 I have created some user defined aggregate functions. However, I cannot see a way to export them in pgAdmin. Or any other way. I can see the underlying functions, but not the CREATE AGGREGATE definitions. SquirrelSQL doesn't show anything useful either! 回答1: Most probably you need to enable the display of aggregates in your Options: File -> Options -> Browser -> Display After refreshing the view you get a separate node Aggregates in the object browser: The reverse engineered CREATE script for

Resample on time series data

偶尔善良 提交于 2019-12-11 02:45:47
问题 I have a table with time series column in the millisecond, I want to resample the time series and apply mean on the group. How can I implement it in Postgres? "Resample" means aggregate all time stamps within one second or one minute. All rows within one second or one minute form a group. table structure date x y z 回答1: Use date_trunc() to truncate timestamps to a given unit of time, and GROUP BY that expression: SELECT date_trunc('minute', date) AS date_truncated_to_minute ,avg(x) AS avg_x

Group and count events per time intervals, plus running total

家住魔仙堡 提交于 2019-12-11 02:23:39
问题 I'm a fairly new Postgres user, I'm sure there's an answer to this already but I can't find it. I need to analyze some data in an activity log table, grouping the the results by time period. A simple version of the problem would be a table with three fields: Column | Type | Modifiers --------------+--------------------------+------------------------------------- period_start | timestamp with time zone | not null user_id | text | not null action | text | not null The action string I want to

Oracle LISTAGG() for multiple attributes?

北城余情 提交于 2019-12-11 01:39:25
问题 I wonder if there is a better (i.e. faster execution) solution to the problem described below. Step 1) create table t (k number, v1 number, v2 number); insert into t values (1,1,1); insert into t values (1,2,2); insert into t values (1,2,3); insert into t values (1,3,3); insert into t values (1,4,3); insert into t values (2,7,8); insert into t values (2,7,9); Step 2) I would like to return the following data set (k, v1_list, v2_list) 1, (1,2,3,4), (1,2,3) 2, (7), (8,9) Step 3) I am able to

Complicated COUNT query in MySQL

三世轮回 提交于 2019-12-10 23:22:44
问题 I am trying to find the number of video credits a particular user has. The following are the three tables that are relevant: CREATE TABLE `userprofile_userprofile` ( `id` int(11) NOT NULL AUTO_INCREMENT, `full_name` varchar(100) NOT NULL, ... ) CREATE TABLE `userprofile_videoinfo` ( `id` int(11) NOT NULL AUTO_INCREMENT, `title` varchar(256) NOT NULL, `uploaded_by_id` int(11) NOT NULL, ... KEY `userprofile_videoinfo_e43a31e7` (`uploaded_by_id`), CONSTRAINT `uploaded_by_id_refs_id

Is there Performance related difference in using aggregate function in ORDER BY clause and alias of aggregate function?

隐身守侯 提交于 2019-12-10 23:02:02
问题 I have a question related to ORDER BY or GROUP BY clause. For example I have below queries SELECT country_name,COUNT(*) FROM user_location WHERE country_name IS NOT NULL GROUP BY country_name ORDER BY COUNT(*) DESC And SELECT country_name,COUNT(*) As Total FROM user_location WHERE country_name IS NOT NULL GROUP BY country_name ORDER BY Total DESC In 2nd query I am using alias Total for COUNT(*) in ORDER BY clause. Is there any performance related differences in two queries ? 回答1: I've run the

SQL Select only rows with Max Value on a Column FILTERED by Column

て烟熏妆下的殇ゞ 提交于 2019-12-10 22:58:21
问题 This is a followup question to the excellent answer: SQL Select only rows with Max Value on a Column SQLFiddle http://sqlfiddle.com/#!2/3077f/2 Table "yourtable": | id | val | ignore | content | ------------------------------- | 1 | 10 | 0 | A | | 1 | 20 | 0 | B | | 1 | 30 | 1 | C | | 2 | 40 | 0 | D | | 2 | 50 | 0 | E | | 2 | 60 | 1 | F | When looking for max val per id, following sql is used: select yt1.* from yourtable yt1 left outer join yourtable yt2 on (yt1.id = yt2.id and yt1.val < yt2

NHibernate - Select full records from aggregates

三世轮回 提交于 2019-12-10 22:57:23
问题 In short: Given an aggregate query (one with Max, Min, Count, etc) in NHibernate, how can you modify the query to also return the full record associated with the aggregated value? My example: I have 2 tables: People (primary key: PersonId) with a 1-to-many relationship to Events (primary key: EventId; other columns: PersonId, EventDate). I want to select the last event per person and generate a list of these events . The SQL for last event per person would be something like SELECT PersonId,