Cakephp 3 gt, lt, gte and lte issue - cakephp

I got a problem with condition in CakePHP 3. I'm trying to deal with deep associated models and build big queries for search engine.
here the query debbuged:
SELECT
Ouvrages.zone_id AS `Ouvrages__zone_id`,
Ouvrages.parent_id AS `Ouvrages__parent_id`,
Ouvrages.classe_ouvrage_id AS `Ouvrages__classe_ouvrage_id`,
Ouvrages.numero AS `Ouvrages__numero`,
Valeurs.valeur AS `Valeurs__valeur`
FROM
ouvrages Ouvrages
INNER JOIN
elements Elements
ON Ouvrages.id = (
Elements.ouvrage_id
)
INNER JOIN
valeurs Valeurs
ON Elements.id = (
Valeurs.element_id
)
INNER JOIN
caracteristiques Caracteristiques
ON Caracteristiques.id = (
Valeurs.caracteristique_id
)
WHERE
(
Ouvrages.zone_id in (
:c0,:c1,:c2,:c3,:c4,:c5,:c6,:c7,:c8,:c9,:c10,:c11
)
AND Ouvrages.classe_ouvrage_id = :c12
AND Elements.classe_element_id = :c13
AND Valeurs.caracteristique_id = :c14
AND valeur > :c15
)
GROUP BY
Valeurs.id
if i'm testing it in mysql console it works fine.
but cakephp's ORM doesn't give me the same result. With function gt lt gte or lte it doesn't work.
i used $this->log($query, 'debug') and configure my app config with new query log file but i got the same result if i have used debug:
SELECT
Ouvrages.zone_id AS `Ouvrages__zone_id`,
Ouvrages.parent_id AS `Ouvrages__parent_id`,
Ouvrages.classe_ouvrage_id AS `Ouvrages__classe_ouvrage_id`,
Ouvrages.numero AS `Ouvrages__numero`,
Valeurs.valeur AS `Valeurs__valeur`
FROM
ouvrages Ouvrages
INNER JOIN
elements Elements
ON Ouvrages.id = (
Elements.ouvrage_id
)
INNER JOIN
valeurs Valeurs
ON Elements.id = (
Valeurs.element_id
)
INNER JOIN
caracteristiques Caracteristiques
ON Caracteristiques.id = (
Valeurs.caracteristique_id
)
WHERE (
Ouvrages.zone_id in (
:c0,:c1,:c2,:c3
)
AND Ouvrages.classe_ouvrage_id = :c4
AND Elements.classe_element_id = :c5
AND Valeurs.caracteristique_id = :c6
AND valeur >= :c7)
GROUP BY
Valeurs.id
Thanks to ndm for explanation to debug sql query in AJAX. I configured my connection with log at true and i got this kind of result:
WHERE (
Ouvrages.zone_id in (
34,35,44,46
)
AND Ouvrages.classe_ouvrage_id = '60'
AND Elements.classe_element_id = '62'
AND Valeurs.caracteristique_id = '25'
AND valeur >= '5')
I'm trying to cast those numbers, but i didn't found how yet. A simply (int) doesn't work.

I understood my mistake. Actually we build a weird Valeurs table with a column value which is associated to a caracteristic. This value column is defined as varchar(255) but can contain Numeric, String or Date. We know the type of the current value coz we also have a type column.
But CakePHP ORM cast automatically every data to string because of the varchar definition. We must cast our datas ourself:
function ($exp) use ($data) {
return $exp->{$data['operand']}('valeur', $data['valeur'], 'integer');
};

Related

EF6 query that behaves strangely using contains

