SQL Query to Join Multiple Tables in Sandbox - mysql

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'

Related

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.

(another) LEFT JOIN issue with ACCESS

I want summary data from my 'Data' table for all companies in 'Companies' table including blank rows where there is no record in Data.
If I summarise the data in a nested SELECT clause (or in a stored query i get nothing from the data table. For example
This is the sub select
SELECT transco,
sum(m1) AS Jan15,
FROM data
WHERE (QVmeasure = 'Vol')
GROUP BY QVmeasure, transco
which outputs:
transco Jan15
0292 154373665
1419 134915098
If I use it in a sub select as follows
SELECT c.SAP_Code,
Jan15
FROM Companies AS c
LEFT JOIN (
SELECT transco,
sum(m1) as Jan15
FROM data
WHERE (QVmeasure = 'Vol')
GROUP BY QVmeasure, transco)
AS d
ON c.SAP_Code = d.transco
I get:
SAP_Code Jan15
0292
1419
I can get the correct result via a temporary table:
SELECT sum(m1) as Jan15,
transco
INTO Temp_Table
FROM data
WHERE (QVmeasure = 'Vol')
GROUP BY QVmeasure, transco
followed by
Select c.SAP_code,
jan15
FROM companies AS c
LEFT JOIN Temp_Table as i
ON (c.SAP_Code = i.transco)
giving:
SAP_code jan15
0292 154373665
1419 134915098
1423
but if I use temporary tables I will have to create macros and i want users to be able to run just a query.
The following works for this simple case but I can't apply it in other circumstances:
SELECT c.SAP_Code,
sum(m1) AS Jan15
FROM Companies AS c
LEFT JOIN data as d
ON c.SAP_Code = d.transco
WHERE (d.QVmeasure = 'Vol') OR (d.QVmeasure is null)
GROUP BY d.QVmeasure,c.sap_code
Is there something wrong with my sub select syntax or is it ACCESS (2013)
TIA
You could nest the sub-query like this:
SELECT c.SAP_Code,
(SELECT sum(d.m1)
FROM data AS d
WHERE d.QVmeasure = 'Vol' AND c.SAP_Code = d.transco
) AS [Jan15]
FROM Companies AS c

Mysql joining derived tables

This is one where I'm not sure if there is a better approach or not. maybe I just have a syntax error that I'm not seeing. either way here's the issue.
I am trying to get the number of my "friends" (defined by me following them and them following me ,join 1 creating a derived table) that are also following the object I'm looking at in the database. (join 2). this is in a custom function not a stored procedure so I can reuse it in several places. the only pertinant columns are profile_id and follower_profile_id. profile_id is the profile being followed, follower_profile_id is the one doing the following.
here is the query:
set followsThatCorrelate =
(
Select count(id)
from
(
select * from followers as shouldFollow
join
(
(select *
from followers
where followers.profile_id = authUserId
) as followingMe
join
(select *
from followers
where followers.follower_profile_id = authUserId
) as myFollows
on myFollows.profile_id = followingMe.follower_profile_id
) as friends1
on shouldFollow.follower_profile_id = friends1.follower_profile_id
where shouldFollow.profile_id = otherId
)
);
it says my syntax error is after I alias the derived table friends. no matter what I put after that it always throws the exception.
CREATE TABLE followers (
profile_id NVARCHAR(5),
follower_profile_id NVARCHAR(5)
);
INSERT INTO followers
VALUES ('Tom','Dick'),('Dick','Tom'),('Carry', 'Tom'),('Tom','Carry'),('John','Tom');
SET #authUserID = 'Tom';
SELECT followsme.follower_profile_id as followerID
FROM followers followsme
-- Join back to the followers table
INNER JOIN followers ifollow
-- get all of your followers followers
ON followsme.follower_profile_id = ifollow.profile_id
-- filter your followers followers to only you.
AND ifollow.follower_profile_id = #authUserId
-- this restricts the original set to only your followers
WHERE followsme.profile_id = #authUserId

mysql error code 1248

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

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'