SQL :
select distinct b.nom, b.moyenne_pond
from previ.rdt_usine b
join previ.rdt r on (r.id_us=b.id_us)
where st_intersects(st_transform(st_setsrid(st_makepoint(55.40513629386126, -21.23713970920021), 4326), 32740),b.geom)
and r.annee=2018;
PHP :
$query = 'select distinct "b.nom", "b.moyenne_pond"
from "previ"."'.$name_table.'" b
join "previ"."rdt" r on (r.id_us=b.id_us)
where st_intersects(st_transform(st_setsrid(st_makepoint(?, ?), 4326), 32740), b.geom)
and r.annee=?';
$data = $db->select($query, $coord);
//$coord is an array of data
When I tried to use my request I have a this : column "b.nom" doesn't exist, but when I tried the query in pgAdmin the query is successfull, and you could see that the queries are exactly the same
Remove double quotes and add back tick
$query = 'select distinct `b.nom`, `b.moyenne_pond`
from `previ`.`$name_table` as b
join `previ`.`rdt` as r on (r.id_us=b.id_us)
where st_intersects(st_transform(st_setsrid(st_makepoint(?, ?), 4326), 32740), b.geom)
and r.annee=?';
Related
I have sql query as below, i need to convert sql to codeigniter active record.
select * from color c
left join (select pc.* from product_color pc
LEFT join product p on pc.product_id=p.product_id
WHERE p.product_id=1)x on c.id=x.color_id
In codeigniter you can get compiled query using get_compiled_select function.
Let's create inner query first.
$inner_query = $this->db->select('pc.*')
->from('product_color pc')
->join('product p','pc.product_id = p.product_id','left')
->where('p.product_id',1)
->get_compiled_select();
Now we will use inner query to create our final query.
$final_query = $this->db->select('*')
->from('color c')
->join("($inner_query) x",'c.id=x.color_id','left')
->get_compiled_select();
echo $final_query; die;
Try this into model file. and it's always better to keep sql keywords in capital.
$query = "SELECT * FROM color c
LEFT JOIN (SELECT pc.* FROM product_color pc
LEFT JOIN product p ON pc.product_id=p.product_id
WHERE p.product_id=1)x on c.id=x.color_id"
$query = $this->db->query($query);
$data = $query->result_array();
I have update query which returns updated rows ID. Execution time of query is about 90 seconds. When i remove Returning clause, then execution time is 1ms.
Table update_table has 39000 rows.
Query updates 0 rows in this case. When updates 3 rows- execution time is same.
DECLARE
type intTable IS TABLE OF INTEGER;
idCol intTable;
BEGIN
UPDATE
update_table
SET
prop1 = 3, prop2 = NULL
WHERE EXISTS (
SELECT null FROM update_table f
INNER JOIN rel_table1 u ON f.ID= u.ID
INNER JOIN rel_table2 VP ON f.another_ID = VP.another_ID
WHERE (u.prop1 = 3)
AND VP.prop1 = 1
AND (u.prop2 = 75)
AND f.ID = update_table.ID
)
ReTURNING ID BULK COLLECT INTO idCol;
.
.
.
END;
Why returning clause slows down query?
A good part of using Oracle is knowing what is "supposed" to happen and what isn't.
Adding a RETURNING INTO clause is not "supposed" to make your update run more slowly. When something happens that isn't supposed to happen, check Oracle's support site to see whether it is a known bug.
In your case, it looks like you are encountering:
Bug 27131648 - SUB OPTIMAL PLAN ON UPDATE STATEMENT WITH RETURNING INTO
I am not sure if there is a patch, but there is a simple workaround: use the UNNEST hint. In your case, that would be:
UPDATE
update_table
SET
prop1 = 3, prop2 = NULL
WHERE EXISTS (
SELECT /*+ UNNEST */ null FROM update_table f
INNER JOIN rel_table1 u ON f.ID= u.ID
INNER JOIN rel_table2 VP ON f.another_ID = VP.another_ID
WHERE (u.prop1 = 3)
AND VP.prop1 = 1
AND (u.prop2 = 75)
AND f.ID = update_table.ID
)
ReTURNING ID BULK COLLECT INTO idCol;
I would recommend splitting it into two parts, first BULK COLLECT and next FORALL collected ID's, both extremely fast and you'll keep being able to further reference updated ID's from idCol.
DECLARE
type intTable IS TABLE OF INTEGER;
idCol intTable;
BEGIN
SELECT f.id
BULK COLLECT INTO idCol
FROM update_table f
INNER JOIN rel_table1 u ON f.ID= u.ID
INNER JOIN rel_table2 VP ON f.another_ID = VP.another_ID
WHERE (u.prop1 = 3)
AND VP.prop1 = 1
AND (u.prop2 = 75);
FORALL indx IN 1..idCol.COUNT
UPDATE update_table
SET prop1 = 3, prop2 = NULL
WHERE id = idCol(indx);
.
.
.
END;
I hope I helped!
I am using jpql jpa eclipselink Following query wont work :
SELECT c FROM TableA c WHERE c.forumId = :forumId AND c.isDefault = true HAVING MAX (c.validFrom)
The error im getting "The expression is not a valid conditional expression"
The HAVING clause only works with a GROUP BY expression.
The HAVING clause allows for the results of a GROUP BY to be filtered.
Your question is:
i want o get max validFrom how can i make expression ot of this
But you can make a query without GROUP BY and HAVING to do what you want:
select c from TableA c WHERE c.validFrom = (
SELECT MAX(c2.validFrom)
FROM TableA c2
WHERE c2.Id = c.Id AND c.forumId = :forumId AND c.isDefault = true
)
If you would like to use GROUP BY and HAVING, you can do:
SELECT c FROM TableA c WHERE c.validFROM = (
SELECT MAX(validFROM)
FROM TableA
WHERE forumId = :forumId AND isDefault = true
GROUP BY validFROM
HAVING validFROM=c.validFROM
)
I have a trouble using HQL (in Groovy):
A simple query works without problems, like below (please note that I am using distinct):
def eqLiquid = liq.Liquidacion.executeQuery(
"""SELECT
distinct b.id ,
l.nombre as name
FROM Liquidacion l JOIN l.detalles ll JOIN ll.bioquimico b
WHERE l.id=:liqid
""", [liqid: liqid])
But I need to return these data as a map, then I have modified the query (only add new map()) :
def eqLiquid = liq.Liquidacion.executeQuery(
"""SELECT new map(
distinct b.id ,
l.nombre as name
)
FROM Liquidacion l JOIN l.detalles ll JOIN ll.bioquimico b
WHERE l.id=:liqid
""", [liqid: liqid])
Then I get an error: "unexpected token: distinct near line 2, column 17 [SELECT new map(
distinct b.id ,"
If I quit the distinct in the last query, it works.
Anyone had this problem?
It seems that MAP and DISTINCT can not work togheter in an HQL query
I Have a two pl/sql query but there is no two coulmns at (1) queries.They are AcademicTitles and ManagerialTitles.So I gave they as default value 'YOK' at (1) query.
I want to UNION (1) and (2) queries.
Here is My Queries:
1-
SELECT
h1."IL_KODU",
h1."KURUM_ILI",
h1.ILCE_KODU,
h1."KURUM_ILCESI",
h1."KURUM_KODU",
h1."KURUM_ADI",
(
CASE
WHEN h1."STATU" = 'K' THEN
'Devlet'
END
) AS KURUM_STATU,
h1."KURUM_TUR_ADI",
br3."Type" AS Unvan,
br."BranchName" AS Brans,
'YOK' AS AkademikUnvan,
'YOK' AS IdariUnvan,
COUNT (1) AS Total
FROM KAMU_PERSONEL k1
INNER JOIN "SptsBranches" s1 ON s1."CkysBranchCode" = k1.BRANS_KODU
INNER JOIN "Branches" br ON s1."BranchId" = br."BranchId"
INNER JOIN "BranchBranchTypes" br2 ON br."BranchId" = br2."BranchId"
INNER JOIN "BranchTypes" br3 ON br3."BranchTypeId" = br2."BranchTypeId"
INNER JOIN HOSPITALS h1 ON h1.KURUM_KODU = k1.CALIS_BIRIM
GROUP BY
h1."IL_KODU",
h1."KURUM_ILI",
h1.ILCE_KODU,
h1."KURUM_ILCESI",
h1."KURUM_KODU",
h1."KURUM_ADI",
h1."STATU",
h1."KURUM_TUR_ADI",
br3."Type",
br."BranchName"
2-
SELECT
p3."IL_KODU",
p3."KURUM_ILI",
p3.ILCE_KODU,
p3."KURUM_ILCESI",
p3."KURUM_KODU",
p3."KURUM_ADI",
(
CASE
WHEN p3."STATU" = 'O' THEN
'Özel'
WHEN p3."STATU" = 'U' THEN
'Üniversite'
END
) AS KURUM_STATU,
p3."KURUM_TUR_ADI",
b2."Type" AS Unvan,
b1."BranchName" AS Brans,
u1."Title" AS AkademikUnvan,
u2."Title" AS IdariUnvan,
COUNT (1) AS Total
FROM
"SptsDatas" p1
INNER JOIN "PersonStatus" p2 ON p1."TcKimlik" = p2."Tckn"
INNER JOIN "Branches" b1 ON p1."Brans_BranchId" = b1."BranchId"
INNER JOIN "BranchTypes" b2 ON p1."Unvan_BranchTypeId" = b2."BranchTypeId"
INNER JOIN HOSPITALS p3 ON p2."HospitalCode" = p3."KURUM_KODU"
INNER JOIN "AcademicTitles" u1 ON u1."Id" = p1."AkademikUnvan_Id"
INNER JOIN "ManagerialTitles" u2 ON u2."Id" = p1."IdariUnvan_Id"
WHERE
p2."Statu" = 1
AND p3."AKTIF" = 1
GROUP BY
p3."IL_KODU",
p3."KURUM_ILI",
p3.ILCE_KODU,
p3."KURUM_ILCESI",
p3."KURUM_KODU",
p3."KURUM_ADI",
b1."BranchName",
b2."Type",
u1."Title",
u2."Title",
p3."STATU",
p3."KURUM_TUR_ADI"
When try to UNION two queries this error occuring.
[Err] ORA-12704: character set mismatch
How can I fix this error.
Thanks
My guess is that value of KURUM_STATU column in the second query is converted to NVARCHAR2, because of "Ö" and "Ü" in its value. As a result, Oracle tries to mix varchar2 with nvarchar2 giving the mentioned ORA error.
To confirm this, try to remove those characters from the statement and if it helps, convert the values explicitly to nvarchar2, or get rid of the trouble characters.
If you cast the two columns in second query that should solve it I believe.
In your second query try replacing
u1."Title" AS AkademikUnvan,
u2."Title" AS IdariUnvan,
with
CAST( u1."Title" AS varchar2(1000) ) AS AkademikUnvan,
CAST( u2."Title" AS varchar2(1000) ) AS IdariUnvan
Let me know how that goes.