Inner join + set max date + naming in ACCESS SQL - sql-server

I am migrating a query from tables held in MS SQL Server to MS Access 2016. I can't seem to figure out how to use the very common max_date inner join from my SQL query and import to my Access query.
I have searched the site and seen many explanations for how to do it in SQL, and my query in SQL works just fine. My Access query will not work.
INNER JOIN (
SELECT dbo_LTS_TRANSACTION_HISTORY.[RFIDTAGID], max(dbo_LTS_TRANSACTION_HISTORY.[TRANSCATION_DATE]) as [max_trxn_date]
FROM dbo_LTS_TRANSACTION_HISTORY
GROUP BY dbo_LTS_TRANSACTION_HISTORY.[RFIDTAGID]) as b
ON dbo_LTS_TRANSACTION_HISTORY.[RFIDTAGID] = b.[RFIDTAGID] AND (dbo_LTS_TRANSACTION_HISTORY.[TRANSCATION_DATE] = b.[max_trxn_date]))
I expect my [TRANSCATION_DATE] to only hold the [max_trxn_date] so only one max date record is pulled for each [RFIDTAGID]. Without naming the join "as b" I don't know how to make this work. Access doesn't seem to take the naming structure I'm used to in SQL. I am getting a compile error for syntax.
Full code below
SELECT dbo_LTS_TRANSACTION_HISTORY.PROD_ORDER, dbo_LTS_TRANSACTION_HISTORY.PART_NUMBER, dbo_LTS_PACKAGE.QUANTITY, dbo_LTS_TRANSACTION_HISTORY.LOCATION, dbo_LTS_TRANSACTION_HISTORY.TRANSCATION_DATE, dbo_LTS_TRANSACTION_HISTORY.CONTAINER_RFIDTAGID, dbo_LTS_TRANSACTION_HISTORY.RFIDTAGID, dbo_LTS_DISCRETE_JOB_SUMMARY.MFG_DELIVERY_DATE, dbo_LTS_DISCRETE_JOB_SUMMARY.EXTENSION_DATE, dbo_LTS_DISCRETE_JOB_SUMMARY.STATUS, dbo_LTS_DISCRETE_JOB_SUMMARY.PBG, dbo_LTS_LOCATION.TYPE
FROM (dbo_LTS_LOCATION
INNER JOIN (dbo_LTS_DISCRETE_JOB_SUMMARY
INNER JOIN dbo_LTS_TRANSACTION_HISTORY
ON (dbo_LTS_DISCRETE_JOB_SUMMARY.[PROD_ORDER] = dbo_LTS_TRANSACTION_HISTORY.[PROD_ORDER])
AND (dbo_LTS_DISCRETE_JOB_SUMMARY.[DISCRETE_JOB_NUMBER] = dbo_LTS_TRANSACTION_HISTORY.[JOB_NUMBER]))
ON dbo_LTS_LOCATION.[DESIGNATION] = dbo_LTS_TRANSACTION_HISTORY.[LOCATION])
INNER JOIN dbo_LTS_PACKAGE ON dbo_LTS_TRANSACTION_HISTORY.[RFIDTAGID] = dbo_LTS_PACKAGE.[RFIDTAGID]
INNER JOIN (
SELECT dbo_LTS_TRANSACTION_HISTORY.[RFIDTAGID], max(dbo_LTS_TRANSACTION_HISTORY.[TRANSCATION_DATE]) as [max_trxn_date]
FROM dbo_LTS_TRANSACTION_HISTORY
GROUP BY dbo_LTS_TRANSACTION_HISTORY.[RFIDTAGID]) as b
ON dbo_LTS_TRANSACTION_HISTORY.[RFIDTAGID] = b.[RFIDTAGID] AND (dbo_LTS_TRANSACTION_HISTORY.[TRANSCATION_DATE] = b.[max_trxn_date]))
WHERE (((dbo_LTS_TRANSACTION_HISTORY.PROD_ORDER)="123456"));

