MySQL query error 1064 (syntax error) - mysql

I'm trying to make the following query work but it seems to contain an error that I can't find. The query in question is:
select
A.*,
SD.*
from
(
( select
V71.Sollevamento_idAttrezzatura,
V71.Num_id_ON,
V71.Affidato_INAIL,
V71.Esito_Verifica,
V71.Prima_Verifica,
V71.Sospensione,
V71.Data_Verbale,
V71.Scadenza,
CO.Nome,
CO.Cognome,
CO.CF,
V71.Costo,
V71.Sconto as ScontoVerbale,
V71.Maggiorazione as MaggiorazioneVerbale,
V71.Indirizzo,
V71.Comune,
V71.Cap,
V71.Provincia,
V71.Regione
from
Verbale_Art71 as V71
join Collaboratore as CO
on V71.Codice_Tecnico = CO.Codice_Collaboratore
where
V71.Data_Verbale >= '2014-01-01'
and V71.Data_Verbale <= '2014-12-31' ) as VC
join
( select
S.Natura,
S.Anno_Immatricolazione,
S.Codice_Attribuzione_Matricola,
S.Provincia_Immatricolazione,
S.Numero_Matricola,
S.idSpecifica,
S.N_Panieri_Idroestrattori,
S.Modello,
S.Numero_Fabbrica,
S.Anno_Costruzione,
S.Sconto as ScontoAttr,
S.Maggiorazione as MaggiorazioneAttr,
P71.Tipo_Attr,
S.Sede,
S.idAttrezzatura
from
Sollevamento as S
join Periodicita_Art71 as P71
on S.idPeriodicita = P71.idPeriodicita ) as SP
on VC.Sollevamento_idAttrezzatura = SP.idAttrezzatura
) as A
join
( select
D.Ragione_Sociale,
D.Indirizzo,
D.Cap,
D.Comune,
D.Provincia,
D.Regione,
D.CF as CF_Ditta,
D.P_IVA,
SC.idSede
from
Ditta as D
join Sede as SC
on SC.Ditta = D.idDitta ) as SD
on ATTR71.Sede = SD.idSede
MySQL notifies that the syntax error is near as A join (select D.Ragione_Sociale, D.Indirizzo, D.Cap, D.Comune, D.Provincia
Anyone can help me find it? I've been looking for it since early morning and I'm going nuts. Thank anyone that will help me in advance, Giacomo

You are missing a select clause
select A.*, SD.*
from
(/* You are missing a select * from here */
(
select ..
) as VC
join
Try with
select A.*, SD.*
from
(select * from
(
select V71.Sollevamento_idAttrezzatura, V71.Num_id_ON, V71.Affidato_INAIL, V71.Esito_Verifica, V71.Prima_Verifica, V71.Sospensione, V71.Data_Verbale, V71.Scadenza, CO.Nome, CO.Cognome, CO.CF, V71.Costo, V71.Sconto as ScontoVerbale, V71.Maggiorazione as MaggiorazioneVerbale, V71.Indirizzo, V71.Comune, V71.Cap, V71.Provincia, V71.Regione
from Verbale_Art71 as V71
join
Collaboratore as CO
on V71.Codice_Tecnico = CO.Codice_Collaboratore
where V71.Data_Verbale >= '2014-01-01' and V71.Data_Verbale <= '2014-12-31'
) as VC
join
(
select S.Natura, S.Anno_Immatricolazione, S.Codice_Attribuzione_Matricola, S.Provincia_Immatricolazione, S.Numero_Matricola, S.idSpecifica, S.N_Panieri_Idroestrattori, S.Modello, S.Numero_Fabbrica, S.Anno_Costruzione, S.Sconto as ScontoAttr, S.Maggiorazione as MaggiorazioneAttr, P71.Tipo_Attr, S.Sede, S.idAttrezzatura
from Sollevamento as S
join
Periodicita_Art71 as P71
on S.idPeriodicita = P71.idPeriodicita
) as SP
on VC.Sollevamento_idAttrezzatura = SP.idAttrezzatura
) as A
join
(
select D.Ragione_Sociale, D.Indirizzo, D.Cap, D.Comune, D.Provincia, D.Regione, D.CF as CF_Ditta, D.P_IVA, SC.idSede
from Ditta as D
join Sede as SC
on SC.Ditta = D.idDitta
) as SD
on ATTR71.Sede = SD.idSede

Your final alias
on ATTR71.Sede = SD.idSede
should be
on A.Sede = SD.idSede
You can see it easily once the formatting / readability is better. The "A" was the alias result of the first JOINs using the ATTR71 alias table... The last join was only to the "A" alias and not the ATTR71 table.

Related

Convert SQL query to sqlachemy for complex query

WITH suite_table(parent_id, parent_name, child_name, parent_description, child_description) as
(
Select
test_suite.id, test_suite.name,
(Select test_suite.name from test_suite where id = sub_test_suites.child_suite_id) as child_name,
test_suite.description,
(Select test_suite.description from test_suite where id = sub_test_suites.child_suite_id) as child_description
from
test_suite
left join sub_test_suites on test_suite.id = sub_test_suites.parent_suite_id
) select
suite_table.parent_name, json_agg(case when suite_table.child_name is not null then
json_build_object(suite_table.child_name,
json_build_object(‘description’,suite_table.child_description))
else
null end )
from suite_table
group by suite_table.parent_name
;
Please provide sqlalchemy query. Its challenging but appreciate. I did not find tool to translate auomatically but sqlitis dont support WITH etc.

Alternative to Where Exist clause MySQL

I have this select statement that is taking quite a while to run on a larger dataset
select lookup_svcscat_svcscatnew.SVCSCAT_NEW_DESC as svc_type,
enrolid, msclmid, dx1, dx2, dx3,
proc1,msk_cpt_mapping.surg_length_cd as SL_CD,
msk_cpt_mapping.days as day_window,o.svcdate_form, pay,
table_label
from ccaeo190_ky o
left join lookup_svcscat_svcscatnew on o.svcscat = lookup_svcscat_svcscatnew.svcscat
left join msk_cpt_mapping on o.proc1 = msk_cpt_mapping.cpt_code
where EXISTS
(
select 1
from eoc_op_mapping e
where e.msclmid = o.msclmid
and e.enrolid = o.enrolid
and proc1 =27447
)
ORDER BY svcdate_form, msclmid;
I want to return any row in my ccaeo190_ky table that meets the requirements of the where EXISTS clause on table eoc_op_mapping. Is there any way to achieve these results using joins or select statements?
I was thinking something like:
select lookup_svcscat_svcscatnew.SVCSCAT_NEW_DESC as svc_type,
o.enrolid, o.msclmid, dx1, dx2, dx3,
proc1,msk_cpt_mapping.surg_length_cd as SL_CD,
msk_cpt_mapping.days as day_window,o.svcdate_form, pay,
table_label
from ccaeo190_ky o
left join lookup_svcscat_svcscatnew on o.svcscat = lookup_svcscat_svcscatnew.svcscat
left join msk_cpt_mapping on o.proc1 = msk_cpt_mapping.cpt_code
inner join
(select msclmid, SUM(IF(proc1 = 27447,1,0)) AS cpt
from eoc_op_mapping
group by enrolid
HAVING cpt > 0) e
on e.enrolid = o.enrolid
group by o.enrolid;
But I don't know if this is in the right direction
Usually EXISTS performs better than a join.
If you want to try a join, this the equivalent to your WHERE EXISTS:
.......................................................
inner join (
select distinct msclmid, enrolid
from eoc_op_mapping
where proc1 = 27447
) e on e.msclmid = o.msclmid and e.enrolid = o.enrolid
.......................................................
You can remove distinct if there are no duplicate msclmid, enrolid combinations in eoc_op_mapping.

Access 2010 - optimization a query

Can help me please to optimize this query in database (access 2010), this query to work very slowly (if use it in a big table):
SELECT П1.Code, П1.Field, П1.Number, П1.Data, [П1].[Number]-(select П3.Number from [Table] as П3
where П3.Field = П1.Field
and П3.Data = (select Max(Data)
from [Table] as П2
where П2.Field = П1.Field and П1.Data > П2.Data)) AS Difference
FROM [Table] AS П1
ORDER BY П1.Field, П1.Data;
I'm attach a picture: http://s33.postimg.org/otm859xtb/Table_query.png
Link in the database: http://s000.tinyupload.com/?file_id=06711692152703646964
How about...
SELECT t.Code,t.Field,t.Number,t.Data,t.Number-tm.LastNumber AS Difference
FROM [Table] AS t
INNER JOIN (
SELECT t.Field,t.Number AS LastNumber
FROM [Table] AS t
INNER JOIN (
SELECT Field,MAX(Data) AS MaxData
FROM [Table]
GROUP BY Field
) AS tm ON tm.Field=t.Field AND tm.MaxData=t.Data
) AS tm ON tm.Field=t.Field
The sub-selects can give you some issues with formatting in Access, but should work.
PLease try this one, just the same like Kevin ))
SELECT Table.Code, Table.Field, Table.Number, Table.Data, [Table].[Number]-[t1].[number] AS Difference
FROM
(SELECT Table.Code, Table.Field, Table.Number, Table.Data
FROM [Table] INNER JOIN
(SELECT Table.Field, Max(Table.Data) AS MaxOfData
FROM [Table] GROUP BY Table.Field) AS t2 ON (t2.MaxOfData = Table.Data) AND
(Table.Field = t2.Field)) AS t1 INNER JOIN [Table] ON t1.Field = Table.Field;

