<<-sql in Ruby to get a relation? [duplicate] - sql

This question already has an answer here:
Rails relation ordering?
(1 answer)
Closed 1 year ago.
So I have a function like this:
def get_relation(my_offset, my_limit):
query = <<-SQL
SELECT *
FROM my_table
Limit my_limit <--- HERE
Offset my_offset <--- HERE
Order by id
SQL
// How can I return a relation here with model MyTable?
So I am wondering two things:
How can I use my variables my_limit and my_offset inside the <<-sql? Is my syntax to include those correct?
How can I run this raw query and get a relation out of it?
Thanks!

You can simply use the Rails query language instead of manually building a string with a SQL statement. The following line is identical to your SQL statement and returns an ActiveRecord Relation.
MyModel.limit(my_limit).offset(my_offset).order(:id)

Related

How to use NOT IN sql query in django [duplicate]

This question already has answers here:
Django equivalent of SQL not in
(4 answers)
Closed 3 years ago.
Im new to Django. Anyone help me to write "Not In" sql query in django.
here i used raw() query set without raw() query set how to write this query in django.
query = 'SELECT basic_uom FROM uom_master WHERE id="'+ id +'" and basic_uom not in (SELECT next_uom from uom_master WHERE id="'+ id +'") and basic_uom not in(SELECT std_uom FROM std_uom_master WHERE id"'+ id +'")ORDER BY next_uom ASC'
data = uom_master.objects.raw(query)
If [1,2,3] is your list then you can do something like this, you modify according to your table names
uom_master.objects.filter(id=some_id).exclude(id__in=[1,2,3]).order_by("next_uom")
To get list of ids you can do like
list_of_ids = std_uom_master.objects.filter().values_list("id",flat=True)
filter is your filtering criteria and exclude is records to be omitted.
See filters like id__in and guide about making queries

Simple SQL statement in MS Access [duplicate]

This question already has an answer here:
MS Access 2010 SQL Select Into Calculated Column Issue
(1 answer)
Closed 6 years ago.
I'm trying what should be a simple task in Access. Basically to create a new table based on a string matching query. The Hazus_Schools table already exists in my database. The Hazus_Public does not, and I'm trying to create it. The PUBLIC field is a calculated column from another field. The following snippet
SELECT * FROM Hazus_Schools INTO Hazus_Public
WHERE Type = "PUBLIC";
Gives me the following error:
Syntax error in FROM clause
Any ideas what is wrong here?
The order of your INTO and FROM is off, see W3schools Select Into
Try the following:
SELECT *
INTO Hazus_Public
FROM Hazus_Schools
WHERE Type = "PUBLIC"

Update multiple values in an oracle table using values from an APEX collection

I am using APEX collections to store some values and pass them between pages in Oracle Application Express 4.2.3.
I would like to then perform an update statement on a table called "project" with the values from the collection.
My code so far is as follows:
update project
SET name=c.c002,
description=c.c007,
start_date=c.c004,
timeframe=c.c005,
status=c.c009
FROM
apex_collections c
WHERE
c.collection_name = 'PROJECT_DETAILS_COLLECTION'
and id = :p14_id;
where :p14_id is the value of a page item.
However, I am getting the following error:
ORA-00933: SQL command not properly ended
Anyone have any idea on how to approach this?
Thanks!
The UPDATE syntax you are using is not valid in Oracle; it does not allow you to use FROM in the way you are attempting.
The simplest way to do this in Oracle would with a subquery:
update project
set (name, description, start_date, timeframe, status) =
(select c.c002, c.c007, c.c004, c.c005, c.c009
FROM
apex_collections c
WHERE
c.collection_name = 'PROJECT_DETAILS_COLLECTION'
)
WHERE
id = :p14_id
;
Note that if the subquery returns no rows, the columns in the target table will be updated to NULL; this could be avoided by adding a similar EXISTS condition in the predicate for the update. It could also be avoided by using a MERGE statement instead of an UPDATE.
If the subquery returns multiple rows, the statement will throw an error. It looks like tthat should not be the case here.

'WHERE 1 =1' in SQL Stored Procedure [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
Why would someone use WHERE 1=1 AND <conditions> in a SQL clause?
I've been tasked with reviewing some SQL stored procedures and have seen many that look like the following:
SELECT
X, Y, Z
FROM
Table
WHERE
1 = 1
ORDER BY
X
Why would someone use '1 = 1' for the where clause?
Thanks!
It's common in dynamic SQL, in order to append additional criteria to a WHERE clause. Otherwise, it's useless and it is ignored by the optimizer.
possibly to dynamically add conditions to the where clause.

Oracle SQL: How to use more than 1000 items inside an IN clause [duplicate]

This question already has answers here:
SQL IN Clause 1000 item limit
(5 answers)
Closed 8 years ago.
I have an SQL statement where I would like to get data of 1200 ep_codes by making use of IN clause. When I include more than 1000 ep_codes inside IN clause, Oracle says I'm not allowed to do that. To overcome this, I tried to change the SQL code as follows:
SELECT period, ...
FROM my_view
WHERE period = '200912'
...
AND ep_codes IN (...1000 ep_codes...)
OR ep_codes IN (...200 ep_codes...)
The code was executed succesfully but the results are strange (calculation results are fetched for all periods, not just for 200912, which is not what I want). Is it appropriate to do that using OR between IN clauses or should I execute two separate codes as one with 1000 and the other with 200 ep_codes?
Pascal Martin's solution worked perfectly. Thanks all who contributed with valuable suggestions.
The recommended way to handle this in Oracle is to create a Temporary Table, write the values into this, and then join to this. Using dynamically created IN clauses means the query optimizer does a 'hard parse' of every query.
create global temporary table LOOKUP
(
ID NUMBER
) on commit delete rows;
-- Do a batch insert from your application to populate this table
insert into lookup(id) values (?)
-- join to it
select foo from bar where code in (select id from lookup)
Not sure that using so many values in a IN() is that good, actually -- especially for performances.
When you say "the results are strange", maybe this is because a problem with parenthesis ? What if you try this, instead of what you proposed :
SELECT ...
FROM ...
WHERE ...
AND (
ep_codes IN (...1000 ep_codes...)
OR ep_codes IN (...200 ep_codes...)
)
Does it make the results less strange ?
Actually you can use collections/multisets here. You'll need a number table type to store them.
CREATE TYPE NUMBER_TABLE AS TABLE OF NUMBER;
...
SELECT *
FROM my_view
WHERE period MEMBER OF NUMBER_TABLE(1,2,3...10000)
Read more about multisets here:
Seems like it would be a better idea, both for performance and maintainability, to put the codes in a separate table.
SELECT ...
FROM ...
WHERE ...
AND ep_code in (select code from ep_code_table)
could you insert the 1200 ep_code values into a temporary table and then INNER JOIN to that table to filter rows instead?
SELECT a.*
FROM mytable a
INNER JOIN tmp ON (tmp.ep_code = a.ep_code)
WHERE ...