optimizing a union join inside select statement of other joins - mysql

I have a query I built in 3 -4 parts. This takes over 140secs to run once I add the union join with join. How can I change the union join to execute it faster.
SELECT
testing.CLIENTID,
testing.COMPANY,
testing.CONTACT,
testing.CONTACTID,
`orders`.`ORDERNO` AS `ORDERNO`,
`orders`.`BIDNO` AS `BIDNO`,
`projects`.`PROJID` AS `PROJID`,
`projects`.`PROJCODE` AS `PROJCODE`,
`projects`.`StartDate` AS `StartDate`,
`category`.`type` AS `CATEGORY`,
`projects`.`country` AS `COUNTRY`,
`projects`.`VALUE` AS `VALUE`,
`projects`.`PROCESSOR` AS `PROCESSOR`,
`projects`.`NES` AS `NES`,
`projects`.`SPECSALE` AS `SPECSALE`,
`projects`.`OFFICE` AS `OFFICE`,
`projects`.`LORM` AS `LORM`,
`lookupcountry`.`REGION` AS `REGION`
FROM
(
(
(
(
(
(
SELECT
contactmerge.CLIENTID,
contactmerge.CONTACT,
contactmerge.CONTACTID,
accountmerge.COMPANY
FROM
(
SELECT
`hdb`.`contacts`.`CONTACTID` AS `CONTACTID`,
`hdb`.`contacts`.`CLIENTID` AS `CLIENTID`,
concat(
`hdb`.`contacts`.`FIRSTNAME`,
" ",
`hdb`.`contacts`.`LASTNAME`
) AS CONTACT,
_utf8 'paradox' AS `SOURCEDATABASE`
FROM
`hdb`.`contacts`
UNION
SELECT
`sugarcrm`.`contacts`.`id` AS `CONTACTID`,
`sugarcrm`.`accounts_contacts`.`account_id` AS `CLIENTID`,
concat(
`sugarcrm`.`contacts`.`first_name`,
" ",
`sugarcrm`.`contacts`.`last_name`
) AS CONTACT,
_utf8 'sugar' AS `SOURCEDATABASE`
FROM
(
(
(
(
`sugarcrm`.`contacts`
LEFT JOIN `sugarcrm`.`email_addr_bean_rel` ON (
(
(
`sugarcrm`.`contacts`.`id` = `sugarcrm`.`email_addr_bean_rel`.`bean_id`
)
AND (
(
`sugarcrm`.`email_addr_bean_rel`.`primary_address` = 1
)
OR (
(
`sugarcrm`.`email_addr_bean_rel`.`primary_address` IS NOT NULL
)
AND (
`sugarcrm`.`email_addr_bean_rel`.`primary_address` <> 0
)
)
)
)
)
)
LEFT JOIN `sugarcrm`.`accounts_contacts` ON (
(
`sugarcrm`.`contacts`.`id` = `sugarcrm`.`accounts_contacts`.`contact_id`
)
)
)
JOIN `sugarcrm`.`email_addresses` ON (
(
`sugarcrm`.`email_addr_bean_rel`.`email_address_id` = `sugarcrm`.`email_addresses`.`id`
)
)
)
LEFT JOIN `sugarcrm`.`accounts` ON (
(
`sugarcrm`.`accounts`.`id` = `sugarcrm`.`accounts_contacts`.`account_id`
)
)
)
) AS contactmerge
LEFT JOIN (
SELECT
CLIENTID,
`hdb`.`clients`.`COMPANY` AS `COMPANY`
FROM
`hdb`.`clients`
UNION
SELECT
id AS CLIENTID,
`sugarcrm`.`accounts`.`name` AS `COMPANY`
FROM
`sugarcrm`.`accounts`
) AS accountmerge ON contactmerge.CLIENTID = accountmerge.CLIENTID
) AS testing
)
JOIN `orders` ON (
(
`testing`.`CONTACTID` = `orders`.`CONTACTID`
)
)
)
JOIN `projects` ON (
(
`orders`.`ORDERNO` = `projects`.`ORDERNO`
)
)
)
JOIN `category` ON (
(
`category`.`category_id` = `projects`.`category_id`
)
)
)
LEFT JOIN `lookupcountry` ON (
(
CONVERT (
`lookupcountry`.`COUNTRY` USING utf8
) = CONVERT (
`projects`.`country` USING utf8
)
)
)
)
ORDER BY
`testing`.`COMPANY`,
`projects`.`StartDate`
The table alias called testing is the one taking long to execute. I need to then turn this into a view
Original query without the joining of sugarcrm.
SELECT
`clients`.`CORPORATE` AS `CORPORATE`,
`clients`.`COMPANY` AS `COMPANY`,
`clients`.`CLIENTID` AS `CLIENTID`,
`contacts`.`CONTACTID` AS `CONTACTID`,
concat(
`contacts`.`LASTNAME`,
`contacts`.`FIRSTNAME`,
`contacts`.`INITIALS`
) AS `Contact`,
`orders`.`ORDERNO` AS `ORDERNO`,
`orders`.`BIDNO` AS `BIDNO`,
`projects`.`PROJID` AS `PROJID`,
`projects`.`PROJCODE` AS `PROJCODE`,
`projects`.`StartDate` AS `StartDate`,
`category`.`type` AS `CATEGORY`,
`projects`.`country` AS `COUNTRY`,
`projects`.`VALUE` AS `VALUE`,
`projects`.`PROCESSOR` AS `PROCESSOR`,
`projects`.`NES` AS `NES`,
`projects`.`SPECSALE` AS `SPECSALE`,
`projects`.`OFFICE` AS `OFFICE`,
`projects`.`LORM` AS `LORM`,
`lookupcountry`.`REGION` AS `REGION`
FROM
(
(
(
(
(
`clients`
JOIN `contacts` ON (
(
`clients`.`CLIENTID` = `contacts`.`CLIENTID`
)
)
)
JOIN `orders` ON (
(
`contacts`.`CONTACTID` = `orders`.`CONTACTID`
)
)
)
JOIN `projects` ON (
(
`orders`.`ORDERNO` = `projects`.`ORDERNO`
)
)
)
JOIN `category` ON (
(
`category`.`category_id` = `projects`.`category_id`
)
)
)
LEFT JOIN `lookupcountry` ON (
(
CONVERT (
`lookupcountry`.`COUNTRY` USING utf8
) = CONVERT (
`projects`.`country` USING utf8
)
)
)
)
ORDER BY
`clients`.`CORPORATE`,
`clients`.`COMPANY`,
`contacts`.`LASTNAME`,
`projects`.`StartDate`

