mysql error code 1248 - mysql

I am getting an error code 1248 and have no idea why. Thanks in advance.
select substr(cbt,1,8) day,
max((delta_GTP_InDataPktGn+delta_GTP_OutDataPktGn))/900
from
(
select a.cbt, a.datetime,
a.GTP_InDataPktGn - b.GTP_InDataPktGn as delta_GTP_InDataPktGn,
a.GTP_OutDataPktGn - b.GTP_OutDataPktGn as delta_GTP_OutDataPktGn
from
( select cbt, STR_TO_DATE(substr(cbt,1,12),'%Y%m%d%H%i') as datetime,
GTP_InDataPktGn,
GTP_OutDataPktGn
from sgsn_GTP
) a
right join
(
select cbt, STR_TO_DATE(substr(cbt,1,12),'%Y%m%d%H%i') as datetime,
GTP_InDataPktGn,
GTP_OutDataPktGn
from sgsn_GTP
)
on a.datetime = addtime(b.datetime,"00:15:00")
) z
where z.cbt between 20130429000000 and 20130430980000
group by day

Error 1248 is ER_DERIVED_MUST_HAVE_ALIAS, "Every derived table must have its own alias".
The derived table on the right of your join has no alias:
right join
(
select cbt, STR_TO_DATE(substr(cbt,1,12),'%Y%m%d%H%i') as datetime,
GTP_InDataPktGn,
GTP_OutDataPktGn
from sgsn_GTP
)
-- ^ missing b

Related

SQL Query to Join Multiple Tables in Sandbox

