aggregate-functions

How to get count of two-way combinations from two columns?

巧了我就是萌 提交于 2019-12-12 12:14:13
问题 I work for a trucking company, and we're interested in getting a count of the number of times one of our trucks travels between two cities, in either direction. I have a table that lists an origin and destination for each trip segment, such as: Origin Destination City 1 City 2 City 2 City 1 City 3 City 4 City 2 City 1 I need a query that tells me that there were three trips between City 1 and City 2 and one trip between City 3 and City 4. Thanks very much! 回答1: I think the following should do

First and last value aggregate functions in postgresql that work correctly with NULL values

喜夏-厌秋 提交于 2019-12-12 11:13:50
问题 I know there are aggregate functions for getting last and first value of rows in postgresql My problem is, that they do not work as i need. And i could use the help of one postgresql wizard. I'm using postgresql 9.2 - in case the version makes offering solution easyer. Query select v.id, v.active, v.reg_no, p.install_date, p.remove_date from vehicle v left join period p on (v.id = p.car_id) where v.id = 1 order by v.id, p.install_date asc Returns 6 rows: id, active, reg_no, install_date,

The best way to check existence of filtered rows in Cassandra? by user-defined aggregate?

对着背影说爱祢 提交于 2019-12-12 10:18:30
问题 I have a Cassandra table, CREATE TABLE read_locks ( parent_path text, filename text, instance text, PRIMARY KEY ((parent_path, filename), instance) ); Logically I want to check the existence of any locks on a file by the following statement: select count(*)>0 as result from read_locks where parent_path='...' and filename='...'; Of course, I have at least 2 implementations. select count(*) as result from read_locks where parent_path='...' and filename='...'; and then to use other code, i.e. C+

Custom aggregate function inside a package

半世苍凉 提交于 2019-12-12 09:48:34
问题 I'm trying to write a custom aggregate function in Oracle and group that function inside a package together with some other functions that I have. As an example (to simulate the problem I have) suppose my custom aggregation to do a summation of numbers looks like: CREATE OR REPLACE TYPE SUM_AGGREGATOR_TYPE AS OBJECT ( summation NUMBER, STATIC FUNCTION ODCIAggregateInitialize(agg_context IN OUT SUM_AGGREGATOR_TYPE) RETURN NUMBER, MEMBER FUNCTION ODCIAggregateIterate(self IN OUT SUM_AGGREGATOR

SQL vs MySQL: Rules about aggregate operations and GROUP BY

倾然丶 夕夏残阳落幕 提交于 2019-12-12 07:49:19
问题 In this book I'm currently reading while following a course on databases, the following example of an illegal query using an aggregate operator is given: Find the name and age of the oldest sailor. Consider the following attempt to answer this query: SELECT S.sname, MAX(S.age) FROM Sailors S The intent is for this query to return not only the maximum age but also the name of the sailors having that age. However, this query is illegal in SQL--if the SELECT clause uses an aggregate operation,

Spring Data JPA - Custom Query with multiple aggregate functions in result

烂漫一生 提交于 2019-12-12 07:13:48
问题 I was trying to return an average and count of a set of ratings in one query. I managed it fairly easily in two queries following the example I found browsing. For example: @Query("SELECT AVG(rating) from UserVideoRating where videoId=:videoId") public double findAverageByVideoId(@Param("videoId") long videoId); but as soon as I wanted an average and a count in the same query, the trouble started. After many hours experimenting, I found this worked, so I am sharing it here. I hope it helps. 1

Calculate the sum of a field filtered by a window defined on another field

两盒软妹~` 提交于 2019-12-12 04:56:14
问题 I have table event : event_date, num_events, site_id I can easily use aggregate SQL to do SELECT SUM(num_events) GROUP BY site_id . But I also have another table site : site_id, target_date I'd like to do a JOIN, showing the SUM of num_events within 60 days of the target_date , 90 days, 120 days, etc. I thought this could easily be done using a WHERE clause in the aggregate SQL. However, this is complicated by two challenges: The target_date is not fixed, but varies for each site_id I'd like

R aggregate list of dataframe

烈酒焚心 提交于 2019-12-12 04:15:55
问题 I do have a list of 53 data frames (purchase01 to purchase53), ordered by date, with 18 variables and distinct number of rows (tryed, but coulden't paste a example on the following). I want to aggregate each distinct data frame by its repeated values of "V9"- factor- by sum the column "V2"- numeric. I coulden't find the answer yet. For only one dataframe I can use aggregate.data.frame(purchase00$V12, by = list(purchase00($V9),FUN = sum) and itjust works fine. I tryed llply, llply(.data =

Get records whose count matches max(count) for a category

↘锁芯ラ 提交于 2019-12-12 04:05:24
问题 Given the following rows of course,section,grade,count of grades within course section: course SECTION grade gradeCount ----------------------------------- 1301 001 C 3 1301 001 C+ 3 1301 001 C- 4 1301 001 D 5 1301 001 D+ 3 1301 001 D- 2 1301 001 F 18 1301 002 A- 1 1301 002 B 1 1301 002 B- 3 1301 002 C 2 I want to get a list of course/sections with the greatest number of each grade. For example: Grade|Course|Section|Count A | 1301| 023 | 75 // 1301-023 had the most A's, 75 of them B | 1301|

String aggregate using a variable

谁说我不能喝 提交于 2019-12-12 03:27:28
问题 Is is okay to use a variable to concatenate a value from several rows ( as an implicit aggregate function )? It seems to work fine on my machine, but I haven't seen it recommended. declare @v_str varchar(4000) = '' select top 5 @v_str = @v_str + ',' + city_name from city_table order by city_name print @v_str 回答1: From nvarchar concatenation / index / nvarchar(max) inexplicable behavior "The correct behavior for an aggregate concatenation query is undefined." 来源: https://stackoverflow.com