473,387 Members | 3,801 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Complex Query

Thats my situation:
I have three tables:
1. Main table: tblDB_Daten
Fields: [ID], [Title], [PublisherLocation], [PublisherName], [ISBN]
2. Table containing the information for the query: tblMark_PV
Fields: [ISBN], [PubLocSingle], [PubLocCombin], [PubName]
3. Table for the result of query: tblDB_PV
Fields: [ID_Daten], [PV]

As a result the fields [tblDB_PV].[PV] in the "result-table" should be filled with the text "PV" in case
1. Field [tblDB_Daten].[ISBN] in Main-table contains [tblMark_PV].[ISBN] from the "query-table"
2. Field [tblDB_Daten].[PublisherLocation] in Main-table contains [tblMark_PV].[PubLocSingle] from the "query-table"
3. Field [tblDB_Daten].[PublisherLocation] in Main-table contains [tblMark_PV].[PubLocCombin] from the "query-table"
and additionally
Field [tblDB_Daten].[PublisherName] in Main-table contains [tblMark_PV].[PubName] from the "query-table"

I have tried for days to generate a working SQL-Code (using UPDATE and LEFT JOIN) but failed. So I am asking for some help now.
Oct 2 '15 #1

✓ answered by jforbes

I think this will work for you, maybe:
Expand|Select|Wrap|Line Numbers
  1. "WHERE D.[ISBN] LIKE '*' & M.PV_ISBN & '*'"
or better yet:
Expand|Select|Wrap|Line Numbers
  1. "WHERE D.[ISBN] LIKE M.PV_ISBN & '*'"
I apologize for not providing this earlier, I was mixing and matching SQL Server and MS-Access syntax.

11 1191
jforbes
1,107 Expert 1GB
What does your current query look like?
Oct 5 '15 #2
NeoPa
32,556 Expert Mod 16PB
Is the "query-table" a reference to [tblDB_PV]?

In general you've included all the important information, but I'm sorry to say that it doesn't make clear sense (Certainly not to me at least). I expect something's been lost in translation so I have to say you've done a good job, but if you could make it clearer that would be very helpful.

As JForbes says, it would also be very helpful, and allow us to help you more, if you were to post in what you have got to so far in the way of your SQL.

I can say that you'll need JOINs between at least two tables. How those JOINs would work I'm not clear on yet without a better understanding of what you're trying to do.
Oct 5 '15 #3
Thnks for the replies!
I start a new trial:
tblDB_Daten contains a set of data, that will be imported from an external source once a week. From this dataset the following information should be extracted: Is the ISBN from a publisher located in Bavaria, is the publishers location in Bavaria. As the location is not always distinct, an additional query is necessary: Location im combination with publishers name.
The relevant information for the query is in tblMark_PV.
I indend to write the query result in a seperate table tblDB_PV.
There also is a query-table with the data from tblDB_Daten and hopefully the data from tblDB_PV.
As the user should have the choice of the criterias (ISBN, location ... etc), I thoght this seperate table tblDB_PV might be helpfull.
By now I fail even with the first query-step.
Expand|Select|Wrap|Line Numbers
  1. Dim s As String
  2. ' Transfers the IDs from tblDB_Daten to tblDB_PV
  3.     Set db = CurrentDb
  4.         s = "UPDATE tblDB_PV AS P " & _
  5.         "RIGHT JOIN tblDB_Daten AS D " & _
  6.         "ON D.[ID]=P.[ID_tblDB_Daten] " & _
  7.         "SET P.[ID_tblDB_Daten] = D.[ID] "
  8.     db.Execute s
  9. ' Should fill tblDB_PV with "PV" according to tblMark_PV
  10.     s = "UPDATE tblDB_PV AS P " & _
  11.         "RIGHT Join (tblDB_Daten AS D " & _
  12.                    "LEFT JOIN tblMark_PV AS M " & _
  13.                    "ON D.ISBN=M.[PV_ISBN]) " & _
  14.        "ON D.ID=P.[ID_tblDB_Daten] " & _
  15.         "SET P.[DB_Daten_PV] = 'PV' " & _
  16.         "WHERE D.[ISBN] LIKE '" & M.[PV_ISBN] & "*'"
  17.     db.Execute s
  18.     Set db = Nothing
  19.  
