window-functions

Increment value on column every N records on table

别等时光非礼了梦想. 提交于 2021-02-10 18:32:28
问题 I need to increment +1 every 4 records over a table column, I've tried to use ROW_NUM() but my dirty workaround does not make sense. This is what I need: Index PeriodID 1 1 1 2 1 3 1 4 2 5 2 6 2 7 2 8 PeriodID is the primary key (clustered index) for table "Periods", I've heard about window functions LAG() and LEAD() but not sure if I can apply the concept for this scenario, the following syntax is my failed dirty trick attempt: select row_number() over (order by periodid)/4+1, periodid from

Oracle SQL lead lag across different group

这一生的挚爱 提交于 2021-02-08 08:30:31
问题 Suppose I have the below table. They key is just concat P1, P2, P3. I want to compare between key for each day. for example, from day 1 to day2, abc is removed and abe, aby is added. P1 P2 P3 DAY KEY a b c 1 abc a b e 2 abe a b y 2 aby a b x 3 abx a b c 3 abc Expected result set: KEY OPERATION DAY abc ADD 1 abe ADD 2 aby ADD 2 abc REMOVE 2 abx ADD 3 abc ADD 3 abe REMOVE 3 aby REMOVE 3 And what if the day is not sequential. For example: P1 P2 P3 DAY KEY a b c 1 abc a b e 2 abe a b y 2 aby a b

LAG is not a recognized built in function name

一世执手 提交于 2021-02-07 17:24:11
问题 I have a script that creates the following stored procedure : CREATE PROCEDURE [dbo].[GetDurationFree] @EquipmentName varchar(50) AS UPDATE dbo.EquipmentMessages SET UnlockDuration = (SELECT DATEDIFF (SECOND, (SELECT TOP 1 LAG(TimeUnlock) OVER (ORDER BY TimeUnlock) TimeUnlock FROM dbo.EquipmentMessages WHERE EquipmentName = @EquipmentName ORDER BY TimeLock DESC), (SELECT TOP 1 TimeLock FROM dbo.EquipmentMessages WHERE EquipmentName = @EquipmentName ORDER BY TimeLock DESC))) WHERE TimeLock =

LAG is not a recognized built in function name

笑着哭i 提交于 2021-02-07 17:23:29
问题 I have a script that creates the following stored procedure : CREATE PROCEDURE [dbo].[GetDurationFree] @EquipmentName varchar(50) AS UPDATE dbo.EquipmentMessages SET UnlockDuration = (SELECT DATEDIFF (SECOND, (SELECT TOP 1 LAG(TimeUnlock) OVER (ORDER BY TimeUnlock) TimeUnlock FROM dbo.EquipmentMessages WHERE EquipmentName = @EquipmentName ORDER BY TimeLock DESC), (SELECT TOP 1 TimeLock FROM dbo.EquipmentMessages WHERE EquipmentName = @EquipmentName ORDER BY TimeLock DESC))) WHERE TimeLock =

How do I order groups by each group's highest value

╄→гoц情女王★ 提交于 2021-02-05 12:14:19
问题 I have data: query url score a www.google.com 3 a www.facebook.com 2 a www.google.com 1 I want to 'group' entries by their domain, and then order (desc) the domain groups by each group's highest score so I get: query url score a www.google.com 3 a www.google.com 1 a www.facebook.com 2 Trying: select * from table order by score desc, url asc doesnt work. It gives (no apparent change): query url score a www.google.com 3 a www.facebook.com 2 a www.google.com 1 回答1: You can use window functions -

how to execute this “MySQL” query for all values that are in my column “horse” … here this query execture 1 value in my column “horse”

二次信任 提交于 2021-02-05 08:28:07
问题 how to execute a "MySQL" query for all values that are in my column ? Here is my table Table A |----------------------|---------------------|------------------| | id | CL | horse | |----------------------|---------------------|------------------| | 1 | 1er | C.Ferland | | 2 | 5e | Abrivard | | 3 | 3e | P. Hawas | |----------------------|---------------------|------------------| I want the output to be: +------------+--------+---------+---------+-----------+ | horse | Top_1 | Top_2_3 | TOP_4_5

athena presto - multiple columns from long to wide

柔情痞子 提交于 2021-02-04 08:37:25
问题 I am new to Athena and I am trying to understand how to turn multiple columns from long to wide format. It seems like presto is what is needed, but I've only successfully been able to apply map_agg to one variable. I think my below final outcome can be achieved with multimap_agg but cannot quite get it to work. Below I walk through my steps and data. If you have some suggestions or questions, please let me know! First, the data starts like this: id | letter | number | value ------------------

Creating a calculated column based on the flag - TSQL

倖福魔咒の 提交于 2021-01-29 17:43:07
问题 I want to calculate the sum of the orders based on the flag. Sample table: +--------------+---------------+---------------+ | Order | Flag | Amount | +--------------+---------------+---------------+ | Order1 | Yes | 500 | | Order1 | Yes | 325 | | Order2 | Yes | 799 | | Order2 | No | 550 | | Order2 | Yes | 675 | | Order3 | No | 800 | +--------------+---------------+---------------+ I want to create 2 new columns. One column is the total amount of order and another one is the total amount of

How do I transform a data table column from cumulative to difference when reading CSV into spring boot application?

假如想象 提交于 2021-01-29 10:17:43
问题 I have data in a table like date | city | Cumulative total --------------------------------- 1/1/2020 | NYC | 10 1/2/2020 | NYC | 15 1/3/2020 | NYC | 31 1/4/2020 | NYC | 36 1/5/2020 | NYC | 55 . . // more data for NYC continued . 1/1/2020 | BER | 1 1/2/2020 | BER | 5 1/3/2020 | BER | 13 1/4/2020 | BER | 42 1/5/2020 | BER | 45 . . // more data for BER continued . I want this data to not hold the cumulative , but rather hold the difference . Basically I want to subtract the next day from the

MYSQL Stored Procedure - Concat Row Value

拈花ヽ惹草 提交于 2021-01-29 05:19:50
问题 Update Sorry for the delay in responding. I was traveling yesterday when I sent that in. I am on version 5.6. This is what I was thinking of and it does mark when there is change, but the issue is the sort causes the result set to fall out of order. I guess I will need to create a temporary table and just apply the conditional formatting once the order has been set. SET @previousstate = ''; SELECT frm.fi_details.PortfolioCode AS 'Portfolio Code', id, if(@previousstate != frm.fi_details