Implementing "Except" SQL Operation in MS Access 2003 - sql

I think that I should explain my problem a little bit in detail. May be you can help me to solve it.
I want to save a resault of a query in a "Temporary" Table hence I tried to use this query:
SELECT *
INTO Temp_tbl
FROM (tb_KonzeptDaten LEFT JOIN tb_Fahrzeug ON tb_KonzeptDaten.Konzept = tb_Fahrzeug.ID) LEFT JOIN tb_MSG ON tb_Fahrzeug.Motor_SG = tb_MSG.ID
I got the error 3090 which says "Resultant table not allowed to have more than one AutoNumber field."
that was correct! in eac of these three table I have an autonumber field
then I've deceided to select all fields without these Autonumber fields and then I've found that we can do this job with SQL EXCEPT operator but unfortunatlly it seems that this operator doesn't work in MS Access 2003
then I tried to use SQL "NOT EXIST" operator:
SELECT *
FROM (tb_KonzeptDaten LEFT JOIN tb_Fahrzeug ON tb_KonzeptDaten.Konzept = tb_Fahrzeug.ID) LEFT JOIN tb_MSG ON tb_Fahrzeug.Motor_SG = tb_MSG.ID
WHERE NOT EXISTS(SELECT tb_KonzeptDaten.ID FROM tb_KonzeptDaten)
but I didn't get my desired answer
What do you mean? How can I solve this problem?

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

Filtering on a derived column in UniOLEDB

I am working on a piece of SQL for a IBM U2 Rocket database. It's not a flavour of db platform I'm familiar with.
I do not have direct access to this database: I can only access it by composing statements in the calling code and testing it from there. That makes it kind of awkward to figure out where the problem is when there are syntax errors.
I'm trying to compose a query which has a condiational on a dervied column. As far as I can tell from what I've read about the database, this ought to work:
SELECT a.*
FROM (
SELECT dp.NAME
,dp.Code
,dp.BusinessType + ts.BusinessType AS bType
FROM dataPoints dp
LEFT OUTER JOIN trialSuppliers ts
WHERE ts.AccountStatus = 'A'
) a
WHERE a.bType LIKE '%cho%'
However, it throws this error:
Died in UCI::SQLExecDirect() with SQLSTATE 37000, Native error:0
[IBM][SQL Client][UNIDATA]
If you just run the inner query, it works fine. Trying to use any kind of inner select statement causes it to throw the same error, i.e. this:
SELECT *
FROM (
SELECT dp.NAME
,dp.Code
,dp.BusinessType + ts.BusinessType AS bType
FROM dataPoints dp
LEFT OUTER JOIN trialSuppliers ts
WHERE ts.AccountStatus = 'A'
)
Still fails.
What's the correct syntax to be able to filter my query by the derived column?
Just doing some clean-up, did the last comment resolve the issue?
I am not a UniData user but in its half cousin UniVerse the "from clause" has to be a table. You have to do sub-queries in the where clause. I would do what you want to do adding a couple of I-descriptors in the dictionary. I have often found the limited SQL compliance of the U2 products to be somewhat of a hindrance, but when your metadata is external to and separate from your data you are going to lose a bit of structured query sanity. – Van Amburg Sep 21 '17 at 17:31

MS Access: LEFT JOIN with Filters

