aggregate-functions

MySQL only within the current month?

时间秒杀一切 提交于 2019-11-30 02:58:45
问题 I have the following MySQL query, and I'm trying to adjust it so it only fetches results which are within the current month (of the current year), I'm guessing you may require more info about my MySQL structure so here it goes - I have a UNIX timestamp generated by PHP's time() stored within the time column (under the referrals table), so with the below setup it would be t2.time . So my problem is I'm unsure how to proceed, I'm guessing it would be something like adding the following to end

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

纵饮孤独 提交于 2019-11-30 02:00:39
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) class Consumption(models.Model): food = models.ForeignKey("Food") user = models.ForeignKey("User") I want

COUNT CASE and WHEN statement in MySQL

六月ゝ 毕业季﹏ 提交于 2019-11-30 01:26:53
How to use COUNT CASE and WHEN statement in MySQL query, to count when data is NULL and when it is not NULL in one MySQL query? Use: SELECT SUM(CASE WHEN t.your_column IS NULL THEN 1 ELSE 0 END) AS numNull, SUM(CASE WHEN t.your_column IS NOT NULL THEN 1 ELSE 0 END) AS numNotNull FROM YOUR_TABLE t That will sum up the column NULL & not NULL for the entire table. It's likely you need a GROUP BY clause, depending on needs. 来源: https://stackoverflow.com/questions/5045124/count-case-and-when-statement-in-mysql

How to update fields that is the aggregate result of another table in MySQL?

只谈情不闲聊 提交于 2019-11-30 01:01:42
问题 UPDATE a JOIN b ON a.app_id=b.app_id GROUP BY a.app_id SET remark_avg=AVG(b.score),remark_count=COUNT(b.id); The above is basically what I want to do,but it's not a valid MySQL statement,how to write it correctly? 回答1: UPDATE a INNER JOIN (SELECT AVG(b.score) avg_score, COUNT(b.id) cnt_id, b.app_id FROM b GROUP BY b.app_id) x ON x.app_id = a.app_id SET remark_avg = x.avg_score, remark_count = x.cnt_id; 来源: https://stackoverflow.com/questions/5752075/how-to-update-fields-that-is-the-aggregate

Difference between scalar, table-valued, and aggregate functions in SQL server?

两盒软妹~` 提交于 2019-11-29 22:41:43
What is the difference between scalar-valued, table-valued, and aggregate functions in SQL server? And does calling them from a query need a different method, or do we call them in the same way? Scalar Functions Scalar functions (sometimes referred to as User-Defined Functions / UDFs) return a single value as a return value, not as a result set, and can be used in most places within a query or SET statement, except for the FROM clause (and maybe other places?). Also, scalar functions can be called via EXEC , just like Stored Procedures, though there are not many occasions to make use of this

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

自闭症网瘾萝莉.ら 提交于 2019-11-29 20:17:30
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 located in New York, Egon Spengler would drop out and the next one (Sarah Connor / Los Angeles) would come in

Example of the Scala aggregate function

╄→尐↘猪︶ㄣ 提交于 2019-11-29 18:57:30
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 , and combop ? I'm unclear on what these parameters do. Didier Dupont The aggregate function does not do

Is Aggregate fatally flawed because each into clause is executed separately?

烂漫一生 提交于 2019-11-29 18:25:29
问题 Is VB.NET's Aggregate query fatally flawed when used as the first (outer) clause of a Linq expression with multiple Into clauses because each Into clause is executed separately? The "obvious" answer to SELECT MIN(ZoneMin), MAX(ZoneMin) FROM Plant in LINQ to SQL is Dim limits = Aggregate p In Plants Select p.ZoneMin Into Min(), Max() However, this answer actually retrieves each of Min and Max (and if you include other aggregate functions like Count and Average ) in separate SQL queries. This

Sum totals of two queries

只愿长相守 提交于 2019-11-29 18:16:15
问题 I have two basic queries which I need to sum the totals of: Select hours, sum(hours) FROM table WHERE name='xxx' and Description='Worked' Select hours2, sum(hours2) FROM table WHERE name='xxx' and Description2='Worked' I've tried UNION and it will get me the totals of each query but it will not combine them. Table setup is: ID name hours description hours2 description2 I need to correlate hours to description and hours2 to description2 which is why I have the two different queries. I need to

Is it possible to use Aggregate function in a Select statment without using Group By clause?

烈酒焚心 提交于 2019-11-29 17:44:36
问题 So far I have written Aggregate function followed by Group By clause to find the values based on SUM, AVG and other Aggregate functions. I have a bit confusion in the Group By clause. When we use Aggregate functions what are the columns I need to specify in the Group By clause. Otherwise Is there any way to use Aggregate functions without using Group By clause. 回答1: All columns in the SELECT clause that do not have an aggregate need to be in the GROUP BY Good: SELECT col1, col2, col3, MAX