Unknown table in field list

I'm trying to build what appears to be a fairly simple nested query, but when I combine the following two working queries I'm getting the following error:
Unknown table 'cm' in field list
ErrorNr. = 1109.
SELECT `cm`.`cans_id` AS `cans_id`,
`cm`.`cans_date` AS `cans_date`,
`cm`.`begintfin` AS `begIntFin`,
`c`.`cans_id` AS `CAN_ID`,
`c`.`q001` AS `q001`,
`c`.`q002` AS `q002`,
`c`.`q093` AS `q093`,
`c`.`q094` AS `q094`,
`c`.`mru` AS `mru2`,
(SELECT Count(0) AS `count(*)`
FROM `cans2notes` `nt`
JOIN `cans2meta` `cm`
ON `cm`.`cans_id` = `nt`.`cans_id`
JOIN `cans2surveys` `c`
ON `c`.`cans_id` = `cm`.`cans_id`
WHERE ( `nt`.`cans_id` = `c`.`cans_id` ));
Any ideas on this would be very appreciated.
You are referring to a table, but there is no FROM or WHERE clause.
Basically you have
select col,
col,
col,
col,
(select count(*) from sometable)
But you don't have a
FROM table cm
INNER JOIN table c
ON CM.ID = C.OTHER_ID
I'm guessing what you are trying to do is this:
SELECT `cm`.`cans_id` AS `cans_id`,
`cm`.`cans_date` AS `cans_date`,
`cm`.`begintfin` AS `begIntFin`,
`c`.`cans_id` AS `CAN_ID`,
`c`.`q001` AS `q001`,
`c`.`q002` AS `q002`,
`c`.`q093` AS `q093`,
`c`.`q094` AS `q094`,
`c`.`mru` AS `mru2`,
`count(*)` AS 'count'
FROM `cans2notes` `nt`
JOIN `cans2meta` `cm`
ON `cm`.`cans_id` = `nt`.`cans_id`
JOIN `cans2surveys` `c`
ON `c`.`cans_id` = `cm`.`cans_id`
WHERE ( `nt`.`cans_id` = `c`.`cans_id` )
GROUP BY `cm`.`cans_id`,
`cm`.`cans_date`,
`cm`.`begintfin`,
`c`.`cans_id`,
`c`.`q001`,
`c`.`q002`,
`c`.`q093`,
`c`.`q094`,
`c`.`mru`;

