SQL nested queries and subqueries - mysql

i've been having an error with this query and i'm not sure how to fix it. this query is supposed to filter occupations stored in my database to match the volunteer's occupation. please help me fix my query. all the names in this query are correctly spelled, i double checked all the spellings before writing the query here.
the error says "#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'SELECT (SELECT count(*) FROM occupation_event WHERE event_id='8' AND occupationN' at line 1"
SELECT
*
FROM
volunteer_details
WHERE
user_id=73
AND
volunteer_occupation in (
SELECT
occupationName
FROM
occupation_event
WHERE
event_id=8
OR SELECT (
SELECT
count(*)
FROM
occupation_event
WHERE
event_id='8'
AND
occupationName = 'No Occupation Required') > 0 AS need
)

I think the error is the as need at the end. I would write this as:
SELECT vd.*
FROM volunteer_details vd
WHERE user_id = 73 AND
(vd.volunteer_occupation in (SELECT oe.occupationName FROM occupation_event oe WHERE oe event_id = 8) or
exists (select 1 from occupation_event oe where event_id = 8 and oe.occupationName = 'No Occupation Required')
);

Related

Trying to perform divison using MySQL subqueries but getting syntax error

I am trying to get the value from MySQL subqueries but I am getting 'Error Code: 1064. You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ')' at line 1'
The code is:
UPDATE national_avg_tariff SET Value= (CASE WHEN ((SELECT * FROM((SELECT Value
/ ((Select Sum_CPP_MlnRs from ppp_fy2020_2030_annex4.FY_CPP_exclKE_MlnRs where FiscalYear=2020) + (Select UoSC_MoF_exclKE_MlnRs from ppp_fy2020_2030_annex4.FY_UoSC_MoF_exclKE_MlnRs where FiscalYear=2020))/ (SELECT Sum(Sum_of_AvgMW) FROM ppp_fy2020_2030_annex4.AVG_OF_FY_MAX_DEMAND where FiscalYear=2020 GROUP BY FiscalYear)FROM tariff_true_cost.national_avg_tariff WHERE Sector='Residential' AND Category='Up to 50 Units' AND Parameter= 'Fixed Charge(Rs/kW/M)' AND FiscalYear=2020)temptbl8))>0)
THEN
(SELECT * FROM((SELECT Value
/ ((Select Sum_CPP_MlnRs from ppp_fy2020_2030_annex4.FY_CPP_exclKE_MlnRs where FiscalYear=2020) + (Select UoSC_MoF_exclKE_MlnRs from ppp_fy2020_2030_annex4.FY_UoSC_MoF_exclKE_MlnRs where FiscalYear=2020))/ (SELECT Sum(Sum_of_AvgMW) FROM ppp_fy2020_2030_annex4.AVG_OF_FY_MAX_DEMAND where FiscalYear=2020 GROUP BY FiscalYear)FROM tariff_true_cost.national_avg_tariff WHERE Sector='Residential' AND Category='Up to 50 Units' AND Parameter= 'Fixed Charge(Rs/kW/M)' AND FiscalYear=2020)temptbl8))
ELSE NULL END) where FiscalYear=2020 AND Parameter='Fixed Charge(%)';
The code doesn't show any red underline or pre-execution error something, the error only appears once I execute it.
Any help in rectifying syntax error of this piece of code would be appreciable. Thanks

How to implement "SHOW COLUMN FROM (SELECT Result) WHERE Column_name = 'column_name'"?