Related

How can I get 3 tables INNER JOIN in MS SQL Server

From the specialist table, retrieve the first name, last name and contact number for the people that provide care to penguins from the species table.
There are 3 tables: tbl_specialist, tbl_species, tbl_care
I need help trying to INNER JOIN the tables to display First, Last, And Contact for penguins.
SELECT specialist_fname, specialist_lname, specialist_contact
FROM ((tbl_specialist
INNER JOIN tbl_species ON species_care = tbl_species.species_care)
INNER JOIN tbl_care ON care_id = tbl_care.care_id)
WHERE species_name = 'penguin'
;
It's a bit difficult without seeing the exact schema of the tables, but your syntax for the subquery is a bit off and you need to alias columns that are found in multiple tables in a JOIN statment. Try rewriting your SQL like this:
SELECT spl.specialist_fname, spl.specialist_lname, spl.specialist_contact
FROM tbl_specialist spl
INNER JOIN tbl_species s
ON spl.species_care = s.species_care
INNER JOIN tbl_care c
ON s.care_id = c.care_id
WHERE s.species_name = 'penguin'
I'm obviously inferring which tables certain columns come from in the join, but hopefully you get the idea.
I figured it out thank you.
SELECT specialist_fname, specialist_lname, specialist_contact
FROM ((tbl_specialist
INNER JOIN tbl_care ON tbl_care.care_specialist = tbl_specialist.specialist_id)
INNER JOIN tbl_species ON tbl_species.species_care= tbl_care.care_id)
WHERE species_name = 'penguin'
;

Filter based on two databases

I have the same query on two databases, but in the first database it finds me data, while in the second one it does not. Is it possible to build on these two bases so that I can filter all those that exist in the first base and not the second. These are my sql query:
use firstDB;
SELECT A_ANSPRECHPARTNER.AAS_ID FROM A_ANSPRECHPARTNER LEFT OUTER JOIN A_ADRESSEN ON A_ANSPRECHPARTNER.AAS_ADR_ID = A_ADRESSEN.ADR_ID WHERE ADR_Nr = 106740
use secondDB;
SELECT A_ANSPRECHPARTNER.AAS_ID FROM A_ANSPRECHPARTNER LEFT OUTER JOIN A_ADRESSEN ON A_ANSPRECHPARTNER.AAS_ADR_ID = A_ADRESSEN.ADR_ID WHERE ADR_Nr = 106740
the current result
If these databases are on the same server you can put each of your queries in a subquery specifying the database name when referencing the table. Then left join the subquery on firstDB to the subquery on secondDB filtering the records where there is a record in firstDB, but not in secondDB.
SELECT DB_1.*
FROM (
SELECT A_ANSPRECHPARTNER.AAS_ID
FROM firstDB.dbo.A_ANSPRECHPARTNER
LEFT OUTER JOIN firstDB.dbo.A_ADRESSEN ON A_ANSPRECHPARTNER.AAS_ADR_ID = A_ADRESSEN.ADR_ID
) DB_1
LEFT JOIN (
SELECT A_ANSPRECHPARTNER.AAS_ID
FROM secondDB.dbo.A_ANSPRECHPARTNER
LEFT OUTER JOIN secondDB.dbo.A_ADRESSEN ON A_ANSPRECHPARTNER.AAS_ADR_ID = A_ADRESSEN.ADR_ID
) DB_2 ON DB_1.ADR_Nr = DB_2.ADR_Nr
WHERE DB_2.ADR_Nr IS NULL
AND DB_1.ADR_Nr = 106740
You can just remove the AND DB_1.ADR_Nr = 106740 in order to find all records in firstDB that are not in secondDB. If these databases are on different servers you would have to set up a linked server and add that to the beginning of your table reference.

Multiple 'Inner Join' with openquery

