Hive Query issue - Invalid table alias or column reference - mysql

I have a query which contains Case statement and the output of the CASE is used to compare another column in Hive.I am not able to run the same query.Here is the query.
SELECT
AL1.RECORD_ID,
AL1.CARRIER_CODE,
AL1.ORIG_AP_CTY_CDE,
AL1.ORIG_STATE_CODE,
AL1.ORIG_COUNTRY_CODE,
AL1.ORIG_GEOGRAPHIC_ZONE_NUMBER,
CASE
WHEN AL1.ORIG_GEOGRAPHIC_ZONE_NUMBER > 99 THEN CONCAT(CAST(AL1.ORIG_GEOGRAPHIC_ZONE_NUMBER/10 AS INT), '0')
WHEN AL1.ORIG_GEOGRAPHIC_ZONE_NUMBER IN ('1','2','4','6','7','8','9') THEN '000'
ELSE AL1.ORIG_GEOGRAPHIC_ZONE_NUMBER
END AS ALL_ORIG_GEOGRAPHIC_ZONE,
AL1.ORIG_WORLD_AREA_NUMBER,
AL1.DEST_AP_CTY_CDE,
AL1.DEST_STATE_CODE,
AL1.DEST_COUNTRY_CODE,
AL1.DEST_GEOGRAPHIC_ZONE_NUMBER,
CASE
WHEN AL1.DEST_GEOGRAPHIC_ZONE_NUMBER > 99 THEN CONCAT(CAST(AL1.DEST_GEOGRAPHIC_ZONE_NUMBER/10 AS INT), '0')
WHEN AL1.DEST_GEOGRAPHIC_ZONE_NUMBER IN ('1','2','4','6','7','8','9') THEN '000'
ELSE AL1.DEST_GEOGRAPHIC_ZONE_NUMBER
END AS ALL_DEST_GEOGRAPHIC_ZONE,
AL1.DEST_WORLD_AREA_NUMBER,
AL1.FRBS_CDE,
AL1.OW_RT_CDE,
AL1.PUB_RULE_TRF_NUM,
AL1.FARE_RULE_NUM,
AL1.PUB_RTG_NUM,
AL1.PUB_FTNTE_ID_CDE,
AL1.FARE_TYPE_CODE,
AL1.SEASON_TYPE_CODE,
AL1.DAY_OF_WEEK_TYPE_CODE,
AL2.CATEGORY_CONTROL_ID,
AL2.CATEGORY_NUMBER AS GROUP_CATEGORY_NUMBER,
AL2.SEQUENCE_NUMBER,
AL2.LOCATION1_TYPE_CODE,
AL2.LOCATION1_CODE,
AL2.LOCATION2_TYPE_CODE,
AL2.LOCATION2_CODE,
AL2.FARE_CLASS_CODE,
AL2.GENERAL_RULE_TARIFF_NUMBER,
AL2.GENERAL_RULE_NUMBER,
AL2.GENERAL_RULE_IND,
'F' AS REC_IND
FROM TMP_TD_CNSTR_WINNING_FARES AL1
INNER JOIN TMP_TD_CNSTRPOST_CAT_CONTROL_ONLY_LIMITED_F AL2
ON (
AL1.CARRIER_CODE=AL2.CARRIER_CODE
AND AL1.PUB_RULE_TRF_NUM=AL2.TARIFF_NUMBER
AND AL1.FARE_RULE_NUM = AL2.RULE_FOOTNOTE_CODE
AND AL1.PUB_RTG_NUM = AL2.ROUTING_NUMBER
AND AL1.SEASON_TYPE_CODE = AL2.SEASON_TYPE_CODE
AND AL1.PUB_FTNTE_ID_CDE = AL2.FOOTNOTE_CODE
AND AL1.OW_RT_CDE = AL2.OW_RT_IND
AND AL1.FARE_TYPE_CODE = AL2.FARE_TYPE_CODE
AND AL1.DAY_OF_WEEK_TYPE_CODE = AL2. DAY_OF_WEEK_TYPE_CODE
)
INNER JOIN TMP_TD_CNSTRPOST_FRBS_MATCH_F AL4
ON ( AL1.CARRIER_CODE = AL4.CARRIER_CODE
AND AL1.PUB_RULE_TRF_NUM = AL4.PUB_RULE_TRF_NUM
AND AL1.FARE_RULE_NUM = AL4.RULE_NUM
AND AL1.FRBS_CDE = AL4.FRBS_CDE
AND AL2.FARE_CLASS_CODE = AL4.FARE_CLASS_CODE
)
WHERE
( (
(COALESCE(LOCATION1_TYPE_CODE, '') = '') OR
(LOCATION1_TYPE_CODE = 'C' AND LOCATION1_CODE = ORIG_AP_CTY_CDE) OR
(LOCATION1_TYPE_CODE = 'S' AND LOCATION1_CODE = ORIG_STATE_CODE) OR
(LOCATION1_TYPE_CODE = 'N' AND LOCATION1_CODE = ORIG_COUNTRY_CODE) OR
(LOCATION1_TYPE_CODE = 'Z' AND LOCATION1_CODE = ORIG_GEOGRAPHIC_ZONE_NUMBER) OR
(LOCATION1_TYPE_CODE = 'Z' AND LOCATION1_CODE = ALL_ORIG_GEOGRAPHIC_ZONE) OR
(LOCATION1_TYPE_CODE = 'A' AND LOCATION1_CODE = ORIG_WORLD_AREA_NUMBER)
)
AND
(
(COALESCE(LOCATION2_TYPE_CODE, '') = '') OR
(LOCATION2_TYPE_CODE = 'C' AND LOCATION2_CODE = DEST_AP_CTY_CDE) OR
(LOCATION2_TYPE_CODE = 'S' AND LOCATION2_CODE = DEST_STATE_CODE) OR
(LOCATION2_TYPE_CODE = 'N' AND LOCATION2_CODE = DEST_COUNTRY_CODE) OR
(LOCATION2_TYPE_CODE = 'Z' AND LOCATION2_CODE = DEST_GEOGRAPHIC_ZONE_NUMBER) OR
(LOCATION2_TYPE_CODE = 'Z' AND LOCATION2_CODE = ALL_DEST_GEOGRAPHIC_ZONE) OR
(LOCATION2_TYPE_CODE = 'A' AND LOCATION2_CODE = DEST_WORLD_AREA_NUMBER)
)
)
OR
((
(COALESCE(LOCATION1_TYPE_CODE, '') = '') OR
(LOCATION1_TYPE_CODE = 'C' AND LOCATION1_CODE = DEST_AP_CTY_CDE) OR
(LOCATION1_TYPE_CODE = 'S' AND LOCATION1_CODE = DEST_STATE_CODE) OR
(LOCATION1_TYPE_CODE = 'N' AND LOCATION1_CODE = DEST_COUNTRY_CODE) OR
(LOCATION1_TYPE_CODE = 'Z' AND LOCATION1_CODE = DEST_GEOGRAPHIC_ZONE_NUMBER) OR
(LOCATION1_TYPE_CODE = 'Z' AND LOCATION1_CODE = ALL_DEST_GEOGRAPHIC_ZONE) OR
(LOCATION1_TYPE_CODE = 'A' AND LOCATION1_CODE = DEST_WORLD_AREA_NUMBER)
)
AND
(
(COALESCE(LOCATION2_TYPE_CODE, '') = '') OR
(LOCATION2_TYPE_CODE = 'C' AND LOCATION2_CODE = ORIG_AP_CTY_CDE) OR
(LOCATION2_TYPE_CODE = 'S' AND LOCATION2_CODE = ORIG_STATE_CODE) OR
(LOCATION2_TYPE_CODE = 'N' AND LOCATION2_CODE = ORIG_COUNTRY_CODE) OR
(LOCATION2_TYPE_CODE = 'Z' AND LOCATION2_CODE = ORIG_GEOGRAPHIC_ZONE_NUMBER) OR
(LOCATION2_TYPE_CODE = 'Z' AND LOCATION2_CODE = ALL_ORIG_GEOGRAPHIC_ZONE) OR
(LOCATION2_TYPE_CODE = 'A' AND LOCATION2_CODE = ORIG_WORLD_AREA_NUMBER)
)
)
;
The error I am encountering is
FAILED: SemanticException [Error 10004]: Line 72:59 Invalid table alias or column reference 'ALL_ORIG_GEOGRAPHIC_ZONE
Please help me resolving the issue.Thanks in Advance!!

