group-by

Use percentile_cont with a “group by” statment in T-SQL

我怕爱的太早我们不能终老 提交于 2019-12-21 08:55:25
问题 I'd like to use the percentile_cont function to get median values in T-SQL. However, I also need to get mean values as well. I'd like to do something like the following: SELECT CustomerID , AVG(Expenditure) AS MeanSpend , percentile_cont ( .5) WITHIN GROUP(ORDER BY Expenditure) OVER( ) AS MedianSpend FROM Customers GROUP BY CustomerID Can this be accomplished? I know I can use the OVER clause to group the percentile_cont results... but then I'm stuck using two queries, am I not? 回答1: Just

Use percentile_cont with a “group by” statment in T-SQL

馋奶兔 提交于 2019-12-21 08:54:50
问题 I'd like to use the percentile_cont function to get median values in T-SQL. However, I also need to get mean values as well. I'd like to do something like the following: SELECT CustomerID , AVG(Expenditure) AS MeanSpend , percentile_cont ( .5) WITHIN GROUP(ORDER BY Expenditure) OVER( ) AS MedianSpend FROM Customers GROUP BY CustomerID Can this be accomplished? I know I can use the OVER clause to group the percentile_cont results... but then I'm stuck using two queries, am I not? 回答1: Just

Does “group by” automatically guarantee “order by”?

早过忘川 提交于 2019-12-21 07:39:07
问题 Does "group by" clause automatically guarantee that the results will be ordered by that key? In other words, is it enough to write: select * from table group by a, b, c or does one have to write select * from table group by a, b, c order by a, b, c I know e.g. in MySQL I don't have to, but I would like to know if I can rely on it accross the SQL implementations. Is it guaranteed? 回答1: group by does not order the data neccessarily. A DB is designed to grab the data as fast as possible and only

Alternative for MAX operator on bit fields

…衆ロ難τιáo~ 提交于 2019-12-21 06:57:31
问题 I have table with permissions that has few bit fields. I want to group rows in this table and get result with top permissions. So with this table: UserId, Permisssion1,Permission2, Permisssion3 With this to rows 13,1,0,0 13,0,1,0 I want to get: 13,1,1,0 Problem is that operator MAX doesn't works on bit fields. How to do that in efficient way? (without using CASE) 回答1: As simple as... MAX(CAST(Permisssion1 AS tinyint)) You don't have many other options... 来源: https://stackoverflow.com

Oracle SQL Query to Summarize Statistics, using GROUP BY

做~自己de王妃 提交于 2019-12-21 06:06:41
问题 I have an Oracle table with data that looks like this: ID BATCH STATUS 1 1 0 2 1 0 3 1 1 4 2 0 That is, ID is the primary key, there will be multiple rows for each "batch," and each row will have a status code in the STATUS column. There are a bunch of other columns, but these are the important ones. I need to write a query which will summarize the status codes for each batch ; there are three possible values that can go in the STATUS column, 0, 1, and 2, and I would like output that looks

How to calculate percentage increase from previous row/day after complex Group By?

五迷三道 提交于 2019-12-21 05:44:11
问题 I have a table IntradayPrices1Minute where I store 1 minute timeframe open, high, low and close prices for stocks: CREATE TABLE `IntradayPrices1Minute` ( `ticker` varchar(10) NOT NULL DEFAULT '', `datetime` datetime NOT NULL, `volume` mediumint(11) unsigned NOT NULL, `open` decimal(8,4) unsigned NOT NULL, `high` decimal(8,4) unsigned NOT NULL, `low` decimal(8,4) unsigned NOT NULL, `close` decimal(8,4) unsigned NOT NULL, PRIMARY KEY (`datetime`,`ticker`), UNIQUE KEY `indxTickerDatetime` (

SQL select query using joins, group by and aggregate functions

﹥>﹥吖頭↗ 提交于 2019-12-21 05:21:33
问题 I have two tables with the following fields emp_table: emp_id, emp_name salary_increase: emp_id, inc_date, inc_amount I am required to write a query which gives the employee details, the number of times an employee has received a salary increase, the value of the maximum increase amount and the date of that increase. Here is what i have so far: SELECT e.*, count(i.inc_amount), max(i.inc_amount) FROM salary_increase AS i RIGHT JOIN emp_table AS e ON i.emp_id=e.emp_id GROUP BY e.emp_id; this

pandas how to use groupby to group columns by date in the label?

最后都变了- 提交于 2019-12-21 04:48:17
问题 I have a dataframe 10730 rows × 249 columns, i have columns: Index(['RegionID', 'Metro', 'CountyName', 'SizeRank', '1996-04', '1996-05', '1996-06', '1996-07', '1996-08', '1996-09', ... '2015-11', '2015-12', '2016-01', '2016-02', '2016-03', '2016-04', '2016-05', '2016-06', '2016-07', '2016-08'], dtype='object', length=249) so what i need to do is group the columns by the quarter, jan to march Q1, and so on till Q4(using mean for the values). i know how to group 3 columns for example, but how

Grouping timestamps in MySQL with PHP

别说谁变了你拦得住时间么 提交于 2019-12-21 04:25:08
问题 I want to log certain activities in MySql with a timecode using time() . Now I'm accumulating thousands of records, I want to output the data by sets of hours/days/months etc. What would be the suggested method for grouping time codes in MySQL? Example data: 1248651289 1248651299 1248651386 1248651588 1248651647 1248651700 1248651707 1248651737 1248651808 1248652269 Example code: $sql = "SELECT COUNT(timecode) FROM timecodeTable"; //GROUP BY round(timecode/3600, 1) //group by hour?? Edit:

How to get a list of the grouped values in linq groupby?

时光毁灭记忆、已成空白 提交于 2019-12-21 03:57:42
问题 Linq newbie here, struggling with my first group by query. I have a list of objects of type KeywordInstance which represent a keyword, and the ID of the database record to which the keyword was applied. Keyword RecordID macrophages 1 macrophages 2 cell cycle 3 map kinase 2 cell cycle 1 What I want to have is a collection of all keywords, with a list of the RecordIDs to which each keyword was applied Keyword RecordIDs macrophages 1, 2 cell cycle 1, 3 map kinase 2 I tried using Linq to get it