Error when importing db2 stored procedure to SSRS - reporting-services

enter image description here
error when importing stored procedure AS400 to SSRS
any help?
stored procedure code :
AS400
CREATE OR REPLACE PROCEDURE DWGF047_V2 (IN DD varchar(30),IN DI varchar(30),IN SOC varchar(30),IN NUM_PART varchar(30),IN DAT varchar(30),IN REGL varchar(30))
LANGUAGE SQL
BEGIN
DECLARE c1 CURSOR FOR
SELECT
IKGFFIC_E1.KRYREGP.RGCDOGA ,
IKGFFIC_E1.KFACOMP.DWLBLOBFAC,
IKGFFIC_E1.KRYREGP.RGCDORGA1
FROM
IKGFFIC_E1.KCOMDEP RIGHT OUTER JOIN IKGFFIC_E1.KLGCDEP ON (IKGFFIC_E1.KLGCDEP.LCCDOGA=IKGFFIC_E1.KCOMDEP.CMCDOGA and IKGFFIC_E1.KLGCDEP.LCNOCOMMAN=IKGFFIC_E1.KCOMDEP.CMNOCOMMAN)
RIGHT OUTER JOIN IKGFFIC_E1.KLGFACP ON ( IKGFFIC_E1.KLGFACP.LFCDOGA=IKGFFIC_E1.KLGCDEP.LCCDOGA and IKGFFIC_E1.KLGFACP.LFNOCOMMAN=IKGFFIC_E1.KLGCDEP.LCNOCOMMAN and IKGFFIC_E1.KLGFACP.LFNOLIGCDE=IKGFFIC_E1.KLGCDEP.LCNOLIGCDE AND IKGFFIC_E1.KLGFACP.LFMTTCLIFA<>0)
RIGHT OUTER JOIN IKGFFIC_E1.KRYREGP ON (IKGFFIC_E1.KRYREGP.RGCDOGA=IKGFFIC_E1.KLGFACP.LFCDOGA AND IKGFFIC_E1.KRYREGP.RGNOFACTUR=IKGFFIC_E1.KLGFACP.LFNOFACTUR AND IKGFFIC_E1.KLGFACP.LFMTTCLIFA<>0)
LEFT OUTER JOIN IKGFFIC_E1.KFACTUP ON (IKGFFIC_E1.KRYREGP.RGCDOGA=IKGFFIC_E1.KFACTUP.FACDOGA AND IKGFFIC_E1.KRYREGP.RGNOFACTUR=IKGFFIC_E1.KFACTUP.FANOFACTUR)
LEFT OUTER JOIN IKGFFIC_E1.KFACOMP ON (IKGFFIC_E1.KFACTUP.FACDOGA=IKGFFIC_E1.KFACOMP.DWCDOGA and IKGFFIC_E1.KFACTUP.FANOFACTUR=IKGFFIC_E1.KFACOMP.DWNOFACTUR)
LEFT OUTER JOIN IKGLFIC_E1.PRTP ON (IKGFFIC_E1.KRYREGP.RGCDOGA=IKGLFIC_E1.PRTP.BACDOGA AND IKGFFIC_E1.KRYREGP.RGCDPARREG=IKGLFIC_E1.PRTP.BANOPRT)
LEFT OUTER JOIN XDOCDTA001.ARAUREP ON (XDOCDTA001.ARAUREP.AUAPCD = CASE IKGFFIC_E1.KRYREGP.RGCDOGA
WHEN 'V90' THEN 'BD001' WHEN '069' THEN 'BD003' WHEN '052' THEN 'BD005' WHEN 'A58' THEN 'BD006'
WHEN '440' THEN 'BD00F' WHEN '570' THEN 'BD00G' WHEN '600' THEN 'BD00E'
WHEN '139' THEN 'BD009' WHEN '369' THEN 'BD010' WHEN 'Z92' THEN 'BD008' WHEN 'H85' THEN 'BD007'
WHEN '092' THEN 'BD00J'
END
AND IKGFFIC_E1.KRYREGP.RGNOFACTUR = XDOCDTA001.ARAUREP.AUALNA
AND XDOCDTA001.ARAUREP.AUASCD LIKE 'FACFOU%'
AND XDOCDTA001.ARAUREP.AUATCD = 'NIKOS' )
LEFT OUTER JOIN IKGFFIC_E1.KCTSLSP ON ( IKGFFIC_E1.KLGFACP.LFCDOGA=IKGFFIC_E1.KCTSLSP.CUCDOGA AND IKGFFIC_E1.KLGFACP.LFNOORDCON=IKGFFIC_E1.KCTSLSP.CUNOORDCON AND IKGFFIC_E1.KLGFACP.LFCDLOTMAR=IKGFFIC_E1.KCTSLSP.CUCDLOTMAR AND IKGFFIC_E1.KLGFACP.LFNOORCONL=IKGFFIC_E1.KCTSLSP.CUNOORCONL )
LEFT OUTER JOIN IKGFFIC_E1.KCONTRP ON (IKGFFIC_E1.KCTSLSP.CUNOORDCON=IKGFFIC_E1.KCONTRP.CANOORDCON AND IKGFFIC_E1.KCTSLSP.CUCDOGA=IKGFFIC_E1.KCONTRP.CACDOGA)
LEFT OUTER JOIN IKGFFIC_E1.KMASLSP ON (IKGFFIC_E1.KLGFACP.LFNOLOTMAR=IKGFFIC_E1.KMASLSP.MLNOLOTMAR AND
IKGFFIC_E1.KLGFACP.LFCDLOTMAR=IKGFFIC_E1.KMASLSP.MLCDLOTMAR
AND IKGFFIC_E1.KLGFACP.LFNOSOULOT=IKGFFIC_E1.KMASLSP.MLNOSOULOT
AND IKGFFIC_E1.KLGFACP.LFCDOGA=IKGFFIC_E1.KMASLSP.MLCDOGA
AND IKGFFIC_E1.KRYREGP.RGCDPARREG = IKGFFIC_E1.KMASLSP.MLCDPARTRT)
LEFT OUTER JOIN IKGFFIC_E1.KMARCHP ON (IKGFFIC_E1.KMASLSP.MLNOLOTMAR=IKGFFIC_E1.KMARCHP.MANOLOTMAR AND IKGFFIC_E1.KMASLSP.MLCDOGA=IKGFFIC_E1.KMARCHP.MACDOGA)
LEFT OUTER JOIN IKGFFIC_E1.KLGECHP ON (IKGFFIC_E1.KLGECHP.LECDOGA=IKGFFIC_E1.KLGFACP.LFCDOGA and IKGFFIC_E1.KLGECHP.LENOORDCON=IKGFFIC_E1.KLGFACP.LFNOORDCON and IKGFFIC_E1.KLGECHP.LECDLOTMAR=IKGFFIC_E1.KLGFACP.LFCDLOTSS and IKGFFIC_E1.KLGECHP.LENOORCONL=IKGFFIC_E1.KLGFACP.LFNOORCONL and IKGFFIC_E1.KLGECHP.LENOECHEAN=IKGFFIC_E1.KLGFACP.LFNOECHEAN and IKGFFIC_E1.KLGECHP.LENBANECHE=IKGFFIC_E1.KLGFACP.LFNBANECHE and IKGFFIC_E1.KLGECHP.LEMSECHEAN=IKGFFIC_E1.KLGFACP.LFMSECHEAN)
WHERE
( IKGFFIC_E1.KRYREGP.RGCDOGA IN ('052','A58', 'V90','069', '440', '570', '600', 'AST', '139', '369', 'Z92', 'H85','092') )
AND
(
IKGFFIC_E1.KRYREGP.RGCDOGA IN ( '052','A58','V90','069','440','570','600','AST','139','369','Z92','H85','092' )
AND (IKGFFIC_E1.KRYREGP.RGCDOGA IN ('052','A58', 'V90','069', '440', '570', '600', 'AST', '139', '369', 'Z92', 'H85','092'))
AND
( ( IKGFFIC_E1.KRYREGP.RGCDORGA1 ) = (DD) )--DD
AND
( ( IKGFFIC_E1.KRYREGP.RGCDORGA2 ) = (DI) )--DI
AND
( ( IKGFFIC_E1.KRYREGP.RGCDOGA ) = (SOC) --societe
)
AND
( ( IKGLFIC_E1.PRTP.BANOPRT ) = (NUM_PART) )--numero_partenaire
AND
( ( DIGITS(IKGFFIC_E1.KRYREGP.RGDTECHEAN) ) <= DAT )--date
AND
( ( IKGFFIC_E1.KRYREGP.RGCDMODPAI ) = REGL )--mode de reglement
AND
(
IKGFFIC_E1.KLGFACP.LFNOCPTCPT NOT IN ( '401720','404720' )
OR
IKGFFIC_E1.KFACTUP.FACDTYPFAC IN ( 'LRG' )
)
);
OPEN c1;
END

