aggregate-functions

MySQL Join Query (possible two inner joins)

懵懂的女人 提交于 2019-12-10 20:11:17
问题 I currently have the following: Table Town: id name region Table Supplier: id name town_id The below query returns the number of suppliers for each town: SELECT t.id, t.name, count(s.id) as NumSupplier FROM Town t INNER JOIN Suppliers s ON s.town_id = t.id GROUP BY t.id, t.name I now wish to introduce another table in to the query, Supplier_vehicles. A supplier can have many vehicles: Table Supplier_vehicles: id supplier_id vehicle_id Now, the NumSupplier field needs to return the number of

Using a column in sql join without adding it to group by clause

99封情书 提交于 2019-12-10 20:03:02
问题 My actual table structures are much more complex but following are two simplified table definitions: Table invoice CREATE TABLE invoice ( id integer NOT NULL, create_datetime timestamp with time zone NOT NULL, total numeric(22,10) NOT NULL ); id create_datetime total ---------------------------- 100 2014-05-08 1000 Table payment_invoice CREATE TABLE payment_invoice ( invoice_id integer, amount numeric(22,10) ); invoice_id amount ------------------- 100 100 100 200 100 150 I want to select the

Spark: “Cannot use an UnspecifiedFrame. This should have been converted during analysis. Please file a bug report”

余生长醉 提交于 2019-12-10 19:57:57
问题 Spark 2.3.0 with Scala 2.11. I am trying to write a custom aggregator and run it over a window function per these docs but am getting the error in the title. Here is a stripped-down example. This is written as a FunSuite test. I know the error message says to file a bug report, but this is such a simple example lifted almost directly from the documentation that I wonder if there is something in my code that is causing the error. I wonder if using a collection type as the buffer is somehow not

Limit SQL query to only the top two counts per group [duplicate]

可紊 提交于 2019-12-10 19:32:34
问题 This question already has answers here : Get top results for each group (in Oracle) (5 answers) Closed last year . If I have a DB table called FAVORITE_FLAVOR where each row has a user's favorite flavor of ice cream. User ID | Flavor | State 1 | Chocolate | CA 2 | Vanilla | ND 3 | Chocolate | CA 4 | Rocky Road | CA 5 | vanilla | CA 6 | Vanilla | CA 7 | Vanilla | CA Now, if I want to query the 2 most popular flavors in each state (normalizing capitalization and whitespace), I could query:

How to get a nested sub-query to recognize parent query column

房东的猫 提交于 2019-12-10 18:30:59
问题 I have a problem where I'm trying to calculate a sum of values per given id. I decided to do this using sub-queries (typically I'd use a join, but I'm also keeping a counter for each sub-query for clipping purposes - see this question for more info). For the sake of this question, assume I have the following MySQL query: /* 1. */ SELECT /* 2. */ t1.experiement_id, /* 3. */ (SELECT sum(x.size) /* 4. */ FROM (SELECT size, ( @rownum := @rownum + 1 ) AS `rownum` /* 5. */ FROM data AS t0 /* 6. */

Find movies with highest number of awards in certain year - code duplication

♀尐吖头ヾ 提交于 2019-12-10 17:14:55
问题 I am trying to write a query (PostgreSQL) to get "Movies with highest number of awards in year 2012." I have following tables: CREATE TABLE Award( ID_AWARD bigserial CONSTRAINT Award_pk PRIMARY KEY, award_name VARCHAR(90), category VARCHAR(90), award_year integer, CONSTRAINT award_unique UNIQUE (award_name, category, award_year)); CREATE TABLE AwardWinner( ID_AWARD integer, ID_ACTOR integer, ID_MOVIE integer, CONSTRAINT AwardWinner_pk PRIMARY KEY (ID_AWARD)); And I written following query,

Cannot cumulatively sum `COUNT(*)`

牧云@^-^@ 提交于 2019-12-10 17:13:39
问题 The second section of this answer uses variables to create a cumulative sum of another column. I'm doing the same thing, except that I am using a GROUP BY statement, and summing COUNT(*) instead of a column. Here is my code to create a minimal table and insert values: CREATE TABLE `test_group_cumulative` ( `id` int(11) unsigned NOT NULL AUTO_INCREMENT, `group_id` int(11) unsigned NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=latin1; INSERT INTO `test_group_cumulative` (`id`,

how to create this query

余生长醉 提交于 2019-12-10 16:59:53
问题 how to create a query if i need to include two aggregate function in select row and per each function i need different group by and where conditions in my example i need to returns the playerName, and how many the player win the this can be checked if the results in table game result= first, and how many times he played but do not know how to deal with two aggregate functions . simply i want to join the result of this two queries 1. select playeName,count(*) from player,game where player

Does MySQL use indexes on Having?

纵然是瞬间 提交于 2019-12-10 16:12:49
问题 A portion of my query looks like: HAVING date > '2011-04-13 04:28:03' The date variable is indexed, does this have any effect on the query? EXPLAIN EXTENDED doesn't seem to be using the index, but I don't know if that's only because I have 4 rows in the database that I'm testing with. My query: SELECT AVG(slen) FROM ( SELECT date, COUNT(id) as slen FROM table WHERE product_id = 2830 GROUP BY id HAVING date > '2011-04-13 04:28:02' ) as T There are a few rows that have different date values. I

Why do you need to include a field in GROUP BY when using OVER (PARTITION BY x)?

流过昼夜 提交于 2019-12-10 15:05:25
问题 I have a table for which I want to do a simple sum of a field, grouped by two columns. I then want the total for all values for each year_num. See example: http://rextester.com/QSLRS68794 This query is throwing: "42803: column "foo.num_cust" must appear in the GROUP BY clause or be used in an aggregate function", and I cannot figure out why. Why would an aggregate function using the OVER (PARTITION BY x) require the summed field to be in GROUP BY?? select year_num ,age_bucket ,sum(num_cust) -