SELECT Failed 3706: Syntax error: Expected something between - teradata-sql-assistant

Novice would greatly appreciate assistance on the following 3706 error message:
expected something between the word 'Calls' and the 'case' keyword
SELECT distinct
b.emp_nbr,
b.prim_terr_desc,
a.sales_terr_nbr,
TRIM (B.first_nm) ||' '|| TRIM (B.last_nm) as AE_Name,
count (distinct call_nbr) as Calls
case (when a.sale_call_chanl_desc LIKE 'Face to Face%' then 'F2F'
when a.sale_call_chanl_desc = 'Telephone' then 'Phone'
when a.sale_call_chanl_desc LIKE 'Web Meeting%' then 'Web_Meeting'
end) as Channel
FROM isell_prod_view_db.sfdc_call_action a
LEFT OUTER JOIN isell_prod_view_db.sfdc_customer_info c ON (a.cust_acct_nbr = c.cust_acct_nbr and a.rec_delt_flg = 'N')
LEFT OUTER JOIN isell_prod_view_db.sfdc_opportunity d ON (d.cust_acct_nbr = c.cust_acct_nbr and d.delt_flg = 'N')
LEFT OUTER JOIN isell_prod_view_db.sfdc_user_profile b ON a.crte_by_user_key_nbr = b.user_key_nbr
LEFT OUTER JOIN isell_prod_view_db.sfdc_opportunity_item e ON (e.oprty_key_nbr = d.oprty_key_nbr and e.delt_flg = 'N')
where a.sale_call_stat_desc = 'Completed'
and a.sales_div_nbr = '8'
and a.sales_grp_nbr = '9'
and a.sales_org_nbr ='30'
and b.prim_terr_desc LIKE ('8-9-30%')
and a.priv_entr_flg='N'
and a.sale_call_chanl_desc is not null
and CAST(a.call_dt AS date format 'MM/DD/YYYY') between '06/01/2019' and '05/31/2020'
group by 1,2,3,4

I rejigged the code. This works. I think i needed a comma after "AS Channel"
SELECT distinct
b.emp_nbr,
TRIM (B.first_nm) ||' '|| TRIM (B.last_nm) as AE_Name,
b.prim_terr_desc,
b.prim_terr_seg_nbr,
case when CAST(a.call_dt AS date format 'MM/DD/YYYY') between '12/01/2019' and '02/29/2020' then 'Q3'
when CAST(a.call_dt AS date format 'MM/DD/YYYY') between '03/01/2020' and '05/31/2020' then 'Q4'
end as Qtr,
count (distinct call_nbr) as Calls
FROM isell_prod_view_db.sfdc_call_action a
LEFT OUTER JOIN isell_prod_view_db.sfdc_customer_info c ON (a.cust_acct_nbr = c.cust_acct_nbr and a.rec_delt_flg = 'N')
LEFT OUTER JOIN isell_prod_view_db.sfdc_opportunity d ON (d.cust_acct_nbr = c.cust_acct_nbr and d.delt_flg = 'N')
LEFT OUTER JOIN isell_prod_view_db.sfdc_user_profile b ON a.crte_by_user_key_nbr = b.user_key_nbr
LEFT OUTER JOIN isell_prod_view_db.sfdc_opportunity_item e ON (e.oprty_key_nbr = d.oprty_key_nbr and e.delt_flg = 'N')
where a.sale_call_stat_desc = 'Completed'
and a.sales_div_nbr = '8'
and a.sales_grp_nbr = '9'
and a.sales_org_nbr ='30'
and b.prim_terr_desc LIKE ('8-9-30%')
and A.rec_delt_flg = 'N'
and A.child_event_flg='N'
and a.priv_entr_flg='N'
and B.prim_terr_seg_desc not in ('No Coverage')
and A.sale_call_chanl_desc in ('Telephone', 'Phone', 'Face to Face - Appt', 'Face to Face - No Appt', 'Web Meeting')
and B.prim_terr_seg_nbr not in ('3', '8', '25')
and CAST(a.call_dt AS date format 'MM/DD/YYYY') between '12/01/2019' and '05/31/2020'
group by 1,2,3,4,5

Related

SELECT Statement should return 0 when condition is not met

