aggregate-functions

group by first character

為{幸葍}努か 提交于 2019-11-27 13:28:27
I have a problem with a query in Oracle SQL. I have a first_name column in an employees table. I want to group my records according to the first character in first_name . For example, I have 26 records, one with name = 'Alice' , one with name = 'Bob' , and so on down the alphabet for each name's first character. After the query, there should be 26 groups with one employee each. I tried the following, but it's not working: SELECT employee_id, (SUBSTR(first_name,1,1)) AS alpha FROM employees GROUP BY alpha; name_which_starts_from employees A 10 B 2 C 4 D 9 E 3 G 3 H 3 I 2 J 16 K 7 L 6 M 6 N 4 O

MySQL: How to SUM() a TIMEDIFF() on a group?

别说谁变了你拦得住时间么 提交于 2019-11-27 12:25:09
问题 So I've got a set of results that looks something like this: SELECT User_ID, StartTime, EndTime, TIMEDIFF(EndTime, StartTime) AS TimeDiff FROM MyTable ------------------------------------------------------------------ | User_ID | StartTime | EndTime | TimeDiff | ------------------------------------------------------------------ | 1 | 2010-11-05 08:00:00 | 2010-11-05 09:00:00 | 01:00:00 | ------------------------------------------------------------------ | 1 | 2010-11-05 09:00:00 | 2010-11-05

Extra Fields with SQL MIN() & GROUP BY

好久不见. 提交于 2019-11-27 12:13:00
问题 When using the SQL MIN() function, along with GROUP BY, will any additional columns (not the MIN column, or one of the GROUP BY columns) match the data in the matching MIN row? For example, given a table with department names, employee names, and salary: SELECT MIN(e.salary), e.* FROM employee e GROUP BY department Obviously I'll get two good columns, the minimum salary and the department. Will the employee name (and any other employee fields) be from the same row? Namely the row with the MIN

count without group

試著忘記壹切 提交于 2019-11-27 12:05:23
问题 I have one table named GUYS(ID,NAME,PHONE) and i need to add a count of how many guys have the same name and at the same time show all of them so i can't group them. example: ID NAME PHONE 1 John 335 2 Harry 444 3 James 367 4 John 742 5 John 654 the wanted output should be ID NAME PHONE COUNT 1 John 335 3 2 Harry 444 1 3 James 367 1 4 John 742 3 5 John 654 3 how could i do that? i only manage to get lot of guys with different counts. thanks 回答1: Use an aggregate Query: select g.ID, g.Name, g

Aggregate SQL Function to grab only the first from each group

血红的双手。 提交于 2019-11-27 11:55:50
I have 2 tables - an Account table and a Users table. Each account can have multiple users. I have a scenario where I want to execute a single query/join against these two tables, but I want all the Account data (Account.*) and only the first set of user data (specifically their name). Instead of doing a "min" or "max" on my aggregated group, I wanted to do a "first". But, apparently, there is no "First" aggregate function in TSQL. Any suggestions on how to go about getting this query? Obviously, it is easy to get the cartesian product of Account x Users: SELECT User.Name, Account.* FROM

Count cumulative total in Postgresql

℡╲_俬逩灬. 提交于 2019-11-27 10:43:57
I am using count and group by to get the number of subscribers registered each day: SELECT created_at, COUNT(email) FROM subscriptions GROUP BY created at; Result: created_at count ----------------- 04-04-2011 100 05-04-2011 50 06-04-2011 50 07-04-2011 300 I want to get the cumulative total of subscribers every day instead. How do I get this? created_at count ----------------- 04-04-2011 100 05-04-2011 150 06-04-2011 200 07-04-2011 500 With larger datasets, window functions are the most efficient way to perform these kinds of queries -- the table will be scanned only once, instead of once for

MySQL dynamic cross tab

不问归期 提交于 2019-11-27 09:34:03
I have a table like this: way stop time 1 1 00:55 1 2 01:01 1 3 01:07 2 2 01:41 2 3 01:47 2 5 01:49 3 1 04:00 3 2 04:06 3 3 04:12 and I want a table like this: stop way_1 way_2 way_3 (way_n) 1 00:55 04:00 2 01:01 01:41 04:06 3 01:07 01:47 04:12 5 01:49 There are many solutions online about MySQL cross tab (pivot table), but how can I do this if I don't know how many "way" are there? Thanks The number and names of columns must be fixed at the time you prepare the query. That's just the way SQL works. So you have two choices of how to solve this. Both choices involve writing application code: (1

Group DateTime into 5,15,30 and 60 minute intervals

六月ゝ 毕业季﹏ 提交于 2019-11-27 08:41:12
I am trying to group some records into 5-, 15-, 30- and 60-minute intervals: SELECT AVG(value) as "AvgValue", sample_date/(5*60) as "TimeFive" FROM DATA WHERE id = 123 AND sample_date >= 3/21/2012 i want to run several queries, each would group my average values into the desired time increments. So the 5-min query would return results like this: AvgValue TimeFive 6.90 1995-01-01 00:05:00 7.15 1995-01-01 00:10:00 8.25 1995-01-01 00:15:00 The 30-min query would result in this: AvgValue TimeThirty 6.95 1995-01-01 00:30:00 7.40 1995-01-01 01:00:00 The datetime column is in yyyy-mm-dd hh:mm:ss

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

妖精的绣舞 提交于 2019-11-27 08:40:37
问题 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 回答1: 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. 回答2: 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

MySQL sum, count with group by and joins

痴心易碎 提交于 2019-11-27 08:05:50
问题 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