SQL IN by multiple columns in Slick - sql

I want to express something like
WHERE (col1, col2)
IN ((val1a, val2a), (val1b, val2b), ...)
In pseudo-Slick:
val values = Set(("val1a", "val2a"), ("val1b", "val2b"))
// and in filter
(col1, col2) inSetBind values
Is there a way to do that in Slick?

Related

Query with IN operator not performing well in SQL Server

I have a query in SQL Server something like this.
select sum (col1)
from TableA
where col2 = ?
and col3 = ?
and col4 = in (?, ?, ?... ?)
TableA has a composite index on (col2, col3, col4).
This query is not performing well when the size is increasing in the list of the IN operator.
Is there a good way to rewrite this query for better performance?
List can grow from 1 to 300 items.
Your index should be pretty good. For this query:
select sum(col1)
from TableA
where col2 = ? and
col3 = ? and
col4 in (?, ?, ?... ?);
The optimal index is (col2, col3, col4, col1) (the last column can be included if you prefer.
For the index to be used, you need to be sure that the types are compatible for the comparisons. Conversions -- and changes in collation -- preclude the use of indexes.

More efficient to SELECT constants vs using VALUES when INSERTing a row with constant values?

Which of the following queries is the more efficient and the more idiomatic way of inserting a row with some constant values?
INSERT INTO example_table (col1, col2, col3)
SELECT 123, other_col, 'value' FROM other_table WHERE some_id = 999;
or
INSERT INTO example_table (col1, col2, col3)
VALUES (123,
(SELECT other_col FROM other_table WHERE some_id = 999),
'value');
they have different semantics if there is not exactly one row matching the following query
SELECT other_col FROM other_table WHERE some_id = 999
so choose the one that gives you the semantics you want
If the above query returns 0 rows do you want (a) no rows to be inserted or (b) a row with NULL?
If the above query returns more than one row do you want (a) that number of rows to be inserted or (b) a runtime error?
If you answered (a) for both the above choose the first one. If you answered (b) choose the second one.

Oracle_How to insert data from slow select query into two tables

I have searched but can't get answer for this (maybe wrong keyword...)
I come to this problem today when I need to create a procedure to calculate data to save to 2 report table in 2 different schemas. Let say those two tables have same structure.
The query to calculate data may take more than 60 seconds (data may or may not change the result of SELECT statemant if run again)
I have two way to insert data to those two table:
Just run insert TWO time with that same select query.
Using a GTT - global temporary table to save calculated data from SELECT query, then INSERT to those two tables using data in that GTT.
I wonder if Oracle will keep cache of result for the SELECT query so that the first way will have better performance then second way (but have longer code, and duplicate code, not synchronized?).
So could anyone confirm and explain the right way to solve this for me? Or a better way of doing this?
Thank you,
Appendix 1:
INSERT INTO report_table (col1, col2, ....)
SELECT .....
FROM .....
--(long query)
;
INSERT INTO center_schema.report_table (col1, col2, ....)
SELECT .....
FROM .....
--same select query as above
;
And 2:
INSERT INTO temp_report_table(col1, col2, ...)
SELECT .....
FROM .....
--(long query)
;
INSERT INTO report_table (col1, col2, ....)
SELECT col1, col2, ....
FROM temp_report_table
;
INSERT INTO center_schema.report_table (col1, col2, ....)
SELECT col1, col2, ....
FROM temp_report_table
;
No, you have a third option - the wonderful multi-insert...
INSERT ALL
INTO report_table (col1, col2, ....)
VALUES (X.col1, X.col2, ...)
INTO center_schema.report_table (col1, col2, ...)
VALUES (X.col1, X.col2, ...)
SELECT col1, col2, ...
FROM your_table X
--(long query)
;
For a detailed info on this nice way of loading multiple tables at once please refer to the respective part of Oracle documentation.

HIVE- Divide single row in to multiple rows

I have to prepare the query from source table to target table. table structure are shown in the image. Can any one help on this.http://i.stack.imgur.com/wnUuZ.png
[Tables image]
Hive's stack function should work here.
SELECT stack(2,
col1, col2, col3, '',
col1, col2, '', col4
) AS (newCol1, newCol2, newCol3, newCol4)
FROM source;
Basically, stack generates N rows for each row in the source, and you define each of these new rows.

SQL parametrised query containing multiple options

I would like to write a query
Select col1, col2
from table
where col1 = 'blah' or 'blah2' or 'blah3'
and col2 = 'blah' or 'blah2' or 'blah3'
I am used to writing them like this for a SINGLE option
select
col1, col2
from
table
where
col1 = :col1 and col2 = :col2
Parameters.AddWithValue(":col1", 'blah')
Parameters.AddWithValue(":col2", 'blah')
Now I want to add several options with OR between them and obviously the above code wont work. The SQL is for SQLite. Can anyone suggest how I could do this? I may potential have more then 3 different values for each parameter. I have tried searching but the answer is elusive.
You still have to use complete expressions, i.e., you need to write col1 = or col2 = every time.
Alternative, use IN:
SELECT ... WHERE col1 IN (:c11, :c12, :c13) AND col2 IN (:c21, :c22, :c23);