aggregate-functions

sql conditional aggregate function with date type

点点圈 提交于 2019-12-12 03:26:27
问题 I have table like: CREATE TABLE myissues ( id int IDENTITY(1,1) primary key, title varchar(20), status varchar(30), submitdate datetime, updatedate datetime ); INSERT INTO myissues (title, status,submitdate,updatedate) VALUES ('issue1', 'closed','2014-01-01 07:59:59.000','2014-01-02 10:59:59.000'), ('issue2', 'closed','2014-01-01 08:59:59.000','2014-01-02 12:59:59.000'), ('issue3', 'closed','2014-01-01 09:59:59.000','2014-01-02 10:59:59.000'), ('issue4', 'closed','2014-01-02 07:59:59.000',

SQL: COUNT() grouped results

家住魔仙堡 提交于 2019-12-12 02:41:57
问题 this is my current query: SELECT locationname, eventid FROM events GROUP BY locationname ORDER BY locationname ASC As you can see it's grouped by locationname because there are several rows with the same locationname. In the output i just need a list of "distinct" (grouped) locations but behind every location there should be the total amount of each location. So if in "events" are 4 locationsname with "Congress Center NYC" the output should be "Congress Center NYC (4)". Is there a way to

SQL Server CLR Aggregate: Serialize a Dictionary?

孤街浪徒 提交于 2019-12-12 02:33:10
问题 I'm writing a custom CLR Aggregate in C# to run in SQL Server 2008. I want to hold most of my SqlUserDefinedAggregate instances' states in Dictionaries, so I can't use the builtin serialization. How can I go about using UserDefined serialization with the Read() and Write() methods? 回答1: Using keys and values of types that BinaryReader and BinaryWriter handle, Write each Dictionary out to the BinaryWriter in key0,value0...keyN-1,valueN-1 order. Prefix this sequence with Dictionary's Count .

Perpetual Weighted Average cost Calculation SQL Server 2008

时光总嘲笑我的痴心妄想 提交于 2019-12-12 02:26:52
问题 I want to calculate Perpetual Weighted Average cost for inventory. I tried to understand a solution on following link Inventory Average Cost Calculation in SQL but cannot get it, it is complex for me. here is my database details. Purch_ID Item_ID QTY Unit_Price 01 1 10 10 02 2 10 20 03 3 20 15 04 2 10 20 05 1 10 18 06 2 25 17 I want to calculate the Weighted Average cost after each Purchase using following formula ((old_stock x Old unit price)+(New_Purchase_qty x New unit price))/(old stock

MySQL - Order by values in separate table

这一生的挚爱 提交于 2019-12-12 00:25:00
问题 I have two MySQL tables. Table1: id name otherid Table2: id otherid time In each table, the "id" is a unique identifier for every row (and is also the primary key) the "otherids" correspond between tables. There may be several (or 0) rows in table2 that have an otherid that corresponds to an otherid in table1. Table For example, Table 1: id name otherid 1 bob 24 2 joe 326 Table 2: id otherid time 4 326 2014-03-12 023:38:59 5 24 2013-02-11 22:05:21 6 24 2013-02-10 13:27:58 I am looking to

ActiveRecord aggregate function: is there a database-agnostic 'EXTRACT(WEEK from date)'?

纵然是瞬间 提交于 2019-12-11 22:24:25
问题 i have a method that works just fine (with rails 3 and PostgreSQL) ; i just wonder if it's possible to make it database-agnostic : FarmGatePrice.average :price, :group => 'EXTRACT(WEEK FROM date)' As i understand it, methods to extract a week ISO number from a date are always database-specific. I could use something like a virtual attribute :week as seen here, but it seems the :group options only accepts raw SQL. I've also seen this solution but it doesn't fit my needs. Another way would be

Sum a column that corresponds a aggregate from a column of another table

余生长醉 提交于 2019-12-11 22:17:14
问题 Sorry about the title, but i couldn't think of a better subject for my issue. I've two tables in a many-to-one relationship between them and an other many-to-one relatioshop in the same table. I need to sum a column that corresponds a aggregate from a column of first table. Here is a image that illustrate what i want: I have no idea how to proceed with this. 回答1: This should do: SELECT ISNULL(E.idParent,E.id) Id, SUM(I.Value) [Sum] FROM EXPENSE_TABLE E LEFT JOIN INVOICE_TABLE I ON I.idExpense

Given time/interval to calculate open/high/low/close value in each grouped data

旧城冷巷雨未停 提交于 2019-12-11 20:22:32
问题 Suppose raw data is: Timestamp High Low Volume 10:24.22345 100 99 10 10:24.23345 110 97 20 10:24.33455 97 89 40 10:25.33455 60 40 50 10:25.93455 40 20 60 With a sample time of 1 second, the output data should be as following (they are grouped by second): Timestamp Open Close High Low Volume 10:24 82 83 110 89 70 10:25 50 40 60 20 110 Open means the price of the earliest data in the group Close means the price of the lastest data in the group Volume means the sum(Volume) in the group The

Best way to get aggregate function result inside the entity object

血红的双手。 提交于 2019-12-11 19:42:51
问题 Many time I have to get the SQL aggregate query result inside the Entity Object itself. As of now I could able to achive the same by the following code CriteriaBuilder cb = em.getCriteriaBuilder(); CriteriaQuery<Tuple> q = cb.createTupleQuery(); Root<Test> c = q.from(Test.class); q.multiselect(c, cb.count(c)); q.groupBy(c.get("type")); TypedQuery<Tuple> t = em.createQuery(q); List<Tuple> resultList = t.getResultList(); List<Test> list = new ArrayList<>(); for(Tuple tuple : resultList){ Test

Hive - over (partition by …) with a column not in group by

情到浓时终转凉″ 提交于 2019-12-11 19:06:55
问题 Is it possible to do something like: select avg(count(distinct user_id)) over (partition by some_date) as average_users_per_day from user_activity group by user_type (notably, the partition by column, some_date , is not in the group by columns) The idea I'm going for is something like: the average users per day by user type . I know how to do it using subqueries (see below), but I'd like to know if there is a nice way using only over (partition by ...) and group by . Notes: From reading this