Compare two tables and find matches in ms-access 2007 - sql

I am trying to compare two tables in ms access 2007 to find matches from the tables which I know to have one field in common. When I try to run the following query
SELECT
FROM FirstTrial INNER JOIN ConsolidatedDatabase
ON FirstTrial.ModelNumbr = ConsolidatedDatabase.ModelNumbr;
I get an error 'Query must have at least one destination field'
What do I need to change in order to make the query work?

You need some fields:
SELECT *
FROM FirstTrial INNER JOIN ConsolidatedDatabase
ON FirstTrial.ModelNumbr = ConsolidatedDatabase.ModelNumbr;
It would be better to put some names instead of *
In MS Access, you can use the query design window.

Related

Does Excel as something similar to SQL "Except"?

I am able to perform Union of two tables in MS Query of excel but its throwing error for "Except".
Can someone please let me know is there anyway to use "Except" of two tables in Excel.
SELECT * FROM [Book1query] tbl2
EXCEPT
SELECT * FROM [Merge1] tbl1
EXCEPT returns only rows, which are not available in the second SELECT statement. In other words, a join type of left anti.
In Power Query you can do that with a Merge of two tables, using the Join Kind option for Left Anti.
For a detailed description of all the Join Kind options, visit https://radacad.com/choose-the-right-merge-join-type-in-power-bi

Access SQL Syntax error: missing operator

I am trying to convert a T-SQL query to MS Access SQL and getting a syntax error that I am struggling to find. My MS Access SQL query looks like this:
INSERT INTO IndvRFM_PreSort (CustNum, IndvID, IndvRScore, IndRecency, IndvFreq, IndvMonVal )
SELECT
IndvMast.CustNum, IndvMast.IndvID, IndvMast.IndvRScore,
IndvMast.IndRecency, IndvMast.IndvFreq, IndvMast.IndvMonVal
FROM
IndvMast
INNER JOIN
OHdrMast ON IndvMast.IndvID = OHdrMast.IndvID
INNER JOIN
MyParameterSettings on 1=1].ProdClass
INNER JOIN
[SalesTerritoryFilter_Check all that apply] ON IndvMast.SalesTerr = [SalesTerritoryFilter_Check all that apply].SalesTerr
WHERE
(((OHdrMast.OrdDate) >= [MyParameterSettings].[RFM_StartDate]))
GROUP BY
IndvMast.CustNum, IndvMast.IndvID, IndvMast.IndvRScore,
IndvMast.IndRecency, IndvMast.IndvFreq, IndvMast.IndvMonVal,
[CustTypeFilter_Check all that apply].IncludeInRFM,
[ProductClassFilter_Check all that apply].IncludeInRFM,
[SourceCodeFilter_Check all that apply].IncludeInRFM,
IndvMast.FlgDontUse
I have reviewed differences between MS Access SQL and T-SQL at http://rogersaccessblog.blogspot.com/2013/05/what-are-differences-between-access-sql.html and a few other locations but with no luck.
All help is appreciated.
update: I have removed many lines trying to find the syntax error and I am still getting the same error when running just (which runs fine using T-SQL):
SELECT
IndvMast.CustNum, IndvMast.IndvID, IndvMast.IndvRScore,
IndvMast.IndRecency, IndvMast.IndvFreq, IndvMast.IndvMonVal
FROM
IndvMast
INNER JOIN
OHdrMast ON IndvMast.IndvID = OHdrMast.IndvID
INNER JOIN
[My Parameter Settings] ON 1 = 1
There are a number of items in your query that should also have failed in any SQL-compliant database:
You have fields from tables in GROUP BY not referenced in FROM or JOIN clauses.
Number of fields in SELECT query do not match number of fields in INSERT INTO clause.
The MyParameterSettings table is not properly joined with valid ON expression.
Strictly MS Access SQL items:
For more than one join, MS Access SQL requires paired parentheses but even this can get tricky if some tables are joined together and their paired result joins to outer where you get nested joins.
Expressions like ON 1=1 must be used in WHERE clause and for cross join tables as MyParameterSettings appears to be, use comma-separated tables.
For above reasons and more, it is advised for beginners to this SQL dialect to use the Query Design builder providing table diagrams and links (if you have the MS Access GUI .exe of course). Then, once all tables connect graphically with at least one field selected, jump into SQL view for any nuanced scripting logic.
Below is an adjustment to SQL statement to demonstrate the parentheses pairings and for best practices, uses table aliases especially with long table names.
INSERT INTO IndvRFM_PreSort (CustNum, IndvID, IndvRScore, IndRecency, IndvFreq, IndvMonVal)
SELECT
i.CustNum, i.IndvID, i.IndvRScore, i.IndRecency, i.IndvFreq, i.IndvMonVal
FROM
[MyParameterSettings] p, (IndvMast i
INNER JOIN
OHdrMast o ON i.IndvID = o.IndvID)
INNER JOIN
[SalesTerritoryFilter_Check all that apply] s ON i.SalesTerr = s.SalesTerr
WHERE
(o.OrdDate >= p.[RFM_StartDate])
GROUP BY
i.CustNum, i.IndvID, i.IndvRScore, i.IndRecency, i.IndvFreq, i.IndvMonVal
And in your smaller SQL subset, the last table does not need an ON 1=1 condition and may be redundant as well in SQL Server. Simply a comma separate table will suffice if you intend for cross join. The same is done in above example:
SELECT
IndvMast.CustNum, IndvMast.IndvID, IndvMast.IndvRScore,
IndvMast.IndRecency, IndvMast.IndvFreq, IndvMast.IndvMonVal
FROM
[My Parameter Settings], IndvMast
INNER JOIN
OHdrMast ON IndvMast.IndvID = OHdrMast.IndvID
I suppose there are some errors in your query, the first (more important).
Why do you use HAVING clause to add these conditions?
HAVING (((IndvMast.IndRecency)>(date()-7200))
AND (([CustTypeFilter_Check all that apply].IncludeInRFM)=1)
AND (([ProductClassFilter_Check all that apply].IncludeInRFM)=1)
AND (([SourceCodeFilter_Check all that apply].IncludeInRFM)=1)
AND ((IndvMast.FlgDontUse) Is Null))
HAVING usually used about conditions on aggregate functions (COUNT, SUM, MAX, MIN, AVG), for scalar value you must put in WHERE clause.
The second: You have 12 parenthesis opened and 11 closed in HAVING clause