How can I make following MYSQL statment return 0 in case that the condition is not met.
(SELECT Sum(B.trblksize) AS RxData
FROM referencecoexistence_ber_lte B,
(SELECT C.*
FROM referencecoexistence C
WHERE `sw_ver` = '0.4'
AND `lte_n_frames` = '50'
AND `lte_rb` = '6'
AND `lte_mcs` = '11'
AND `lte_p_mw` = '6.000000e-03'
AND `lte_vmsf_type` = 'BS'
AND `lte_vmsf_subframes` = '8 9'
AND `wlan_mcs` = '5'
AND `wlan_p_mw` = '100'
AND `channel` = 'A330'
AND `lte_freq_hz` = '2403000000'
AND `wlan_freq_hz` = '2412000000'
AND `wlan_ieee` = '802.11n') AS TableX
WHERE TableX.id = B.id
AND B.ber = '0'
GROUP BY lte_dist_m)
The result is: Empty set (0.280 sec)
If the condition includes B.ber = '0' the expected output is:
A result with b.ber = 0 looks like:
RxData
416342016
433004544
...
In my case it would be sufficient if it simply returns one entry:
RxData
0
This statement is embedded in a larger statement that calculates some throughput.
Is it possible without executing the statement twice?
Here we'll change this to a LEFT OUTER JOIN. We will move the B.ber = '0' condition into the FROM clause, because with an outer join, this will give us different results than if the condition is in the WHERE clause.
SELECT Sum(B.trblksize) AS RxData
FROM (SELECT C.*
FROM referencecoexistence C
WHERE `sw_ver` = '0.4'
AND `lte_n_frames` = '50'
AND `lte_rb` = '6'
AND `lte_mcs` = '11'
AND `lte_p_mw` = '6.000000e-03'
AND `lte_vmsf_type` = 'BS'
AND `lte_vmsf_subframes` = '8 9'
AND `wlan_mcs` = '5'
AND `wlan_p_mw` = '100'
AND `channel` = 'A330'
AND `lte_freq_hz` = '2403000000'
AND `wlan_freq_hz` = '2412000000'
AND `wlan_ieee` = '802.11n') AS TableX
LEFT OUTER JOIN referencecoexistence_ber_lte B
ON TableX.id = B.id
AND B.ber = '0'
GROUP BY lte_dist_m
Now when B.ber is zero, it will act like an inner join. However, when B.ber is not zero, then the inner join result will be missing from the join, but the outer join will include the result with all the columns of the B table, but NULL for all the columns of the TableX table. So those records will have B.trblksize as NULL, and their sum will be NULL.
I solved the problem by simply using following approach
(SELECT SUM(IF(T_.BER =0,T_.TrBlkSize,0)) as RxData, Lte_Dist_m FROM
(ReferenceCoexistence WHERE <CONDITION LIST>) as X,
ReferenceCoexistence_BER_Lte as T_
WHERE X.ID = T_.ID GROUP BY Lte_Dist_m)

Data Conversion while creating table in MYSQL

