I am having the following query to fetch the data from the table.
SELECT rental_plans.*,
( ( inventory.total_inventory
+ vehicles.tmp_qty ) - Ifnull(reservation.total_reserved, 0) ) AS
vehicle_inventory
FROM `rental_plans`
INNER JOIN `vehicles`
ON `vehicles`.`id` = `rental_plans`.`vehicle_id`
LEFT JOIN (SELECT Count(*) AS total_inventory,
vehicle_id
FROM vehicle_inventories
GROUP BY vehicle_id) AS inventory
ON `inventory`.`vehicle_id` = `vehicles`.`id`
LEFT JOIN (SELECT vehicle_id,
Sum(qty) AS total_reserved
FROM `reservations`
WHERE ( '2018-12-18' BETWEEN pickup_date AND drop_date )
OR ( '2018-12-28' BETWEEN pickup_date AND drop_date )
AND `status` NOT IN ( 'RETURNED' )
GROUP BY `vehicle_id`) AS `reservation`
ON `rental_plans`.`vehicle_id` = `reservation`.`vehicle_id`
WHERE `rental_plans`.`id` > 0
AND `rental_plans`.`pickup` = '1'
AND `rental_plans`.`drop` = '10'
ORDER BY `rental_plans`.`price` ASC
But I want to handle where condition based on vehicle_inventory.
I tried with ... AND vehicle_inventory > 16 order by `rental_plans`.`price` ASC but this generates the error that column not found in the table
The reason is that vehicle_inventory is an alias and it is resolved after WHERE clause. You can read about that here. You can do something like:
SELECT *
FROM (SELECT rental_plans.*,
( ( inventory.total_inventory
+ vehicles.tmp_qty ) - Ifnull(reservation.total_reserved, 0)
) AS
vehicle_inventory
FROM `rental_plans`
INNER JOIN `vehicles`
ON `vehicles`.`id` = `rental_plans`.`vehicle_id`
LEFT JOIN (SELECT Count(*) AS total_inventory,
vehicle_id
FROM vehicle_inventories
GROUP BY vehicle_id) AS inventory
ON `inventory`.`vehicle_id` = `vehicles`.`id`
LEFT JOIN (SELECT vehicle_id,
Sum(qty) AS total_reserved
FROM `reservations`
WHERE ( '2018-12-18' BETWEEN
pickup_date AND drop_date )
OR ( '2018-12-28' BETWEEN
pickup_date AND drop_date )
AND `status` NOT IN ( 'RETURNED' )
GROUP BY `vehicle_id`) AS `reservation`
ON `rental_plans`.`vehicle_id` =
`reservation`.`vehicle_id`
WHERE `rental_plans`.`id` > 0
AND `rental_plans`.`pickup` = '1'
AND `rental_plans`.`drop` = '10')a
WHERE a.rental_plans > 16
ORDER BY `price` ASC
or
SELECT rental_plans.*,
( (inventory.total_inventory + vehicles.tmp_qty) - Ifnull(reservation.total_reserved, 0) ) AS vehicle_inventory
FROM `rental_plans`
INNER JOIN `vehicles`
ON `vehicles`.`id` = `rental_plans`.`vehicle_id`
LEFT JOIN
(
SELECT Count(*) AS total_inventory,
vehicle_id
FROM vehicle_inventories
GROUP BY vehicle_id) AS inventory
ON `inventory`.`vehicle_id` = `vehicles`.`id`
LEFT JOIN
(
SELECT vehicle_id,
Sum(qty) AS total_reserved
FROM `reservations`
WHERE (
'2018-12-18' BETWEEN pickup_date AND drop_date)
OR (
'2018-12-28' BETWEEN pickup_date AND drop_date)
AND `status` NOT IN ('RETURNED')
GROUP BY `vehicle_id`) AS `reservation`
ON `rental_plans`.`vehicle_id` = `reservation`.`vehicle_id`
WHERE `rental_plans`.`id` > 0
AND `rental_plans`.`pickup` = '1'
AND `rental_plans`.`drop` = '10'
where (
inventory.total_inventory + vehicles.tmp_qty) - ifnull(reservation.total_reserved, 0) > 16
ORDER BY `rental_plans`.`price` ASC
I have latest dates of 2017 in attandance date but its show just 2016 dates kindly plz help me
SELECT
added_on,
types. `type`,
student.`student_id`, student.`roll`, student.`class_id`, student.`name` AS student_name,
SUM(datewise_attandance.`total_classes`) AS SumOfTotal,
datewise_attandance.attandance_date AS attandance_date,
SUM(datewise_attandance.`status`) AS SumOfPresent,
#ROUND(((SUM(datewise_attandance.`status`)/SUM(datewise_attandance.`total_classes`))*100),2) AS percentage,
class.`name` AS class_name, student.`sex`, student.`father_name`, student.`address`,
student.`phone`, subject.`name` AS subject_name,
#types.`type`, types.`type_id`,
subject.`subject_id`,
#MAX( datewise_attandance.`date_to` ) AS MaxOfAtt_Date_To,
student.`session_id`
FROM
(( class
INNER JOIN `subject` ON class.`class_id` = subject.`class_id` )
INNER JOIN datewise_attandance # ( INNER JOIN types ON datewise_attandance.`type_id` = types.`type_id` )
ON ( subject.`subject_id` = datewise_attandance.`subject_id` )
AND ( class.`class_id` = datewise_attandance.`class_id` ))
INNER JOIN `types` ON( types.`type_id` = subject.`subject_type`)
INNER JOIN student ON ( student.`student_id` = datewise_attandance.`std_id` )
AND ( class.`class_id` = student.`class_id` )
GROUP BY student.`student_id`,
student.`roll`, student.`class_id`, student.`name`, class.`name`,
student.`sex`, student.`father_name`, student.`address`,
student.`phone`, subject.`name`,
# types.`type`, types.`type_id`,
subject.`subject_id`, student.`session_id`
HAVING (
(
student.`class_id` = 4
AND student.`roll` = 388
AND student.`session_id` = 5
)
) ORDER BY IFNULL(subject.final_exam,0) DESC
You are using inner join so if there are matching records then only result will be returned for 2017. Why dont you use left join and try the same
I am running the below query, but its extremeely slow. Dows anyone have any advice on how I can optimize it to improve performance.
The main user table only has 2700 row.
The query is:
SELECT
(
SELECT
core_org_chart.translation
FROM
core_org_chart
WHERE
(
core_org_chart.id_dir = t1.idOrg
)
) AS region,
(
SELECT
core_org_chart.translation
FROM
core_org_chart
WHERE
(
core_org_chart.id_dir = t2.idOrg
)
) AS level1,
(
SELECT
core_org_chart.translation
FROM
core_org_chart
WHERE
(
core_org_chart.id_dir = t3.idOrg
)
) AS level2,
(
SELECT
core_org_chart.translation
FROM
core_org_chart
WHERE
(
core_org_chart.id_dir = t4.idOrg
)
) AS level3,
core_user.firstname AS firstname,
core_user.lastname AS lastname,
core_user.email AS email,
core_user.register_date AS register_date,
core_user.lastenter AS lastenter,
(
SELECT
core_field_son.translation
FROM
(
core_field_son
JOIN core_field_userentry ON (
(
core_field_userentry.user_entry = core_field_son.idSon
)
)
)
WHERE
(
(
core_field_userentry.id_user = core_user.idst
)
AND (
core_field_userentry.id_common = 4
)
)
) AS Gender,
(
SELECT
core_field_son.translation
FROM
(
core_field_son
JOIN core_field_userentry ON (
(
core_field_userentry.user_entry = core_field_son.idSon
)
)
)
WHERE
(
(
core_field_userentry.id_user = core_user.idst
)
AND (
core_field_userentry.id_common = 6
)
)
) AS Race,
IF (
(core_user.valid = 1),
'Active',
'Suspended'
) AS UserStatus,
(
SELECT
jet_designations.designation
FROM
jet_designations
WHERE
(
jet_designations.id = core_user.designation
)
) AS UserDesignation,
(
SELECT
jet_designations.designation
FROM
jet_designations
WHERE
(
jet_designations.id = core_user.reports_to
)
) AS Manager
FROM
(
(
(
(
(
core_org_chart_tree t1
LEFT JOIN core_org_chart_tree t2 ON (
(
t2.idParent = t1.idOrg
)
)
)
LEFT JOIN core_org_chart_tree t3 ON (
(
t3.idParent = t2.idOrg
)
)
)
LEFT JOIN core_org_chart_tree t4 ON (
(
t4.idParent = t3.idOrg
)
)
)
JOIN core_group_members ON (
(
core_group_members.idst =
IF (
isnull(t2.idOrg),
t1.idst_ocd,
IF (
isnull(t3.idOrg),
t2.idst_ocd,
IF (
isnull(t4.idOrg),
t3.idst_ocd,
t4.idst_ocd
)
)
)
)
)
)
JOIN core_user ON (
(
core_user.idst = core_group_members.idstMember
)
)
)
WHERE
(t1.lev = 1)
you are using multiple queries in one, joining them will improve performance.
Here's a SQL statement:
SELECT DISTINCT `class`, `student_id` , `student_name`,
(
SELECT SUM( `credits` )
FROM `stumgr_scores` B
JOIN `stumgr_courses` USING ( `course_id` )
WHERE `year` =2012 AND A.`student_id` = B.`student_id`
) AS `total_credits`,
(
SELECT SUM( `credits` * `final_score` )
FROM `stumgr_scores` C
JOIN `stumgr_courses` USING ( `course_id` )
WHERE `year` =2012 AND A.`student_id` = C.`student_id`
) AS `total_scores`
FROM `stumgr_scores` A
NATURAL JOIN `stumgr_students`
WHERE `year` =2012 AND `grade` =2011
You may discover that these two select statement which use aggregate functions are similar. So, I want to merge them into one as following:
SELECT DISTINCT `class`, `student_id` , `student_name`,
(
SELECT
SUM( `credits` ) AS `total_credits`,
SUM( `credits` * `final_score` ) AS `total_scores`
FROM `stumgr_scores` B
JOIN `stumgr_courses` USING ( `course_id` )
WHERE `year` =2012 AND A.`student_id` = B.`student_id`
) AS `something`
FROM `stumgr_scores` A
NATURAL JOIN `stumgr_students`
WHERE `year` =2012 AND `grade` =2011
Of course, the SQL statement above doesn't work and I don't know what to do.
Besides, the query is very slow because of large data, do you have any suggestions? Thanks a lot.
I have had to guess at your table structure slightly, but you should be able to simplify this query massively by using JOINs rather than correlated subqueries:
SELECT st.student_id,
st.student_name,
c.class,
SUM(sc.credits) AS total_credits,
SUM(sc.credits * sc.final_score) AS total_scores
FROM stumgr_students st
INNER JOIN stumgr_scores sc
ON sc.student_id = st.student_id
INNER JOIN stumgr_courses c
ON c.course_id = st.course_id
GROUP BY st.student_id, st.student_name, c.class;
I have a query (equivalent of View in ORACLE, SQL Server) that I saved in an Access database. Now I need to add another column to my select list. But whenever I change the SQL I get errors. Let's say I remove one comma and then put it back. I get errors like "Syntax error after From","Syntax error in Join operation". So if I change anything and then undo the change I still get errors. How can I avoid this?
Edit:Before editing
SELECT A_B.ALICI, Q.QAINOM, M.TIP, Q.QATARIX, Q.CEMIBORC, R2.ADI AS SAT_NOV, M.TAMADI, M.MARK, SA.MIQDAR, SA.SAQIYM, SA.CEMI, SA.FAIZ, A_B.BORC_SU, A_B.MEBLEG, A_B.FERQ, K_G.CEMI_ODEN, A_B.MOBTEL, SA.NOTE
FROM ([SELECT Round(Sum([DBKASSA].[MEBLEQ]),3) AS CEMI_ODEN, DBKASSA.KODAL_GT
FROM DBKASSA
WHERE (((DBKASSA.TIP)=1) AND ((DBKASSA.KODAL_GT)=[INBUYERID]) AND ((DBKASSA.TARIX)=[INTILLDATE]))
GROUP BY DBKASSA.KODAL_GT]. AS K_G RIGHT JOIN ([SELECT AL.KODALAN,AL.MOBTEL, IIf([Su_CEMIBORC] Is Null,0,[Su_CEMIBORC]) AS BORC_SU, IIf([Su_MEBLEQ] Is Null,0,[Su_MEBLEQ]) AS MEBLEG, (IIf([Su_MEBLEQ] Is Null,0,[Su_MEBLEQ]))-(IIf([Su_CEMIBORC] Is Null,0,[Su_CEMIBORC])) AS FERQ, AL!OBYEKT & (IIf(AL!NUMAY Is Not Null," - " & AL!NUMAY)) AS ALICI
FROM
(
SELECT DBKASSA.KODAL_GT, Sum(DBKASSA.MEBLEQ) AS Su_MEBLEQ
FROM DBKASSA
WHERE (((DBKASSA.TIP)=1) AND ((DBKASSA.KODAL_GT)=[INBUYERID]))
GROUP BY DBKASSA.KODAL_GT
) AS OD_AL
RIGHT JOIN (
(
SELECT DBQAIME.KODALAN, Sum(DBQAIME.CEMIBORC) AS Su_CEMIBORC
FROM DBQAIME
WHERE (((DBQAIME.KODALAN)=[INBUYERID]))
GROUP BY DBQAIME.KODALAN
) AS B_AL
RIGHT JOIN DBALAN AS AL ON B_AL.KODALAN = AL.KODALAN) ON OD_AL.KODAL_GT = AL.KODALAN
WHERE (((AL.KODALAN)=[INBUYERID]))
]. AS A_B INNER JOIN DBQAIME AS Q ON A_B.KODALAN = Q.KODALAN) ON K_G.KODAL_GT = A_B.KODALAN) INNER JOIN (DBMAL AS M INNER JOIN (DBSA AS SA INNER JOIN [SELECT R2.KOD, R2.VID, R2.ADI
FROM DBRAB2 AS R2
WHERE (((R2.VID)=3))]. AS R2 ON SA.KODEMLNO = R2.KOD) ON M.KODMAL = SA.KODMAL) ON Q.QAINOM = SA.QAINOM
WHERE (((Q.QAINOM)=[INSALEINVOICE]))
ORDER BY M.TIP, M.TAMADI;
After editing;
SELECT A_B.ALICI, Q.QAINOM, M.TIP, Q.QATARIX, Q.CEMIBORC, R2.ADI AS SAT_NOV, M.TAMADI, M.MARK, SA.MIQDAR, SA.SAQIYM, SA.CEMI, SA.FAIZ, A_B.BORC_SU, A_B.MEBLEG, A_B.FERQ, K_G.CEMI_ODEN, A_B.MOBTEL, SA.NOTE
FROM ([SELECT Round(Sum([DBKASSA].[MEBLEQ]),3) AS CEMI_ODEN, DBKASSA.KODAL_GT
FROM DBKASSA
WHERE (((DBKASSA.TIP)=1) AND ((DBKASSA.KODAL_GT)=[INBUYERID]) AND ((DBKASSA.TARIX)=[INTILLDATE]))
GROUP BY DBKASSA.KODAL_GT]. AS K_G RIGHT JOIN ([SELECT AL.KODALAN,AL.MOBTEL, IIf([Su_CEMIBORC] Is Null,0,[Su_CEMIBORC]) AS BORC_SU, IIf([Su_MEBLEQ] Is Null,0,[Su_MEBLEQ]) AS MEBLEG, (IIf([Su_MEBLEQ] Is Null,0,[Su_MEBLEQ]))-(IIf([Su_CEMIBORC] Is Null,0,[Su_CEMIBORC])) AS FERQ, AL!OBYEKT & (IIf(AL!NUMAY Is Not Null," - " & AL!NUMAY)) AS ALICI
FROM
(
SELECT DBKASSA.KODAL_GT, Sum(DBKASSA.MEBLEQ) AS Su_MEBLEQ
FROM DBKASSA
WHERE (((DBKASSA.TIP)=1) AND ((DBKASSA.KODAL_GT)=[INBUYERID]))
GROUP BY DBKASSA.KODAL_GT
) AS OD_AL
RIGHT JOIN (
(
SELECT DBQAIME.KODALAN, Sum(DBQAIME.CEMIBORC) AS Su_CEMIBORC
FROM DBQAIME
WHERE (((DBQAIME.KODALAN)=[INBUYERID]))
GROUP BY DBQAIME.KODALAN
) AS B_AL
RIGHT JOIN DBALAN AS AL ON B_AL.KODALAN = AL.KODALAN) ON OD_AL.KODAL_GT = AL.KODALAN
WHERE (((AL.KODALAN)=[INBUYERID]))
]. AS A_B INNER JOIN DBQAIME AS Q ON A_B.KODALAN = Q.KODALAN) ON K_G.KODAL_GT = A_B.KODALAN) INNER JOIN (DBMAL AS M INNER JOIN (DBSA AS SA INNER JOIN [SELECT R2.KOD, R2.VID, R2.ADI
FROM DBRAB2 AS R2
WHERE (((R2.VID)=3))]. AS R2 ON SA.KODEMLNO = R2.KOD) ON M.KODMAL = SA.KODMAL) ON Q.QAINOM = SA.QAINOM
WHERE (((Q.QAINOM)=[INSALEINVOICE]))
ORDER BY M.TIP, M.TAMADI;
By the way, it worked in MS Access 2010. Mine is Access 2003
What has happened is that your subquery or derived table is now bracketed like so
[stuff here].
This causes an error when editing.
The easiest thing to do is use notepad or such like, replace []. with () and paste back.
Try:
SELECT A_B.ALICI,
Q.QAINOM,
M.TIP,
Q.QATARIX,
Q.CEMIBORC,
R2.ADI AS SAT_NOV,
M.TAMADI,
M.MARK,
SA.MIQDAR,
SA.SAQIYM,
SA.CEMI,
SA.FAIZ,
A_B.BORC_SU,
A_B.MEBLEG,
A_B.FERQ,
K_G.CEMI_ODEN,
A_B.MOBTEL,
SA.NOTE
FROM ((SELECT Round(SUM([DBKASSA].[MEBLEQ]), 3) AS CEMI_ODEN,
DBKASSA.KODAL_GT
FROM DBKASSA
WHERE ( ( ( DBKASSA.TIP ) = 1 )
AND ( ( DBKASSA.KODAL_GT ) = [INBUYERID] )
AND ( ( DBKASSA.TARIX ) = [INTILLDATE] ) )
GROUP BY DBKASSA.KODAL_GT) AS K_G
RIGHT JOIN ((SELECT AL.KODALAN,
AL.MOBTEL,
Iif([Su_CEMIBORC] IS NULL, 0, [Su_CEMIBORC])
AS
BORC_SU,
Iif([Su_MEBLEQ] IS NULL, 0, [Su_MEBLEQ])
AS
MEBLEG,
( Iif([Su_MEBLEQ] IS NULL, 0, [Su_MEBLEQ]) ) - (
Iif([Su_CEMIBORC] IS NULL, 0, [Su_CEMIBORC]) )
AS
FERQ,
AL ! OBYEKT & ( Iif(AL ! NUMAY IS NOT NULL,
" - " & AL ! NUMAY) ) AS
ALICI
FROM (SELECT DBKASSA.KODAL_GT,
SUM(DBKASSA.MEBLEQ) AS Su_MEBLEQ
FROM DBKASSA
WHERE
( ( ( DBKASSA.TIP ) = 1 )
AND ( ( DBKASSA.KODAL_GT ) = [INBUYERID] ) )
GROUP BY DBKASSA.KODAL_GT) AS OD_AL
RIGHT JOIN ( (SELECT DBQAIME.KODALAN,
SUM(DBQAIME.CEMIBORC) AS
Su_CEMIBORC
FROM DBQAIME
WHERE ((
( DBQAIME.KODALAN ) = [INBUYERID] ))
GROUP BY DBQAIME.KODALAN) AS B_AL
RIGHT JOIN DBALAN AS AL
ON B_AL.KODALAN = AL.KODALAN)
ON OD_AL.KODAL_GT = AL.KODALAN
WHERE (( ( AL.KODALAN ) = [INBUYERID] ))) AS A_B
INNER JOIN DBQAIME AS Q
ON A_B.KODALAN = Q.KODALAN)
ON K_G.KODAL_GT = A_B.KODALAN)
INNER JOIN (DBMAL AS M
INNER JOIN (DBSA AS SA
INNER JOIN (SELECT R2.KOD,
R2.VID,
R2.ADI
FROM DBRAB2 AS R2
WHERE (( ( R2.VID ) = 3 ))) AS R2
ON SA.KODEMLNO = R2.KOD)
ON M.KODMAL = SA.KODMAL)
ON Q.QAINOM = SA.QAINOM
WHERE (( ( Q.QAINOM ) = [INSALEINVOICE] ))
ORDER BY M.TIP,
M.TAMADI;