dense-rank

SQL Query Select first rank 1 row From Multiple ranks/Group

旧巷老猫 提交于 2019-12-06 11:36:28
问题 I have following data Table1 id col1 col2 col3 ---------------------------------- 1 abc 01/01/2012 - 1 abc 01/01/2012 A 2 abc 01/01/2012 - 2 abc 01/02/2012 - 3 abc 01/02/2012 - 3 xyz 01/01/2012 - 4 abc 01/02/2012 - 4 xyz 01/01/2012 - 4 xyz 01/02/2012 - following is order to evaluate - if(col1 is false) then evaluate col2 if(col2 is false) then col3: Col1 - xyz has first preference from all values in this column col2 - min date col3 - not '-' or min(col3) I want to return only one row for each

Islands and Gaps Issue

北慕城南 提交于 2019-12-05 17:57:48
Backstory: I have a database that has data points of drivers in trucks which also contain the. While in a truck, the driver can have a 'driverstatus'. What I'd like to do is group these statuses by driver, truck. As of now, I've tried using LAG/LEAD to help. The reason for this is so I can tell when a driverstatus change occurs, and then I can mark that row as having the last datetime of that status. That in itself is insufficient, because I need to group the statuses by their status and date. For this, I've got something such as DENSE_RANK, but I can't manage to get that right concerning the

Join single row from a table in MySQL

蓝咒 提交于 2019-12-05 12:04:04
I have two tables players and scores . I want to generate a report that looks something like this: player first score points foo 2010-05-20 19 bar 2010-04-15 29 baz 2010-02-04 13 Right now, my query looks something like this: select p.name player, min(s.date) first_score, s.points points from players p join scores s on s.player_id = p.id group by p.name, s.points I need the s.points that is associated with the row that min(s.date) returns. Is that happening with this query? That is, how can I be certain I'm getting the correct s.points value for the joined row? Side note: I imagine this is

SQL Query Select first rank 1 row From Multiple ranks/Group

梦想与她 提交于 2019-12-04 17:24:10
I have following data Table1 id col1 col2 col3 ---------------------------------- 1 abc 01/01/2012 - 1 abc 01/01/2012 A 2 abc 01/01/2012 - 2 abc 01/02/2012 - 3 abc 01/02/2012 - 3 xyz 01/01/2012 - 4 abc 01/02/2012 - 4 xyz 01/01/2012 - 4 xyz 01/02/2012 - following is order to evaluate - if(col1 is false) then evaluate col2 if(col2 is false) then col3: Col1 - xyz has first preference from all values in this column col2 - min date col3 - not '-' or min(col3) I want to return only one row for each id, if col1 fails go to col2, if this fails then go to col3 condition. From above table result should

DENSE_RANK according to particular order

百般思念 提交于 2019-12-04 03:19:22
问题 Hi I have a table of data I want to output the dense_rank of the names starting from the first group of names according to sorted dates order. e.g. DROP TABLE MyTable SELECT * INTO MyTable FROM ( VALUES ('2015-12-23', 'ccc'),('2015-12-21', 'aaa'), ('2015-12-20', 'bbb'),('2015-12-22', 'aaa') ) t (date, name) SELECT DENSE_RANK() OVER (ORDER BY name) rank, * FROM MyTable ORDER BY date For the query above I got rank date name 2 2015-12-20 bbb 1 2015-12-21 aaa 1 2015-12-22 aaa 3 2015-12-23 ccc You

Oracle SQL - DENSE_RANK

半腔热情 提交于 2019-12-01 20:14:22
I have a Client data table, selected columns of which are shown below: Row_ID Client_ID Status_ID From_date To_date 1 123456 4 20/12/2007 18:02 20/12/2007 18:07 2 789087 4 20/12/2007 18:02 20/12/2007 18:07 3 789087 4 20/12/2007 18:07 20/12/2007 18:50 4 789087 4 20/12/2007 18:50 21/12/2007 10:38 5 123456 4 20/12/2007 18:07 20/12/2007 18:50 6 123456 4 20/12/2007 18:50 21/12/2007 10:38 7 123456 4 21/12/2007 10:38 21/12/2007 16:39 8 789087 4 21/12/2007 10:38 21/12/2007 17:54 9 789087 4 21/12/2007 17:54 21/12/2007 18:32 10 789087 4 21/12/2007 18:32 22/12/2007 06:48 11 123456 5 21/12/2007 16:39 12

Convert keep dense_rank from Oracle query into postgres

↘锁芯ラ 提交于 2019-12-01 19:50:28
I'm trying to convert the following Oracle query into Postgres select this_.GLOBAL_TRANSACTION_ID as y0_, this_.BUSINESS_IDENTIFIER as y1_, this_.ENVIRONMENT as y2_, count(*) as y3_, this_.HOST_NAME as y4_, listagg(process, ', ') within group (order by date_time) as process, min(this_.DATE_TIME) as y6_, max(this_.DATE_TIME) as y7_, max(status)keep(dense_rank last order by date_time, decode(status, 'COMPLETED', 'd', 'FAILED', 'c', 'TERMINATED', 'b', 'STARTED', 'a', 'z')) as status from ACTIVITY_MONITOR_TRANSACTION this_ where this_.DATE_TIME between ? and ? and 1=1 group by this_.GLOBAL

Convert keep dense_rank from Oracle query into postgres

老子叫甜甜 提交于 2019-12-01 19:01:30
问题 I'm trying to convert the following Oracle query into Postgres select this_.GLOBAL_TRANSACTION_ID as y0_, this_.BUSINESS_IDENTIFIER as y1_, this_.ENVIRONMENT as y2_, count(*) as y3_, this_.HOST_NAME as y4_, listagg(process, ', ') within group (order by date_time) as process, min(this_.DATE_TIME) as y6_, max(this_.DATE_TIME) as y7_, max(status)keep(dense_rank last order by date_time, decode(status, 'COMPLETED', 'd', 'FAILED', 'c', 'TERMINATED', 'b', 'STARTED', 'a', 'z')) as status from

DENSE_RANK() without duplication

落爺英雄遲暮 提交于 2019-12-01 05:29:39
Here's what my data looks like: | col1 | col2 | denserank | whatiwant | |------|------|-----------|-----------| | 1 | 1 | 1 | 1 | | 2 | 1 | 1 | 1 | | 3 | 2 | 2 | 2 | | 4 | 2 | 2 | 2 | | 5 | 1 | 1 | 3 | | 6 | 2 | 2 | 4 | | 7 | 2 | 2 | 4 | | 8 | 3 | 3 | 5 | Here's the query I have so far: SELECT col1, col2, DENSE_RANK() OVER (ORDER BY COL2) AS [denserank] FROM [table1] ORDER BY [col1] asc What I'd like to achieve is for my denserank column to increment every time there is a change in the value of col2 (even if the value itself is reused). I can't actually order by the column I have denserank on,

implement dense rank with linq

こ雲淡風輕ζ 提交于 2019-11-28 06:34:18
问题 Using the following linq code, how can I add dense_rank to my results? If that's too slow or complicated, how about just the rank window function? var x = tableQueryable .Where(where condition) .GroupBy(cust=> new { fieldOne = cust.fieldOne ?? string.Empty, fieldTwo = cust.fieldTwo ?? string.Empty}) .Where(g=>g.Count()>1) .ToList() .SelectMany(g => g.Select(cust => new { cust.fieldOne , cust.fieldTwo , cust.fieldThree })); 回答1: This does a dense_rank() . Change the GroupBy and the Order