473,395 Members | 1,474 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,395 software developers and data experts.

1 to many relationship between columns

Hi,

Considering a table/dataset of structure below:

Name, Code, Other fields
ABC, 1, ...........
ABC, 1,............
ABC, 2,............
ABC, 3,............
DEF, 1, ............
DEF, 2,............
GHI, 4, ............
JKL, 5,............
......
......

I want to get a result set which would give me all instances where
multiple "Code" exist for each "Name". The result set should look like:

ABC, 1,............
ABC, 2,............
ABC, 3,............
DEF, 1,............
DEF, 2,............
(notice since there aren't multiple codes for "GHI" and "JKL", these
are not included in the resultset)

Can someone please suggest a SQL for this. I am trying this in MS
Access but would like to have an understanding of how this should be
generally handled.

Thanks.

Feb 9 '06 #1
3 2565
Perhaps what you can do is run a grouping query
from the table source and add only the Name field
to it, TWICE. The first field to be set as Group By
and the second set as Count. Then, set the
criteria for the Count column to >1. This will get
only the records that have 2 or more in your dataset.

Next, create a second query with the table as it's
source also and add your fields Name & Code. Also,
whatever fields you need.
Then, add the previous query and just link the two
Name fields together.

Add the fields from the table souce to the QBE grid
and set them both to ascending. Now run the query.

You should end up with the records you require.

Have a nice day.

Regards

Feb 9 '06 #2
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Try:

SELECT [Name], [code]
FROM table_name As T
GROUP BY [Name], [code]
HAVING (SELECT COUNT(*) FROM table_name
WHERE [Name] = T.[Name]) > 1

This won't work if you include the other columns (fields). You'll have
to make it a derived table:

SELECT [Name], [code], <other columns>
FROM
( SELECT [Name], [code]
FROM table_name As T
GROUP BY [Name], [code]
HAVING (SELECT COUNT(*) FROM table_name
WHERE [Name] = T.[Name]) > 1
) AS A

--
MGFoster:::mgf00 <at> earthlink <decimal-point> net
Oakland, CA (USA)

-----BEGIN PGP SIGNATURE-----
Version: PGP for Personal Privacy 5.0
Charset: noconv

iQA/AwUBQ+ugpIechKqOuFEgEQILXwCdF+6eyHIZkTs0xd8Zg+OR9W QNL9EAoMcQ
YRnBenqshV/JIPEoY3X5r4hz
=P43Z
-----END PGP SIGNATURE-----
Aman wrote:
Hi,

Considering a table/dataset of structure below:

Name, Code, Other fields
ABC, 1, ...........
ABC, 1,............
ABC, 2,............
ABC, 3,............
DEF, 1, ............
DEF, 2,............
GHI, 4, ............
JKL, 5,............
.....
.....

I want to get a result set which would give me all instances where
multiple "Code" exist for each "Name". The result set should look like:

ABC, 1,............
ABC, 2,............
ABC, 3,............
DEF, 1,............
DEF, 2,............
(notice since there aren't multiple codes for "GHI" and "JKL", these
are not included in the resultset)

Can someone please suggest a SQL for this. I am trying this in MS
Access but would like to have an understanding of how this should be
generally handled.

Feb 9 '06 #3
Thanks for your replies guys. It did give me some ideas on how to
approach this. Here's what worked for me in MS Access:

SELECT DISTINCTROW tbl_nm.name, tbl_nm.code
FROM tbl_nm, [select distinct name, count(*)
from (
SELECT DISTINCTROW tbl_nm.Name,
tbl_nm.count, Count(*)
FROM tbl_nm
GROUP BY tbl_nm.Name, tbl_nm.code)
group by name
having count(*) > 1]. AS abc
WHERE tbl_nm.name = abc.name
GROUP BY tbl_nm.name, tbl_nm.code

The solution you had proposed Foster did not exclude the unique
combinations of Name and Code, and as I menntioned I did not want them.
I just wanted the names and codes for cases where multiple codes
existed for a name.

Thanks alot!!

Feb 9 '06 #4

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

Similar topics

28
by: Keith | last post by:
I am having a problem creating a many-to-many-to-many type relationship. It works fine, but when I create a view to query it and test it, it does not generate the results I expected. Below...
2
by: MRG | last post by:
I have a geographic points table containing point records with latitude and longitude points. I also have several other tables containing entities that will relate to points in the points table,...
4
by: H Cohen | last post by:
Hi, I have a corporate database with about 60 different tables that spans manufacturing, accounting, marketing, etc. It is possible, but unwieldy, to establish a relationship for each table...
14
by: serge | last post by:
I have a scenario where two tables are in a One-to-Many relationship and I need to move the data from the Many table to the One table so that it becomes a One-to-One relationship. I need to...
2
by: ronenkf | last post by:
I am currently working on access 2003. Created database with 4 tables. For each on e there is a primary key, which is a text data type. Now I'm trying to configure relationship between tables. The...
2
by: Neil Steventon | last post by:
Hi , I have started developing a database application , I say start as at this rate I see no finish. I have a windows form which is bound to mydataset table "Companys" and this works great...
1
by: Michael D. Reed | last post by:
I have two tables in an Access database with a Many-to-Many relationship, there is a connection table. The tables are , , and the connection table is this is a standard Many-to-Many relationship...
6
Plater
by: Plater | last post by:
(I don't even know how to title this) The bossman says I need to add a certain field into a query and I have yet to figure out out to do it. I will try and make a trimmed down example. ...
6
by: BD | last post by:
Hi, all. I need to enforce a one-to-many relationship on 2 tables, with a join table. Say the join table contains account information. It has cust_no and acct_no. Both cust_no and acct_no are...
0
by: drhoades66 | last post by:
I have books, authors and a bkauthlink table linking the other two tables-a many-to-many relationship. I have tried various things but cannot create a Data Relation. Is there no way to do this in...
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...
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...
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
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
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...
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...

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.