Good Evning My Brothers/Sisters.
I want to check whether a column is exist from select result.
I have tried some effort like below but instead it gives me an error.
SHOW COLUMNS
FROM (SELECT
hasil.skpd, hasil.kode_skpd, SUM(hasil.sudah_isi) AS sudah_isi, SUM(hasil.belum_isi) AS belum_isi
FROM
(
SELECT
a.id_pegawai, c.skpd, c.kode_skpd, a.id_satuan_organisasi, a.nama_pegawai, a.nip,
CASE
WHEN i.id_pegawai IS NULL THEN 0
ELSE 1
END AS sudah_isi,
CASE
WHEN i.id_pegawai IS NULL THEN 1
ELSE 0
END AS belum_isi
FROM
tbl_pegawai a
LEFT JOIN ref_skpd c ON a.id_satuan_organisasi = c.id_skpd
LEFT JOIN tbl_bapertarum i ON (a.id_pegawai = i.id_pegawai AND YEAR(i.tgl_lapor)='2015')
GROUP BY
a.id_pegawai
) hasil
WHERE
1 AND hasil.kode_skpd LIKE 'Badan Kepegawaian Daerah%'
GROUP BY
hasil.id_satuan_organisasi
ORDER BY
hasil.kode_skpd) WHERE Field = "kode_skpd"
This is the full error result:
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '(SELECT
hasil.skpd, hasil.kode_skpd, SUM(hasil.sudah_isi) AS s' at line 2
How do i to find the existence of a column or columns from SELECT result ?
Thanks in advance Brothers/Sisters.

Correct syntax to use in mysql 5.5.25 for not exists

Hi I'm trying to write a cursor in powerbuilder 12 to fetch some records . Here this is my query.
I'm trying to fetch some records from the trninvhdr table which are not in the second table.
SELECT
INV_DATE,
INV_NO,
INV_TYPE,
CUR_CODE,
EXCH_RATE,
usd_rate,
CR_TERM,
DUE_DATE,
bl_date,
TOT_AMT
FROM trninvhdr
WHERE
COMP_CODE ='NFL1' AND
CUST_CODE = 'NLML' AND
INV_TYPE ='F' AND
INV_DATE <= '2016-03-25' AND
NOT EXISTS
(SELECT * FROM trninvoiceavailability WHERE trninvoiceavailability.COMP_CODE = trninvhdr.COMP_CODE
AND trninvoiceavailability.INV_TYPE = trninvhdr.INV_TYPE AND trninvoiceavailability.INV_NO = trninvhdr.INV_NO);
Here how I use it in the program.
DECLARE lc_retrieve CURSOR FOR
SELECT
trninvhdr.INV_DATE,
trninvhdr.INV_NO,
trninvhdr.INV_TYPE,
trninvhdr.CUR_CODE,
trninvhdr.EXCH_RATE,
trninvhdr.usd_rate,
trninvhdr.CR_TERM,
trninvhdr.DUE_DATE,
trninvhdr.bl_date,
trninvhdr.TOT_AMT
FROM trninvhdr
WHERE
COMP_CODE = :as_comp_code AND
CUST_CODE = :as_cust_code AND
INV_TYPE ='F' AND
INV_DATE <= :as_inv_date )AND
NOT EXISTS (SELECT * FROM trninvoiceavailability
WHERE trninvoiceavailability.COMP_CODE = trninvhdr.COMP_CODE
AND trninvoiceavailability.INV_TYPE = trninvhdr.INV_TYPE AND
trninvoiceavailability.INV_NO = trninvhdr.INV_NO);
open lc_retrieve ;
The query works fine in the mysql server but in the progra it gives me the following error .
Database c0038 SQLSTATE = 3700 MySQL ODBC 5.2 a Driver mysql id 5.5.25 You have an error in your syntax. check the manual that corresponds to your mysql version for the right syntax to use near NOT EXISTS (SELECT * FROM trninvoiceavailability.... at line 1.
What is the correct Syntax that I should use to work this query.
I can see a bracket in this code... where did it come from and where is it's friend?
INV_DATE <= :as_inv_date )AND
You need to remove ) from your query as per below-
INV_DATE <= :as_inv_date )AND
sholuld be INV_DATE <= :as_inv_date AND

Is it valid to use DISTINCT two times in a single query

