postgresql

How to use multiple selects in same query without messing everything

冷暖自知 提交于 2021-01-29 09:04:50
问题 So, i have this query SELECT nome_equipa, count(id_golo) AS golos_marcados FROM equipa,golo WHERE golo.equipa_id_equipa = id_equipa GROUP BY golo.equipa_id_equipa, nome_equipa ORDER BY count(id_golo)DESC which outputs this table then i have this other query SELECT nome_equipa, sum(CASE WHEN (resultado_visitado = resultado_visitante) AND jogo.equipa_id_equipa = id_equipa OR (resultado_visitante = resultado_visitado) AND jogo.equipa_id_equipa1 = id_equipa THEN 1 ELSE 0 END) AS empates, sum(CASE

Postgresql group by for multiple lines

℡╲_俬逩灬. 提交于 2021-01-29 09:03:04
问题 I have this table named hr_holidays_by_calendar . I just want to filter out the rows where the same employee is having two leaves in same day . Table hr_holidays_by_calendar : Query I tried: Wasn't anywhere near in solving this. select hol1.employee_id, hol1.leave_date, hol1.no_of_days, hol1.leave_state from hr_holidays_by_calendar hol1 inner join (select employee_id, leave_date from hr_holidays_by_calendar hol1 group by employee_id, leave_date having count(*)>1)sub on hol1.employee_id=sub

pivoting large table in postgresql

匆匆过客 提交于 2021-01-29 09:01:29
问题 I'd like to pivot a table, similar to the one below to display as follows The actual table I'm using has over 1.6 million rows, and 1500+ fields, mostly with non-boolean answers.Most of the solutions I've found require me to specify what the Field Answer contents will be in the query, which is not feasible, given the number of fields and variety of field answers. Any help would be appreciated. 来源: https://stackoverflow.com/questions/65587003/pivoting-large-table-in-postgresql

PostgreSQL Select WHERE column = [Array]

戏子无情 提交于 2021-01-29 09:01:24
问题 I have a table1 with column 1 and 2 1 2 Banana x Apple y Orange z I want to call a SELECT on this table with an array as parameter: ['Banana', 'Apple'] I want to get the rows where column 1 contains Banana or Apple (elements of the array) SELECT * FROM table1 WHERE column1 = 'Banana OR column1 = 'Apple' But how is this working dynamically? Like where column1 = elemt in array....? 回答1: Use the ANY operator: select * from the_table where the_column = any( array['Banana', 'Apple'] ); 回答2: Use

PostgresSql 9.6 deletes suddenly became slow

こ雲淡風輕ζ 提交于 2021-01-29 08:45:55
问题 I have a database table where debug log entries are recorded. There are no foreign keys - it is a single standalone table. I wrote a utility to delete a number of entries starting with the oldest. There are 65 million entries so I deleted them 100,000 at a time to give some progress feedback to the user. There is a primary key column called id All was going fine until it got to about 5,000,000 million records remaining. Then it started taking over 1 minute to execute. What is more, if I used

Liquibase does not add interval to current_timestamp

北慕城南 提交于 2021-01-29 08:37:50
问题 I use postgres DB, I faced an issue with liquibase adding interval to the current time as default value: <property name="expired" value="current_timestamp + interval '60 days'" dbms="postgresql"/> <addColumn tableName="user"> <column name="expired" type="timestamp" defaultValueDate="${expired}"> <constraints nullable="false"/> </column> </addColumn> Expired property always returns current date without adding 60 days. Is it possible? Or is there some mistakes in value field? Thank you in

Applying unique constraint of date on TIMESTAMP column in postgresql

前提是你 提交于 2021-01-29 08:31:02
问题 I have a postgresql table as CREATE TABLE IF NOT EXISTS table_name ( expiry_date DATE NOT NULL, created_at TIMESTAMP with time zone NOT NULL DEFAULT CURRENT_TIMESTAMP(0), CONSTRAINT user_review_uniq_key UNIQUE (expiry_date, created_at::date) -- my wrong attempt of using :: ) I want to put uniue constraint on this table in such a way that expiry_date and date of created_at should be unique. Problem is created_at column is timestamp not date. so is there any way to put unique constraint such

How to get arrays from a normalised table that stores array elements by index?

一曲冷凌霜 提交于 2021-01-29 08:26:19
问题 I have a table storing array elements by the array they belong to and their index in the array. It seemed smart because the arrays were expected to be sparse, and have their elements updated individually. Let's say this is the table: CREATE TABLE values ( pk TEXT, i INTEGER, value REAL, PRIMARY KEY (pk, i) ); pk | i | value ----+---+------- A | 0 | 17.5 A | 1 | 32.7 A | 3 | 5.3 B | 1 | 13.5 B | 2 | 4.8 B | 4 | 89.1 Now I would like to get these as real arrays, i.e. {17.5, 32.7, NULL, 53} for

Check whether all dates are present in a year in pandas python

无人久伴 提交于 2021-01-29 08:24:37
问题 I have a data column like below, in which some dates are missing. obstime 2012-01-01 2012-01-02 2012-01-03 2012-01-04 .... 2016-12-28 2016-12-29 2016-12-30 2016-12-31 I want to check for all dates for each month for available years. Like in the following image 回答1: Use: #sample data df = pd.DataFrame({'obstime':pd.date_range('2012-01-01', '2016-12-31')}) removed = ['2013-09-01', '2013-09-02', '2013-09-03','2014-10-09','2016-12-30'] removed1 = pd.date_range('2016-12-16', '2016-12-22') removed2

How to use both Java in Docker and PostgreSQL database?

老子叫甜甜 提交于 2021-01-29 08:08:16
问题 I am developing the Heroku toy app and want to use the PostgreSQL database to store configurations. I either see tutorials on how to use Java + PostgreSQL or Java on Docker. I cannot find a way on how I can use Java in Docker + PostgreSQL. NOTE: Database is a rather unimportant piece. It can be anything that can persist info such as Redis, other databases. I looked through StackOverflow, tutorials, and Heroku doc but so far, no luck. How can I connect to PostgreSQL from Java in Docker on