Error while creating table but the SQL is retrieving result
Please find the below code, the below SQL by itself is working fine retrieving results!
SELECT
CASE WHEN J.JOBID = DJ.JOBNUMBER
THEN DJ.JOBID ELSE J.JOBID
END AS JOBID,
J.DISTRICTID,
CASE WHEN D.DISTRICTID = J.DISTRICTID
THEN D.NAME ELSE NULL
END AS DISTRICT_NAME,
D.SEGMENTID,
CASE WHEN D.SEGMENTID = S.SEGMENTID AND D.DISTRICTID = J.DISTRICTID
THEN S.NAME ELSE NULL
END AS SEGMENT,
J.STARTTIME,
J.ENDTIME,
SJ.COMCATJOBTYPEID,
J.JOBTYPEID,
CASE WHEN J.JOBTYPEID = JT.JOBTYPEID
THEN JT.NAME ELSE NULL
END AS JOBTYPENAME,
TS.SSSEGMENT AS SUBPL_NAME,
CONCAT(D.DISTRICTID,TS.SSSEGMENT) AS DISTRICTSUBPLID
FROM `EAR-AA-242`.JOB J
LEFT JOIN `EAR-AA-242`.DISTRICT D ON J.DISTRICTID = D.DISTRICTID
LEFT JOIN `EAR-AA-242`.DMJOB DJ ON DJ.JOBNUMBER = J.JOBID
LEFT JOIN `EAR-AA-242`.JOBTYPE JT ON JT.JOBTYPEID = J.JOBTYPEID
LEFT JOIN `EAR-AA-242`.STANDARDJOBTYPE SJ ON JT.STANDARDJOBTYPEID = SJ.STANDARDJOBTYPEID
LEFT JOIN `EAR-AA-239`.TBLJOBTYPE TJ ON SJ.COMCATJOBTYPEID = TJ.JOBTYPEID
LEFT JOIN (SELECT SS.SSSEGMENT, SS.NODEID FROM `EAR-AA-239`.TBLSSSEGMENT S,
`EAR-AA-239`.TBLSSSEGMENT SS WHERE S.SSS = SS.PARENTSSS AND SS.DELETED = 0) TS ON TJ.SSSEGMENT =TS.NODEID
LEFT JOIN `EAR-AA-242`.SEGMENT S ON S.SEGMENTID = D.SEGMENTID
WHERE DATE(J.STARTTIME) > DATE_SUB(DATE(SYSDATE()), INTERVAL 367 DAY)
ORDER BY J.JOBID;
The same code code , while creating a table is throwing an
Error Code: 1292. Truncated incorrect DOUBLE value: '11COC0011'
CREATE TABLE ODS.JOB_LOCATION_IDISTRICT AS
SELECT
CASE WHEN J.JOBID = DJ.JOBNUMBER
THEN DJ.JOBID ELSE J.JOBID
END AS JOBID,
J.DISTRICTID,
CASE WHEN D.DISTRICTID = J.DISTRICTID
THEN D.NAME ELSE NULL
END AS DISTRICT_NAME,
D.SEGMENTID,
CASE WHEN D.SEGMENTID = S.SEGMENTID AND D.DISTRICTID = J.DISTRICTID
THEN S.NAME ELSE NULL
END AS SEGMENT,
J.STARTTIME,
J.ENDTIME,
SJ.COMCATJOBTYPEID,
J.JOBTYPEID,
CASE WHEN J.JOBTYPEID = JT.JOBTYPEID
THEN JT.NAME ELSE NULL
END AS JOBTYPENAME,
TS.SSSEGMENT AS SUBPL_NAME,
CONCAT(D.DISTRICTID,TS.SSSEGMENT) AS DISTRICTSUBPLID
FROM `EAR-AA-242`.JOB J
LEFT JOIN `EAR-AA-242`.DISTRICT D ON J.DISTRICTID = D.DISTRICTID
LEFT JOIN `EAR-AA-242`.DMJOB DJ ON DJ.JOBNUMBER = J.JOBID
LEFT JOIN `EAR-AA-242`.JOBTYPE JT ON JT.JOBTYPEID = J.JOBTYPEID
LEFT JOIN `EAR-AA-242`.STANDARDJOBTYPE SJ ON JT.STANDARDJOBTYPEID = SJ.STANDARDJOBTYPEID
LEFT JOIN `EAR-AA-239`.TBLJOBTYPE TJ ON SJ.COMCATJOBTYPEID = TJ.JOBTYPEID
LEFT JOIN (SELECT SS.SSSEGMENT, SS.NODEID FROM `EAR-AA-239`.TBLSSSEGMENT S,
`EAR-AA-239`.TBLSSSEGMENT SS WHERE S.SSS = SS.PARENTSSS AND SS.DELETED = 0) TS ON TJ.SSSEGMENT =TS.NODEID
LEFT JOIN `EAR-AA-242`.SEGMENT S ON S.SEGMENTID = D.SEGMENTID
WHERE DATE(J.STARTTIME) > DATE_SUB(DATE(SYSDATE()), INTERVAL 367 DAY)
ORDER BY J.JOBID;
Any help absolutely appreciated!!!
You should specify the datatypes of the columns explicitly. Without that, MySQL has to figure out what the types are. When you're using a CASE expression to return the values for a column, it assumes that the column datatype will be the same as the type of the first THEN value, but this will be wrong if other cases return a different type. In your code, you have some CASE expressions where the first result is DOUBLE, but another result is a CHAR string that can't be converted to DOUBLE.
So it should be
CREATE TABLE ODS.JOB_LOCATION_IDISTRICT (
JOBID VARCHAR(30),
DISTRICTID INT,
...
) AS
SELECT
...

Combine Union ALL with Inner Join Statement