The where clause is evaluated before the select clause, so you can't use ALL_DEST_GEOGRAPHIC_ZONE in where clause.
You can try it in a derived table or having clause.
relevant question

Thanks Jerrick.It worked for me using derived table.Here is the updated query.
SELECT
*
FROM (SELECT
AL1.RECORD_ID,
AL1.CARRIER_CODE,
AL1.ORIG_AP_CTY_CDE,
AL1.ORIG_STATE_CODE,
AL1.ORIG_COUNTRY_CODE,
AL1.ORIG_GEOGRAPHIC_ZONE_NUMBER,
CASE
WHEN AL1.ORIG_GEOGRAPHIC_ZONE_NUMBER > 99 THEN CONCAT((CAST(AL1.ORIG_GEOGRAPHIC_ZONE_NUMBER / 10 AS int)), '0')
WHEN AL1.ORIG_GEOGRAPHIC_ZONE_NUMBER IN ('1', '2', '4', '6', '7', '8', '9') THEN '000'
ELSE AL1.ORIG_GEOGRAPHIC_ZONE_NUMBER
END AS ALL_ORIG_GEOGRAPHIC_ZONE,
AL1.ORIG_WORLD_AREA_NUMBER,
AL1.DEST_AP_CTY_CDE,
AL1.DEST_STATE_CODE,
AL1.DEST_COUNTRY_CODE,
AL1.DEST_GEOGRAPHIC_ZONE_NUMBER,
CASE
WHEN AL1.DEST_GEOGRAPHIC_ZONE_NUMBER > 99 THEN CONCAT((CAST(AL1.DEST_GEOGRAPHIC_ZONE_NUMBER / 10 AS int)), '0')
WHEN AL1.DEST_GEOGRAPHIC_ZONE_NUMBER IN ('1', '2', '4', '6', '7', '8', '9') THEN '000'
ELSE AL1.DEST_GEOGRAPHIC_ZONE_NUMBER
END AS ALL_DEST_GEOGRAPHIC_ZONE,
AL1.DEST_WORLD_AREA_NUMBER,
AL1.FRBS_CDE,
AL1.OW_RT_CDE,
AL1.PUB_RULE_TRF_NUM,
AL1.FARE_RULE_NUM,
AL1.PUB_RTG_NUM,
AL1.PUB_FTNTE_ID_CDE,
AL1.FARE_TYPE_CODE,
AL1.SEASON_TYPE_CODE,
AL1.DAY_OF_WEEK_TYPE_CODE,
AL2.CATEGORY_CONTROL_ID,
AL2.CATEGORY_NUMBER AS GROUP_CATEGORY_NUMBER,
AL2.SEQUENCE_NUMBER,
AL2.LOCATION1_TYPE_CODE,
AL2.LOCATION1_CODE,
AL2.LOCATION2_TYPE_CODE,
AL2.LOCATION2_CODE,
AL2.FARE_CLASS_CODE,
AL2.GENERAL_RULE_TARIFF_NUMBER,
AL2.GENERAL_RULE_NUMBER,
AL2.GENERAL_RULE_IND,
'F' AS REC_IND
FROM TMP_TD_CNSTR_WINNING_FARES AL1
INNER JOIN TMP_TD_CNSTRPOST_CAT_CONTROL_ONLY_LIMITED_F AL2
ON (
AL1.CARRIER_CODE = AL2.CARRIER_CODE
AND AL1.PUB_RULE_TRF_NUM = AL2.TARIFF_NUMBER
AND AL1.FARE_RULE_NUM = AL2.RULE_FOOTNOTE_CODE
AND AL1.PUB_RTG_NUM = AL2.ROUTING_NUMBER
AND AL1.SEASON_TYPE_CODE = AL2.SEASON_TYPE_CODE
AND AL1.PUB_FTNTE_ID_CDE = AL2.FOOTNOTE_CODE
AND AL1.OW_RT_CDE = AL2.OW_RT_IND
AND AL1.FARE_TYPE_CODE = AL2.FARE_TYPE_CODE
AND AL1.DAY_OF_WEEK_TYPE_CODE = AL2.DAY_OF_WEEK_TYPE_CODE
)
INNER JOIN TMP_TD_CNSTRPOST_FRBS_MATCH_F AL4
ON (AL1.CARRIER_CODE = AL4.CARRIER_CODE
AND AL1.PUB_RULE_TRF_NUM = AL4.PUB_RULE_TRF_NUM
AND AL1.FARE_RULE_NUM = AL4.RULE_NUM
AND AL1.FRBS_CDE = AL4.FRBS_CDE
AND AL2.FARE_CLASS_CODE = AL4.FARE_CLASS_CODE
)) AS WF
WHERE ((
(COALESCE(LOCATION1_TYPE_CODE, '') = '')
OR (LOCATION1_TYPE_CODE = 'C'
AND LOCATION1_CODE = ORIG_AP_CTY_CDE)
OR (LOCATION1_TYPE_CODE = 'S'
AND LOCATION1_CODE = ORIG_STATE_CODE)
OR (LOCATION1_TYPE_CODE = 'N'
AND LOCATION1_CODE = ORIG_COUNTRY_CODE)
OR (LOCATION1_TYPE_CODE = 'Z'
AND LOCATION1_CODE = ORIG_GEOGRAPHIC_ZONE_NUMBER)
OR (LOCATION1_TYPE_CODE = 'Z'
AND LOCATION1_CODE = WF.ALL_ORIG_GEOGRAPHIC_ZONE)
OR (LOCATION1_TYPE_CODE = 'A'
AND LOCATION1_CODE = ORIG_WORLD_AREA_NUMBER)
)
AND (
(COALESCE(LOCATION2_TYPE_CODE, '') = '')
OR (LOCATION2_TYPE_CODE = 'C'
AND LOCATION2_CODE = DEST_AP_CTY_CDE)
OR (LOCATION2_TYPE_CODE = 'S'
AND LOCATION2_CODE = DEST_STATE_CODE)
OR (LOCATION2_TYPE_CODE = 'N'
AND LOCATION2_CODE = DEST_COUNTRY_CODE)
OR (LOCATION2_TYPE_CODE = 'Z'
AND LOCATION2_CODE = DEST_GEOGRAPHIC_ZONE_NUMBER)
OR (LOCATION2_TYPE_CODE = 'Z'
AND LOCATION2_CODE = WF.ALL_DEST_GEOGRAPHIC_ZONE)
OR (LOCATION2_TYPE_CODE = 'A'
AND LOCATION2_CODE = DEST_WORLD_AREA_NUMBER)
)
)
OR ((
(COALESCE(LOCATION1_TYPE_CODE, '') = '')
OR (LOCATION1_TYPE_CODE = 'C'
AND LOCATION1_CODE = DEST_AP_CTY_CDE)
OR (LOCATION1_TYPE_CODE = 'S'
AND LOCATION1_CODE = DEST_STATE_CODE)
OR (LOCATION1_TYPE_CODE = 'N'
AND LOCATION1_CODE = DEST_COUNTRY_CODE)
OR (LOCATION1_TYPE_CODE = 'Z'
AND LOCATION1_CODE = DEST_GEOGRAPHIC_ZONE_NUMBER)
OR (LOCATION1_TYPE_CODE = 'Z'
AND LOCATION1_CODE = WF.ALL_DEST_GEOGRAPHIC_ZONE)
OR (LOCATION1_TYPE_CODE = 'A'
AND LOCATION1_CODE = DEST_WORLD_AREA_NUMBER)
)
AND (
(COALESCE(LOCATION2_TYPE_CODE, '') = '')
OR (LOCATION2_TYPE_CODE = 'C'
AND LOCATION2_CODE = ORIG_AP_CTY_CDE)
OR (LOCATION2_TYPE_CODE = 'S'
AND LOCATION2_CODE = ORIG_STATE_CODE)
OR (LOCATION2_TYPE_CODE = 'N'
AND LOCATION2_CODE = ORIG_COUNTRY_CODE)
OR (LOCATION2_TYPE_CODE = 'Z'
AND LOCATION2_CODE = ORIG_GEOGRAPHIC_ZONE_NUMBER)
OR (LOCATION2_TYPE_CODE = 'Z'
AND LOCATION2_CODE = WF.ALL_ORIG_GEOGRAPHIC_ZONE)
OR (LOCATION2_TYPE_CODE = 'A'
AND LOCATION2_CODE = ORIG_WORLD_AREA_NUMBER)
)
)
;