Troubles with nested queries

I have this query which works perfectly:
SELECT *
FROM Customer
WHERE SacCode IN
(
SELECT SacCode
FROM SacCode
WHERE ResellerCorporateID = 392
ORDER BY SacCode
)
AND CustomerID IN
(
SELECT CxID
FROM CustAppointments
WHERE AppRoomID IN
(
SELECT AppRoomID
FROM ClinicRooms
WHERE ClinID IN
(
SELECT ClinID
FROM AppClinics
WHERE ClinDate >='20090101'
AND ClinDate <='20091119'
)
)
)
However, I need to see the value of ClinDate (inside the last nested query)...
How do I do it?
Thanks.
I'd rewrite the query using joins. Then, you can access any data from any of the joined tables.
For example, you could rewrite your query like this:
SELECT c.*, ac.ClinDate
FROM Customer c
JOIN SacCode sc ON sc.SacCode = c.SacCode
JOIN CustAppointments ca ON ca.CustomerID = c.CustomerID
JOIN ClinicRooms cr ON cr.AppRoomID = ca.AppRoomID
JOIN AppClinic ac ON ac.ClinID = cr.ClinID
WHERE ac.ClinDate >='20090101'
AND ac.ClinDate <='20091119'
AND sc.ResellerCorporateID = 392
Think I'd use derived table in the FROM statement rather than 3 deep nested query, will allow you to access values and will look a LOT better.
You'll need to copy the subselects to the FROM clause or rewrite the query using JOINs.
it should look something like this:
SELECT c.*, a.ClinDate
FROM Customer c
inner join CustAppointments ca
inner join ClinicRooms cr
inner join AppClinics a
where c.SacCode IN
(
SELECT SacCode
FROM SacCode
WHERE ResellerCorporateID = 392
ORDER BY SacCode
)
and c.CustomerID = ca.CxID
and ca.AppRoomID = cr.AppRoomID
and cr.ClinID = a.ClinID
and a.ClinDate >='20090101'
and a.ClinDate <='20091119'