Your LEFT JOIN from sugarcrm.contacts to sugarcrm.email_addr_bean_rel
ON the id=bean_id is ok, but then your test for Primary_Address = 1
OR ( primary address IS NOT NULL AND primary_address <> 0 ) is wasteful.
Not null mean it has a value. The first qualifier of 1 is ok, but then
you test for any address not equal to 0 (thus 1 is, but so is 2, 3, 400, 1809 or
any other number. So why not just take how I've simplified it.
SELECT
O.ORDERNO,
O.BIDNO,
CASE when c.ContactID IS NULL
then sc.id
ELSE c.contactid END as ContactID,
CASE when c.ContactID IS NULL
then sac.account_id
ELSE c.clientid END as ClientID,
CASE when c.ContactID IS NULL
then concat( sc.first_name, " ", sc.last_name )
ELSE concat( c.FIRSTNAME, " ", c.LASTNAME ) END as Contact,
CASE when c.ContactID IS NULL
then sCli.`name`
ELSE cCli.Company END as Company,
CASE when c.ContactID IS NULL
then _utf8 'sugar'
ELSE _utf8 'paradox' END as SOURCEDATABASE,
P.PROJID,
P.PROJCODE,
P.StartDate,
Cat.`type` AS CATEGORY,
P.`country` AS COUNTRY,
P.`VALUE` AS `VALUE`,
P.PROCESSOR,
P.NES,
P.SPECSALE,
P.OFFICE,
P.LORM,
LC.REGION
FROM
orders O
JOIN projects P
ON O.ORDERNO = P.ORDERNO
JOIN category Cat
ON P.category_id = Cat.category_id
LEFT JOIN lookupcountry LC
ON CONVERT( P.`country` USING utf8 ) = CONVERT( LC.COUNTRY USING utf8 )
LEFT JOIN hdb.contacts c
ON O.ContactID = c.ClientID
LEFT JOIN hdb.clients cCli
ON c.ClientID = cCli.ClientID
LEFT JOIN sugarcrm.contacts sc
ON O.ContactID = sc.id
LEFT JOIN sugarcrm.accounts sCli
ON sc.id = sCli.id
LEFT JOIN sugarcrm.accounts_contacts sac
ON sc.id = sac.contact_id
LEFT JOIN sugarcrm.accounts Acc
ON sac.account_id = Acc.id
LEFT JOIN sugarcrm.email_addr_bean_rel EABR
ON sc.id = EABR.bean_id
AND EABR.primary_address IS NOT NULL
LEFT JOIN sugarcrm.email_addresses EA
ON EABR.email_address_id = EA.id
ORDER BY
CASE when c.ContactID IS NULL
then sCli.`name`
ELSE cCli.Company END,
P.StartDate
I don't mind helping, but from now on, you should take a look at what I'm doing... Establish the relationships... Start with the basis of your data (orders) and look at ONE PATH on how to connect to your "contacts" table... Write those joins (as left-joins). THEN, write your paths to the SUGAR account contacts and write THOSE joins (also left-joins). Don't try to prequery all possible contacts, but using the CASE/WHEN to determine which to get based on a null route vs not just as I have with the contact, client, company, etc. You will get the data from one path vs the other... just keep it consistent.

Related

MySQL where condition based on select query

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

Need to fetch last dates from the query mentioned below

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

Query extremely slow

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.

Merge two similar aggregate functions in select statement

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;

MS ACCESS- Editing and undoing SQL causes error

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;