Here comes error 424 "object required".
Im am quite discouraged and very thankful for some help.
Oct 5 '15 #4
jforbes
1,107 Expert 1GB
I'm pretty sure the first Update wont do anything as it is updating with the same fields that are in the Join. If you are expecting it to create records, you will want an Insert Query.

The second Update Joining looks suspect as the Alias D is inside the Temp Table. I think this is more like what you are looking for:
Expand|Select|Wrap|Line Numbers
  1. ' Should fill tblDB_PV with "PV" according to tblMark_PV
  2. s = "UPDATE tblDB_PV AS P " & _
  3.     "INNER JOIN tblMark_PV AS M " & _
  4.     "ON M.ID=P.[ID_tblDB_Daten] " & _
  5.     "INNER JOIN tblDB_Daten AS D " & _
  6.     "ON D.ISBN=M.[PV_ISBN] " & _
  7.     "SET P.[DB_Daten_PV] = 'PV' " 
I changed the Join Type to Inner as I think that is really what you are after and I don't think you need the WHERE as this is being taken care of by the Join. (Inner will only return records where there are records found on both sides of the Join)

A lot of this is just guess work and if it isn't right, let us know.

Thinking about the big picture, I'm not sure why you are doing all this. It's possible you could create a single select query to return all the information that you need without updating tables. Usually, in a situation where there is a data file being supplied on a regular basis, it's easiest to run an insert query to grab a copy the data into a history type table, then use a select query to format the history type table for whatever other function/form needs to use it.
Oct 6 '15 #5
Yes the first UPDATE is unnecessary, if I use INSERT INTO instead. (I forgot about that.)
Your advice about the "history type table" is very helpful. So it should also be possible to export a table, that contains all the additional information.
For the first it is a pity, your code does not work. I need the WHERE/LIKE as only the first numbers of the ISBN are relevant. I tried with modifying: Using brackets for Access, RIGHT JOIN and so on. As there is no correlation of M.ID with P.[ID_tblDB_Daten], changed with M.[PV_ISBN]=P.[DB_Daten_PV]. But nothing works.
By the way my original code works with a string in the WHERE.
Expand|Select|Wrap|Line Numbers
  1.     s = "UPDATE tblDB_PV AS P " & _
  2.         "INNER Join (tblDB_Daten AS D " & _
  3.                    "LEFT JOIN tblMark_PV AS M " & _
  4.                    "ON D.[ISBN]=M.[PV_ISBN]) " & _
  5.        "ON D.ID=P.[ID_tblDB_Daten] " & _
  6.         "SET P.[DB_Daten_PV] = 'PV' " & _
  7.         "WHERE D.[ISBN] LIKE '" & "978" & "*'"
  8.  
When I replace with
Expand|Select|Wrap|Line Numbers
  1. "WHERE D.[ISBN] LIKE '*" & M.PV_ISBN & "*'"
, the "object required erros" appears.
I am very glad about further assistance.
Oct 7 '15 #6
jforbes
1,107 Expert 1GB
I think the trouble you are running into is that M.PV_ISBN isn't available when you are running the VBA Code, it's only available when the SQL is executed. I didn't quite catch this before as I was focusing on whether or not you would need the WHERE clause. If you really feel that you need the WHERE, you could try:
Expand|Select|Wrap|Line Numbers
  1. "WHERE D.[ISBN] = M.PV_ISBN "
  2. 'OR
  3. "WHERE D.[ISBN] LIKE '*' + M.PV_ISBN + '*'"
  4. 'Or If you are attempting to pull the ISBN from
  5. 'the current Form's recordset, the syntax is something like:
  6. "WHERE D.[ISBN] LIKE '*" & Me!PV_ISBN & "*'"
Oct 7 '15 #7
It works fine with
Expand|Select|Wrap|Line Numbers
  1. "WHERE D.[ISBN] = M.PV_ISBN"
  2.  
So "M.PV_ISBN" should be available. It is the LIKE in combination with a "Field-Query", that fails.
Du You have an alternative for the WHERE/LIKE clause? As I said, only the first part of the ISBN is relevant.
Oct 7 '15 #8
jforbes
1,107 Expert 1GB
I think this will work for you, maybe:
Expand|Select|Wrap|Line Numbers
  1. "WHERE D.[ISBN] LIKE '*' & M.PV_ISBN & '*'"
