aggregate-functions

How to sort by annotated Count() in a related model in Django

 ̄綄美尐妖づ 提交于 2019-12-17 22:23:59
问题 I'm building a food logging database in Django and I've got a query related problem. I've set up my models to include (among other things) a Food model connected to the User model through an M2M-field "consumer" via the Consumption model. The Food model describes food dishes and the Consumption model describes a user's consumption of Food (date, amount, etc). class Food(models.Model): food_name = models.CharField(max_length=30) consumer = models.ManyToManyField("User", through=Consumption)

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

∥☆過路亽.° 提交于 2019-12-17 22:10:10
问题 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

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

假装没事ソ 提交于 2019-12-17 21:25:42
问题 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

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

风流意气都作罢 提交于 2019-12-17 20:32:48
问题 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

Precedence of a mysql session variable value in an sql statement

ε祈祈猫儿з 提交于 2019-12-17 20:27:41
问题 What is the standard behaviour of a session variable when used in an SQL statement. Case 1 : In the following example, session variable is behaving as expected. mysql> set @m1=0, @m2=0, @m3=0; Query OK, 0 rows affected (0.00 sec) mysql> mysql> select -> @m1 := 55 m1, @m2 := 42 m2, @m3 := 66 m3, -> @m1, @m2, @m3, -> @b1 := greatest( @m1, @m2, @m3 ) b1, -> @b2 := ( ( @total := @m1 + @m2 + @m3 ) -> - ( @b1 + least( @m1, @m2, @m3 ) )) b2, -> @total total; +----+----+----+------+------+------+----

Why does the following join increase the query time significantly?

落花浮王杯 提交于 2019-12-17 17:05:35
问题 I have a star schema here and I am querying the fact table and would like to join one very small dimension table. I can't really explain the following: EXPLAIN ANALYZE SELECT COUNT(impression_id), imp.os_id FROM bi.impressions imp GROUP BY imp.os_id; QUERY PLAN -------------------------------------------------------------------------------------------------------------------------------------- HashAggregate (cost=868719.08..868719.24 rows=16 width=10) (actual time=12559.462..12559.466 rows=26

SQL Query: Return Max value record of a Group

我的梦境 提交于 2019-12-17 14:57:04
问题 I have a sample table with similar structure & data as shown below: +------+---------+-------------+------------+ | S_ID | S_NAME | SUBJECT | MARK_VALUE | +------+---------+-------------+------------+ | 1 | Stud | SUB_1 | 50 | | 2 | Stud | SUB_2 | 60 | | 3 | Stud | SUB_3 | 70 | | 4 | Stud_1 | SUB_1 | 40 | | 5 | Stud_1 | SUB_2 | 50 | | 6 | Stud_2 | SUB_2 | 40 | +------+---------+-------------+------------+ Table has consolidated mark of each student in all subjects each that student has

How to combine aggregates within a group with aggregates across groups within SSRS

不羁岁月 提交于 2019-12-17 12:46:20
问题 With this dataset: Category | Amount A | 5 A | 3 B | 6 B | 2 B | 1 C | 7 I want to create a tablix grouping on category, displaying the percentage of the total amount: Category | Percentage A | 33% B | 38% C | 29% Which should be a simple calculation: Category | Percentage A | ((Sum of Amount within group / Sum of Amount across groups) * 100)% B | ((Sum of Amount within group / Sum of Amount across groups) * 100)% C | ((Sum of Amount within group / Sum of Amount across groups) * 100)% But I

group by first character

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-17 10:47:54
问题 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

Aggregate a single column in query with many columns

自作多情 提交于 2019-12-17 06:56:27
问题 Is there a proper way to aggregate a single column when I have many other columns in the query? I've tried this answer which works, but my query has become a lot more verbose. My current query looks like this: SELECT t1.foo1, t1.foo2, t2.foo3, t2.foo4, string_agg(t3.aggregated_field, ', ') FROM tbl1 t1 LEFT JOIN tbl2 t2 ON t1.id = t2.fkeyid LEFT JOIN tbl3 t3 ON t2.id = t3.fkeyid GROUP BY t1.foo1, t1.foo2, t2.foo3, t2.foo4, t2.foo5, t2.foo6 ORDER BY t2.foo5, t2.foo6 The query has many more