Problem with adding custom sql to finder condition - sql

I am trying to add the following custom sql to a finder condition and there is something not quite right.. I am not an sql expert but had this worked out with a friend who is..(yet they are not familiar with rubyonrails or activerecord or finder)
status_search = "select p.*
from policies p
where exists
(select 0 from status_changes sc
where sc.policy_id = p.id
and sc.status_id = '"+search[:status_id].to_s+"'
and sc.created_at between "+status_date_start.to_s+" and "+status_date_end.to_s+")
or exists
(select 0 from status_changes sc
where sc.created_at =
(select max(sc2.created_at)
from status_changes sc2
where sc2.policy_id = p.id
and sc2.created_at < "+status_date_start.to_s+")
and sc.status_id = '"+search[:status_id].to_s+"'
and sc.policy_id = p.id)" unless search[:status_id].blank?
My find statement:
Policy.find(:all,:include=>[{:client=>[:agent,:source_id,:source_code]},{:status_changes=>:status}],
:conditions=>[status_search])
and I am getting this error message in my log:
ActiveRecord::StatementInvalid (Mysql::Error: Operand should contain 1 column(s): SELECT DISTINCT `policies`.id FROM `policies` LEFT OUTER JOIN `clients` ON `clients`.id = `policies`.client_id WHERE ((((policies.created_at BETWEEN '2009-01-01' AND '2009-03-10' OR policies.created_at = '2009-01-01' OR policies.created_at = '2009-03-10')))) AND (select p.*
from policies p
where exists
(select 0 from status_changes sc
where sc.policy_id = p.id
and sc.status_id = '2'
and sc.created_at between 2009-03-10 and 2009-03-10)
or exists
(select 0 from status_changes sc
where sc.created_at =
(select max(sc2.created_at)
from status_changes sc2
where sc2.policy_id = p.id
and sc2.created_at < 2009-03-10)
and sc.status_id = '2'
and sc.policy_id = p.id)) ORDER BY clients.created_at DESC LIMIT 0, 25):
what is the major malfunction here - why is it complaining about the columns?

The conditions modifier is expecting a condition (e.g. a boolean expression that could go in a where clause) and you are passing it an entire query (a select statement).
It looks as if you are trying to do too much in one go here, and should break it down into smaller steps. A few suggestions:
use the query with find_by_sql and don't mess with the conditions.
use the rails finders and filter the records in the rails code
Also, note that constructing a query this way isn't secure if the values like status_date_start can come from users. Look up "sql injection attacks" to see what the problem is, and read the rails documentation & examples for find_by_sql to see how to avoid them.

