group-by

SQL Count Grouping by a sequence of numbers

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-23 04:35:09
问题 i have a SQL table like this: id pNum ----- -------- 100 12 100 13 100 15 100 16 100 17 200 18 200 19 300 20 300 21 300 25 and i want to group by the id and the pNum sequences, and count the number of rows. having a result like this. id res ----- -------- 100 2 100 3 200 2 300 2 300 1 any idea on how to do it? 回答1: If your DBMS supports window functions (e.g. SQL Server 2005+) SELECT id, count(*) AS res FROM (SELECT *, [pNum] - ROW_NUMBER() OVER (PARTITION BY [id] ORDER BY [pNum]) AS Grp FROM

SQL Count Grouping by a sequence of numbers

Deadly 提交于 2019-12-23 04:35:08
问题 i have a SQL table like this: id pNum ----- -------- 100 12 100 13 100 15 100 16 100 17 200 18 200 19 300 20 300 21 300 25 and i want to group by the id and the pNum sequences, and count the number of rows. having a result like this. id res ----- -------- 100 2 100 3 200 2 300 2 300 1 any idea on how to do it? 回答1: If your DBMS supports window functions (e.g. SQL Server 2005+) SELECT id, count(*) AS res FROM (SELECT *, [pNum] - ROW_NUMBER() OVER (PARTITION BY [id] ORDER BY [pNum]) AS Grp FROM

Group results after join?

别说谁变了你拦得住时间么 提交于 2019-12-23 04:15:23
问题 So, I have three tables. Movies , movies_genres and genres . I want to get a movie by its Id, and also join its genres in the result. I managed to join the results, but it doesn't display as i want it to. I'm not sure if what I'm asking is possible. This is my query: SELECT `movies`.*, GROUP_CONCAT(genres.id) AS genre_id, GROUP_CONCAT(genres.name) AS genre_name FROM (`movies`) INNER JOIN `movies_genres` ON `movies_genres`.`movie_id` = `movies`.`id` INNER JOIN `genres` ON `genres`.`id` =

PostgreSQL get relative average with group by

家住魔仙堡 提交于 2019-12-23 03:17:17
问题 I have a table as follows. The rows are in a specific order. id | value ------+--------------------- 1 | 2 1 | 4 1 | 3 2 | 2 2 | 2 2 | 5 I would want to group the rows by the column 'id' and get the average of value displayed in each column in terms of the previous values of the column (As explained in the following example within brackets) id | value | RelativeAverage ------+-------------+-------------------- 1 | 2 | (2/1) = 2 1 | 4 | (2+4 /2) = 3 1 | 3 | (2+4+3 / 3) = 3 2 | 2 | (2/1) = 2 2

GROUP BY timestamp every 15 minutes including missing entries

大城市里の小女人 提交于 2019-12-22 23:14:43
问题 I want to group all reports from a given table named 'reports' by every 15 minutes, including intervals where no reports where filled. Example: id timestamp --------------------------- 1 2015-04-16 20:52:04 2 2015-04-16 20:53:04 3 2015-04-16 20:54:04 4 2015-04-16 19:52:04 5 2015-04-17 22:24:56 6 2015-04-17 22:27:09 7 2015-04-18 06:48:41 After the select query I should have: timestamp count ---------------------- 20:52:04 3 21:07:04 0 21:22:04 0 21:37:04 0 21:52:04 0 22:07:04 0 22:22:04 2 22

Aggregating in SQL

做~自己de王妃 提交于 2019-12-22 18:42:57
问题 I have a table that looks like this: Conversion_Date User_Name Last_Date_Touch Touch_Count 7/15/2017 A 6/17/2017 1 7/16/2017 B 6/24/2017 2 7/19/2017 A 6/20/2017 1 7/19/2017 C 6/29/2017 1 I want to get the sum of Touch_Count from the last 30 days for each Conversion_Date by User_Name in SQL. The only thing I could think of at first was to figure out the time window for each conversion date going back 30 days and then I'm not sure how to aggregate this to get the final result. First it was

Count of consecutive not null values

南楼画角 提交于 2019-12-22 18:40:53
问题 The count should be 3 and 1 in the following query. The count should be of the points earned consecutively. So once the user fails to earn any points, the count restarts. mysql> select name, count(*) from sortest group by name, (points = 0) OR (points is NULL) having name= 'john'; +------+----------+ | name | count(*) | +------+----------+ | john | 4 | | john | 2 | +------+----------+ 2 rows in set (0.00 sec) mysql> select * from sortest; +----+------+--------+ | id | name | points | +----+--

Python numpy split a csv file by the values of a string column

ぐ巨炮叔叔 提交于 2019-12-22 18:14:50
问题 I have 5000 rows of data that looks like the following in a csv file, I would like to group by the last column 6 (ie. A, B) using numpy arrays, as I would be plotting data in each group afterwards. Title Date, Time, Value1, Value2, Value3, Value4, Value5 ,, Unit1, Unit2, Unit3,, 2012-04-02,00:00, 85.5333333333333, 4.87666666666667, 8.96, 323.27,A 2012-04-02,00:30, 196.5, 5.49, 8.42, 323.15,B 2012-04-02,01:00, 68.2, 4.47, 7.83, 325.30,A 2012-04-02,01:30, 320.9, 6.77333333333333, 8.05, 326.63,B

Trying to display 2 tables data

倖福魔咒の 提交于 2019-12-22 13:47:53
问题 I have 2 tables Table name Dist . NAME|Phone|ID ---------------- Oakley|555-555|1 Maui|666-666|2 lux|777-7777|3 Table name Patientacct . Name|prescription|id|orderfrom ------------------------------- bob|20-20|1|oakley billy|15-20|2|Oakley, Maui kim|20-20|3|Lux I'm looking for a display like Oakley -------------------- Bob Billy Maui ------------------ Billy Lux -------------- kim Trials so far SELECT * FROM `dist` JOIN `patientacct` ON patientacct.dist LIKE CONCAT('%', `dist`.name ,'%')

LINQ GROUP BY and MAX()

与世无争的帅哥 提交于 2019-12-22 10:25:46
问题 I'm trying to find out how to write an SQL sentence in LINQ but I can't find a way to do it for the moment, this is the SQL command: SELECT cs.Site_Name, MAX(ed.EffectiveDate_Date) FROM [WAPMaster].[Factsheets].[EffectiveDate] ed, [WAPMaster].[Configuration].[Site] cs WHERE cs.Site_Id = ed.EffectiveDate_SiteId GROUP BY cs.Site_Name Can someone help me witht he linq syntax please? **I'm trying this so far (thanks levanlevi) var test = (from e in this._wapDatabase.EffectiveDates join c in this.