Related

How can I optimize my query it is taking too much time in mysql

Here I'm fetching data using mysql queries but it is taking time around 10 seconds 20 seconds so on ,even though all the columns are indexed. so I'm new to the optimization so please help me
SELECT a.cname,
(SELECT status_name
FROM persontype_status
WHERE status = a.p3_status
LIMIT 1) AS
company_status_psp3,
a.curl,
a.cid,
a.pid,
a.addedbyuser,
a.no_of_emp,
( CASE
WHEN a.lead_status > 0
AND ( a.status = 0
OR a.status > 0 ) THEN (SELECT ( Max(Date(dateadded)) )
FROM d3_info
WHERE cid = a.cid
GROUP BY cid)
WHEN ( a.status > 0
AND a.lead_status = 0 ) THEN (SELECT ( Min(Date(dateadded)) )
FROM sperson_info
WHERE cid = a.cid
GROUP BY cid)
ELSE 0
end ) AS date_new,
a.rank,
a.lead_status,
a.status,
a.magid,
(SELECT pname
FROM personinfo
WHERE pid = a.pid) AS pname,
p1_status,
Count(c.email_id) AS total_mail,
Count(IF(s.mail_type IN( 'First Mail', 'Reminder 1' )
AND c.person_type = 'P1'
AND d.send_status = 1, c.email_id, NULL)) AS t_p1,
Count(IF(s.mail_type IN( 'First Mail', 'Reminder 1' )
AND c.person_type = 'P2'
AND d.send_status = 1, c.email_id, NULL)) AS t_p2,
Count(IF(( s.mail_type IN( 'First Mail', 'Reminder 1' )
AND c.person_type = 'P3'
AND d.send_status = 1 ), c.email_id, NULL)) AS t_p3,
Count(DISTINCT IF(c.person_type = 'P3'
AND c.email_id != '', c.email_id, NULL)) AS t_p3_e,
Count(IF(( s.mail_type IN( 'First Mail', 'Reminder 1' )
AND c.person_type = 'P4'
AND d.send_status = 1 ), c.email_id, NULL)) AS t_p4,
Count(DISTINCT IF(c.person_type = 'P4'
AND c.email_id != '', c.email_id, NULL)) AS t_p4_e,
Count(IF(( s.mail_type IN( 'First Mail', 'Reminder 1' )
AND c.person_type = 'P5'
AND d.send_status = 1 ), c.email_id, NULL)) AS t_p5,
Count(DISTINCT IF(c.person_type = 'P5'
AND c.email_id != '', c.email_id, NULL)) AS t_p5_e,
Count(IF(( s.mail_type IN( 'First Mail', 'Reminder 1' )
AND c.person_type = 'P6'
AND d.send_status = 1 ), c.email_id, NULL)) AS t_p6,
Count(DISTINCT IF(c.person_type = 'P6'
AND c.email_id != '', c.email_id, NULL)) AS t_p6_e,
Count(IF(( s.mail_type IN( 'First Mail', 'Reminder 1' )
AND c.person_type = 'P7'
AND d.send_status = 1 ), c.email_id, NULL)) AS t_p7,
Count(DISTINCT IF(c.person_type = 'P7'
AND c.email_id != '', c.email_id, NULL)) AS t_p7_e,
Count(IF(( s.mail_type IN( 'First Mail', 'Reminder 1' )
AND c.person_type = 'P8'
AND d.send_status = 1 ), c.email_id, NULL)) AS t_p8,
Count(DISTINCT IF(c.person_type = 'P8'
AND c.email_id != '', c.email_id, NULL)) AS t_p8_e FROM mperson_companies a
INNER JOIN aperson_detail c
ON a.cid = c.cid
AND a.pid = c.pid
LEFT JOIN compose d
ON c.email_id = d.receiver_email
AND c.pid = d.pid
LEFT JOIN session_person s
ON d.session_id = s.session_id WHERE a.lead_status = 0
AND a.flag != 'q'
AND a.approval != 5
AND a.pid = 2832
AND a.rank > 1
AND c.email_id != '' GROUP BY a.cid HAVING t_p1 = 2
AND t_p2 = 2
AND IF(t_p3_e = 1, t_p3 = 2, 1 = 1)
AND IF(t_p4_e = 1, t_p4 = 2, 1 = 1)
AND IF(t_p5_e = 1, t_p5 = 2, 1 = 1)
AND IF(t_p6_e = 1, t_p6 = 2, 1 = 1)
AND IF(t_p7_e = 1, t_p7 = 2, 1 = 1)
AND IF(t_p8_e = 1, t_p8 = 2, 1 = 1) ORDER BY date_new DESC;
Need to see SHOW CREATE TABLE and EXPLAIN SELECT.
Meanwhile, this
WHERE a.lead_status = 0
AND a.flag != 'q'
AND a.approval != 5
AND a.pid = 2832
AND a.rank > 1
AND c.email_id != ''
begs for
INDEX(lead_status, pid, -- in either order
flag, approval, -- in either order
rank) -- last
and this
SELECT ( Min(Date(dateadded)) )
FROM sperson_info
WHERE cid = a.cid
GROUP BY cid)
does not need to include GROUP BY cid, since it will return only the one item.
And
( a.status = 0 OR a.status > 0 )
can be simply
a.status >= 0
But, what values are there for status? Inequalities are sometimes less efficient than equalities.
And this
Count(IF(s.mail_type IN( 'First Mail', 'Reminder 1' )
AND c.person_type = 'P2'
AND d.send_status = 1, c.email_id, NULL)
)
can be shortened to
SUM( s.mail_type IN( 'First Mail', 'Reminder 1' )
AND c.person_type = 'P2'
AND d.send_status = 1 )
There may be more suggestions; this could take a few iterations.