I have an EF6 SQL Server query that behaves strangely when it is supplied with a List<int> of IDs to use. If bookGenieCategory = a value it works. If selectedAges is empty (count = 0) all is well. If the selectedAges contains values that exist in the ProductCategory.CategoryId column, the contains fails and NO rows are returned.
Note: AllocationCandidates is a view, which works properly on its own.
CREATE VIEW dbo.AllocationCandidate
AS
SELECT
p.ProductID, p.SKU as ISBN, p.Name as Title,
pv.MSRP, pv.Price, pv.VariantID, pv.Inventory,
ISNULL(plt.DateLastTouched, GETDATE()) AS DateLastTouched,
JSON_VALUE(p.MiscText, '$.AgeId') AS AgeId,
JSON_VALUE(p.MiscText, '$.AgeName') AS AgeName
FROM
dbo.Product AS p WITH (NOLOCK)
INNER JOIN
dbo.ProductVariant AS pv WITH (NOLOCK) ON pv.ProductID = p.ProductID
LEFT OUTER JOIN
dbo.KBOProductLastTouched AS plt WITH (NOLOCK) ON plt.ProductID = p.ProductID
WHERE
(ISJSON(p.MiscText) = 1)
AND (p.Deleted = 0)
AND (p.Published = 1)
AND (pv.IsDefault = 1)
GO
Do I have a typo here or a misplaced parenthesis in the following query?
var returnList = (from ac in _db.AllocationCandidates
join pc in _db.ProductCategories on ac.ProductID equals pc.ProductID
where (bookGenieCategory == 0
|| bookGenieCategory == pc.CategoryID)
&&
(selectedAges.Count == 0 ||
selectedAges.Contains(pc.CategoryID))
orderby ac.AgeId, ac.DateLastTouched descending
select ac).ToList();
Firstly, I would recommend extracting the conditionals outside of the Linq expression. If you only want to filter data if a value is provided, move the condition check outside of the Linq rather than embedding it inside the condition. This is generally easier to do with the Fluent Linq than Linq QL. You should also aim to leverage navigation properties for relationships between entities. This way an AllocationCandidate should have a collection of ProductCategories:
var query = _db.AllocationCandidates.AsQueryable();
if (bookGenieCategory != 0)
query = query.Where(x => x.ProductCategories.Any(c => c.CategoryID == bookGenieCategory);
The next question is what does the selectedAges contains? There is an Age ID on the AllocationCandidate, but your original query is checking against the ProductCategory.CategoryId??
If the check should be against the AllocationCandidate.AgeId:
if (selectedAges.Any())
query = query.Where(x => selectedAges.Contains(x.AgeID));
If the check is as you wrote it against the ProductCategory.CategoryId:
if (selectedAges.Any())
query = query.Where(x => x.ProductCategories.Any(c => selectedAges.Contains(c.AgeID)));
Then add your order by and get your results:
var results = query.OrderBy(x => x.AgeId)
.ThenByDescending(x => x.DateLastTouched);
.ToList();

SQL Query for list within select where field = value and otherfield = othervalue

I need to fudge an existing SQL Server procedure rather quickly. It's a bit of a hack job but needs must.
I need for the following to return a list of voucher codes and invoice numbers rather than just one row of data where the comment is (in the WHERE clause):
SELECT TOP 10
IH.INH_Voucher AS [ID], IH.COY_ID AS COY_ID,
IH.INH_DateSupInv AS ORD_UpdatedOn,
V.VES_ID, V.VES_IMOnumber, IH.INH_Order,
IH.INH_ID AS ORD_ID, IH.INH_INDID
FROM
InvoiceHDR IH (NOLOCK)
INNER JOIN
VESSACCOMP VA ON IH.COY_ID = VA.COY_ID
INNER JOIN
Vessel V ON VA.VES_ID = V.VES_ID
WHERE
v.VSS_ID IN ('01') AND
(IH.INH_Status >= 20 AND IH.INH_Status <= 40) AND
--IH.INH_Voucher = '170CH' AND IH.INH_SupInv = '1532' NEED LIST
IH.INH_INDID IS NOT NULL
So I would need
Voucher = '1700CH' AND SupInv = '1235' AND
Voucher = '180CH' AND SupInv = '1111' AND
And so on for many matching VoucherCodes and InvoiceCodes.
I hope this is clear?
Thanks.
You can apply the following WHERE clause to your query:::
WHERE
v.VSS_ID IN ('01') AND
(IH.INH_Status BETWEEN 20 AND 40) AND
((IH.INH_Voucher = '170CH' AND IH.INH_SupInv = '1532')
OR (IH.INH_Voucher = '180CH' AND IH.INH_SupInv = '1111'))
AND IH.INH_INDID IS NOT NULL

SQL Query Help - searching on multiple 'pairs' or data

I'm struggling to work out how to do a SQL query on a database that I have.
I have a view (which can be changed) which shows the relationships between the tables.
This creates a view as follows:
What I need to be able to do is search on one or more 'Attribute Pairs'
for example
I want to search for records with:
(
(AttributeName='FileExtension' AND AttributeValue='.pdf')
AND (AttributeName='AccountNumber' AND AttributeValue='ABB001'
)
As you can tell, this is not working as AttributeName cant be two things at once. I have this working with an OR filter, but I want it to find records that have all attribute pairs
SELECT
dbo.SiconDMSDocument.SiconDMSDocumentID,
dbo.SiconDMSAttribute.SiconDMSAttributeID,
dbo.SiconDMSAttribute.AttributeFriendlyName,
dbo.SiconDMSAttribute.AttributeName,
dbo.SiconDMSDocumentAttribute.AttributeValue,
dbo.SiconDMSAttribute.DataType,
dbo.SiconDMSDocumentType.SiconDMSDocumentTypeID,
dbo.SiconDMSDocumentType.DocumentTypeName,
dbo.SiconDMSDocumentType.DocumentTypeFriendlyName,
dbo.SiconDMSModule.SiconDMSModuleID,
dbo.SiconDMSModule.ModuleName,
dbo.SiconDMSModule.ModuleFriendlyName,
dbo.SiconDMSDocument.SiconDMSDocumentTypeModuleID
FROM dbo.SiconDMSDocument
INNER JOIN dbo.SiconDMSDocumentAttribute ON dbo.SiconDMSDocument.SiconDMSDocumentID = dbo.SiconDMSDocumentAttribute.SiconDMSDocumentID
INNER JOIN dbo.SiconDMSAttribute ON dbo.SiconDMSDocumentAttribute.SiconDMSAttributeID = dbo.SiconDMSAttribute.SiconDMSAttributeID
AND
(
(dbo.SiconDMSAttribute.AttributeName = 'Reference' AND dbo.SiconDMSDocumentAttribute.AttributeValue='12345')
OR (dbo.SiconDMSAttribute.AttributeName = 'AccountNumber' AND dbo.SiconDMSDocumentAttribute.AttributeValue='ABB001')
)
INNER JOIN dbo.SiconDMSDocumentTypeModule ON dbo.SiconDMSDocument.SiconDMSDocumentTypeModuleID = dbo.SiconDMSDocumentTypeModule.SiconDMSDocumentTypeModuleID
INNER JOIN dbo.SiconDMSDocumentType ON dbo.SiconDMSDocumentTypeModule.SiconDMSDocumentTypeID = dbo.SiconDMSDocumentType.SiconDMSDocumentTypeID
INNER JOIN dbo.SiconDMSModule ON dbo.SiconDMSDocumentTypeModule.SiconDMSModuleID = dbo.SiconDMSModule.SiconDMSModuleID
WHERE
(dbo.SiconDMSDocument.Deleted = 0)
AND (dbo.SiconDMSDocumentAttribute.Deleted = 0)
AND (dbo.SiconDMSAttribute.Deleted = 0)
AND (dbo.SiconDMSDocumentType.Deleted = 0)
AND (dbo.SiconDMSDocumentTypeModule.Deleted = 0)
AND (dbo.SiconDMSModule.Deleted = 0)
Are there any SQL functions that will allow me to do something like this?
I'm not sure what your complicated query has to do with the question of searching for attribute pairs.
Assuming you want the document ids that have both attributes:
select SiconDMSDocumentID
from yourview y
where (AttributeName = 'FileExtension' AND AttributeValue = '.pdf') or
(AttributeName = 'AccountNumber' AND AttributeValue = 'ABB001'
group by SiconDMSDocumentID
having count(*) = 2;
Or, if the attributes could have multiple values:
having count(distinct AttributeName) = 2

SQL Filtering on unique events and datetime

I did ask this question before but deleted the post.
Here is my question: I have a table in SQL Server with multiple events in the table, I am trying to filter on the events and the datetime between the events on a certain X min.
I need help with T-SQL queries.
What can use use to do the following (any links will be appreciated):
take event_id and datetime of device, compare to a list of other event_id's to see if there are any other events withing the last 24 min for that device.
What would work for me? Nested queries? Where clause? CTE?
I have tried 6 queries but not getting the result I need, do I use (max) datetime and then compare on event_ID's?
I know this is not much but any help or links would be appreciated....
On Request (Full Query no changes)
1.
SELECT A.[Unit_id]
,A.[TransDate]
,A.[event_id]
,A.[event_msg]
,B.[TransDate]
,B.[event_id]
,B.[event_msg]
FROM
(Select
A.[Unit_id]
,MAX(A.[TransDate]) AS Transdate
,A.[event_id]
,A.[event_msg]
FROM [JammingEvents].[dbo].[EventLogExtended] AS A
WHERE event_id = '345'
GROUP BY A.[Unit_id]
,A.[event_id]
,A.[event_msg]
) AS A
INNER JOIN
(SELECT
B.[Unit_id]
,MAX(B.[TransDate]) AS Transdate
,B.[event_id]
,B.[event_msg]
FROM [JammingEvents].[dbo].[EventLogExtended] AS B
WHERE B.event_id = '985'
GROUP BY B.[Unit_id]
,B.[event_id]
,B.[event_msg]
) AS B
ON
A.Unit_id = B.Unit_id
WHERE (B.TransDate BETWEEN DATEADD(MINUTE,-24,A.Transdate) AND DATEADD(MINUTE,24,A.Transdate))
this works but only if you compare 2 event Id's. the problem is i need to do this on a bulk scale.
2.
SET NOCOUNT ON
GO
SELECT MAX(ELE.Transdate) AS Transdate
,ELE.[Unit_id]
,ELE.[event_id]
,ELE.[event_msg]
,ELE.[Latitude]
,ELE.[Longitude]
,ELE.[date_ack]
,ELE.[user_id_ack]
,ELE.[date_closed]
,ELE.[user_id_closed]
,ELE.[action]
,ELE.[EventSeqNo]
,ELE.[MsgSeqNo]
,ELE.[Speed]
,ELE.[Heading]
,ELE.[Status1]
,ELE.[Status2]
,ELE.[GeoLocation]
,ELE.[Reg_No]
,ELE.[Company]
,ELE.[Fleet_Code]
,ELE.[IgnStatus]
,ELE.[comboActioned]
FROM
[JammingEvents].[dbo].[EventLogExtended] ELE
INNER JOIN
(
SELECT
F.Transdate ,F.[Unit_id] ,F.[event_id] ,F.[event_msg] ,F.[Latitude] ,F.[Longitude] ,F.[date_ack] ,F.[user_id_ack]
,F.[date_closed] ,F.[user_id_closed] ,F.[action] ,F.[EventSeqNo] ,F.[MsgSeqNo] ,F.[Speed] ,F.[Heading] ,F.[Status1]
,F.[Status2] ,F.[GeoLocation] ,F.[Reg_No] ,F.[Company] ,F.[Fleet_Code] ,F.[IgnStatus] ,F.[comboActioned]
FROM [JammingEvents].[dbo].[EventLogExtended] AS F
WHERE
F.event_id IN
('302'
,'303'
,'304'
,'305'
,'309'
,'340'
,'341'
,'345'
,'962'
,'963'
,'973'
,'974'
,'975'
,'976'
,'985'
,'987'
,'989'
,'C220'
,'C222'
,'C224'
,'C227'
,'C228')
) AS A
ON
ELE.Unit_id = A.Unit_id
INNER JOIN
(
SELECT
G.Transdate ,G.[Unit_id] ,G.[event_id] ,G.[event_msg] ,G.[Latitude] ,G.[Longitude] ,G.[date_ack]
,G.[user_id_ack] ,G.[date_closed] ,G.[user_id_closed] ,G.[action] ,G.[EventSeqNo] ,G.[MsgSeqNo] ,G.[Speed] ,G.[Heading]
,G.[Status1] ,G.[Status2] ,G.[GeoLocation] ,G.[Reg_No] ,G.[Company] ,G.[Fleet_Code] ,G.[IgnStatus] ,G.[comboActioned]
FROM [JammingEvents].[dbo].[EventLogExtended] AS G
WHERE
G.event_id = '345'
) AS B
ON
A.Unit_id = B.Unit_id
WHERE (ELE.TransDate BETWEEN DATEADD(MINUTE,-24,A.Transdate) AND DATEADD(MINUTE,24,A.Transdate))
AND
(ELE.event_id IN
(
'302'
,'303'
,'304'
,'305'
,'309'
,'340'
,'341'
,'345'
,'962'
,'963'
,'973'
,'974'
,'975'
,'976'
,'985'
,'987'
,'989'
,'C220'
,'C222'
,'C224'
,'C227'
,'C228'
))
AND
(ELE.action = '0')
GROUP BY ELE.TransDate
,A.Unit_id
,ELE.[Unit_id]
,ELE.[event_id]
,ELE.[event_msg]
,ELE.[Latitude]
,ELE.[Longitude]
,ELE.[date_ack]
,ELE.[user_id_ack]
,ELE.[date_closed]
,ELE.[user_id_closed]
,ELE.[action]
,ELE.[EventSeqNo]
,ELE.[MsgSeqNo]
,ELE.[Speed]
,ELE.[Heading]
,ELE.[Status1]
,ELE.[Status2]
,ELE.[GeoLocation]
,ELE.[Reg_No]
,ELE.[Company]
,ELE.[Fleet_Code]
,ELE.[IgnStatus]
,ELE.[comboActioned]
ORDER BY ELE.TransDate, ELE.Unit_id DESC
so far this got me the closest but i have no idea what to do, i am very poor with SQL syntax and writing queries.
You could use an exists subquery to demand that there was another event for the same device within the preceeding 24 minutes:
select *
from Events e1
where exists
(
select *
from Events e2
where e2.Device_ID = e1.Device_ID
and e2.event_dt between
dateadd(minute, e1.event_dt, -24)
and e1.event_dt
)
Self join should work as well:
SELECT *, DATEDIFF(minute,b.transdate,a.transdate) diff FROM EventLogWxtended a
JOIN eventLogExtended b
ON (a.unit_id=b.unit_id AND b.transdate<a.transdate
AND DATEDIFF(minute,b.transdate,a.transdate)<24)

Converting T-SQL Left Join Subqueries to MS-Access

I need help in converting this T-SQL query to MS ACCESS. The error that I'm getting is JOIN expression not supported.
Update:
I can't add:
DDA ON TT.[Description] = DDA.AccountTypeDesc AND
H.AccountNumber = DDA.AccountNumber
But
DDA ON TT.[Description] = DDA.AccountTypeDesc
works. Is there a way to add the second condition?
T-SQL Query:
SELECT
*
FROM
(
SELECT
[PesoAmount] = CASE WHEN FE.IsoCode IS NULL THEN
LTRIM(STR(DFCF.CurrencyAmount, 20, 2))
ELSE
LTRIM(STR(DFCF.CurrencyAmount * FE.PhpConversionRate, 20, 2))
END,
DFCF.TransactionNumber,
DFCF.AccountNumber,
DFCF.CountryCd,
DFCF.TransactionTypeCd,
DFCF.Time,
DFCF.Date,
DFCF.TransactionStatusCd,
DFCF.TransactionCurrencyCd,
DFCF.BranchNumber,
DFCF.RemitterExtPartyCd,
DFCF.BeneficiaryExtPartyCd,
DFCF.PostedDate,
DFCF.AssociateNumber,
DFCF.ExecutingPartyNumber,
DFCF.CurrencyAmount,
DFCF.CurrencyAmountInTxnCcy,
DFCF.CurrencyAmountInAccountCcy,
DFCF.SecondaryAccountKey,
DFCF.RelatedInd,
DFCF.ThirdPartyInd,
DFCF.TransactionDescription,
DFCF.SecurityName,
DFCF.DealNumber
FROM
dbo.DesFactCashFlow DFCF (NOLOCK) LEFT JOIN
dbo.ForeignExchange FE (NOLOCK) ON DFCF.TransactionCurrencyCd = FE.IsoCode
)
H LEFT JOIN
dbo.Ctr C (NOLOCK) ON H.PesoAmount = C.PesoAmountFaceValueSumInsured AND
H.AccountNumber = C.AccountNumber AND
H.TransactionTypeCd = C.TransactionType LEFT JOIN
dbo.TransactionType TT (NOLOCK) ON H.TransactionTypeCd = TT.Code LEFT JOIN
(
SELECT
[AccountNumber] = DDA2.AccountNumber,
[AccountTypeDesc] = DDA2.AccountTypeDesc,
[LineOfBusinessName] = MAX(DDA2.LineOfBusinessName),
[AccountCurrencyCode] = MAX(DDA2.AccountCurrencyCode),
[AccountCurrencyName] = MAX(DDA2.AccountCurrencyName),
[AccountRegistrationTypeDesc] = MAX(DDA2.AccountRegistrationTypeDesc),
[AccountRegistrationName] = MAX(DDA2.AccountRegistrationName),
[AccountName] = MAX(DDA2.AccountName),
[AlternateName] = MAX(DDA2.AlternateName),
[AccountOpenDate] = MAX(DDA2.AccountOpenDate),
[AccountCloseDate] = MAX(DDA2.AccountCloseDate),
[AccountStatusDesc] = MAX(DDA2.AccountStatusDesc),
[DormantInd] = MAX(DDA2.DormantInd),
[ProductLineName] = MAX(DDA2.ProductLineName),
[ProductCategoryName] = MAX(DDA2.ProductCategoryName),
[ProductTypeName] = MAX(DDA2.ProductTypeName),
[ProductName] = MAX(DDA2.ProductName),
[ProductNumber] = MAX(DDA2.ProductNumber),
[AccountTaxId] = MAX(DDA2.AccountTaxId),
[AccountTaxIdTypeCode] = MAX(DDA2.AccountTaxIdTypeCode),
[AccountTaxStateCode] = MAX(DDA2.AccountTaxStateCode),
[AccountPrimaryBranchName] = MAX(DDA2.AccountPrimaryBranchName),
[MailingAddress1] = MAX(DDA2.MailingAddress1),
[MailingAddress2] = MAX(DDA2.MailingAddress2),
[MailingCityName] = MAX(DDA2.MailingCityName),
[MailingStateCode] = MAX(DDA2.MailingStateCode),
[MailingStateName] = MAX(DDA2.MailingStateName),
[MailingPostalCode] = MAX(DDA2.MailingPostalCode),
[MailingCountryCode] = MAX(DDA2.MailingCountryCode),
[MailingCountryName] = MAX(DDA2.MailingCountryName),
[CurrencyBasedAccountInd] = MAX(DDA2.CurrencyBasedAccountInd),
[MaturityDate] = MAX(DDA2.MaturityDate),
[OriginalLoanAmount] = MAX(DDA2.OriginalLoanAmount),
[CollateralTypeDesc] = MAX(DDA2.CollateralTypeDesc),
[CollateralTypeCode] = MAX(DDA2.CollateralTypeCode),
[InsuredAmount] = MAX(DDA2.InsuredAmount),
[EmployeeInd] = MAX(DDA2.EmployeeInd)
FROM
dbo.DesDimAccount DDA2 (NOLOCK)
GROUP BY
DDA2.AccountNumber,
DDA2.AccountTypeDesc
)
DDA ON RTRIM(TT.[Description]) = RTRIM(DDA.AccountTypeDesc) AND
H.AccountNumber = DDA.AccountNumber
EDIT: I replaced the query with the AS keyword. I get the same error.
MS Access Query with Error:
SELECT
'H' AS [HeaderRecordIndicator],
'1' AS [SupervisingAgency],
'0' + I.InstitutionCode AS [InstitutionCode],
CONVERT(char(8), H.Date, 112) AS [ReportDate],
'CTR' AS [ReportType],
'21' AS [FormatCode],
'1' AS [SubmissionType]
FROM
(((
SELECT
IIF(ISNULL(FE.IsoCode), FORMAT(DFCF.CurrencyAmount, "##################.00"), FORMAT(DFCF.CurrencyAmount * FE.PhpConversionRate, "##################.00")) AS [PesoAmount],
DFCF.TransactionNumber,
DFCF.AccountNumber,
DFCF.CountryCd,
DFCF.TransactionTypeCd,
DFCF.Time,
DFCF.Date,
DFCF.TransactionStatusCd,
DFCF.TransactionCurrencyCd,
DFCF.BranchNumber,
DFCF.RemitterExtPartyCd,
DFCF.BeneficiaryExtPartyCd,
DFCF.PostedDate,
DFCF.AssociateNumber,
DFCF.ExecutingPartyNumber,
DFCF.CurrencyAmount,
DFCF.CurrencyAmountInTxnCcy,
DFCF.CurrencyAmountInAccountCcy,
DFCF.SecondaryAccountKey,
DFCF.RelatedInd,
DFCF.ThirdPartyInd,
DFCF.TransactionDescription,
DFCF.SecurityName,
DFCF.DealNumber
FROM
DesFactCashFlow DFCF LEFT JOIN
ForeignExchange FE ON DFCF.TransactionCurrencyCd = FE.IsoCode
) AS
H LEFT JOIN
Ctr C ON H.PesoAmount = C.PesoAmountFaceValueSumInsured AND
H.AccountNumber = C.AccountNumber AND
H.TransactionTypeCd = C.TransactionType) LEFT JOIN
TransactionType TT ON H.TransactionTypeCd = TT.Code) LEFT JOIN
(
SELECT
DDA2.AccountNumber AS [AccountNumber],
DDA2.AccountTypeDesc AS [AccountTypeDesc],
MAX(DDA2.LineOfBusinessName) AS [LineOfBusinessName],
MAX(DDA2.AccountCurrencyCode) AS [AccountCurrencyCode],
MAX(DDA2.AccountCurrencyName) AS [AccountCurrencyName],
MAX(DDA2.AccountRegistrationTypeDesc) AS [AccountRegistrationTypeDesc],
MAX(DDA2.AccountRegistrationName) AS [AccountRegistrationName],
MAX(DDA2.AccountName) AS [AccountName],
MAX(DDA2.AlternateName) AS [AlternateName],
MAX(DDA2.AccountOpenDate) AS [AccountOpenDate],
MAX(DDA2.AccountCloseDate) AS [AccountCloseDate],
MAX(DDA2.AccountStatusDesc) AS [AccountStatusDesc],
MAX(DDA2.DormantInd) AS [DormantInd],
MAX(DDA2.ProductLineName) AS [ProductLineName],
MAX(DDA2.ProductCategoryName) AS [ProductCategoryName],
MAX(DDA2.ProductTypeName) AS [ProductTypeName],
MAX(DDA2.ProductName) AS [ProductName],
MAX(DDA2.ProductNumber) AS [ProductNumber],
MAX(DDA2.AccountTaxId) AS [AccountTaxId],
MAX(DDA2.AccountTaxIdTypeCode) AS [AccountTaxIdTypeCode],
MAX(DDA2.AccountTaxStateCode) AS [AccountTaxStateCode],
MAX(DDA2.AccountPrimaryBranchName) AS [AccountPrimaryBranchName],
MAX(DDA2.MailingAddress1) AS [MailingAddress1],
MAX(DDA2.MailingAddress2) AS [MailingAddress2],
MAX(DDA2.MailingCityName) AS [MailingCityName],
MAX(DDA2.MailingStateCode) AS [MailingStateCode],
MAX(DDA2.MailingStateName) AS [MailingStateName],
MAX(DDA2.MailingPostalCode) AS [MailingPostalCode],
MAX(DDA2.MailingCountryCode) AS [MailingCountryCode],
MAX(DDA2.MailingCountryName) AS [MailingCountryName],
MAX(DDA2.CurrencyBasedAccountInd) AS [CurrencyBasedAccountInd],
MAX(DDA2.MaturityDate) AS [MaturityDate],
MAX(DDA2.OriginalLoanAmount) AS [OriginalLoanAmount],
MAX(DDA2.CollateralTypeDesc) AS [CollateralTypeDesc],
MAX(DDA2.CollateralTypeCode) AS [CollateralTypeCode],
MAX(DDA2.InsuredAmount) AS [InsuredAmount],
MAX(DDA2.EmployeeInd) AS [EmployeeInd]
FROM
DesDimAccount DDA2
GROUP BY
DDA2.AccountNumber,
DDA2.AccountTypeDesc
) AS
DDA ON RTRIM(TT.[Description]) = RTRIM(DDA.AccountTypeDesc) AND
H.AccountNumber = DDA.AccountNumber
Here is the simplified query with the same error:
SELECT
*
FROM
(((
SELECT
IIF(ISNULL(FE.IsoCode), FORMAT(DFCF.CurrencyAmount, "##################.00"), FORMAT(DFCF.CurrencyAmount * FE.PhpConversionRate, "##################.00")) AS [PesoAmount],
DFCF.TransactionNumber,
DFCF.TransactionCurrencyCd,
FROM
DesFactCashFlow DFCF LEFT JOIN
ForeignExchange FE ON DFCF.TransactionCurrencyCd = FE.IsoCode
) AS
H LEFT JOIN
Ctr C ON H.PesoAmount = C.PesoAmountFaceValueSumInsured AND
H.AccountNumber = C.AccountNumber AND
H.TransactionTypeCd = C.TransactionType) LEFT JOIN
TransactionType TT ON H.TransactionTypeCd = TT.Code) LEFT JOIN
(
SELECT
DDA2.AccountNumber AS [AccountNumber],
DDA2.AccountTypeDesc AS [AccountTypeDesc],
MAX(DDA2.LineOfBusinessName) AS [LineOfBusinessName],
FROM
DesDimAccount DDA2
GROUP BY
DDA2.AccountNumber,
DDA2.AccountTypeDesc
) AS
DDA ON RTRIM(TT.[Description]) = RTRIM(DDA.AccountTypeDesc) AND
H.AccountNumber = DDA.AccountNumber
Give up trying to convert the SQL text from your T-SQL query to Access SQL. Create a new Access query from scratch and use the T-SQL query only as a road map. Add your data sources and set up the joins. The query designer will guarantee you create the joins in the manner which keeps the db engine happy: addition and positioning of parentheses it requires for queries with more than one join; the rules which apply for LEFT JOINs; and so forth. Just let the designer handle those details for you.
The designer will choke in Design View due to the functions in this part of your last join:
RTRIM(TT.[Description]) = RTRIM(DDA.AccountTypeDesc)
So leave out the RTRIM() functions while you're setting up the joins in Design View. Don't worry that the query doesn't return the correct results. After you get joins which satisfy the db engine, switch to SQL View and add the RTRIM() functions back in.
After you get the joins set up correctly, then add in your field expressions to the SELECT list.
Also you may find it easier to manage your complex query by breaking out the subqueries as separate saved queries --- then reference those queries by name in the master query just as you would table sources.
The problem is with the penultimate line:
DDA ON RTRIM(TT.[Description]) = RTRIM(DDA.AccountTypeDesc) AND
The Design View of the Access query designer can't work with functions in the ON part of the clause. You must remove the RTRIM.
Access is parenthesis happy. Wrap each join expression in parentheses, the ON clauses themselves, and each pair of tables.
You can't use CONVERT, NOLOCK, or CASE.
Which version of MSAccess you are using in your system? I just tried in 2007 version and RTRIM is working.

Resources