gaps-and-islands

how to fetch continuous occurrence count of a column value in sql?

 ̄綄美尐妖づ 提交于 2019-12-25 09:04:32
问题 I have added one more id column for the order purpose, if the table is like this +-----+-------+ | id | cs_id | +-----+-------+ | 1 | a | | 2 | b | | 3 | a | | 4 | a | | 5 | a | | 6 | b | | 7 | b | | 8 | b | | 9 | b | +-----+-------+ i want the continuous occurrence of cs_id order by id column +-----+-------+--------------------------------- | id | cs_id | continuous_occurrence_cs_id +-----+-------+---------------------------------| | 1 | a | 1 | 2 | b | 1 | 3 | a | 1 | 4 | a | 2 | 5 | a | 3

sum two rows and order by date / total

痞子三分冷 提交于 2019-12-25 01:46:18
问题 need some help to build a query, this is my current scheme: users: +----+------------+ | id | username | +----+------------+ | 1 | rob | | 2 | john | | 3 | jane | <--- jane never has donated | 4 | mike | +----+------------+ donations: +--------------------+------------+ | uid | amount | date | +---------+----------+------------+ | 1 | 20 | 2013-10-10 | | 2 | 5 | 2013-10-03 | | 2 | 50 | 2013-09-25 | | 2 | 5 | 2013-10-01 | | 4 | 100 | 2012-10-01 | <-- past year +---------+----------+-----------

SQL Date rows for inactivity days

人盡茶涼 提交于 2019-12-24 08:48:10
问题 Let's say there is this table which stores the number of visitors for each day. When I want to query the table and create a graph from it a problem arises. The days without activity have no corresponding rows on the table. For example Day1 - 7 Day2 - 8 Day4 - 7 And the graph generated would not be correct. Since it needs a 0 for Day3. Now, without using anything other than SQL is it possible to create those values for the inactivity days? I thought of creating another table which would create

Generating all realized and unrealized trades using sql?

痞子三分冷 提交于 2019-12-24 08:25:42
问题 PROBLEM: Given some positions in different financial securities, I would like to apply trades to them up to a given date. In doing so I would like to keep track which trades closed the position and opened a new one. The position is CLOSED when the Position Quantity becomes 0 after a trade is applied. A position is considered OPENED when the Position Quantity changes from 0 to something. SETUP: SQL DEMO SOURCE Let's say I have the following tables: CREATE TABLE tPosition ( SecNum INT, Position

SQL: Gaps and Island Problem - Date not consecutive causing rank inaccurate

霸气de小男生 提交于 2019-12-24 06:22:58
问题 This is a follow up on the other question I asked. Quarter Segment Customer *Counter* Q1 2018 A A1 1 Q2 2018 A A1 2 Q3 2018 A A1 3 Q4 2018 B A1 1 Q1 2019 B A1 2 Q2 2019 A A1 1 Q1 2020 A A1 *1* I want 1 not 2 here because it's not consecutive (we don't have Q3 & Q4 2019) Q2 2020 A A1 *2* I want 2 not 3 here because it reset in Q1 2020. Below query works if the dates are consecutive. How would I adjust the query to get what I'm looking for? I tried adding a new column that is 1 row lag, and

SQL: Gaps and Island Problem - Date not consecutive causing rank inaccurate

与世无争的帅哥 提交于 2019-12-24 06:20:05
问题 This is a follow up on the other question I asked. Quarter Segment Customer *Counter* Q1 2018 A A1 1 Q2 2018 A A1 2 Q3 2018 A A1 3 Q4 2018 B A1 1 Q1 2019 B A1 2 Q2 2019 A A1 1 Q1 2020 A A1 *1* I want 1 not 2 here because it's not consecutive (we don't have Q3 & Q4 2019) Q2 2020 A A1 *2* I want 2 not 3 here because it reset in Q1 2020. Below query works if the dates are consecutive. How would I adjust the query to get what I'm looking for? I tried adding a new column that is 1 row lag, and

counting islands in R csv

[亡魂溺海] 提交于 2019-12-24 05:04:48
问题 I would like to count islands along rows in a .csv. I say "islands" meaning consecutive non-blank entries on rows of the .csv. If there are three non-blank entries in a row, I would like that to be counted as 1 island. Anything less than three consecutive entries in a row counts as 1 "non-island". I would then like to write the output to a dataframe: Name,,,,,,,,,,,,, Michael,,,1,1,1,,,,,,,, Peter,,,,1,1,,,,,,,,, John,,,,,1,,,,,,,,, Desired dataframe output: Name,island,nonisland, Michael,1,0

How to select sequential duplicates in SQL Server

泄露秘密 提交于 2019-12-24 02:44:09
问题 I would like to select duplicate entries from a SQL Server table, but only if the id is consecutive. I have been trying to twist this answer to my needs, but I can't get it to work. The above answer is for Oracle, but I see that SQL Server also has lead and lag functions. Also, I think that the answer above puts a * next to duplicates, but I only want to select the duplicates. select id, companyName, case when companyName in (prev, next) then '*' end match, prev, next from (select id,

SQL Server 2008 R2 - Islands and Gaps [closed]

北战南征 提交于 2019-12-24 00:52:16
问题 It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 6 years ago . I have a simple 2 column table. PctGain contains weekly percentage stock market gains. WSeqkey contains a contiguous integer value that increments each new week. There are approximately 3300 rows in the above

How to find groups of sequential integers in Oracle?

匆匆过客 提交于 2019-12-23 05:33:05
问题 I am using oracle 10g. My (simplified) table definition is CREATE TABLE Student ("Rno" INT PRIMARY KEY) ; Which contains the following rows | RNO | |-----| | 1 | | 2 | | 3 | | 6 | | 8 | | 9 | | 12 | | 13 | | 14 | | 18 | How can I produce the following result set? | RESULT | |------------------------| | 1-3, 6, 8-9, 12-14, 18 | 回答1: You can use SELECT LISTAGG("Txt", ', ') WITHIN GROUP (ORDER BY "Rno") "Result" FROM (SELECT CASE WHEN MIN("Rno") = MAX("Rno") THEN CAST(MIN("Rno") AS VARCHAR2(11))