SUM and GROUP BY diferent columns

I need to group the sum by different columns:
SUM1 group by Banco_Recarga
SUM2 group by BancoRetiro
SELECT Banco_Recarga, BancoRetiro, bancos_recargas.banco,
SUM(CASE WHEN Tipo='1' AND (Status = '1' OR Status = '2') THEN '1' ELSE 0 END) AS SUM1,
SUM(CASE WHEN Tipo='2' AND (Status = '1' OR Status = '2') THEN '1' ELSE 0 END) AS SUM2
FROM transaccionesrr
INNER JOIN bancos_recargas ON (transaccionesrr.Banco_Recarga = bancos_recargas.id)
WHERE Fecha_Recarga BETWEEN '2017-11-01' AND '2017-11-10' GROUP BY Banco_Recarga ORDER BY SUM1;
I have been looking for hours but i can't find a solution
Try this:
SELECT
CASE
WHEN Tipo = '1' AND (Status = '1' OR Status = '2') THEN Banco_Recarga
WHEN Tipo = '2' AND (Status = '1' OR Status = '2') THEN BancoRetiro
ELSE 'Unexpected'
END AS banco
, SUM(CASE WHEN Tipo = '1' AND (Status = '1' OR Status = '2') THEN '1' ELSE 0 END) AS sum1
, SUM(CASE WHEN Tipo = '2' AND (Status = '1' OR Status = '2') THEN '1' ELSE 0 END) AS sum2
FROM transaccionesrr
INNER JOIN bancos_recargas ON (transaccionesrr.Banco_Recarga = bancos_recargas.id)
WHERE Fecha_Recarga BETWEEN '2017-11-01' AND '2017-11-10'
GROUP BY
CASE
WHEN Tipo = '1' AND (Status = '1' OR Status = '2') THEN Banco_Recarga
WHEN Tipo = '2' AND (Status = '1' OR Status = '2') THEN BancoRetiro
ELSE 'Unexpected'
END
ORDER BY sum1, sum2
You may need to add conditions into the where clause.

