sql

Generate Row Number for every 3 rows

你离开我真会死。 提交于 2021-02-19 06:39:09
问题 I want generate number for every three rows CREATE TABLE #test(period INT) INSERT INTO #test VALUES (602),(603),(604),(605),(606),(607),(608),(609) I know we can generate sequence using row_number window function or while loop or cursor SELECT period, ( Row_number()OVER(ORDER BY period) - 1 ) / 3 + 1 FROM #test Result; +--------+-----+ | period | seq | +--------+-----+ | 602 | 1 | | 603 | 1 | | 604 | 1 | | 605 | 2 | | 606 | 2 | | 607 | 2 | | 608 | 3 | | 609 | 3 | +--------+-----+ Is there any

Indexing Foreign Keys in Postgresql

点点圈 提交于 2021-02-19 05:59:52
问题 Like many Postgres n00bs we have a lot of tables with foreign key constraints that are not indexed. I some cases this should not be a big performance hit - but this would be subject for further analysis. I have read the following article: https://www.cybertec-postgresql.com/en/index-your-foreign-key/ And used the following query to find all foreign keys without an index: SELECT c.conrelid::regclass AS "table", /* list of key column names in order */ string_agg(a.attname, ',' ORDER BY x.n) AS

Select max value in subquery

我只是一个虾纸丫 提交于 2021-02-19 05:47:03
问题 I have these two tables: Student: | name | email | |---------------------|-------------------------| | Arturo Vidal | arturo.vidal@usm.cl | | Bastian Quezada | bastian@usm.cl | | Javier Jeria | javier@usm.cl | | Sebastian Piñera | sebastian@presidente.cl | | Sebastian Gallardo | sebastian@usm.cl | Class: | classId | email | signUpDate | |---------|-------------------------|-------------| | 1 | sebastian@usm.cl | 2018-01-01 | | 1 | javier@usm.cl | 2019-10-01 | | 1 | bastian@usm.cl | 2018-07-01

Allow login to run stored procedure without being able to select from table

非 Y 不嫁゛ 提交于 2021-02-19 05:33:44
问题 I have a SQL Server with two databases: Database1 Database2 Login 'MyLogin' has read access on Database2 only. Database2 has a stored procedure as follows: CREATE PROCEDURE Get_LogData @ProductID int AS BEGIN If Exists (Select (1) From Database2.dbo.Products Where ProductID = @ProductID) Select LogTimeStamp , ProductID , Description from Database1.dbo.Log Where ProductID = @ProductID Else Print 'Get_LogData: Unable to find ProductID ' + CAST(@ProductID AS VARCHAR) END; GO BEGIN GRANT EXEC ON

Optimizing Hive GROUP BY when rows are sorted

北城余情 提交于 2021-02-19 05:31:39
问题 I have the following (very simple) Hive query: select user_id, event_id, min(time) as start, max(time) as end, count(*) as total, count(interaction == 1) as clicks from events_all group by user_id, event_id; The table has the following structure: user_id event_id time interaction Ex833Lli36nxTvGTA1Dv juCUv6EnkVundBHSBzQevw 1430481530295 0 Ex833Lli36nxTvGTA1Dv juCUv6EnkVundBHSBzQevw 1430481530295 1 n0w4uQhOuXymj5jLaCMQ G+Oj6J9Q1nI1tuosq2ZM/g 1430512179696 0 n0w4uQhOuXymj5jLaCMQ G

How do you generate random unique identifiers in a multi process and multi thread environment?

依然范特西╮ 提交于 2021-02-19 05:09:53
问题 Every solution I come up with is not thread save. def uuid(cls,db): u = hexlify(os.urandom(8)).decode('ascii') db.execute('SELECT sid FROM sessions WHERE sid=?',(u,)) if db.fetch(): u=cls.uuid(db) else: db.execute('INSERT INTO sessions (sid) VALUES (?)',(u,)) return u 回答1: Your algorithm is OK (thread safe as far as your DB API module is safe) and probably is the best way to go. It will never give you duplicate (assuming you have PRIMARY or UNIQUE key on sid), but you have a neglectfully

3 Month Moving Average - Redshift SQL

给你一囗甜甜゛ 提交于 2021-02-19 05:06:34
问题 I am trying to create a 3 Month Moving Average based on some data that I have while using RedShift SQL or Domo BeastMode (if anyone is familiar with that). The data is on a day to day basis, but needs to be displayed by month. So the quotes/revenue need to be summarized by month, and then a 3MMA needs to be calculated (excluding the current month). So, if the quote was in April, I would need the average of Jan, Feb, Mar. The input data looks like this: Quote Date MM/DD/YYYY Revenue 3/24/2015

3 Month Moving Average - Redshift SQL

末鹿安然 提交于 2021-02-19 05:04:32
问题 I am trying to create a 3 Month Moving Average based on some data that I have while using RedShift SQL or Domo BeastMode (if anyone is familiar with that). The data is on a day to day basis, but needs to be displayed by month. So the quotes/revenue need to be summarized by month, and then a 3MMA needs to be calculated (excluding the current month). So, if the quote was in April, I would need the average of Jan, Feb, Mar. The input data looks like this: Quote Date MM/DD/YYYY Revenue 3/24/2015

How to flatten a PostgreSQL result

Deadly 提交于 2021-02-19 05:04:28
问题 I have experiments, features, and feature_values. Features have values in different experiments. So I have something like: Experiments: experiment_id, experiment_name Features: feature_id, feature_name Feature_values: experiment_id, feature_id, value Lets say, I have three experiments (exp1, exp2, exp3) and three features (feat1, feat2, feat3). I would like to have a SQL-result that looks like: feature_name | exp1 | exp2 | exp3 -------------+------+------+----- feat1 | 100 | 150 | 110 feat2 |

Find out number of months between 2 dates

做~自己de王妃 提交于 2021-02-19 05:02:55
问题 select (age('2012-11-30 00:00:00'::timestamp, '2012-10-31 00:00:00'::timestamp)), (age('2012-12-31 00:00:00'::timestamp, '2012-10-31 00:00:00'::timestamp)), (age('2013-01-31 00:00:00'::timestamp, '2012-10-31 00:00:00'::timestamp)), (age('2013-02-28 00:00:00'::timestamp, '2012-10-31 00:00:00'::timestamp)) which gives the followings: 0 years 0 mons 30 days 0 hours 0 mins 0.00 secs 0 years 2 mons 0 days 0 hours 0 mins 0.00 secs 0 years 3 mons 0 days 0 hours 0 mins 0.00 secs 0 years 3 mons 28