I'm writing a query to combine data from multiple tables within a sandbox environment. I'm getting a SQL 1100 error message on first CREATE TABLE after the DROP.
Error message:
SQL Error [1100] [HY000]: ERROR: 'CREATE TABLE
Sandbox..CF_Promocodeused AS ( SELECT DISTINCT visitid, visitdate,
post_evar23, post_evar16, post_evar17, post_evar10 FROM
IDM..HITDATAHISTORY WHERE brandcode = ‘FDM’ AND post_evar23 IS NOT
NULL )' error
^ found "‘" (at char 179) Illegal initial character in identifier
I welcome any ideas from the community. Thank you in advance.
Code used:
DROP TABLE Sandbox..CF_Promocodeused IF EXISTS;
CREATE TABLE Sandbox..CF_Promocodeused AS
(
SELECT DISTINCT visitid, visitdate, post_evar23, post_evar16, post_evar17, post_evar10
FROM IDM..HITDATAHISTORY
WHERE brandcode = ‘FDM’
AND post_evar23 IS NOT NULL
)
;
DROP TABLE Sandbox..CF_Promocodeused1 IF EXISTS;
CREATE TABLE Sandbox..CF_Promocodeused1 AS (
SELECT DISTINCT post_evar23, visitdate, post_evar16, post_evar17, post_evar10, COUNT(DISTINCT visitid) AS visits, calendardate, subtotalamount
FROM IDM..Sandbox..CF_Promocodeused AS h
INNER JOIN IDM..ORDERFACT AS o ON h.ordernumber = o.ordernumber
INNER JOIN IDM..DATEDIM AS d ON d.fiscalmonthname = h.fiscalmonthname
WHERE calendardate BETWEEN '01/27/2021 00:00:00’ AND 02/23/2021 23:59:59'
AND subtotalamount >= '200.00'

MySQL query: How to fix Error 1052 (ambiguous column)

I keep getting an error when I run both of the following queries that the CUST_NUM is ambiguous. How can I fix this?
SELECT INV_NUM, CUST_NUM, CUST_LNAME, CUST_FNAME, INV_DATE, INV_AMOUNT
FROM CH08_INVOICE i
INNER JOIN CH08_CUSTOMER c1 ON (i.CUST_NUM = c1.CUST_NUM)
WHERE CUST_BALANCE>=1000;
SELECT CUST_LNAME, CUST_FNAME
FROM CH08_CUSTOMER c1 JOIN CH08_CUSTOMER_2 c2
ON (c1.CUST_LNAME = c2.CUST_LNAME AND c1.CUST_FNAME = c2.CUST_FNAME);
SELECT i.INV_NUM, i.CUST_NUM, i.CUST_LNAME, i.CUST_FNAME, i.INV_DATE, i.INV_AMOUNT FROM CH08_INVOICE i INNER JOIN CH08_CUSTOMER c1 ON (i.CUST_NUM = c1.CUST_NUM) WHERE i.CUST_BALANCE>=1000;
SELECT c1.CUST_LNAME, c1CUST_FNAME FROM CH08_CUSTOMER c1 JOIN CH08_CUSTOMER_2 c2 ON (c1.CUST_LNAME = c2.CUST_LNAME AND c1.CUST_FNAME = c2.CUST_FNAME);
Please check this query
Ambiguous column means the database don't know which table it must use the column.
Try using
SELECT INV_NUM, i.CUST_NUM ...
or
SELECT INV_NUM, c1.CUST_NUM ...
for explicity defining the table.

MySQL query error 1064 (syntax error)

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.

How do I do a SQL JOIN on 3 tables?

I am using MySQL and I am trying to get data out of three different tables but am not sure of the syntax that I should use.
I have a table called full_daily_data_1 with the following fields
Symbol
Trade_Date
Day_Open
Day_High
I also have a table called custom_indices_xref with the following fields:
symbol
Custom_Index_Name
And a table called daily_index_weightings with the following fields:
Custom_Index_Name
Symbol
Trade_date
combo_weighting
Right now, I have this as a select statement to get the data I need out of two tables:
SELECT
Symbol,
Trade_Date,
Day_Open,
Day_High
FROM
full_daily_data_1
WHERE
trade_date >= '2012/01/01' AND
trade_date <= '2012/01/31' AND
symbol in (SELECT symbol from custom_indices_xref WHERE Custom_Index_Name = 'Agricultural-Chemical-and-Fertilizer-Stocks');
But what I want is the following data for each symbol for each date in the date range
symbol
Day_Open
Day_High,
Custom_Index_Name
Symbol
Trade_date
combo_weighting
So basically, needing to add combo_weighting from the daily_index_weightings table for the selected days and symbols. What should my SQL statement look like to accomplish this?
I tried this, but I get a SQL syntax error, so not sure what I'm doing wrong:
SELECT
full_daily_data_1.Symbol,
full_daily_data_1.Trade_Date,
full_daily_data_1.Day_Open,
full_daily_data_1.Day_High,
full_daily_data_1.Day_Low,
daily_index_weightings.combo_weighting
FROM
full_daily_data_1
WHERE
trade_date >= '2012/01/01' AND
trade_date <= '2012/01/31' AND
Symbol in (SELECT symbol from custom_indices_xref WHERE Custom_Index_Name = 'Agricultural-Chemical-and-Fertilizer-Stocks')
JOIN
daily_index_weightings ON
daily_index_weightings.symbol = full_daily_data_1.Symbol AND
daily_index_weightings.Trade_Date = full_daily_data_1.Trade_Date ;
SELECT fdd.Symbol, fdd.Trade_Date, fdd.Day_Open, fdd.Day_High, fdd.Day_Low, diw.combo_weighting
FROM full_daily_data_1 fdd
INNER JOIN custom_indicies_xref cix ON fdd.symbol=cix.symbol
INNER JOIN daily_index_weighings diw ON fdd.symbol = diw.symbol
WHERE
trade_date >= '2012/01/01' AND
trade_date <= '2012/01/31' AND
cix.Custom_Index_Name = 'Agricultural-Chemical-and-Fertilizer-Stocks'
The INNER JOINS could be changed to LEFT JOIN if required.
Alternatively, since your second and third tables also match the Custom_Index_Name fields (which is bad design, by the way):
SELECT fdd.Symbol, fdd.Trade_Date, fdd.Day_Open, fdd.Day_High, fdd.Day_Low, diw.combo_weighting
FROM full_daily_data_1 fdd
INNER JOIN (custom_indicies_xref cix ON fdd.symbol=cix.symbol
INNER JOIN daily_index_weighings diw ON cix.Custom_Index_Name = diw.Custom_Index_Name)
ON fdd.symbol = cix.symbol
WHERE
trade_date >= '2012/01/01' AND
trade_date <= '2012/01/31' AND
cix.Custom_Index_Name = 'Agricultural-Chemical-and-Fertilizer-Stocks'
Your sql clauses are out of order and you should use alias for your table names, just makes it easier to read. Another thing I noticed is your fields in your WHERE clause are ambiguous.
SELECT
FDD.Symbol,
FDD.Trade_Date,
FDD.Day_Open,
FDD.Day_High,
FDD.Day_Low,
DIW.combo_weighting
FROM
full_daily_data_1 FDD
JOIN
daily_index_weightings DIW ON
DIW.symbol = FDD.Symbol AND
DIW.Trade_Date = FDD.Trade_Date ;
WHERE
FDD.trade_date >= '2012/01/01' AND
FDD.trade_date <= '2012/01/31' AND
FDD.Symbol in
(SELECT symbol from custom_indices_xref WHERE Custom_Index_Name = 'Agricultural-Chemical-and-Fertilizer-Stocks')

Invalid use of group function while updating a table with sum function

I have two tables: o_daily_lcsgeneration and o_daily_generation.
While trying to update the o_daily_generation I receive an error saying:
error(1111) invalid use of Group function
Here is the code I am running:
update o_daily_generation join o_daily_lcsgeneration
on o_daily_generation.Location =o_daily_lcsgeneration.Location
and o_daily_generation.Date =o_daily_lcsgeneration.Date
set o_daily_lcsgeneration.Turbine_Generation =sum(o_daily_generation.Turbine_Generation)
Try this instead:
UPDATE o_daily_generation od
INNER JOIN
(
SELECT Location, SUM(Turbine_Generation) TurbineSum
FROM o_daily_lcsgeneration
GROUP BY Location
) og ON od.Location = og.Location
SET od.Turbine_Generation = og.TurbineSum
I did update the above query based on above answer but there is an issue.
UPDATE tmpTotal t1
INNER JOIN
(
SELECT thirdlevel_delivery_id, MAX(baseline_id) MaxBaseLineId
FROM tmpTotal
GROUP BY release_id
) t2 ON t1.release_id = t2.release_id
SET t1.planned_delivery_date = t2.MaxBaseLineId;
It gives
Error Code : 1054
Unknown column 't2.release_id' in 'on clause'