aggregate-functions

Selecting a Record With MAX Value

£可爱£侵袭症+ 提交于 2019-11-26 12:17:20
问题 In SQL Server 2008 I have a table CUSTOMERS that has two columns as: ID, BALANCE How can I write the query that selects the ID of the customer who has maximum balance, \" in the most effective way \"? Option 1: ORDER BY BALANCE and SELECT TOP(1) --> costs too much. Option 2: Firstly Get MAX amount , then make another query that uses the amount in where clause --> costs too much and not seem reliable. 回答1: Note : An incorrect revision of this answer was edited out. Please review all answers. A

Custom aggregate function (concat) in SQL Server

时光怂恿深爱的人放手 提交于 2019-11-26 11:09:46
问题 Question: I want to write a custom aggregate function that concatenates string on group by. So that I can do a SELECT SUM(FIELD1) as f1, MYCONCAT(FIELD2) as f2 FROM TABLE_XY GROUP BY FIELD1, FIELD2 All I find is SQL CRL aggregate functions, but I need SQL, without CLR. Edit:1 The query should look like this: SELECT SUM(FIELD1) as f1, MYCONCAT(FIELD2) as f2 FROM TABLE_XY GROUP BY FIELD0 Edit 2: It is true that it isn\'t possible without CLR. However, the subselect answer by astander can be

Aggregate function in MySQL - list (like LISTAGG in Oracle)

妖精的绣舞 提交于 2019-11-26 09:47:23
问题 I need function, that returns list of strings. I have data in table like this: Id MyString ------------------------ 1 First 2 Second 3 Third 4 Fourth I need function like this (something like this works in oracle): select LISTAGG(MyString, \', \') as myList where id < 4 That returns something like this: myList ------------------------ First, Second, Third Any ideas? 回答1: You're looking for GROUP_CONCAT() Try this: select group_concat(MyString separator ', ') as myList from table where id < 4

Why can&#39;t you mix Aggregate values and Non-Aggregate values in a single SELECT?

…衆ロ難τιáo~ 提交于 2019-11-26 09:42:43
问题 I know that if you have one aggregate function in a SELECT statement, then all the other values in the statement must be either aggregate functions, or listed in a GROUP BY clause. I don\'t understand why that\'s the case. If I do: SELECT Name, \'Jones\' AS Surname FROM People I get: NAME SURNAME Dave Jones Susan Jones Amy Jones So, the DBMS has taken a value from each row, and appended a single value to it in the result set. That\'s fine. But if that works, why can\'t I do: SELECT Name,

Best way to count records by arbitrary time intervals in Rails+Postgres

只愿长相守 提交于 2019-11-26 09:23:51
问题 My app has a Events table with time-stamped events. I need to report the count of events during each of the most recent N time intervals. For different reports, the interval could be \"each week\" or \"each day\" or \"each hour\" or \"each 15-minute interval\". For example, a user can display how many orders they received each week, day, or hour, or quarter-hour. 1) My preference is to dynamically do a single SQL query (I\'m using Postgres) that groups by an arbitrary time interval. Is there

SQL Server: Difference between PARTITION BY and GROUP BY

﹥>﹥吖頭↗ 提交于 2019-11-26 08:39:06
问题 I\'ve been using GROUP BY for all types of aggregate queries over the years. Recently, I\'ve been reverse-engineering some code that uses PARTITION BY to perform aggregations. In reading through all the documentation I can find about PARTITION BY , it sounds a lot like GROUP BY , maybe with a little extra functionality added in? Are they two versions of the same general functionality, or are they something different entirely? 回答1: They're used in different places. group by modifies the entire

Get the distinct sum of a joined table column

做~自己de王妃 提交于 2019-11-26 08:35:38
问题 I have a problem here, and I\'m hoping there is an easy solution. I\'ll try to make this as simple as possible: A ticket belongs to an attendee Example: select * from tickets JOIN attendees ON attendee.id = tickets.attendee_id An attendee has a decimal column called \"revenue\" That said, I need to run a query that will return a variety of information about the tickets, including the total revenue. The problem is that if 2 tickets belong to the same attendee, it counts their revenue twice.

Return rows matching elements of input array in plpgsql function

不羁的心 提交于 2019-11-26 07:49:20
问题 I would like to create a PostgreSQL function that does something like the following: CREATE FUNCTION avg_purchases( IN last_names text[] DEFAULT \'{}\' ) RETURNS TABLE(last_name text[], avg_purchase_size double precision) AS $BODY$ DECLARE qry text; BEGIN qry := \'SELECT last_name, AVG(purchase_size) FROM purchases WHERE last_name = ANY($1) GROUP BY last_name\' RETURN QUERY EXECUTE qry USING last_names; END; $BODY$ But I see two problems here: It is not clear to me that array type is the most

SQL comma-separated row with Group By clause

自作多情 提交于 2019-11-26 07:48:53
问题 I have the following query: SELECT Account, Unit, SUM(state_fee), Code FROM tblMta WHERE MTA.Id = \'123\' GROUP BY Account,Unit This of course throws an exception because the Code is not in the group by clause. Each state_fee has a code. How do I get this code to display in 1 record (1 code per state_fee which is multiple state_fee per unit) as a comma-separated list? I looked into different solutions on here but I couldn\'t find any that worked with a group by . 回答1: You want to use FOR XML

Selecting data into a Postgres array

六眼飞鱼酱① 提交于 2019-11-26 06:46:18
问题 I have the following data: name id url John 1 someurl.com Matt 2 cool.com Sam 3 stackoverflow.com How can I write an SQL statement in Postgres to select this data into a multi-dimensional array, i.e.: {{John, 1, someurl.com}, {Matt, 2, cool.com}, {Sam, 3, stackoverflow.com}} I\'ve seen this kind of array usage before in Postgres but have no idea how to select data from a table into this array format. Assuming here that all the columns are of type text . 回答1: You cannot use array_agg() to