or better yet:
Expand|Select|Wrap|Line Numbers
  1. "WHERE D.[ISBN] LIKE M.PV_ISBN & '*'"
I apologize for not providing this earlier, I was mixing and matching SQL Server and MS-Access syntax.
Oct 7 '15 #9
Thanks for your engagement.
When I try this Code there is no errror message, but it gives back all titles thal have an ISBN. So for me it looks as if the "M.PV_ISBN" is empty. Thats only the wildcard "'*" does the job.
Surprisingly "M.PV_ISBN" is correctly filled when the clause ""WHERE D.[ISBN] = M.PV_ISBN"" is used.
Possibly its time to give up and engage with your suggestion of a history type table. Otherwise I`d like to understand at least.
Oct 7 '15 #10
NeoPa
32,556 Expert Mod 16PB
Hi.

Please post that last explanation within the context if you would. It doesn't seem to make sense as you've expressed it so it's hard to understand what you mean.

IE. Show both the working and the non-working WHERE clauses in full, in separate code windows, and with the results of each explained.

That would be very helpful.
Oct 9 '15 #11
Thanks for your offer.
I managed by modifying like this:
Expand|Select|Wrap|Line Numbers
  1.     s = "UPDATE tblDB_PV AS P " & _
  2.         "RIGHT Join (tblDB_Daten AS D " & _
  3.                    "RIGHT JOIN tblMark_PV AS M " & _
  4.                    "ON D.[ID] > M.[ID] OR D.[ID] <= M.[ID]) " & _
  5.         "ON D.[ID]=P.[ID_tblDB_Daten] " & _
  6.         "SET P.[DB_Daten_PV] = 'PV' " & _
  7.             "WHERE  (D.[ISBN] LIKE ('*' &  M.[PV_ISBN] &'*')  )"
  8.  
Presumably it did not work because of "ON D.[ISBN]=M.[PV_ISBN])" or either "ON D.[ID]=M.[ID])" in my original code, as there are no identical ISBNs or IDs in the "D-table" and the "M-table".
Oct 14 '15 #12

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: awarsd | last post by:
------=_NextPart_000_0007_01C34C8B.2CF5D7A0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable Hi, I'm looking at some search engine where we can have...
0
by: schan | last post by:
Hi there, I was wondering if someone could shed some light on a problem I have no idea on how to fix. I created an Excel Add-In that uses an ADO connection to an Access database on a file...
1
by: arun | last post by:
Query is too complex -------------------------------------------------------------------------------- Hi, I was trying to solve this problem since last two days but couldn't find any solution. ...
3
blyxx86
by: blyxx86 | last post by:
Hey there, I'm running into a slight problem today... I have a few things to show... I'm running into duplicate values being shown in my query, but I cannot use a "Select DISTINCT" as it...
5
by: binky | last post by:
Question for all you SQL gurus out there. I have a (seemingly) complex query to write, and since I'm just learning SQL server, am not sure how to go about it. Tables: t_trans t_account All...
1
by: Rudolf Bargholz | last post by:
Hi, We have created triggers to log modifications to tables in our application. The triggers work fine, just on one of the tables in our database the triggers fail with the error message...
0
crystal2005
by: crystal2005 | last post by:
Hi, I am having trouble with some complex SQL queries. I’ve got winestore database, taken from Web Database Application with PHP and MySQL book. And some question about queries as the following ...
2
BeemerBiker
by: BeemerBiker | last post by:
I put together a (what I consider) complex query using the Access wizard. It works fine in access but fails when I code it up. I can actually make it work in code by "simplifying" it, but then it...
3
by: william67 | last post by:
I'm having a hard time building a query to do what I need to do and was hoping some genius could help me out, I need to do a complex query and any and all help is much appreciated this is the...
0
by: Kozy | last post by:
Hello everyone, i have a big problem with imports. I have only see this problem on 9.5, i have never goten it on 9.1. 99% of time db2 works great ( version 9.5 fixpack 5 ) on windows 2008 (...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.