WQL Query and displaying custom fields

I'm running a WQL Query from SCCM to grab a list of computers with one of two applications installed. I'd like to display the name of the offending application in a column alongside the computer name. Do you know of an easy way to do this?
Another way to accomplish this would be multiple Select Statements, of course.
My query code:
select SMS_G_System_SYSTEM.ResourcelD From SMS_R_System inner
join SMS_G_System_SYSTEM on SMS_G_System_5VSTEM.ResourcelD =
SMS_R_System.ResourcelD inner join
SMS_G_System_ADD_REMOVE_PROGRAMS on
SMS_G_System_ADD_REMOVE_PROGRAM5. ResourcelD =
SMS_R_System. Resourceld where
(SMS_G_System_ADD_REMOVE_PROGRAMS. DisplayName like "%Chrome%") or
(SMS_G_System_ADD_REMOVE_PROGRAMS,DisplayName like "%Firefox%”)
To add a column you add it to your select.
SELECT
SMS_R_System.Netbios_Name0 as 'Name',
SMS_G_System_ADD_REMOVE_PROGRAMS.DisplayName as 'Software'
FROM
....
etc
Let me know if this helps...

Need help forming SQLite join query

I've been able to make a query using MS Access 2010 that does just what I want but I am having trouble doing so in SQLite3. Here is the Access SQL
SELECT pubacc_lo.*
FROM pubacc_en
LEFT JOIN pubacc_lo ON pubacc_en.call_sign = pubacc_lo.call_sign;
Basically it selects all of the columns in the pubacc_lo table and the rows where the call_sign fields are equal between the tables. This does not select any of the pubacc_en data into the final query in MS Access.
Playing around in SQLite 3, the closest I've gotten was
SELECT * FROM PUBACC_LO, PUBACC_EN WHERE PUBACC_en.call_sign=PUBACC_LO.call_sign
But this statement selects all of the data in the EN table along with the LO table (cross join?). I've tried some left outer joins but haven't had any luck. Any tips would be appreciated!
You should be able to use the same query as you got from Access. SQLite3 does support left outer joins.
As for your query, if you only want the fields from the LO table, then ask for that in your SELECT clause like this:
SELECT PUBACC_LO.*
FROM PUBACC_LO, PUBACC_EN
WHERE PUBACC_en.call_sign=PUBACC_LO.call_sign
but the problem here is that it will only return call_signs with entries in both tables, while the outer join from access will return all rows from PUBACC_EN irrespective of whether there is a corresponding PUBACC_LO entry..
SELECT pubacc_lo.* FROM PUBACC_LO, PUBACC_EN WHERE UBACC_en.call_sign=PUBACC_LO.call_sign
If you only want to select pubbac_lo's field, this is what you can use.

Cross-database queries with numbered database name

I'm a bit of a novice when it comes to SQL Server 2005. I have a database containing most of the stored procedures and tables (we'll call it 'GrandDatabase'). Each user has its own separate database named after the user's numbered ID. So I have a database list as follows, for example:
GrandDatabase
100
101
102
...
I need to join tables across the GrandDatabase and a user's database. I've read elsewhere that the following should work, when executed from GrandDatabase:
SELECT
*
FROM
GrandDatabase.User INNER JOIN
100.dbo.UserInfo ON GrandDatabase.User.UserID = 100.dbo.UserInfo.UserID
This gives me a syntax error, complaining about the '.' right after the first reference to the 100 database. I did a little tweaking and discovered that this code works fine when I use non-numbered databases (for instance, replacing the '100' above with 'User100'). Does anybody know how to make this work with numbered database names?
Thanks!
Chris
Try using [100].dbo.UserInfo instead of just the 100.
Try putting the numbers into square brackets and using aliases, e.g.:
SELECT *
FROM GrandDatabase.User
INNER JOIN [100].dbo.UserInfo u
ON GrandDatabase.User.UserID = u.UserID
Try enclosing the database name with brackets:
SELECT
*
FROM
GrandDatabase.User INNER JOIN
[100].dbo.UserInfo ON GrandDatabase.User.UserID = [100].dbo.UserInfo.UserID