sql-in

SQL In Clause with 20000 values

∥☆過路亽.° 提交于 2021-02-11 14:26:35
问题 I have a xls with 20000 IDs I need to extract the rows of a table that have these IDs in Col1 Is there a clever way to do this in Oracle SQL ? I only have a read access to this db. I thought to slice the 20000 IDs, in order to put the first 1000 in a variable p_list1 , the next 1000 in a variable p_list2, ect and use a IN clause and union to get the whole result But I'm not used to use paramters in my query. Could you please review it ? Thanks in advance for your help DECLARE p_list1 VARCHAR2

SQL JOIN with negative WHERE condition

若如初见. 提交于 2021-02-11 12:32:30
问题 I have two tables Document and Label (not my real case, I am using analogy). One Document can have N Labels. When I need to select Documents that has listed labels I can do easily this select D.id from document D join label L on D.id = L.document_id where L.value in('label1','label2',...) How to write a query where I need Documents that DO NOT have listed labels? When I do this select D.id from document D join label L on D.id = L.document_id where L.value not in('label1','label2',...) then it

How to search an enum in list of strings by postgresql query?

末鹿安然 提交于 2021-02-10 22:31:53
问题 Consider a SQL Statement: select * from table where status in <statuses> Where status is an enum: CREATE TYPE statusType AS ENUM ( 'enum1', 'enum2'; In Java I have a list of the enums in string representation: List<String> list = new ArrayList<>(); list.add("enum1"); list.add("enum2"); I then try to build and execute the query using SqlStatement: handle.createQuery("select * from table where status in <statuses>") .bindList("statuses", statuses) .map(Mapper.instance) .list()); I keep getting

How to search an enum in list of strings by postgresql query?

笑着哭i 提交于 2021-02-10 22:28:47
问题 Consider a SQL Statement: select * from table where status in <statuses> Where status is an enum: CREATE TYPE statusType AS ENUM ( 'enum1', 'enum2'; In Java I have a list of the enums in string representation: List<String> list = new ArrayList<>(); list.add("enum1"); list.add("enum2"); I then try to build and execute the query using SqlStatement: handle.createQuery("select * from table where status in <statuses>") .bindList("statuses", statuses) .map(Mapper.instance) .list()); I keep getting

The NOT IN with NULL values dilemma in ORACLE SQL

前提是你 提交于 2021-01-29 13:23:19
问题 When I used this code WHEN col1 NOT IN (SELECT col2 FROM table_name) THEN 'something' it didn't give the expected results knowing that col2 contains a NULL value, Why did this happened ? Does using IN with NULL values messes with data stored in memory or what? 回答1: This is not an issue with Oracle. This is how SQL is defined. When the subquery returns a NULL value with NOT IN , then no rows match at all. For this reason, I strongly recommend always using NOT EXISTS instead: WHEN NOT EXISTS

PostgreSQL ORDER BY values in IN() clause

↘锁芯ラ 提交于 2020-06-08 17:28:01
问题 Ok, there are some answers out there on how to do this. But all of the answers are assuming that the query is selecting all. If you have a distinct select, the methods no longer work. See here for that method: Simulating MySQL's ORDER BY FIELD() in Postgresql Basically I have SELECT DISTINCT id FROM items WHERE id IN (5,2,9) ORDER BY CASE id WHEN 5 THEN 1 WHEN 2 THEN 2 WHEN 9 THEN 3 END Of course, this breaks and says "PGError: ERROR: for SELECT DISTINCT, ORDER BY expressions must appear in

Room - Select query with IN condition?

人走茶凉 提交于 2020-03-17 04:18:32
问题 Is it possible to use SQLite's IN condition with Room? I'm trying to select a list of items from my database where the value of a certain column (in this case a TEXT column) matches any one of a set of filter values. That's pretty easily done in SQL and SQLite, by my knowledge, just by adding an IN condition to your SELECT statement (see here). However, I can't seem to make it work with Room. I keep getting this error: Error:(70, 25) error: no viable alternative at input 'SELECT * FROM Table

Using IN clause in a native sql query

限于喜欢 提交于 2019-12-29 05:57:32
问题 We are trying to dynamically generate an IN clause for a native sql query to return a JPA entity. Hibernate is our JPA provider. Our code looks something like this. @NamedQuery( name="fooQuery", queryString="select f from Foo f where f.status in (?1)" ) .... Query q = entityManager.createNamedQuery("fooQuery"); q.setParameter(1, "('NEW','OLD')"); return q.getResultList(); This doesn't work, the in clause does not recognize any of the values passed in via this manner. Does anyone know of a

Using 'IN' with a sub-query in SQL Statements

被刻印的时光 ゝ 提交于 2019-12-28 06:57:06
问题 Are there any performance issues of using "IN" keyword in SQL statements in places where we can use JOIN? SELECT xxx FROM xxx WHERE ID IN (SELECT Id FROM xxx) 回答1: No, it's OK to use. You can write the query above using IN, EXISTS in all RDBMS, some also support INTERSECT. Semantically this is a semi-join which "give me rows from table A where I have a at least one match in tableB". An INNER JOIN is "give me all matching rows" So if TableA has 3 rows and TableB has 5 rows that match: an INNER

What is the most performant way to rewrite a large IN clause?

感情迁移 提交于 2019-12-23 20:07:45
问题 I wrote an API using go and gorm that runs calculations on our database and returns the results. I just hit the parameter limit for an IN condition when using an aggregate. Example query: SELECT SUM(total_amount) from Table where user_id in(...70k parameters) group by user_id One of my current edge cases has > 65535 user ids so my Postgres client is throwing an error: got 66037 parameters but PostgreSQL only supports 65535 parameters I'm not sure what the best way to approach this is. One