Im trying to translate an SQL query to OPENQUERY. I have this code that works well:
Select
F_ART.CODART,
F_ART.REFART,
F_ART.DESART,
F_LTA.PRELTA,
F_STO.DISSTO,
F_PRO.NOCPRO,
F_FAM.DESFAM,
F_SEC.DESSEC
From
[LINKED]...F_ART Inner Join
[LINKED]...F_FAM On F_FAM.CODFAM = F_ART.FAMART Inner Join
[LINKED]...F_LTA On F_LTA.ARTLTA = F_ART.CODART Inner Join
[LINKED]...F_PRO On F_PRO.CODPRO = F_ART.PHAART Inner Join
[LINKED]...F_SEC On F_SEC.CODSEC = F_FAM.SECFAM Inner Join
[LINKED]...F_STO On F_STO.ARTSTO = F_ART.CODART
I want to transform it to use OPENQUERY so I have tried this code:
Select
CODART,
REFART,
DESART,
IMGART,
DISSTO
From
OPENQUERY ([LINKED], 'SELECT * FROM
F_ART Inner Join
F_STO On (F_STO.ARTSTO = F_ART.CODART)
')
But when I add the next Inner Join sentences it fails.
Can I use multiple Inner Join with openquery?
Im using this code from MS ACCESS to query a Linked SQL Server.
I have solved this creting a 'View' in the SQL server.
That 'View' contains all the 'Joins' needed.
Then I create the query pointing to the 'View'.

SQL Server to SalesForce Linked Server Nightmare

I am getting inconsistent results from joining Linked Servers. It's something that should be relatively simple... but has taken me hours to get this figured out. I am using SQL Server 2014 and the CData ODBC Driver to Join to SalesForce. I'm not doing anything fancy just trying to perform standard CRUD operations but again and again it seems that when ever I filter these linked server tables that sometimes results do not produce.
My current and main issue right now is I am having difficulty JOINING two Linked tables to two Local tables. If I remove one of the Linked tables from the join results are produced. But whenever I add two linked tables to the joins it produces and empty record set.
And yes all the related identifiers exist so it really is an issue with the Linked Server. Here are the three variations that I've tried:
SELECT * FROM Offer_Interest oi
INNER JOIN Offer o ON oi.Offer_ID_SQL = o.Offer_ID_SQL
INNER JOIN OPENQUERY([TR-SF-PROD], 'SELECT Id, OFFER_ID_SQL__C FROM Offer__c') osf ON o.Offer_ID_SQL =osf.OFFER_ID_SQL__C
INNER JOIN Interest i ON oi.Interest_ID_SQL = i.Interest_ID_SQL
INNER JOIN OPENQUERY([TR-SF-PROD], 'SELECT INTEREST_ID_SQL__C, Id FROM Interest__c') isf ON i.Interest_ID_SQL =isf.Interest_ID_SQL__c
WHERE o.PrimaryContact_ID_SQL = 2803
I've also tried without OPENQUERY:
SELECT * FROM FROM Offer_Interest oi
INNER JOIN Offer o ON oi.Offer_ID_SQL = o.Offer_ID_SQL
INNER JOIN [TR-SF-PROD].[CDataSalesforce].[Salesforce].[Offer__c] osf ON o.Offer_ID_SQL =osf.OFFER_ID_SQL__C
INNER JOIN Interest i ON oi.Interest_ID_SQL = i.Interest_ID_SQL
INNER JOIN [TR-SF-PROD].[CDataSalesforce].[Salesforce].[Interest__c] isf ON i.Interest_ID_SQL =isf.Interest_ID_SQL__c
WHERE o.PrimaryContact_ID_SQL = 2803
And Lastly I've also created Synonyms to the Linked Server tables. All of these work using the same filter or WHERE CLAUSE if I run them seperately although the linked server tables seem buggy if I filter them without OPENQUERY.
This is my first experience Linking a Server to SQL Server so anyone with experience in this or what the issue may be would be greatly appreciated!
Not the answer I was hoping as it should just work. But a temp fix I came up with is stuffing the linked server values I need into Temp Tables then joining on the temp tables, which worked... but of course this adds time onto the execution of the overall stored procedure so it is definitely not the ideal solution. If anyone has a better idea please still answer!!
IF EXISTS(SELECT [NAME] FROM tempdb.sys.tables WHERE [NAME] like '#TempOffer%') BEGIN
DROP TABLE #TempOffer;
END;
SELECT * INTO #TempOffer FROM OPENQUERY([TR-SF-PROD], 'SELECT Id, OFFER_ID_SQL__C FROM Offer__c')
IF EXISTS(SELECT [NAME] FROM tempdb.sys.tables WHERE [NAME] like '#TempInterest%') BEGIN
DROP TABLE #TempInterest;
END;
SELECT * INTO #TempInterest FROM OPENQUERY([TR-SF-PROD], 'SELECT INTEREST_ID_SQL__C, Id FROM Interest__c')
SELECT * FROM Offer_Interest oi
INNER JOIN Offer o ON oi.Offer_ID_SQL = o.Offer_ID_SQL
INNER JOIN #TempOffer osf ON o.Offer_ID_SQL =osf.OFFER_ID_SQL__C
INNER JOIN Interest i ON oi.Interest_ID_SQL = i.Interest_ID_SQL
INNER JOIN #TempInterest isf ON i.Interest_ID_SQL =isf.Interest_ID_SQL__c
WHERE o.PrimaryContact_ID_SQL = 2803

Having trouble converting SQL query to Access

I have this query which works fine in SQL Server, but not in Access, and I'm having trouble converting it. I've always heard that JET is missing some TSQL features, and I suppose complex joins is one of them.
SELECT C.[Position], TT.[Description] as TrainingType, T.ProgramTitle, T.ProgramSubTitle, T.ProgramCode, ET.CompletedDate
from HR_Curriculum C
LEFT JOIN HR_Trainings T ON C.TrainingID = T.TrainingID
LEFT JOIN HR_TrainingTypes TT ON T.TrainingTypeID = TT.TrainingTypeID
LEFT JOIN HR_EmployeeTrainings ET ON C.TrainingID = ET.TrainingID
AND (ET.AvantiRecID IS NULL OR ET.AvantiRecID = '637')
where ( c.[Position] = 'Customer Service Representative'
OR C.[Position] = 'All Employees')
order by [Position], Description, ProgramTitle
I tried putting the extra join clause down in the WHERE clause, but for some reason this does not yield the proper count of records.
When you have more than one JOIN in ms-access you need to wrap them with parenthesis like this:
SELECT C.[Position], TT.[Description] as TrainingType, T.ProgramTitle, T.ProgramSubTitle, T.ProgramCode, ET.CompletedDate
from (((HR_Curriculum C
LEFT JOIN HR_Trainings T ON C.TrainingID = T.TrainingID)
LEFT JOIN HR_TrainingTypes TT ON T.TrainingTypeID = TT.TrainingTypeID)
LEFT JOIN HR_EmployeeTrainings ET ON C.TrainingID = ET.TrainingID
AND (ET.AvantiRecID IS NULL OR ET.AvantiRecID = '637'))
where ( c.[Position] = 'Customer Service Representative'
OR C.[Position] = 'All Employees')
order by [Position], Description, ProgramTitle
or you will have the Missing Operator error
Check that your table alias are delacred with 'as'. Access doesn't like [tablename] [alias], instead try [tablename] as [alias]. I know the complex left joins shouldn't be a problem, but Access might be choking an the alias delcarations if it's returning some join error. I would also try quering out the restriction on the ET table, and then joining that to the larger query. I've noticed that trying to put restrictions on records involved in left or right joins will often not produce the correct records as Access will limit the set after the join.

Resources