Why the query is giving result while the destCurrency is not 0 -

SELECT *
FROM xyz
WHERE applyat = 'anita'
AND applyAt = 'Create'
AND country = '50'
AND dest_country = '108'
AND originCurrency = 'EUR'
AND destCurrency = 0 AND isEnable = 'Y'
AND agentID = 0
AND Module = 'abc'
ORDER BY ruleID DESC
You should add quotes as it is a varchar
SELECT * FROM xyz WHERE applyat = 'anita' AND applyAt = 'Create' AND country = '50' AND dest_country = '108' AND originCurrency = 'EUR' AND destCurrency = '0' AND isEnable = 'Y' And agentID = 0 AND Module = 'abc' ORDER BY ruleID DESC

not retrieving exact result from case statement

I am using below query but Y column not retrieving exact result. Could you guys please help me out
SELECT FAPI.*,
CASE WHEN (SELECT DISTINCT 'Y'
FROM FLOOR_PI FAPI,
NUATON NUMF
WHERE FAPI.BRCH = NUMF.BRCH
AND FAPI.BASE = NUMF.BASE
AND FAPI.NUM = 0
AND NVL(FAPI.RATION, 'X') <> 'D'
AND FAPI.CODE = 'A'
AND NUMF.R_DATE
BETWEEN FAPI.EFF_DATE
AND FAPI.EXP_DATE ) = 'Y'
THEN 'Y'
ELSE 'N' END NEW
FROM FLOOR_PI FAPI
Thank you,
Rave
Based on #SlimsGohst comment:
You should replace FAPIx with FAPI1 or FAPI2 whichever is applicable
SELECT FAPI1.*,
CASE WHEN (SELECT DISTINCT 'Y'
FROM FLOOR_PI FAPI2,
NUATON NUMF
WHERE FAPIx.BRCH = NUMF.BRCH
AND FAPIx.BASE = NUMF.BASE
AND FAPIx.NUM = 0
AND NVL(FAPIx.RATION, 'X') <> 'D'
AND FAPIx.CODE = 'A'
AND NUMF.R_DATE
BETWEEN FAPIx.EFF_DATE
AND FAPIx.EXP_DATE ) = 'Y'
THEN 'Y'
ELSE 'N' END NEW
FROM FLOOR_PI FAPI1

