aggregate-functions

How to order a table by aggregate function in a ruby?

痴心易碎 提交于 2019-12-11 17:53:21
问题 I have 2 attributes opening_date and closing_date in a Tender model. On the index page what I display is the remaining date which is ( closing_date-Date.today ). However, remaining date is not saved in a database but I wanted to order and display the data in the table by remaining date. What should I use? Is there an aggregate function for that? I am using Ruby 1.9.2 and Rails 3.2.2 //TendersController def index @tenders= Tender.where("company_id= ? ", current_user.company.id).order('closing

MYSQL fetch 10 posts, each w/ vote count, sorted by vote count, limited by where clause on posts

天大地大妈咪最大 提交于 2019-12-11 17:33:28
问题 I want to fetch a set of Posts w/ vote count listed, sorted by vote count (e.g.) Post 1 - Post Body blah blah - Votes: 500 Post 2 - Post Body blah blah - Votes: 400 Post 3 - Post Body blah blah - Votes: 300 Post 4 - Post Body blah blah - Votes: 200 I have 2 tables: Posts - columns - id , body , is_hidden Votes - columns - id , post_id , vote_type_id Here is the query I've tried: SELECT p.*, v.yes_count FROM posts p LEFT JOIN (SELECT post_id, vote_type_id, COUNT(1) AS yes_count FROM votes

Using Collect_set after exploding in a groupedBy object in Pyspark

江枫思渺然 提交于 2019-12-11 17:31:55
问题 I have a data-frame which has schema like this : root |-- docId: string (nullable = true) |-- field_a: array (nullable = true) | |-- element: string (containsNull = true) |-- field_b: array (nullable = true) | |-- element: string (containsNull = true) I want to perform a groupBy on field_a and use collect_set to keep all the distinct values (basically inner values in the list) in the field_b in aggregation, I don't want to add a new column by exploding field_b and then do collect_set in

Using aggregation function to filter record based on MIN timestamp

允我心安 提交于 2019-12-11 17:12:27
问题 SELECT * FROM ABC_CUSTOMER_DETAILS abc_detail INNER JOIN ABC_CUSTOMERS abc_cust ON abc_detail.ID=abc_cust.CUSTOMER_ID WHERE abc_detail.COUNTRY_CODE='KE' AND CREATION_TIMESTAMP=(SELECT MIN (CREATION_TIMESTAMP) FROM ABC_CUSTOMER_DETAILS abc_detail INNER JOIN ABC_CUSTOMERS abc_cust ON abc_detail.ID=abc_cust.CUSTOMER_ID WHERE abc_detail.COUNTRY_CODE='KE'); Above script query join record from ABC_CUSTOMER_DETAILS to ABC_CUSTOMERS nd select thw one with earliest timestamp. Anyway if I able not to

Subquery aggregate function with SUM(CASE SUBQUERY)

只愿长相守 提交于 2019-12-11 17:08:35
问题 I'm getting an error while executing my query Cannot perform an aggregate function on an expression containing an aggregate or a subquery. Code: SELECT S.id, SUM(CASE WHEN sc.coverage IN (SELECT number FROM ArrayOfIntegersFromString(@dynamicData)) THEN 1 ELSE 0 END) as sm FROM Storefronts s LEFT JOIN StorefrontCoverages sc ON s.id = sc.storefront LEFT JOIN Vendors v ON s.vendor = v.Id WHERE ( v.active = 1 AND s.approved = 1 AND s.status = 1 ) GROUP BY S.id HAVING SUM(CASE WHEN sc.coverage IN

Custom aggregate function

霸气de小男生 提交于 2019-12-11 15:31:39
问题 I am trying to understand aggregate functions and I need help. So for instance the following sample: CREATE OR REPLACE FUNCTION array_median(timestamp[]) RETURNS timestamp AS $$ SELECT CASE WHEN array_upper($1,1) = 0 THEN null ELSE asorted[ceiling(array_upper(asorted,1)/2.0)] END FROM (SELECT ARRAY(SELECT ($1)[n] FROM generate_series(1, array_upper($1, 1)) AS n WHERE ($1)[n] IS NOT NULL ORDER BY ($1)[n] ) As asorted) As foo ; $$ LANGUAGE 'sql' IMMUTABLE; CREATE AGGREGATE median(timestamp) (

Group by ID except the NULL records

让人想犯罪 __ 提交于 2019-12-11 14:49:14
问题 I am working with the next table: **ID Value Data** 1 30 25/4 1 20 26/4 1 20 27/4 3 10 25/4 4 20 26/4 5 30 26/4 NULL 50 25/4 NULL 10 26/4 And I need to query the table and have the next result: **ID Value Data** 1 70 25/4 3 10 25/4 4 20 26/4 5 30 26/4 NULL 50 25/4 NULL 10 26/4 I have this query: select id, sum(value), min(data) from t group by id; But the query sums the NULL IDs How can I do that? 回答1: The simplest method is probably union all : select id, sum(value), min(data) from t where

How to aggregrate without using `GROUP BY`?

强颜欢笑 提交于 2019-12-11 13:37:41
问题 I have query like the following SELECT a.*, b.* (SELECT ATTR1, ATTR2, sum(QUANTITY) AS TOTAL_QTY, ATTR3 FROM TABLE_A WHERE ATTR4 > 0 GROUP BY ATTR1, ATTR2, ATTR3) a, TABLE_B b WHERE a.ATTR1 = b.ATTR1 AND a.ATTR2 = b.ATTR2 I need to GROUP BY only ATTR1 to calculate the correct TOTAL_QTY , but the only reason I am grouping other attributes because Oracle requires that if GROUP BY clause is present then all SELECT attributes should be in the GROUP BY clause too. This means every time I need an

Looping in select query

那年仲夏 提交于 2019-12-11 13:19:35
问题 I want to do something like this: select id, count(*) as total, FOR temp IN SELECT DISTINCT somerow FROM mytable ORDER BY somerow LOOP sum(case when somerow = temp then 1 else 0 end) temp, END LOOP; from mytable group by id order by id I created working select: select id, count(*) as total, sum(case when somerow = 'a' then 1 else 0 end) somerow_a, sum(case when somerow = 'b' then 1 else 0 end) somerow_b, sum(case when somerow = 'c' then 1 else 0 end) somerow_c, sum(case when somerow = 'd'

Remove duplicate column after SQL query

▼魔方 西西 提交于 2019-12-11 13:10:08
问题 I have this query but I'm getting two columns of houseid : How do I only get one? SELECT vehv2pub.houseid, vehv2pub.vehid, vehv2pub.epatmpg, dayv2pub.houseid, dayv2pub.trpmiles FROM vehv2pub, dayv2pub WHERE vehv2pub.vehid >= 1 AND dayv2pub.trpmiles < 15 AND dayv2pub.houseid = vehv2pub.houseid; And also, how do I get the average of the epatmpg ? So the query would just return the value? 回答1: The most elegant way would be to use the USING clause in an explicit join condition: SELECT houseid, v