aggregate-functions

Find row with maximum value of id in MySQL

别等时光非礼了梦想. 提交于 2020-01-13 10:53:31
问题 Take a look at the MySQL table below called "Articles": +----+-----------+---------+------------------------+--------------------------+ | id | articleId | version | title | content | +----+-----------+---------+------------------------+--------------------------+ | 1 | 1 | 0.0 | ArticleNo.1 title v0.0 | ArticleNo.1 content v0.0 | | 2 | 1 | 1.0 | ArticleNo.1 title v1.0 | ArticleNo.1 content v1.0 | | 3 | 1 | 1.5 | ArticleNo.1 title v1.5 | ArticleNo.1 content v1.5 | | 4 | 1 | 2.0 | ArticleNo.1

Count each condition within group

空扰寡人 提交于 2020-01-13 08:27:23
问题 For every unique GroupId I would like to get a count of each IsGreen , IsRound , IsLoud condition and a total number of rows. Sample data: ----------------------------------------------------- id | ItemId | GroupId | IsGreen | IsRound | IsLoud ----+--------+---------+---------+---------+--------- 1 | 1001 | 1 | 0 | 1 | 1 2 | 1002 | 1 | 1 | 1 | 0 3 | 1003 | 2 | 0 | 0 | 0 4 | 1004 | 2 | 1 | 0 | 1 5 | 1005 | 2 | 0 | 0 | 0 6 | 1006 | 3 | 0 | 0 | 0 7 | 1007 | 3 | 0 | 0 | 0 Desired result: --------

including missing (zero-count) rows when using GROUP BY

主宰稳场 提交于 2020-01-13 03:48:30
问题 I have an application that receives sms messages. What i want to do is make a statistic with mysql that will count meessages in a hour. For example in 7 am i received 10 sms messages, in 8 am i received 20 etc. My table has this columns ID, smsText, smsDate ... (others are not important). When i run this script: SELECT HOUR(smsDate), COUNT(ID) FROM SMS_MESSAGES GROUP BY HOUR(smsDate) it show how many messages i get in every hour. The problem is when i dont receive any message for example in

How to minus instead of add in a Sum() like sql function [closed]

三世轮回 提交于 2020-01-12 09:53:33
问题 It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 11 years ago . I have a group by clause in a sql statement and need to use an aggregate function to minus all the values in each group instead of adding like the Sum() function. i.e. SELECT Sum(A) FROM ( SELECT 2 AS A UNION

Ordering distinct column values by (first value of) other column in aggregate function

爷,独闯天下 提交于 2020-01-11 00:51:11
问题 I'm trying to order the output order of some distinct aggregated text based on the value of another column with something like: string_agg(DISTINCT sometext, ' ' ORDER BY numval) However, that results in the error: ERROR: in an aggregate with DISTINCT, ORDER BY expressions must appear in argument list I do understand why this is, since the ordering would be "ill-defined" if the numval of two repeated values differs, with that of another lying in-between. Ideally, I would like to order them by

How to SUM and SUBTRACT using SQL?

流过昼夜 提交于 2020-01-09 09:58:33
问题 I am using MySQL and I have two tables: master_table ORDERNO ITEM QTY stock_bal ITEM BAL_QTY Master table has duplicate ORDERNO and ITEM values. I have get total QTY using SQL 'GROUP BY' clause. I need to deduct/subtract BAL_QTY from SUM of ITEM (master_table). I've got SUM QTY value using query (actually there are many rows). 回答1: I think this is what you're looking for. NEW_BAL is the sum of QTY s subtracted from the balance: SELECT master_table.ORDERNO, master_table.ITEM, SUM(master_table

How to SUM and SUBTRACT using SQL?

时光怂恿深爱的人放手 提交于 2020-01-09 09:58:07
问题 I am using MySQL and I have two tables: master_table ORDERNO ITEM QTY stock_bal ITEM BAL_QTY Master table has duplicate ORDERNO and ITEM values. I have get total QTY using SQL 'GROUP BY' clause. I need to deduct/subtract BAL_QTY from SUM of ITEM (master_table). I've got SUM QTY value using query (actually there are many rows). 回答1: I think this is what you're looking for. NEW_BAL is the sum of QTY s subtracted from the balance: SELECT master_table.ORDERNO, master_table.ITEM, SUM(master_table

Is there a way to do aggregate functions on Google App Engine?

旧街凉风 提交于 2020-01-09 07:55:06
问题 One of the nice things relational databases support are the aggregate functions like count, sum, avg etc. But it seems that if you are using GAE, when inserting or updating a record you must calculate and store the count, sum, avg, etc. values of the whole table. But what if you have many conditional groupings? Given a Person: class Person { @Id Integer age; String city; } If I want the total number of persons and the average age Is it correct that everytime I create, update or delete a

mySQL: sum by a different group-by

强颜欢笑 提交于 2020-01-07 02:23:27
问题 I have a table: (Edited with x5) _date(2014-03-05;2014-04-05) _period(1,2,3,4,5,1,2,3,4,5,1,2; 1,2,3,4,5,1,2,3,4,5) _id(x1,x1,x1,x1,x1,x2,x2,x2,x2,x2,x3,x3;x4,x4,x4,x4,x4,x5,x5,x5,x5,x5) _reading(1,1,1,1,1,2,2,2,2,2,5,6;1,1,1,1,1,2,1,1,1,1) _reading2 (5,5,5,5,5,4,4,4,4,4,7,7;1,1,1,1,1,1,1,1,1,1) image of the table 1 i need a result like below which eliminates all data which doesn't have full period 1 to 5 for the _id, then calculates the sum of _reading for a day and sum of _reading2 for each

Concatenate rows in select query (in Advantage Data Architect)

安稳与你 提交于 2020-01-06 19:41:31
问题 How can I concatenate rows in select query? (in Advantage Data Architect) I tried run the following scripts: The first script: declare @str string; set @str = ''; select @str = @str + field_1 from my_table1 But I get a result where all rows contain "false", like this picture: Second script: declare @str string; select @str = coalesce(@str + ', ','') + field_1 from my_table1 This time, all rows are empty (note: the field from "my_table1" is not null). Picture: I tried to search the solution on