I have a problem how to combine the sql statements (inner join and union all. as a Newbie in SQL. Hopefully all members can help me to solve the problems.attached herewith the SQL. The leave_Trx and leave_History contain same values that need to be union. Thank you.
select m.name, t.startdt, t.enddt,t.noday,t.createdDT,
(case when t.approveST = 'Y' then 'Approved' when t.approveST = 'N' then 'Not Approved' else 'Pending' end) as appST
from leave_Trx t
inner join leave_MType m on m.typeID = t.trxID
inner join hr_personaldata b on b.pers_ID = #pers_ID
where year(t.startdt) = #yyear
and b.pers_ID = #pers_ID and b.pers_name LIKE '%'+#pers_name+'%'
and b.pers_compID LIKE ''+#compID+''
union all
select * from leave_History h
where year(h.startdt) = #yyear and h.status = 'A'
ORDER BY t.startdt
select t.pers_ID,t.startdt, t.enddt,t.noday,t.createdDT,
(case when t.approveST = 'Y' then 'Approved' when t.approveST = 'N' then 'Not Approved' else 'Pending' end) as appST
from leave_Trx t
inner join leave_MType m on m.typeID = t.pers_ID
inner join hr_personaldata l on l.pers_ID = #pers_ID
where year(t.startdt) = #yyear and t.status = 'A'
union all
select h.pers_ID, h.startdt, h.enddt,h.noday,h.createdDT,
(case when h.approveST = 'Y' then 'Approved' when h.approveST = 'N' then 'Not Approved' else 'Pending' end) as appST
from leave_History h
inner join leave_MType m on m.typeID = h.pers_ID
inner join hr_personaldata b on b.pers_ID = #pers_ID
where year(h.startdt) = #yyear and h.status = 'A'

Using If Else in Mysql

SELECT process.process_name, com_jobcard.job_card_num,
IF process.UOM = '1' THEN SET (com_jobcard.dept_qty) AS Total_qty
ELSEIF process.UOM = '2' THEN SET (com_jobcard.total_stones) AS Total_qty
FROM timer_completed
INNER JOIN PROCESS ON process.id = timer_completed.process_id
INNER JOIN com_jobcard ON timer_completed.job_card_id = com_jobcard.id
AND timer_completed.report_date = '2015-09-15'
Hi friends I would like to retrieve the value of department quantity When process.UOM = 1 else if process.UOM = '2' THEN SET (com_jobcard.total_stones) AS Total_qty , please help me with the code , thanks in advance
You can use case-when something as
SELECT
process.process_name,
com_jobcard.job_card_num,
case
when process.UOM = '1' then com_jobcard.dept_qty
when process.UOM = '2' then com_jobcard.total_stones
end as Total_qty
FROM timer_completed
INNER JOIN PROCESS ON process.id = timer_completed.process_id
INNER JOIN com_jobcard ON timer_completed.job_card_id = com_jobcard.id
AND timer_completed.report_date = '2015-09-15'
NOTE : Right now there is no else so its better to add an else if both condition fails
case
when process.UOM = '1' then com_jobcard.dept_qty
when process.UOM = '2' then com_jobcard.total_stones
else 'something you want'
end as Total_qty
IF(condition, true_value, false_value) can be used this way
SELECT process.process_name, com_jobcard.job_card_num,
IF(process.UOM = '1', com_jobcard.dept_qty, IF(process.UOM = '2', com_jobcard.total_stones, 0)) AS Total_qty
FROM timer_completed
INNER JOIN PROCESS ON process.id = timer_completed.process_id
INNER JOIN com_jobcard ON timer_completed.job_card_id = com_jobcard.id
AND timer_completed.report_date = '2015-09-15'

MySQL GROUP BY with substring not looping through

I am trying to group by a substring. I only get the one result:
Region = Coastal, Value (R) = 1144900.
I am supposed to get 2 results.
This is my code:
SELECT
DISTINCT SUBSTRING_INDEX(`team_name`, '-', 1) AS 'Region',
SUM(`opportunities`.`value`) AS 'Value (R)'
FROM
`opportunities`
LEFT JOIN cf_type_link_data ON cf_type_link_data.sourceid = opportunities.id
LEFT JOIN cf_lu_type_fields ON cf_type_link_data.fieldid = cf_lu_type_fields.id
LEFT JOIN cf_lu_types_fields_dropdown ON cf_type_link_data.`value` = cf_lu_types_fields_dropdown.id AND cf_lu_type_fields.id = cf_lu_types_fields_dropdown.fieldid
LEFT JOIN `lu_teams` ON `lu_teams`.`contactid` = `opportunities`.`user_allocation`
LEFT JOIN `teams` ON `teams`.`id` = `lu_teams`.`teamid`
LEFT JOIN `lu_opportunity_status` ON `lu_opportunity_status`.`id` = `opportunities`.`status`
WHERE 1
AND `cf_lu_types_fields_dropdown`.`values` = 'Building Project'
AND cf_lu_type_fields.fieldname = 'Scaffolding Segment'
AND (`opportunities`.`expecteddate` >= '2012-01-01' AND `opportunities`.`expecteddate` <= '2012-07-24')
GROUP BY 'Region'
ORDER BY cf_lu_types_fields_dropdown.`values`;
Any help is appreciated.
You can not use same alias from select in GROUP BY clause
try
SELECT DISTINCT SUBSTRING_INDEX(team_name, '-', 1) AS 'Region',
SUM(opportunities.value) AS 'Value (R)'....
GROUP BY SUBSTRING_INDEX(team_name, '-', 1)
ORDER BY cf_lu_types_fields_dropdown.values;
Try not to use quotes for field identifiers -
'Region' -> Region
In your case you see only one record in result data set because you group by string 'Region', but you should group by value(s).