I'm getting this following error:
MYSQL ERROR
1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use
near 'DISTINCT R.roomtype as roomtype , I1.meal_plan as
mealplan , I1.`bed_t' at line 3
My Code is
SELECT
DISTINCT H.`hotelname` as `hotelname` ,
DISTINCT R.`roomtype` as `roomtype` ,
I1.`meal_plan` as `mealplan` ,
I1.`bed_type` as `bedtype` ,
I1.`roomrate` as `roomrate` ,
I1.`pax` as `pax` ,
I1.`childrate` as `childrate` ,
I1.`childrate1` as `childrate1` ,
I1.`childrate2` as `childrate2` ,
I1.`childrate3` as `childrate3` ,
P.`profitmarkup_type` as `profit_type` ,
P.`applyprofit_val_room` as `applyprofit_val_room` ,
P.`applyprofit_val_bed` as `applyprofit_val_bed`
FROM `hoteldetails` H
INNER JOIN `roomdetails` R
on H.`hotelname` = R.`hotelname`
INNER JOIN `inventorypolicy` I1
on I1.`hotel_name` = H.`hotelname`
AND I1.`room_plan` = R.`roomtype`
AND I1.`bed_type`=R.`bedtype`
AND I1.`meal_plan`=R.`mealplan`
AND I1.`suppliername`=H.`supplier`
INNER JOIN `profitmarkup` P
on P.`hotel_name` = H.`hotelname`
AND P.`suppliername` = H.`supplier`
WHERE H.`active`='1'
AND H.`country`='MAL'
AND H.`city`='KL'
AND H.`show2web`='1'
AND H.`expiry_date` >= 11/09/2014
AND I1.`inventory_status`='1'
AND P.`markup_status` = '1'
AND 20140911 BETWEEN ((I1.`date1`)+(I1.`month1`*100)+(I1.`year1`*10000)) AND ((I1.`date2`)+(I1.`month2`*100)+(I1.`year2`*10000))
AND 20140911 BETWEEN ((P.`date1`)+(P.`month1`*100)+(P.`year1`*10000)) AND ((P.`date2`)+(P.`month2`*100)+(P.`year2`*10000))
I dont know how to fix this. Any help is greatly appreciated.
As you can learn from the documentation of the SELECT MySQL statement, DISTINCT can be placed before any field or expression to be selected. This automatically forces a single instance of it in a query.
And somewhere down the road there is a sentence that says everything:
DISTINCTROW is a synonym for DISTINCT.

Error in creating view in mysql. while query runs correctly when independently run

This mysql query runs just fine independently. But when I use this query to create a view then Error Code: 1064 message appears in sql yog. Please identify the reason of this error.
CREATE
VIEW `databaseName`.`viewName`
AS
((SELECT
`tblgrn`.`InitialLabNo`
, `invlabtes`.`Code` AS LabNo
, `invlabmaterial`.`Description` AS material
, `invlabtessubtable`.`SrNo`
, `invlabtessubtable`.`Result`
,COALESCE(NULL, 'Verified') AS TYPE
, `tblgrn`.`Comp_Code`
, `tblgrn`.`Unit_Code`
, `tblgrn`.`UserId`
FROM
`tblgrn`
INNER JOIN `invlabtes`
ON (`tblgrn`.`InitialLabNo` = `invlabtes`.`Code`)
INNER JOIN `invlabtessubtable`
ON (`invlabtes`.`Code` = `invlabtessubtable`.`Code`)
INNER JOIN `invlabmaterial`
ON (`invlabtessubtable`.`TestCode` = `invlabmaterial`.`Code`))
UNION
(SELECT
`tblgrn`.`InitialLabNo`
, `invlabtesscale`.`Code` AS LabNo
, `invlabmaterial`.`Description`
, `invlabscalesubtable`.`SrNo`
, `invlabscalesubtable`.`Result`
,COALESCE(NULL, 'Running') AS TYPE
, `tblgrn`.`Comp_Code`
, `tblgrn`.`Unit_Code`
, `tblgrn`.`UserId`
FROM
`tblgrn`
INNER JOIN `invlabtesscale`
ON (`tblgrn`.`InitialLabNo` = `invlabtesscale`.`IniLabNo`)
INNER JOIN `invlabscalesubtable`
ON (`invlabtesscale`.`Code` = `invlabscalesubtable`.`Code`)
INNER JOIN `invlabmaterial`
ON (`invlabscalesubtable`.`TestCode` = `invlabmaterial`.`Code`)));
this is the error message:
Error Code: 1064
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'UNION
SELECT
tblgrn.InitialLabNo
, invlabtesscale.Code AS LabNo' at line 22
Execution Time : 0 sec
Transfer Time : 0 sec
Total Time : 0.018 sec
Remove the bracket between UNION and SELECT.
UNION
(SELECT
should be
UNION
SELECT
You may need to remove the closing bracket too.