aggregate-functions

How to group mysql rows with same column value into one row?

牧云@^-^@ 提交于 2019-11-28 17:11:39
I have two tables, keywords and data. Table keywords have 2 columns (id, keyword), table data have 3 columns (id[foreign key of keywords.id], name, value). I am using this query: SELECT k.id, d.value, d.name FROM keywords AS k INNER JOIN data as d ON k.id = d.id it returns something like: 1 123 name1 1 456 name2 2 943 name1 3 542 name1 3 532 name2 3 682 name3 Each id can have values from 0 to 3 (maybe more in the future). How can I retrieve all the rows with the same id in the same row? Like 1 123 456 2 943 3 542 532 682 I want to do this because I want to be able to sort the values. Use GROUP

MySQL: Select N rows, but with only unique values in one column

混江龙づ霸主 提交于 2019-11-28 16:48:06
问题 Given this data set: ID Name City Birthyear 1 Egon Spengler New York 1957 2 Mac Taylor New York 1955 3 Sarah Connor Los Angeles 1959 4 Jean-Luc Picard La Barre 2305 5 Ellen Ripley Nostromo 2092 6 James T. Kirk Riverside 2233 7 Henry Jones Chicago 1899 I need to find the 3 oldest persons, but only one of every city. If it would just be the three oldest, it would be... Henry Jones / Chicago Mac Taylor / New York Egon Spengler / New York However since both Egon Spengler and Mac Taylor are

How to average/sum data in a day in SQL Server 2005

妖精的绣舞 提交于 2019-11-28 14:52:31
I'm trying to average data in SQL Server 2005 in a day. Here is what my database look like this if I use simple query as SELECT timestamp, FEED FROM ROASTER_FEED ORDER timestamp Data: timestamp Feed 02/07/2011 12:00:01 1246 02/07/2011 12:00:01 1234 02/07/2011 12:00:01 1387 02/07/2011 12:00:02 1425 02/07/2011 12:00:03 1263 ... 02/07/2011 11:00:01 1153 02/07/2011 11:00:01 1348 02/07/2011 11:00:01 1387 02/07/2011 11:00:02 1425 02/07/2011 11:00:03 1223 .... 03/07/2011 12:00:01 1226 03/07/2011 12:00:01 1245 03/07/2011 12:00:01 1384 03/07/2011 12:00:02 1225 03/07/2011 12:00:03 1363 I don't know how

Oracle aggregate for joining strings with comma, and about writing custom aggregates

大城市里の小女人 提交于 2019-11-28 14:20:38
How can i create a function on Oracle like sum, min, max? it will run on result set. Select join(',', Name), sum(total) From Account Group By IdCity Here is a link to the documentation that describes how you can create your own aggregate functions: http://download.oracle.com/docs/cd/E11882_01/appdev.112/e10765/aggr_functions.htm Regards, Rob. Use wmsys.wm_concat if Oracle version is 10 or more. You can also do a lookup on creating aggregate functions in Oracle. Note that SQLite has the group_concat builtin aggregate if maybe you were interested in it. Check out the listagg function . It's a

Example of the Scala aggregate function

自古美人都是妖i 提交于 2019-11-28 14:15:18
问题 I have been looking and I cannot find an example or discussion of the aggregate function in Scala that I can understand. It seems pretty powerful. Can this function be used to reduce the values of tuples to make a multimap-type collection? For example: val list = Seq(("one", "i"), ("two", "2"), ("two", "ii"), ("one", "1"), ("four", "iv")) After applying aggregate: Seq(("one" -> Seq("i","1")), ("two" -> Seq("2", "ii")), ("four" -> Seq("iv")) Also, can you give example of parameters z , segop ,

MySQL sum, count with group by and joins

旧时模样 提交于 2019-11-28 14:05:26
I have three tables types, post and insights. Types table contains the types of post. post table contains the post that have been made. the insight table contains the insights of post on daily basis. Here is the link to my sql fiddle SQL Fiddle . Now i want to generate a report which contains number of post against each type and the sum of their likes and comments i.e. Type | COUNT(post_id) | SUM(likes) | SUM(comments). These are my tries: select type_name, count(p.post_id), sum(likes), sum(comments) from types t left join posts p on t.type_id = p.post_type left join insights i on p.post_id =

SQL query to sum the data

夙愿已清 提交于 2019-11-28 13:12:04
I have my table data as follows TaxTypeCode1 TaxTypeCode2 PNO Amount ----------------------------------------- TX01 TX02 124 600 TX02 null 124 700 TX03 TX04 124 200 TX04 null 124 300 TX05 TX06 126 400 TX06 null 127 500 TX07 null 128 800 I would like to write SQL query to retrieve data. Conditions apply IF pno is same and TaxTypeCode1 contain TaxTypeCode2 then sum the amt, otherwise display actual amt My expected output is PNO Amount --------------- 124 1300 124 500 126 400 127 500 128 800 124 has 1300 because pno is same and TaxTypeCode2 (TX02) TaxTypeCode1 (TX02) are same then sum TX01 (TX02)

Query to ORDER BY the number of rows returned from another SELECT

大兔子大兔子 提交于 2019-11-28 13:11:10
I'm trying to wrap my head around SQL and I need some help figuring out how to do the following query in PostgreSQL 9.3. I have a users table, and a friends table that lists user IDs and the user IDs of friends in multiple rows. I would like to query the user table, and ORDER BY the number of mutual friends in common to a user ID. So, the friends table would look like: user_id | friend_user_id 1 | 4 1 | 5 2 | 10 3 | 7 And so on, so user 1 lists 4 and 5 as friends, and user 2 lists 10 as a friend, so I want to sort by the highest count of user 1 in friend_user_id for the result of user_id in

Find most common elements in array with a group by

情到浓时终转凉″ 提交于 2019-11-28 12:47:06
I have a table of rows with the following structure name TEXT, favorite_colors TEXT[], group_name INTEGER where each row has a list of everyone's favorite colors and the group that person belongs to. How can I GROUP BY group_name and return a list of the most common colors in each group? Could you do a combination of int[] && int[] to set for overlap, int[] & int[] to get the intersection and then something else to count and rank? Quick and dirty: SELECT group_name, color, count(*) AS ct FROM ( SELECT group_name, unnest(favorite_colors) AS color FROM tbl ) sub GROUP BY 1,2 ORDER BY 1,3 DESC;

replace NULL values with latest non-NULL value in resultset series (SQL Server 2008 R2)

瘦欲@ 提交于 2019-11-28 11:19:51
for SQL Server 2008 R2 I have a resultset that looks like this (note [price] is numeric, NULL below represents a NULL value, the result set is ordered by product_id and timestamp) product timestamp price ------- ---------------- ----- 5678 2008-01-01 12:00 12.34 5678 2008-01-01 12:01 NULL 5678 2008-01-01 12:02 NULL 5678 2008-01-01 12:03 23.45 5678 2008-01-01 12:04 NULL I want to transform that to a result set that (essentially) copies a non-null value from the latest preceding row, to produce a resultset that looks like this: product timestamp price ------- ---------------- ----- 5678 2008-01