My MS Access version is 2003, in case that matters.
I have a single table with daily values for securities in an account. I'd like to compare the values of all securities in each account, one year ago versus today (and create an expression for the difference). The securities in the account may change over the course of a year, so there must be NULL values when linking by security. Accordingly, I'd like to perform a FULL OUTER JOIN, which I understand is not possible in MS Access. Alternatively, I'll have to create a UNION of a LEFT JOIN and RIGHT JOIN, as suggested in this SO post.
Although the below query behaves like an INNER JOIN, I believe the picture will help illustrate what I'm trying to accomplish:
I understand that creating this query in Design View causes the filters to go into the WHERE clause, which is filtering out data before the LEFT JOIN is performed. I'm attempting to replicate the solution proposed in this SO post, so far unsuccessfully. Following is my current SQL statement:
SELECT dbo_vw_Core_Monitor_Historical.AsOFdate,
dbo_vw_Core_Monitor_Historical.Account,
dbo_vw_Core_Monitor_Historical.SecID,
dbo_vw_Core_Monitor_Historical.YTM,
dbo_vw_Core_Monitor_Historical_1.AsOFdate,
dbo_vw_Core_Monitor_Historical_1.Account,
dbo_vw_Core_Monitor_Historical_1.SecID,
dbo_vw_Core_Monitor_Historical_1.YTM,
[dbo_vw_Core_Monitor_Historical_1.YTM] - [dbo_vw_Core_Monitor_Historical.YTM] AS YTM_Change
FROM dbo_vw_Core_Monitor_Historical
LEFT JOIN
dbo_vw_Core_Monitor_Historical AS dbo_vw_Core_Monitor_Historical_1
ON ((dbo_vw_Core_Monitor_Historical.Account = dbo_vw_Core_Monitor_Historical_1.Account)
AND (dbo_vw_Core_Monitor_Historical.SecID = dbo_vw_Core_Monitor_Historical_1.SecID)
AND ((dbo_vw_Core_Monitor_Historical_1.AsOFdate)=#12/8/2015#))
WHERE ((dbo_vw_Core_Monitor_Historical.AsOFdate)=#12/8/2014#);
I've tried a few different queries, but I believe the above is most correct based on what I've gathered from SO. This causes MS Access to immediately crash. I'm expecting output something like the below (where the highlights are for SecID's no longer in the account as of 12/8/2015:
Any advice? Is this just a symptom of using MS Access, rather than some more robust database?
You must make a difference between records that are there but with NULL fields and records that are missing on the right (outer) side. If you are dealing with missing records, your query looks correct. I have no idea why Access crashes. You could change the query into a pass-through query. This means that the query will be executed by SQL-Server. Of course it must be written in T-SQL then:
SELECT
A.AsOFdate,
A.Account,
A.SecID,
A.YTM,
B.AsOFdate,
B.Account,
B.SecID,
B.YTM,
B.YTM - A.YTM AS YTM_Change
FROM
dbo.vw_Core_Monitor_Historical A
LEFT JOIN dbo.vw_Core_Monitor_Historical AS B
ON A.Account = B.Account AND
A.SecID = B.SecID AND
B.AsOFdate = '2015/12/08'
WHERE
A.AsOFdate = '2014/12/08';

Inner Join Ambiguous Syntax

I'm not super familiar with SQL but I know the basics. I was recently trying to replicate some logic form reports to SQL Server 2012. I started with the custom query from Webi (a reporting tool) and was trying to make a view from it in SQL.
Here is what the query look like:
SELECT
dimGlobalSalesAnalysisTbl.globalSalesAnalysisDesc,
dimGlobalShipDestinationCountryTbl.area,
dimGlobalShipDestinationCountryTbl.subarea,
dimGlobalCurrentProductTbl.sbuCodeDesc,
dimGlobalShipDateVw.shipDayOfWeekDesc,
sum(factSalesTblVw.globalSalesValue) AS 'Global Sales Value',
SUM(factSalesTblVw.salesUnitQuantity*GlobalFiles.dimCurrentGTINTbl.unitQty) AS 'Sales Unit Quantity'
FROM
dimGlobalCookCompaniesTbl INNER JOIN factSalesTblVw ON
(dimGlobalCookCompaniesTbl.globalCookCompanyID=factSalesTblVw.globalCookCompanyID)
INNER JOIN dimGlobalHistProductTbl ON (dimGlobalHistProductTbl.globalHistProductID=factSalesTblVw.globalHistProductID)
INNER JOIN dimGlobalCurrentProductTbl ON (dimGlobalHistProductTbl.globalCurrentProductID=dimGlobalCurrentProductTbl.globalCurrentProductID)
INNER JOIN dimGlobalHistShipCustomerTbl ON (factSalesTblVw.globalHistShipCustomerID=dimGlobalHistShipCustomerTbl.globalHistShipCustomerID)
INNER JOIN dimGlobalCurrentShipCustomerTbl ON (dimGlobalHistShipCustomerTbl.shipCustomerID=dimGlobalCurrentShipCustomerTbl.globalCurrentShipCustomerID)
***INNER JOIN dimGlobalCountryTbl dimGlobalShipDestinationCountryTbl ON (dimGlobalCurrentShipCustomerTbl.shipDestCountryDesc=dimGlobalShipDestinationCountryTbl.countryCode)***
INNER JOIN dimGlobalSalesAnalysisTbl ON (factSalesTblVw.globalSalesAnalysisID=dimGlobalSalesAnalysisTbl.globalSalesAnalysisID)
INNER JOIN dimGlobalShipDateVw ON (dimGlobalShipDateVw.shipJulianDate=factSalesTblVw.shipDateID)
INNER JOIN GlobalFiles.dimCurrentGTINTbl ON (GlobalFiles.dimCurrentGTINTbl.curGtinId=factSalesTblVw.GtinID)
WHERE
(
dimGlobalShipDateVw.shipYearNumber IN (DATEPART(yy,GETDATE())-1)
AND
dimGlobalCurrentShipCustomerTbl.shipCustomerNumberDesc
IN ( 'JPC000222-3','CNC000012-1' )
AND
dimGlobalSalesAnalysisTbl.globalSalesAnalysisDesc = 'Return Credits'
)
GROUP BY
dimGlobalShipDateVw.shipDate,
dimGlobalSalesAnalysisTbl.globalSalesAnalysisDesc,
dimGlobalShipDestinationCountryTbl.area,
dimGlobalShipDestinationCountryTbl.subarea,
dimGlobalCurrentProductTbl.sbuCodeDesc,
Upper(dimGlobalCurrentProductTbl.familyCodeDesc),
dimGlobalShipDateVw.shipYearNumber,
dimGlobalShipDateVw.shipDayOfWeekDesc,
dimGlobalCurrentProductTbl.madeByAbbr,
dimGlobalCookCompaniesTbl.companyDesc
This particular query runs on the production system if ran in the relevant database. When trying to make a view of this query in a different database, I precede the objects by [database_name].[schema/dbo] name.
On running the query, I get the error:
Invalid object name 'WWS.dbo.dimGlobalShipDestinationCountryTbl'
I try to find this particular table on the database, but it isn't there, though hovering over the table name in the query give a table definition but no script.
This table is present in an weird looking inner join (6th inner join) syntax like this:
INNER JOIN dimGlobalCountryTbl dimGlobalShipDestinationCountryTbl ON
(dimGlobalCurrentShipCustomerTbl.shipDestCountryDesc=dimGlobalShipDestinationCountryTbl.countryCode)
Two questions:
1. Can someone please explain this query syntax for inner join ?
2. This is pretty stupid but any ideas on how to look into possibly hidden table definitions ?
Two questions: 1. Can someone please explain this query syntax for inner join ?
The inner join in this case is nothing more than a table alias. The creator of the query thought aliasing the table would be easier to understand this name instead of the actual table name, or the same table is referenced twice and one would have to have an alias.
2. This is pretty stupid but any ideas on how to look into possibly hidden table definitions ?
Why? I think you just have a syntax error on your SQL when you added the database_name.schema syntax.
Think of the table alias like a column alias.... but and just like columns, you can omit the 'AS' keyword...
dimGlobalCountryTbl dimGlobalShipDestinationCountryTbl is the same as
dimGlobalCountryTbl AS dimGlobalShipDestinationCountryTbl

LEFT JOINing on additional criteria in MS Access

I have the following T-SQL query (a simple test case) running fine in MS SQL but cannot get the equivalent query in MS Access (JET-SQL). The problem is the additional criteria in the LEFT JOIN. How can I do this in MS Access?
T-SQL:
SELECT * FROM A
LEFT OUTER JOIN B ON A.ID = B.A_ID
AND B.F_ID = 3
JET-SQL (what I have so far but crashes Access!):
SELECT * FROM dbo_A
LEFT JOIN dbo_B ON (dbo_A.ID = dbo_B.A_ID AND dbo_B.F_ID = 3)
You need to use a subselect to apply the condition:
SELECT *
FROM dbo_A LEFT JOIN
[SELECT dbo_B.* FROM dbo_B WHERE dbo_B.F_ID = 3]. AS dbo_B
ON dbo_A.ID = dbo_B.A_ID;
If you're running Access with "SQL 92" compatibility mode turned on, you can do the more standard:
SELECT *
FROM dbo_A LEFT JOIN
(SELECT dbo_B.* FROM dbo_B WHERE dbo_B.F_ID = 3) AS dbo_B
ON dbo_A.ID = dbo_B.A_ID;
Do you need this to be editable in Access? If not, just use a passthrough query with the native T-SQL. If so, I would likely create a server-side view for this, and I'd especially want to move it server-side if the literal value is something you would parameterize (i.e., the F_ID=3 is really F_ID=N where N is a value chosen at runtime).
BTW, I write these subselect derived table SQL statements every single day while working in Access. It's not that big a deal.
Do you get an error message when it crashes or does it just lock up? Judging by the dbo_B name I'm going to guess that these are linked tables in Access. I believe that when you do a join like that Access doesn't tell SQL server that it needs the result of the join, it says, "Give me all of the rows of both tables" then it tries to join them itself. If the tables are very large this can cause the application to lock up.
You're probably better off creating a view on SQL Server for what you need.
I think ms access expect to both tables name in each section of Joins ON clause. As a trick this work for me:
SELECT * FROM A
LEFT OUTER JOIN B ON A.ID = B.A_ID
AND B.F_ID = IIF(True, 3, A.ID)
A.ID or any other else field from table A
That last condition technically isn't a join but a comparison to a literal value. Put it in a WHERE clause:
SELECT *
FROM a LEFT OUTER JOIN b ON a.ID = b.a_id
WHERE b.f_id = 3;