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

Tricky Query Help

I have 2 tables joined together by the IDs, People and the pets they
own

PEOPLE
ID NAME
1 JohnSMith
2 JaneDoe

PETS
ID PET
1 Dog
2 Cat
2 Hamster
2 Hamster
2 Fish

I have create another where the PETS are in one column separated by
semi-colons and removing the dups

NEW TABLE
ID NAME ALLPETS
1 JohnSmith Dog
2 JaneDoe Cat;Hamster;Fish

What is the best way to do it? The only way I can think of is to run
an update where it checks to see if the value already exists

THanks!
Jul 20 '05 #1
4 1458
Angel Cat (ki***********@hotmail.com) writes:
I have 2 tables joined together by the IDs, People and the pets they
own

PEOPLE
ID NAME
1 JohnSMith
2 JaneDoe

PETS
ID PET
1 Dog
2 Cat
2 Hamster
2 Hamster
2 Fish

I have create another where the PETS are in one column separated by
semi-colons and removing the dups

NEW TABLE
ID NAME ALLPETS
1 JohnSmith Dog
2 JaneDoe Cat;Hamster;Fish

What is the best way to do it? The only way I can think of is to run
an update where it checks to see if the value already exists


Yes, this is tricky, because your new table violates a fundamental
rule in database design: no repeating groups. So I will not really
give an answer, because it would be ugly and ineffecient. Rather I
would recommend you to reconsider your design.
--
Erland Sommarskog, SQL Server MVP, es****@sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 20 '05 #2
See this link http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=19647

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 20 '05 #3
>> I have 2 tables joined together by the IDs, People and the pets
they own <<

Please post DDL, so that people do not have to guess what the keys,
constraints, Declarative Referential Integrity, datatypes, etc. in
your schema are. Sample data is also a good idea, along with clear
specifications.

Both your approaches are completely wrong. Try somethign more like
this:

CREATE TABLE People
(person_id CHAR(10) NOT NULL,
name CHAR(35) NOT NULL,
...);

CREATE TABLE Pets
(pet_id CHAR(10) NOT NULL,
name CHAR(35) NOT NULL,
...);

CREATE TABLE Ownership
(person_id CHAR(10) NOT NULL
REFERENCES People(person_id),
pet_id CHAR(10) NOT NULL
REFERENCES Pets (pet_id),
PRIMARY KEY (person_id, pet_id),
..);

Now you have to decide if onwership is 1:1 (as shown), 1:m or n:m.
I have create another where the PETS are in one column separated by

semi-colons and removing the dups <<

That is insane as well as a violation of 1NF. Why did you think of
this? What was your mental model of the data?
Jul 20 '05 #4
I think you guys are missing the point of her question. My take is
that she is a beginner and does not even know what the next step
should be.

Celko,
You should leave your datamodel the way it is, that is only two
tables. You are done with this part. The other guys are right, you
should NOT create a 3rd table, rather you should write a resultset to
the existing data to morph into what you want. In order to do this
you can either create a view, a stored procedure or simply a direct
sql statement. I would probably create a stored procedure since this
is easily accessible from the calling application and has some
performance and security benifits.

Now there are two basic approaches you can take. You can either make
two really simple stored procedures (one for each table) that each
load in seperate objects in your code. Or make one stored procedure
that joins both tables together. If it's a quick and dirty
application, I would probably make one proc which could look something
like this:

create procedure up_somename_list
as

set nocount on

select pets.ID,
pets.PET,
people.[Name]
from PETS pets
join PEOPLE people on people.ID = pets.ID
order by people.[Name], pets.PET

-dave
jc*******@earthlink.net (--CELKO--) wrote in message news:<18**************************@posting.google. com>...
I have 2 tables joined together by the IDs, People and the pets they own <<

Please post DDL, so that people do not have to guess what the keys,
constraints, Declarative Referential Integrity, datatypes, etc. in
your schema are. Sample data is also a good idea, along with clear
specifications.

Both your approaches are completely wrong. Try somethign more like
this:

CREATE TABLE People
(person_id CHAR(10) NOT NULL,
name CHAR(35) NOT NULL,
...);

CREATE TABLE Pets
(pet_id CHAR(10) NOT NULL,
name CHAR(35) NOT NULL,
...);

CREATE TABLE Ownership
(person_id CHAR(10) NOT NULL
REFERENCES People(person_id),
pet_id CHAR(10) NOT NULL
REFERENCES Pets (pet_id),
PRIMARY KEY (person_id, pet_id),
..);

Now you have to decide if onwership is 1:1 (as shown), 1:m or n:m.
I have create another where the PETS are in one column separated by

semi-colons and removing the dups <<

That is insane as well as a violation of 1NF. Why did you think of
this? What was your mental model of the data?

Jul 20 '05 #5

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

Similar topics

15
by: dracolytch | last post by:
Good day all, Ok, I have a pretty tricky problem that I need some help with. I pass around search query information a fair amount (specifically WHERE statements). Normally, I just rawurlencode()...
7
by: Joel Thornton | last post by:
I'm having much difficulty figuring out how to write the following query. Please help! I have this table: Event EventId int Primary Key PatientId int SeverityLevel int
5
by: scott | last post by:
Hello, I'm trying to find the most optimal way to perform a tricky query. I'm hoping this is some sort of standard problem that has been solved before, but I'm not finding anything too useful so...
1
by: Pea | last post by:
I'm working with a system usage database. I want to filter out repetitive logins. The query I have retrieves data like this: USER_DATE USER_TIME1 USER_USERID USER_ACCOUNT...
6
by: pointBoarder | last post by:
Thanks in advance to all who read this. I've got 3 tables which were created from a txt file dumped from some old system. Header ID -- autonumber, primary OrderNum -- field I want Line
5
by: ric_deez | last post by:
Hi there, I would like to create a simple search form to allow users to search for a job number based on a number of parameters. I think I understand how to use parameteres associated with Stored...
1
by: MorrganMail | last post by:
Or at least I find it tricky. :-) Assume we have three tables A, B and C. Table A contains a path and the distance for traveling that path: A (PathId, NodeId, Dist (from previous node)) 1, 1,...
15
by: edouard.spooner | last post by:
Hi, I have a tricky SQL query problem that I'm having probs with. I have a table which resembles something like this Date | Price1 | Price2 | Price3 01 Jan 2006 | 100 | 100 | 100 02 Jan...
0
by: KevLe | last post by:
I'm building a log search function in c# for a certain management app and would like some help on the design how to solve this, here is my solution (on paper) so far: The log files are saved to...
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...
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
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
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.