473,398 Members | 2,125 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,398 software developers and data experts.

One SQL to test multiple records

Hi,

I've an Access DB table ("Groups") where data are as follow:

Id Group Rank Item
1 1 1 7364
2 1 2 283
3 1 3 34888
4 2 1 277
5 2 2 8233
(...)

Each record belongs to a group in which it has a rank.

I have an ASP script that writes new groups in the table, but before
writing anything, I'd need to check whether a similar group already
exists, and I think it can be done with a single sql statement. (NB:A
group is similar to another if it has the same Item value at the same Rank.)
The group I want to check is stored in a dictionary object (where the
key stands for the rank).
My basic idea is to iterate through the items to build an sql statement
with nested SELECTs:

sql = "Groups"
For Each a In Dict.Keys
sql = "SELECT * FROM (" & sql1 & ") WHERE Item=" & Dict.Item(a) & " AND
Rank=" & a
Next
sql1 = sql1 & ";"
RS1.Open sql1,Conn

But... of course it doesn't work because the first SELECT matches a
definite record from which the second iteration matches nothing. What I
should have is a set of all groups where the Item at Rank 1 is equal to
the given Item, and so on.

So I guess there should be some (self-)JOIN in the sql statement, but so
far all my attemps have failed.

Any idea? Thanks a lot in advance.

Ivor
Dec 7 '06 #1
2 2063

"Ivor Somerset" <so*******@hiddenembassy.comwrote in message
news:45***********************@news.free.fr...
Hi,

I've an Access DB table ("Groups") where data are as follow:

Id Group Rank Item
1 1 1 7364
2 1 2 283
3 1 3 34888
4 2 1 277
5 2 2 8233
(...)

Each record belongs to a group in which it has a rank.

I have an ASP script that writes new groups in the table, but before
writing anything, I'd need to check whether a similar group already
exists, and I think it can be done with a single sql statement. (NB:A
group is similar to another if it has the same Item value at the same
Rank.)
The group I want to check is stored in a dictionary object (where the key
stands for the rank).
My basic idea is to iterate through the items to build an sql statement
with nested SELECTs:

sql = "Groups"
For Each a In Dict.Keys
sql = "SELECT * FROM (" & sql1 & ") WHERE Item=" & Dict.Item(a) & " AND
Rank=" & a
Next
sql1 = sql1 & ";"
RS1.Open sql1,Conn

But... of course it doesn't work because the first SELECT matches a
definite record from which the second iteration matches nothing. What I
should have is a set of all groups where the Item at Rank 1 is equal to
the given Item, and so on.

So I guess there should be some (self-)JOIN in the sql statement, but so
far all my attemps have failed.

Any idea? Thanks a lot in advance.

Ivor
What you are trying to achieve is not very clear. On the face of it, it
seems straightforward, but then you introduce "nested selects" which
confuses things.

If what you want to do is iterate over the collection in the dictionery
object, checking to see if each entry has a match in the Access table, then
you have to execute the sql within each iteration.

<%
For Each a In Dict.Keys
sql = "SELECT Id FROM GROUPS WHERE Item=" & Dict.Item(a) & " AND
Rank=" & a
Set rs1 = conn.execute(sql)
If Not rs1.EOF Then 'you have a match
...
Else 'you don't
...
End If
Next
%>

Your current SQL statement - ignoring the fact that you swap from "sql" to
"sqll" will only ever end up containing the values from the final item in
the dictionery object when yo ucome to execute it. You will have
overwritten all the preceding ones without ever having tested them.

Or were you trying to achieve something else?

--
Mike Brind
Dec 7 '06 #2
Hello Mike,

Thanks for taking the time to have a look at my obscure problem. It was
probably not well explained and the sql/sql1 typo surely didn't help.
In fact I was on the right track mentioning self-joins and I finally
found the solution.

sql1 = "Groups"
For Each a In Dict.Keys
sql1 = "(SELECT T2.IdGroup,T2.Rank,T2.IdItem FROM (" & sql1 & " AS T1
LEFT JOIN Groups AS T2 ON T1.IdGroup=T2.IdGroup) WHERE T1.IdItem=2712
AND T1.Rank=1)"
Next
sql1 = sql1 = Mid(sql1,2,Len(sql1) - 2) & ";"
RS1.Open sql1,Conn
Response.Write "Matching group in the table? " & Not(RS1.EOF)