Slow MySQL replace / complex query

I am working on a MySQL query that needs to go through 1+ million rows in table A and 5+ million in table B. The query selects all unique people from table A and inner joins with the sales for each person on table B. Index is set on both tables where necessary.
The goal here is to add all unique Email addresses from table A with data from table B into table C.
I am looking for the most optimized way to do this. Included is the select part of a REPLACE INTO query being used that will pull records in the specified id's. It is a simple CASE statement with an IN condition.
I also have the exact same query using NOT IN vs IN for the sub query. That one times out.
Looking forward to any help anyone can provide and hopefully a more optimal way of doing this.
SELECT
c.Email,
MAX(c.Birthdate)
UPPER(c.Deleted) AS Deleted,
UPPER(c.Inactive) AS Inactive,
UPPER(c.SendEmail) AS SendEmail,
CONCAT('[',GROUP_CONCAT(DISTINCT c.Site SEPARATOR ']['),']') AS SITEID,
CONCAT('[',GROUP_CONCAT(DISTINCT c.Studio SEPARATOR ']['),']') AS STUDIO,
( SELECT CONCAT('[',GROUP_CONCAT(DISTINCT s2.Service SEPARATOR ']['),']')
FROM my_example c2
INNER JOIN my_example_sales s2 ON s2.idMember = c2.idMember AND s2.Site = c2.Site WHERE c2.Email = c.Email
) as SERVICES,
( SELECT MAX(date(s1.Date)) as Date
FROM my_example_sales s1
WHERE s1.idMember = c.idMember
AND s1.Site = c.Site
AND (CASE WHEN s1.Site = '1' THEN s1.ProductID IN ('1','6','7','12','18','22')
WHEN s1.Site = '2' THEN s1.ProductID = '156'
WHEN s1.Site = '3' THEN s1.ProductID IN ('3','5','6')
WHEN s1.Site = '4' THEN s1.ProductID IN ('11','15')
WHEN s1.Site = '5' THEN s1.ProductID = '23'
WHEN s1.Site = '6' THEN s1.ProductID = '23'
WHEN s1.Site = '7' THEN s1.ProductID = '23'
WHEN s1.Site = '8' THEN s1.ProductID = '23'
WHEN s1.Site = '9' THEN s1.ProductID = '23'
WHEN s1.Site = '10' THEN s1.ProductID IN ('7','11','17','30','31')
WHEN s1.Site = '11' THEN s1.ProductID = '23'
WHEN s1.Site = '12' THEN s1.ProductID IN ('7','11','17','30','31')
WHEN s1.Site = '13' THEN s1.ProductID = '23' END)
WHERE 1
ORDER BY s1.Date DESC
LIMIT 0,1
) as lastPurchaseFreeWeek,
NOW() as dateModified
FROM my_example c
WHERE c.Email !=''
GROUP BY c.Email
ORDER BY c.ModDate DESC
Moving your subselects to joins should yield some performance increase:
SELECT
c.Email,
MAX(c.Birthdate)
UPPER(c.Deleted) AS Deleted,
UPPER(c.Inactive) AS Inactive,
UPPER(c.SendEmail) AS SendEmail,
CONCAT('[',GROUP_CONCAT(DISTINCT c.Site SEPARATOR ']['),']') AS SITEID,
CONCAT('[',GROUP_CONCAT(DISTINCT c.Studio SEPARATOR ']['),']') AS STUDIO,
c2.SERVICES,
s1.`Date` as lastPurchaseFreeWeek,
NOW() as dateModified
FROM my_example c
INNER JOIN (
SELECT CONCAT('[',GROUP_CONCAT(DISTINCT s2.Service SEPARATOR ']['),']') AS SERVICES
FROM my_example c2
INNER JOIN my_example_sales s2 ON s2.idMember = c2.idMember AND s2.Site = c2.Site
) c2 ON c2.Email = c.Email
INNER JOIN (
SELECT MAX(date(s1.Date)) as `Date`
FROM my_example_sales s1
WHERE (CASE WHEN s1.Site = '1' THEN s1.ProductID IN ('1','6','7','12','18','22')
WHEN s1.Site = '2' THEN s1.ProductID = '156'
WHEN s1.Site = '3' THEN s1.ProductID IN ('3','5','6')
WHEN s1.Site = '4' THEN s1.ProductID IN ('11','15')
WHEN s1.Site = '5' THEN s1.ProductID = '23'
WHEN s1.Site = '6' THEN s1.ProductID = '23'
WHEN s1.Site = '7' THEN s1.ProductID = '23'
WHEN s1.Site = '8' THEN s1.ProductID = '23'
WHEN s1.Site = '9' THEN s1.ProductID = '23'
WHEN s1.Site = '10' THEN s1.ProductID IN ('7','11','17','30','31')
WHEN s1.Site = '11' THEN s1.ProductID = '23'
WHEN s1.Site = '12' THEN s1.ProductID IN ('7','11','17','30','31')
WHEN s1.Site = '13' THEN s1.ProductID = '23' END)
) s1 ON ( s1.idMember = c.idMember AND s1.Site = c.Site )
WHERE c.Email !=''
GROUP BY c.Email
ORDER BY c.ModDate DESC