Hi all,
I have a table (tblCodes) that has two fields: 'ID' and 'Code'.
A small portion of the table is as follows:
|-------------|
| tblCodes |
|-------------|
| ID | Code |
|------|------|
| A-01 | TBN |
| A-01 | SPN |
| A-01 | EWK |
| A-01 | EMX |
| A-02 | QIM |
| A-02 | TBN |
| A-02 | EWK |
| A-03 | TBN |
| A-04 | DOY |
| A-04 | CAV |
| A-04 | ICP |
| A-04 | CYG |
| A-04 | MER |
| A-05 | ICP |
|------|------|
The table can grow very large. ID numbers in the
thousands each with up to 400 different codes (but
usually a max of 20 codes per ID, with an average
of between 2-5 codes per ID).
Is there a way to build a SQL statement that returns
the codes that intersect 2 or more IDs? For example,
'A-01' Intersects 'A-02' = TBN, EWK
or
'A-01' Intersects 'A-02' Intersects 'A-03' = TBN
I've been trying to figure a way for a while now with
no luck. I'm at a point were I might just loop throgh
the table and pass duplicates to an array, but I suspect
it will be very slow on large tables. Any ideas or pointers
would be very helpful. Thank you.
Pete 6 7376 br******@inspection.gc.ca wrote: Hi all,
I have a table (tblCodes) that has two fields: 'ID' and 'Code'. A small portion of the table is as follows:
|-------------| | tblCodes | |-------------| | ID | Code | |------|------| | A-01 | TBN | | A-01 | SPN | | A-01 | EWK | | A-01 | EMX | | A-02 | QIM | | A-02 | TBN | | A-02 | EWK | | A-03 | TBN | | A-04 | DOY | | A-04 | CAV | | A-04 | ICP | | A-04 | CYG | | A-04 | MER | | A-05 | ICP | |------|------|
The table can grow very large. ID numbers in the thousands each with up to 400 different codes (but usually a max of 20 codes per ID, with an average of between 2-5 codes per ID).
Is there a way to build a SQL statement that returns the codes that intersect 2 or more IDs? For example,
'A-01' Intersects 'A-02' = TBN, EWK
or
'A-01' Intersects 'A-02' Intersects 'A-03' = TBN
I've been trying to figure a way for a while now with no luck. I'm at a point were I might just loop throgh the table and pass duplicates to an array, but I suspect it will be very slow on large tables. Any ideas or pointers would be very helpful. Thank you.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Will this do:
SELECT C1.ID, C1.Code
FROM tblCodes As C1 INNER JOIN tblCodes As C2
ON C1.Code = C2.Code
GROUP BY C1.ID, C1.Code
HAVING Count(*) > 1
ORDER BY C1.CODE, C1.ID
--
MGFoster:::mgf00 <at> earthlink <decimal-point> net
Oakland, CA (USA)
-----BEGIN PGP SIGNATURE-----
Version: PGP for Personal Privacy 5.0
Charset: noconv
iQA/AwUBQxYOSoechKqOuFEgEQL4SgCgi42es58lXIVTDf7SOz93Er zv8e8An0tm
KUVa7becMpFx8ZFKK82DzkVY
=s77s
-----END PGP SIGNATURE-----
Hi MG,
Thanks for the code! The output of the query using your code and the
sample table (tblCodes) is:
C1 C2
A-01 EWK
A-02 EWK
A-04 ICP
A-05 ICP
A-01 TBN
A-02 TBN
A-03 TBN
I was hoping to find a query where specific IDs could be entered (2 or
more) and only the common codes would be returned. For example:
SELECT C1.Code
FROM tblCodes AS C1 INNER JOIN tblCodes AS C2 ON C1.Code = C2.Code
GROUP BY C1.ID, C1.Code
HAVING (((C1.ID)="A-01") AND ((Count(*))>1)) OR (((C1.ID)="A-02")) OR
(((C1.ID)="A-03"))
ORDER BY C1.Code, C1.ID;
This code does not work, but you get the idea.
Using 2 IDs (i.e. A-01 and A-02), the query would return:
C1
TBN
EWK
Using 3 IDs (i.e. A-01, A-02, and A-03), the query would return:
C1
TBN
Not sure if it is possible, but I really appreciate your help so far.
Thanks!
Peter
pcb wrote: Hi MG,
Thanks for the code! The output of the query using your code and the sample table (tblCodes) is:
C1 C2 A-01 EWK A-02 EWK A-04 ICP A-05 ICP A-01 TBN A-02 TBN A-03 TBN
I was hoping to find a query where specific IDs could be entered (2 or more) and only the common codes would be returned. For example:
SELECT C1.Code FROM tblCodes AS C1 INNER JOIN tblCodes AS C2 ON C1.Code = C2.Code GROUP BY C1.ID, C1.Code HAVING (((C1.ID)="A-01") AND ((Count(*))>1)) OR (((C1.ID)="A-02")) OR (((C1.ID)="A-03")) ORDER BY C1.Code, C1.ID;
This code does not work, but you get the idea.
Using 2 IDs (i.e. A-01 and A-02), the query would return:
C1 TBN EWK
Using 3 IDs (i.e. A-01, A-02, and A-03), the query would return:
C1 TBN
Not sure if it is possible, but I really appreciate your help so far.
Thanks! Peter
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Try this:
SELECT C1.Code
FROM tblCodes AS C1 INNER JOIN tblCodes AS C2 ON C1.Code = C2.Code
WHERE C1.ID IN ('A-01','A-02')
GROUP BY C1.Code
HAVING Count(*) > 1
--
MGFoster:::mgf00 <at> earthlink <decimal-point> net
Oakland, CA (USA)
-----BEGIN PGP SIGNATURE-----
Version: PGP for Personal Privacy 5.0
Charset: noconv
iQA/AwUBQxdG1oechKqOuFEgEQLrqQCguQJnOOqdYNmxaazlKTEFY8 3AAw0AoOi+
EnutTtclJ0UzVfaTfV5oGWVe
=Bi8w
-----END PGP SIGNATURE-----
Hi MG,
Almost!! It works for the specific case of (A-01, A-02) on the sample
data, but not for the general case (i.e. A-01, A-02, A-03) or other
groups of IDs that are present in the larger data set. Thanks for you
help.
Peter
pcb wrote: Hi MG,
Almost!! It works for the specific case of (A-01, A-02) on the sample data, but not for the general case (i.e. A-01, A-02, A-03) or other groups of IDs that are present in the larger data set. Thanks for you help.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Save this query:
SELECT C1.ID, C1.Code
FROM tblCodes AS C1 INNER JOIN tblCodes AS C2 ON C1.Code = C2.Code
WHERE C1.ID IN ('A-01','A-02','A-03')
GROUP BY C1.ID, C1.Code
HAVING Count(*) > 1;
as "Codes_matching" and then use the following query to get the Code
that is unique to all IDs.
SELECT C.Code
FROM (SELECT Code, COUNT(*) As CodeCount FROM Codes_matching GROUP BY
Code) As C
INNER JOIN (SELECT Count(*) As IDCount FROM (SELECT DISTINCT ID FROM
Codes_matching)) As I ON C.CodeCount=I.IDCount
There may be a cleaner way to do this, but this is the 1st thing I could
get to work. It would have been cleaner if Access had the
Count(Distinct ...) function.
--
MGFoster:::mgf00 <at> earthlink <decimal-point> net
Oakland, CA (USA)
-----BEGIN PGP SIGNATURE-----
Version: PGP for Personal Privacy 5.0
Charset: noconv
iQA/AwUBQxdhYIechKqOuFEgEQIttgCbB3V4B3ZYfHWhJzTBYHGbQh qjfZUAn2ms
Fp9vxDAfOHGxq+s2kTH2PGIa
=c7gO
-----END PGP SIGNATURE-----
Hi MG,
Yes! That works! Thank you very much. Now I have to figure out how to
implement it in VBA. But you have already spent more time than you
should have on somebody else's problem. Thank you again.
Peter This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Muhd |
last post by:
To start off i have a database table that consists of "profiles", another
that consists of "users" and finally one that consists of "exclusions",
these are defined in the DDL below. I am trying to...
|
by: Robert Brown |
last post by:
suppose I have the following table:
CREATE TABLE (int level, color varchar, length int, width int, height
int)
It has the following rows
1, "RED", 8, 10, 12
2, NULL, NULL, NULL, 20...
|
by: Alexander Korovyev |
last post by:
Suppose I have two tables:
CREATE TABLE Tab1 (
NOT NULL,
NOT NULL,
NOT NULL,
NOT NULL)
CREATE TABLE Tab2 (
NOT NULL,
|
by: stu_gots |
last post by:
I have been losing sleep over this puzzle, and I'm convinced my train
of thought is heading in the wrong direction. It is difficult to
explain my circumstances, so I will present an identical...
|
by: plaztik8 |
last post by:
Hello,
Can someone please help me with a query?
The table looks like this:
BookedRooms
===========
CustomerID RoomID BookDateID
|
by: hharry |
last post by:
Hello All,
I have the following table:
CREATE TABLE . (
(50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
(50) COLLATE SQL_Latin1_General_CP1_CI_AS
NULL
) ON
GO
|
by: phillip.s.powell |
last post by:
MySQL 3.23.58 - 4.0.17 (yep, several database server instances, don't
ask)
I have database Spring with table Students
I have database Summer with table Students
I am tasked to produce a...
|
by: AJ |
last post by:
Hi all,
I have this monster query (at least i think it is).
SELECT
c.ID, c.Company_Name, p., 1 As QueryNbr
FROM
(Company AS c LEFT JOIN Sale AS s ON c.ID = s.Company_ID)
LEFT JOIN
Package...
|
by: shades234 |
last post by:
I saw a post about a year ago, that pondered if an opposite to a union query was possible, i.e. an intersect query. however, the OP was concerned about varous fields intersectiong with one another....
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
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...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
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
|
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...
|
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...
|
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,...
|
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...
| |