aggregate-functions

How to count NULL values in MySQL?

[亡魂溺海] 提交于 2019-12-02 22:53:54
I want to know how can i find all the values that are NULL in the MySQL database for example I'm trying to display all the users who don't have an average yet. Here is the MySQL code. SELECT COUNT(average) as num FROM users WHERE user_id = '$user_id' AND average IS_NULL SELECT COUNT(*) as num FROM users WHERE user_id = '$user_id' AND average IS NULL A more generic version (that doesn't depend on the where clause and hence limits your overall results): SELECT SUM(CASE WHEN average IS NULL THEN 1 ELSE 0 END) As null_num, SUM(CASE WHEN average IS NOT NULL THEN 1 ELSE 0 END) AS not_null_num FROM

Does PHP have a function for aggregating time values from an array?

僤鯓⒐⒋嵵緔 提交于 2019-12-02 22:05:55
问题 So my query is like this: SELECT TIMEDIFF('24:00:00',(TIMEDIFF('22:00:00',TIME(end)))) AS time FROM sworkingtime WHERE user='magdalena' AND type='work' AND start BETWEEN '2014-03-01' AND '2014-03-28' AND (HOUR(`end`) > 22 OR HOUR(`end`) < 4) AND completed=1 My query returns this: 02:02:36 03:17:24 03:07:03 02:24:17 03:14:09 Is there a way to modify this query to return only SUM of this fields, so one field only? Thank you 回答1: No, there's no special sum function in this case - especially

Postgres - aggregate two columns into one item

北慕城南 提交于 2019-12-02 22:03:42
I would like to aggregate two columns into one "array" when grouping. Assume a table like so: friends_map: ================================= user_id friend_id confirmed ================================= 1 2 true 1 3 false 2 1 true 2 3 true 1 4 false I would like to select from this table and group by user_id and get friend_id and confirmed as a concatenated value separated by a comma. Currently I have this: SELECT user_id, array_agg(friend_id) as friends, array_agg(confirmed) as confirmed FROM friend_map WHERE user_id = 1 GROUP BY user_id which gets me: ================================= user

How to use a SQL window function to calculate a percentage of an aggregate

☆樱花仙子☆ 提交于 2019-12-02 19:30:26
I need to calculate percentages of various dimensions in a table. I'd like to simplify things by using window functions to calculate the denominator, however I am having an issue because the numerator has to be an aggregate as well. As a simple example, take the following table: create temp table test (d1 text, d2 text, v numeric); insert into test values ('a','x',5), ('a','y',5), ('a','y',10), ('b','x',20); If I just want to calculate the share of each individual row out of d1, then windowing functions work fine: select d1, d2, v/sum(v) over (partition by d1) from test; "b";"x";1.00 "a";"x";0

Multiple aggregate functions in one query using Linq to SQL [duplicate]

谁说胖子不能爱 提交于 2019-12-02 19:19:16
问题 This question already has answers here : Return multiple aggregate columns in LINQ (4 answers) Closed 2 years ago . I'm trying to achieve the following in LINQ (EF6): SELECT count(A), sum(B), average(C) FROM TableA, LEFT JOIN TableB ON ... LEFT JOIN TableC ON ... WHERE (very complicated conditions) The C# code looks like following: IQueryable<Entity> = dbSet .Include(e => e.entityB) .Include(e => e.EntityC) .Where( <very complicated conditions> ); How can I apply multiple aggregate functions

Pad arrays with NULL to maximum length for custom aggregate function

烂漫一生 提交于 2019-12-02 12:32:57
问题 From the answer of the question How to use array_agg() for varchar[], We can create a custom aggregate function to aggregate n-dimensional arrays in Postgres like: CREATE AGGREGATE array_agg_mult (anyarray) ( SFUNC = array_cat ,STYPE = anyarray ,INITCOND = '{}' ); A constrain is that the values have to share the same array extents and same length , handling empty values and different lengths doesn't work. From the answer: There is no way around that, the array type does not allow such a

Parse Date from MM/DD/YYYY to a format MYSQL can work with

戏子无情 提交于 2019-12-02 11:03:46
Currently working with a MLS data dump in which all their dates are formatted in MM/DD/YYYY , Trying to get the min/max age MIN( DateDiff( NOW(), idx_common.list_date ))) AS listage_min, MAX( DateDiff( NOW(), idx_common.list_date ))) AS listage_max Obviously this does not work because idx_common.list_date is in said format. Any ideas? Use str_to_date to convert idx_common.list_date . MIN( DateDiff( NOW(), str_to_date(idx_common.list_date,'%m/%d/%Y') ))) AS listage_min, MAX( DateDiff( NOW(), str_to_date(idx_common.list_date,'%m/%d/%Y') ))) AS listage_max http://dev.mysql.com/doc/refman/5.5/en

counting mysql values

本小妞迷上赌 提交于 2019-12-02 08:59:40
I have a table with two fields, TAG_ID and TAG_DESC my primary is tag_id. I'm looking for a query that will display each TAG_DESC and the amount of times it occurs next to it. Thanks, Ledhund SELECT TAG_DESC as 'Description', COUNT(TAG_DESC) as 'Occurances' FROM table_name GROUP BY TAG_DESC select TAG_DESC, count(*) from MyTable group by TAG_DESC 来源: https://stackoverflow.com/questions/3808062/counting-mysql-values

Update using a subquery with aggregates and groupby in Postgres

拥有回忆 提交于 2019-12-02 08:46:27
问题 I'm trying to update a column in a table with the max value of that column grouped by another column. So for example, say we have a table named transactions with two columns: quantity and item_name . And for whatever reason we want to set quantity equal to the maximum quantity found for each item_name . I'm pretty stumped and bad at doing things like this in SQL, but here's what I have so far: UPDATE transactions SET quantity = subquery.quantity FROM (select max(quantity), item_name from

Multiple aggregate functions in one query using Linq to SQL [duplicate]

纵饮孤独 提交于 2019-12-02 08:25:21
This question already has an answer here: Return multiple aggregate columns in LINQ 4 answers I'm trying to achieve the following in LINQ (EF6): SELECT count(A), sum(B), average(C) FROM TableA, LEFT JOIN TableB ON ... LEFT JOIN TableC ON ... WHERE (very complicated conditions) The C# code looks like following: IQueryable<Entity> = dbSet .Include(e => e.entityB) .Include(e => e.EntityC) .Where( <very complicated conditions> ); How can I apply multiple aggregate functions on different fields? Specifically, in a way, which won't cause the complicated conditions to be copied over and over in