Ok, I've managed to retool this so it is more friendly to a conditions modifier and I think it is doing the sql query correctly.. however, it is returning policies that when I try to list the current status (the policy.status_change.last.status) it is set to the same status used in the query - which is not correct
here is my updated condition string..
status_search = "status_changes.created_at between ? and ? and status_changes.status_id = ?) or
(status_changes.created_at = (SELECT MAX(sc2.created_at) FROM status_changes sc2
WHERE sc2.policy_id = policies.id and sc2.created_at < ?) and status_changes.status_id = ?"
is there something obvious to this that is not returning all of the remaining associated status changes once it finds the one in the query?
here is the updated find..
Policy.find(:all,:include=>[{:client=>[:agent,:source_id,:source_code]},:status_changes],
:conditions=>[status_search,status_date_start,status_date_end,search[:status_id].to_s,status_date_start,search[:status_id].to_s])

Related

Update From Select with Correlated Subquery and Join in PostgreSQL

I am migrating from SQL Server to Postgres and it has gone okay for the most part. One of the issues is that I am unable to figure out how to make this query work in Postgres:
update
"Measure"
set
DefaultStrataId = StrataId
FROM (SELECT "Strata"."MeasureId",
Min("Strata"."index") AS "Index"
FROM "Strata",
"Measure"
WHERE "Strata"."MeasureId" = "Measure"."MeasureId" and "Strata"."StrataId" in (select strataid from point)
GROUP BY "Strata"."MeasureId") a
INNER JOIN strata
ON "Strata"."index" = a."index"
where "Strata"."MeasureId" = "Measure"."MeasureId";
It complains: SQL Error [42601]: ERROR: syntax error at or near "FROM"
How do I get this to work?
You can use a DISTINCT ON for this to simplify it.
The ORDER BY in the sub-query will make sure that it's the "StrataId" for the minimum "index".
UPDATE "Measure" m
SET "DefaultStrataId" = q."StrataId"
FROM
(
SELECT DISTINCT ON (s."MeasureId") s."MeasureId", s."index", s."StrataId"
FROM "Strata" s
JOIN "Point" p ON p."StrataId" = s."StrataId"
JOIN "Measure" m ON m."MeasureId" = s."MeasureId"
ORDER BY s."MeasureId", s."index"
) q
WHERE q."MeasureId" = m."MeasureId";
Test on db<>fiddle here
Btw, the query below also works with the test data in that fiddle.
Basically, in Postgresql quoting a name isn't the same as not quoting a name.
MS Sql Server is much more tolerant in that aspect.
UPDATE "Measure"
SET "DefaultStrataId" = "StrataId"
FROM (SELECT "Strata"."MeasureId",
Min("Strata"."index") AS "index"
FROM "Strata",
"Measure"
WHERE "Strata"."MeasureId" = "Measure"."MeasureId" and "Strata"."StrataId" in (select "StrataId" from "Point")
GROUP BY "Strata"."MeasureId") a
INNER JOIN "Strata"
ON "Strata"."index" = a."index"
where "Strata"."MeasureId" = "Measure"."MeasureId";
Funky. After feeling shamed by #wildplasser to make my query non-disgusting, everything magically started working. It's not like I changed anything except switching to standard joins and adding aliases:
update
"Measure" m set
"DefaultStrataId" = "StrataId"
from
(
select
s."MeasureId",
min(s."Index") as "Index"
from
"Measure" m
inner join "Strata" s on
s."MeasureId" = m."MeasureId"
where s."StrataId" in (
select
s."StrataId"
from
"Point")
group by
s."MeasureId") a
inner join "Strata" s on
s."Index" = a."Index"
where
s."MeasureId" = m."MeasureId";
Your only goal appears to be getting the minimal value from Strata
Omitting all the ugly quotes,and adding some aliasses (assuming that only one record with the minumum value exists) :
UPDATE Measure m
SET DefaultStrataId = s.StrataId
FROM Strata s
WHERE s.MeasureId = m.MeasureId
AND NOT EXISTS (
SELECT * FROM Strata nx
where nx.MeasureId = s.MeasureId
AND nx."index" < s."index"
)
;

MS Access - SQL LEFT JOIN multiple conditions

I have this code which is working fine except that I need to add one more condition:
SELECT record1.*,
tbl_mpsregion.maintenanceteam,
tbl_mpsregion.regionmps
INTO tbl_sapforecast
FROM tbl_mpsregion
RIGHT JOIN
(
SELECT sap_ip19.*,
dateserial(RIGHT(trim([SAP_IP19].[PlanDate]),4),mid(trim([SAP_IP19].[PlanDate]),4,2),LEFT(trim([SAP_IP19].[PlanDate]),2)) AS [DATE/FORECAST],
tbl_labourstandard.re,
tbl_labourstandard.manning,
tbl_labourstandard.skillset AS skillset,
tbl_regionmapping.maintenanceplant,
tbl_regionmapping.area,
tbl_regionmapping.region AS region,
tbl_regionmapping.onresponse,
[RE]*[Manning]/60 AS hours
FROM (sap_ip19
LEFT JOIN tbl_labourstandard
ON (
LEFT(sap_ip19.[Task list description],3) = tbl_labourstandard.jemenawc)
AND (
sap_ip19.[MntPlan] = cdbl(tbl_labourstandard.supplypoint )))
LEFT JOIN tbl_regionmapping
ON sap_ip19.location = cdbl([Tbl_RegionMapping].[FittersDistricts])) AS record1
ON (
record1.region = [Tbl_MPSRegion].[Region])
AND (
record1.skillset = [Tbl_MPSRegion].[Skillset]) ;
Criteria to add is: If SAP_IP19.MntPlan does not match Tbl_LabourStandard.SupplyPoint then use 0 for Tbl_LabourStandard.SupplyPoint. I am not using Server 2000 so using CASE is not a solution. Have tried IIF and SWITCH but they are not taking query to sleep mode (not evaluating). I read that JOINS with IIF or SWITCH cannot be used. Please help!
You should be able to add if's or switches but you could always handle this with an OR - it's not the most performance friendly but it should get the job done, example below:
LEFT JOIN tbl_labourstandard
ON
(LEFT(sap_ip19.[Task list description],3) = tbl_labourstandard.jemenawc)
AND
((Tbl_LabourStandard.SupplyPoint = SAP_IP19.MntPlan AND
sap_ip19.[MntPlan] = cdbl(tbl_labourstandard.supplypoint))
OR (sap_ip19.[MntPlan] = 0))

SQL - updating Oracle 10g database from a complex query

trying to update a single column based on criteria. The rows that need to be updated are returned by the query
SELECT
it.objectid,
it.versionnumber,
it.itemnumber "Item Number",
it.itemtype,
itcovprem.basisofsettlement,
itcovprem.coverage "Coverage"
FROM
itemcoveragesandpremiums itcovprem,
items it,
policies pl
WHERE
pl.objectid = it.parentobjectid AND
pl.policytype in ('H','F') AND
it.objectid = itcovprem.itemobjectid AND
it.itemtype in ('SHOA','SHOB','SHOC','SHOD','SHOK','SHOL') and
it.versionnumber = itcovprem.itemversionnumber AND
((itcovprem.coverage like ('A - Dwg Bldg%')) or
(itcovprem.coverage like ('#42 - Repl Cost Plus%')) or
(itcovprem.coverage like ('#42 - Limited GRC%'))) and
it.effectivedate >= TO_DATE('01-JAN-2006', 'DD-MON-YYYY') and
exists (select * from itemcoveragesandpremiums icp where icp.itemobjectid = it.objectid and icp.itemversionnumber = it.versionnumber and icp.coverage like ('#42%')) and
itcovprem.basisofsettlement not in ('LGRC')
I've tried a few options to convert this to an update query, but no joy.
I get Error - line 3 - SQL command not properly ended when using
update itemcoveragesandpremiums
set basisofsettlement = 'LGRC'
from itemcoveragesandpremiums as itcovprem
inner join items as it
on it.objectid = itcovprem.itemobjectid and it.versionnumber = itcovprem.itemversionnumber
inner join policies as pl
on pl.objectid = it.parentobjectid
where [cut, rest of query was below]
I get Error - line 6 - missing right parenthesis when trying use an inline query
update
(
SELECT
itcovprem.basisofsettlement as OLD
FROM
itemcoveragesandpremiums as itcovprem inner join items as it on it.objectid = itcovprem.itemobjectid and it.versionnumber = itcovprem.itemversionnumber inner join policies AS pl on pl.objectid = it.parentobjectid
WHERE [query snipped]
) t
set t.old = 'LGRC'
Seems that SQL*Plus just wants to stop looking at the update before it gets to the meat of my select query. I'm not sure if I'm formatting it incorrectly or going at it from the wrong direction. Any advice?
In your first attempt, the error at line 3 is because update doesn't support a from or join clause. In your second attempt the immediate error at line 6 is because you're trying to alias a table with as itcovprem; but you can only use as for column aliases, not for table aliases. (The first attempt is trying to do that too, but it isn't getting as far as encountering that problem). But you can't generally update an inline-view with joins anyway, so it would still error with that - something like an ORA-01779.
You would need to do something like:
update itemcoveragesandpremiums
set basisofsettlement = 'LGRC'
where (itemobjectid, itemversionnumber, basisofsettlement, coverage) in (
select it.objectid,
it.versionnumber,
itcovprem.basisofsettlement,
itcovprem.coverage
from ...
)
Assuming that itemobjectid, itemversionnumber, basisofsettlement, coverage identifies a row sufficiently such that you don't risk updating anything you shouldn't. It might be safer to add a rowid to the select and use that for the update instead to avoid ambiguity:
update itemcoveragesandpremiums
set basisofsettlement = 'LGRC'
where rowid in (
select itcovprem.rowid as target_rowid
from ...
)

Rails 3.2 - How to turn this complex query to be in parameterized format?

I am currently helping out a Rails 3.2 project.
Got this complex query that I would like to be in parameterized format.
project.supporters.connection.select_all( "SELECT t2.* FROM
(SELECT MAX(id) AS max_supporter_id FROM supporters WHERE
supporters.project_id = ? GROUP BY supporters.supporter_id) AS
t1 INNER JOIN supporters AS t2 ON t1.max_supporter_id =
t2.id", 1).
Above query is what I would like to be like but I know it doesn't work because select_all doesn't do parameterized query.
So far this is what I got:
project.supporters.group(:supporter_id).maximum(:id) which will
generate this sql: SELECT MAX("supporters"."id") AS maximum_id,
supporter_id AS supporter_id FROM "supporters" WHERE
"supporters"."project_id" = 1 GROUP BY supporter_id
But I haven't made significant progress to do the inner join and finally have the outer SELECT.
Can anyone help?
project_id = 1
supporters = Arel::Table.new(:supporters)
max_id = supporters.project(supporters[:id].maximum.as('max_supporters_id')).group(supporters[:supporter_id])
query = supporters.project(max_id)
.join(supporters[:project_id])
.on(supporters[:max_supporter_id].eq(supporters[:id]))
.where(supporters[:project_id].eq(project_id))
query.to_sql
# => "SELECT (SELECT MAX(\"supporters\".\"id\") AS max_supporters_id FROM \"supporters\" GROUP BY \"supporters\".\"supporter_id\") FROM \"supporters\" INNER JOIN \"supporters\".\"project_id\" ON \"supporters\".\"max_supporter_id\" = \"supporters\".\"id\" WHERE \"supporters\".\"project_id\" = 1"
query.to_a # executes the query and returns records
For more informations about building the complex queries in rails, you could read there https://github.com/rails/arel
I wish it helps.

SQL query for filtering data

I`m working on some sql queries to get some data out of a table; I have made 2 queries for the
same data but both give another result. The 2 queries are:
SELECT Samples.Sample,
data_overview.Sample_Name,
data_overview.Sample_Group,
data_overview.NorTum,
data_overview.Sample_Plate,
data_overview.Sentrix_ID,
data_overview.Sentrix_Position,
data_overview.HybNR,
data_overview.Pool_ID
FROM tissue INNER JOIN (
( patient INNER JOIN data_overview
ON patient.Sample = data_overview.Sample)
INNER JOIN Samples ON
(data_overview.Sample_id = Samples.Sample_id) AND
(patient.Sample = Samples.Sample)
) ON
(tissue.Sample_Name = data_overview.Sample_Name) AND
(tissue.Sample_Name = patient.Sample_Name)
WHERE data_overview.Sentrix_ID= 1416198
OR data_overview.Pool_ID='GS0005701-OPA'
OR data_overview.Pool_ID='GS0005702-OPA'
OR data_overview.Pool_ID='GS0005703-OPA'
OR data_overview.Pool_ID='GS0005704-OPA'
OR data_overview.Sentrix_ID= 1280307
ORDER BY Samples.Sample;")
And the other is
SELECT Samples.Sample,
data_overview.Sample_Name,
data_overview.Sample_Group,
data_overview.NorTum,
data_overview.Sample_Plate,
data_overview.Sentrix_ID,
data_overview.Sentrix_Position,
data_overview.HybNR,
data_overview.Pool_ID
FROM tissue INNER JOIN
(
(patient INNER JOIN data_overview
ON patient.Sample = data_overview.Sample)
INNER JOIN Samples ON
(data_overview.Sample_id = Samples.Sample_id)
AND (patient.Sample = Samples.Sample)) ON
(tissue.Sample_Name = data_overview.Sample_Name)
AND (tissue.Sample_Name = patient.Sample_Name)
WHERE ((
(data_overview.Sentrix_ID)=1280307)
AND (
(data_overview.Pool_ID)="GS0005701-OPA"
OR (data_overview.Pool_ID)="GS0005702-OPA"
OR (data_overview.Pool_ID)="GS0005703-OPA"
OR (data_overview.Pool_ID)="GS0005704-OPA"))
OR (((data_overview.Sentrix_ID)=1416198))
ORDER BY data_overview.Sample;
The one in the top is working quite well but it still won't filter the sentrix_ID.
The second 1 is created with Access but when I try to run this Query in R it gave
a unexpected symbol error. So if anyone knows how to create a query that filter POOL_ID and Sentrix_id with the given parameters thanks in advance
Is it a case of making the where clause something like this:
WHERE Sentrix_ID = 1280307 AND (Pool_ID = 'VAL1' OR Pool_ID = 'VAL2' OR Pool_ID = 'VAL3')
i.e. making sure you have brackets around the "OR" components?
Maybe you meant:
...
WHERE data_overview.Sentrix_ID IN (1280307,1416198 )
AND data_overview.Pool_ID IN ("GS0005701-OPA", "GS0005702-OPA", "GS0005703-OPA" ,"GS0005704-OPA")
;