Related

Unknown column in 'IN/ALL/ANY subquery'

Why am I getting this error? Error is showing up in the OR clause, but I'm selecting gl_ID inside the SELECT statement.
; DECLARE v_firstDate DATETIME
SET #p_ID = 368
SELECT SUBDATE(NOW(), 1) INTO v_firstDate;
SELECT t_ID, t_firstName, t_lastName
FROM t_table
WHERE
((#p_ID IN (
SELECT tt_ID
FROM tt_tableTwo
INNER JOIN st_stats ON (st_ID = tt_ID)
INNER JOIN da_data ON (da_ID = st_ID AND da_name IN ("allCompanies", "allglobals"))
)
))
OR
(
(gl_ID IN ( //problem is here
SELECT gl_ID
FROM gl_globals
INNER JOIN tr_transport ON (tr_id = gl_caseID AND tr_idOther = #p_ID)
INNER JOIN co_countries ON (co_ID = gl_ID AND co_ID = #p_ID)
)
)
)
Error message:
Unknown column 'gl_ID' in 'IN/ALL/ANY subquery'
Should I be using an AS or a HAVING?
gl_ID is not accessible in the OR clause because it doesn't exist in t_table. An inner join under the first FROM clause solves the problem.
; DECLARE v_firstDate DATETIME
SET #p_ID = 368
SELECT SUBDATE(NOW(), 1) INTO v_firstDate;
SELECT t_ID, t_firstName, t_lastName
FROM t_table
INNER JOIN gl_globals ON (t_ID = gl_ID) // fix
WHERE
((#p_ID IN (
SELECT tt_ID
FROM tt_tableTwo
INNER JOIN st_stats ON (st_ID = tt_ID)
INNER JOIN da_data ON (da_ID = st_ID AND da_name IN ("allCompanies", "allglobals"))
)
))
OR
(
(gl_ID IN (
SELECT gl_ID
FROM gl_globals
INNER JOIN tr_transport ON (tr_id = gl_caseID AND tr_idOther = #p_ID)
INNER JOIN co_countries ON (co_ID = gl_ID AND co_ID = #p_ID)
)
)
)

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

how to build nested select on zend db

Hi i have trouble with this query
SELECT * FROM(
SELECT `b`.*,`owner`.firstname,`owner`.lastname,`owner`.email,
(
SELECT COUNT(`ps`.profile_id) FROM `profile` AS `ps`
LEFT JOIN `xref_store_profile_brand` AS `xbp` ON `xbp`.profile_id = `ps`.profile_id
WHERE `xbp`.brand_id = b.brand_id AND ps.role = 'salesrep' AND `xbp`.store_id IS NULL
) AS `salesrepTotal`,
(
SELECT GROUP_CONCAT(`ms`.firstname) FROM `profile` AS `ps`
LEFT JOIN `xref_store_profile_brand` AS `xbp` ON `xbp`.profile_id = `ps`.profile_id
LEFT JOIN `member` AS `ms`ON `ms`.member_id = `ps`.member_id
WHERE `xbp`.brand_id = `b`.brand_id AND ps.role = 'salesrep' AND `xbp`.store_id IS NULL
) AS `salesrep`,
(
SELECT COUNT(`s`.store_id) FROM `store` AS `s`
LEFT JOIN `xref_store_profile_brand` AS `xbs` ON `xbs`.store_id = `s`.store_id
WHERE `xbs`.brand_id = `b`.brand_id AND `xbs`.brand_id IS NOT NULL
) AS `storeTotal`,
(
SELECT GROUP_CONCAT(`s`.name) FROM `store` AS `s`
LEFT JOIN `xref_store_profile_brand` AS `xbs` ON `xbs`.store_id = `s`.store_id
WHERE `xbs`.brand_id = `b`.brand_id AND `xbs`.brand_id IS NOT NULL
) AS `store`
FROM `brand` AS `b`
LEFT JOIN
(
SELECT `m`.firstname,`m`.lastname,`m`.email,`xspb`.brand_id FROM `member` AS `m`
LEFT JOIN `profile` as `p` ON `p`.member_id = `m`.member_id AND `p`.role = 'designer' AND `p`.isPrimary = 1
LEFT JOIN `xref_store_profile_brand` AS `xspb` ON `xspb`.profile_id = `p`.profile_id AND `xspb`.store_id IS NULL
) AS `owner` ON `owner`.brand_id =`b`.brand_id
GROUP BY `b`.brand_id
) AS `final`
how can i convert this in to Zend_Db_Select object?
Th main problem is
this part
SELECT `b`.*,`owner`.firstname,`owner`.lastname,`owner`.email,
(
SELECT COUNT(`ps`.profile_id) FROM `profile` AS `ps`
LEFT JOIN `xref_store_profile_brand` AS `xbp` ON `xbp`.profile_id = `ps`.profile_id
WHERE `xbp`.brand_id = b.brand_id AND ps.role = 'salesrep' AND `xbp`.store_id IS NULL
) AS `salesrepTotal`,
You need to use Zend_Db_Expr objects in your query and array structures for select AS.
below is the solution you are looking for:
<?php
$db = Zend_Db_Table::getDefaultAdapter();
// inner query
$sqlSalesRepTotal = $db->select()
->from(array('ps' => 'profile'))
->joinLeft(array('xbp' => 'xref_store_profile_brand'), 'xbp.profile_id = ps.profile_id')
->where('xbp.brand_id = b.brand_id')
->where('ps.role = ?', 'salesrep')
->where('xbp.store_id IS NULL');
// main query
$sql = $db->select()
->from(array('b' => 'brand'), array(
// NOTE: have to add parentesis around the expression
'salesrepTotal' => new Zend_Db_Expr("($sqlSalesRepTotal)")
))
->where('....')
->group('brand_id');
// debug
var_dump($db->fetchAll($sql));

optimizing a union join inside select statement of other joins

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.

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;