window-functions

pandas rolling() function with monthly offset

一个人想着一个人 提交于 2021-02-20 09:27:25
问题 I'm trying to use the rolling() function on a pandas data frame with monthly data. However, I dropped some NaN values, so now there are some gaps in my time series. Therefore, the basic window parameter gives a misleading answer since it just looks at the previous observation: import pandas as pd import numpy as np import random dft = pd.DataFrame(np.random.randint(0,10,size=len(dt)),index=dt) dft.columns = ['value'] dft['value'] = np.where(dft['value'] < 3,np.nan,dft['value']) dft = dft

MySQL 8 Calculating Average by Partitioning By Date

隐身守侯 提交于 2021-02-19 09:12:23
问题 I've setup a fiddle here: https://www.db-fiddle.com/f/snDGExYZgoYASvWkDGHKDC/2 But also: Schema: CREATE TABLE `scores` ( `id` bigint unsigned NOT NULL AUTO_INCREMENT, `shift_id` int unsigned NOT NULL, `employee_name` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL, `score` double(8,2) unsigned NOT NULL, `created_at` timestamp NOT NULL, PRIMARY KEY (`id`) ); INSERT INTO scores(shift_id, employee_name, score, created_at) VALUES (1, "John", 6.72, "2020-04-01 00:00:00"), (1, "Bob", 15.71, "2020

MySQL 8 Calculating Average by Partitioning By Date

醉酒当歌 提交于 2021-02-19 09:09:50
问题 I've setup a fiddle here: https://www.db-fiddle.com/f/snDGExYZgoYASvWkDGHKDC/2 But also: Schema: CREATE TABLE `scores` ( `id` bigint unsigned NOT NULL AUTO_INCREMENT, `shift_id` int unsigned NOT NULL, `employee_name` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL, `score` double(8,2) unsigned NOT NULL, `created_at` timestamp NOT NULL, PRIMARY KEY (`id`) ); INSERT INTO scores(shift_id, employee_name, score, created_at) VALUES (1, "John", 6.72, "2020-04-01 00:00:00"), (1, "Bob", 15.71, "2020

MySQL 8 Calculating Average by Partitioning By Date

本秂侑毒 提交于 2021-02-19 09:05:39
问题 I've setup a fiddle here: https://www.db-fiddle.com/f/snDGExYZgoYASvWkDGHKDC/2 But also: Schema: CREATE TABLE `scores` ( `id` bigint unsigned NOT NULL AUTO_INCREMENT, `shift_id` int unsigned NOT NULL, `employee_name` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL, `score` double(8,2) unsigned NOT NULL, `created_at` timestamp NOT NULL, PRIMARY KEY (`id`) ); INSERT INTO scores(shift_id, employee_name, score, created_at) VALUES (1, "John", 6.72, "2020-04-01 00:00:00"), (1, "Bob", 15.71, "2020

MySQL dense rank for each group/partition

岁酱吖の 提交于 2021-02-19 08:57:39
问题 I am trying to build a dense rank kind of functionality but not exactly dense rank. I want to rank each group but within each group I want to keep the rank same for same values. I've written the following query which gives me a dense rank kind of functionality SELECT id, item_code @curRank := @curRank + 1 AS rank FROM table t, (SELECT @curRank := 0) r WHERE <several filters> Whereas, I want this The closest question I found is this one MySQL give a rank to each group Please help. 回答1: You can

Error: invalid input syntax for integer: “”

£可爱£侵袭症+ 提交于 2021-02-18 11:12:14
问题 I have this table tbl_buku : id_buku judul_buku tahun_buku 1 Bioogi 2010 2 Fisika 2010 3 Informatika 2012 4 Kimia 2012 I use query like this, but I am getting an error: select case when t1.tahun_buku=t2.tahun_buku then '' else t1.tahun_buku end tahun_buku,t1.judul_buku from tbl_buku t1 left join tbl_buku t2 on t1.id_buku-1=t2.id_buku; I want to show table like this: tahun_buku judul_buku 2010 Biologi Fisika 2012 Informatika Kimia How to achieve this? 回答1: I think the problem in your query is

Error: invalid input syntax for integer: “”

為{幸葍}努か 提交于 2021-02-18 11:12:07
问题 I have this table tbl_buku : id_buku judul_buku tahun_buku 1 Bioogi 2010 2 Fisika 2010 3 Informatika 2012 4 Kimia 2012 I use query like this, but I am getting an error: select case when t1.tahun_buku=t2.tahun_buku then '' else t1.tahun_buku end tahun_buku,t1.judul_buku from tbl_buku t1 left join tbl_buku t2 on t1.id_buku-1=t2.id_buku; I want to show table like this: tahun_buku judul_buku 2010 Biologi Fisika 2012 Informatika Kimia How to achieve this? 回答1: I think the problem in your query is

Select only partial result but get total number of rows

南楼画角 提交于 2021-02-11 18:22:43
问题 I got stuck on SQL subquery selection. Right now, I have a table products: id | name | description ----+-------+---------------------- 6 | 123 | this is a + | | girl. 7 | 124 | this is a + | | girl. 8 | 125 | this is a + | | girl. 9 | 126 | this is a + | | girl. 10 | 127 | this is a + | | girl. 11 | 127 | this is a + | | girl. Isn't this? 12 | 345 | this is a cute slair 13 | ggg | this is a + | | girl 14 | shout | this is a monster 15 | haha | they are cute 16 | 123 | this is cute What I want

Running difference month over month

╄→гoц情女王★ 提交于 2021-02-11 12:30:15
问题 I have a sample data, i want to get the Difference in month over month data 'Lag' column for only row B 回答1: If there always is just one row per month and id , then just use lag() . You can wrap this in a case expression so it only applies to id 'B' . select id, date, data, case when id = 'B' then data - lag(data) over(partition by id order by date) end lag_diff from mytable 来源: https://stackoverflow.com/questions/62160736/running-difference-month-over-month

Postgres: which index to add

雨燕双飞 提交于 2021-02-11 12:24:26
问题 I have a table mainly used by this query (only 3 columns are in use here, meter , timeStampUtc and createdOnUtc , but there are other in the table), which starts to take too long: select rank() over (order by mr.meter, mr."timeStampUtc") as row_name , max(mr."createdOnUtc") over (partition by mr.meter, mr."timeStampUtc") as "createdOnUtc" from "MeterReading" mr where "createdOnUtc" >= '2021-01-01' order by row_name ; (this is the minimal query to show my issue. It might not make too much