Suppose I have a dictionary object containing:
Item("1") = "2712"
Item("2") = "5598"
Each key/value pair stands for the Rank and IdItem fields of my Groups
table in the DB.

Once built, the sql1 statement is:

SELECT T2.IdGroup,T2.Rank,T2.IdItem FROM ((SELECT
T2.IdGroup,T2.Rank,T2.IdItem FROM (Groups AS T1 LEFT JOIN Groups AS T2
ON T1.IdGroup=T2.IdGroup) WHERE T1.IdItem=2712 AND T1.Rank=1) AS T1 LEFT
JOIN Groups AS T2 ON T1.IdGroup=T2.IdGroup) WHERE T1.IdItem=5598 AND
T1.Rank=2;

When I execute it, Not(RS.EOF) tells me if the Groups table has 2
records such as:
IdGroup:[some Id] / IdItem:2712 / Rank:1
AND
IdGroup:[some Id] / IdItem:5598 / Rank:2

[someId] having of course the same value in both records.

(In fact it's not quite over yet, I still have to check whether there's
a third record where IdGroup:[some Id], because it would mean that the
group described by the dictionary object and the group in the DB are not
similar.)

I don't know if my problem is any learer to you now.

I think this is a quick way to compare a "set of records" against a
table, certainly more straightforward than a recursive function.

Ivor
>
What you are trying to achieve is not very clear. On the face of it, it
seems straightforward, but then you introduce "nested selects" which
confuses things.

If what you want to do is iterate over the collection in the dictionery
object, checking to see if each entry has a match in the Access table, then
you have to execute the sql within each iteration.

<%
For Each a In Dict.Keys
sql = "SELECT Id FROM GROUPS WHERE Item=" & Dict.Item(a) & " AND
Rank=" & a
Set rs1 = conn.execute(sql)
If Not rs1.EOF Then 'you have a match
...
Else 'you don't
...
End If
Next
%>

Your current SQL statement - ignoring the fact that you swap from "sql" to
"sqll" will only ever end up containing the values from the final item in
the dictionery object when yo ucome to execute it. You will have
overwritten all the preceding ones without ever having tested them.

Or were you trying to achieve something else?

--
Mike Brind

Dec 8 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: shank | last post by:
I have a recordset that contains multiple records of product a user is purchasing. For clarity, I converted the recordset fields to variables. I need to take that entire recordset and insert it...
7
by: Drew | last post by:
I have a db table like the following, UID, int auto-increment RegNo Person Relation YearsKnown Now here is some sample data from this table,
1
by: Charlie | last post by:
Hello, I have data in an Access table that I would like to export to multiple HTML tables. I would like to split the data in the Access table (about 92,000 records) into multiple HTML...
5
by: jhutchings | last post by:
Hello everyone, I have a database where I collect shipment data from various tables. However, I have a problem. Whenever I want to see shipping data for orders that were set to ship on or before...
52
by: MP | last post by:
Hi trying to begin to learn database using vb6, ado/adox, mdb format, sql (not using access...just mdb format via ado) i need to group the values of multiple fields - get their possible...
4
by: ApexData | last post by:
I have a continuous form that has Allow- Add/Edit/Del set to False, so that the form is in View mode only. I have New/Edit/Del buttons in the form header. My New & Edit buttons work as a like. ...
5
by: mctime | last post by:
Hello, I am attempting to split a raw data table into a new table that has split out a specific field in the raw data and created a new record for each split but I have come to an impasse due to...
7
by: =?Utf-8?B?TG9zdEluTUQ=?= | last post by:
Hi All :) I'm converting VB6 using True DBGrid Pro 8.0 to VB2005 using DataGridView. True DBGrid has a MultipleLines property that controls whether individual records span multiple lines. Is...
4
4Him
by: 4Him | last post by:
First off, let me say this is a great site! I've just started working with Access and much of my success is from what I've read here! Background: I have a form, driven off a single table. Goal:...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.