











CompTIA
Exam Vouchers
Save money on CompTIA exams
| Question of the day
Sign up to receive
interactive practice questions
for MCSE, CompTIA
Cisco and other exams
| TestKing
Get MCSE, MCSD, CCNA, CCNP,A+, N+ and many more | * ExamSheets *
Guide for Success!
Actual Questions & Answers
MCSE, MCSD, A+ ,CCNA, CCNP
Oracle 8i, Oracle 9i Online practice tests
Certification sites Online university Online college Online education Distance learning Software forum Server administration forum Programming resources
|
|  |
| Author |
SQL Query Question (non-certificate related)
|
J-Ho
Member
Registered: Mar 2002 Location: Country: Ireland State: Certifications: MCDBA, MCSD, MCSA, MCSE Working on: MCAD
Total Posts: 85
|
|
SQL Query Question (non-certificate related)
Hi all,
this question is _not_ directly exam related, but something I got stuck on today...
Here's an example. I have a database with three tables - tblSkills, tblPeeps and tblJctSkillsPeeps. The last table is used to establish a many to many relationship between peeps and skills. Tables:
tblSkills
ID Text
-- -----
01 SQL
02 ASP
03 IIS
tblPeeps
ID Name
-- ------
01 Joe
02 Jill
03 John
tblJctSkillsPeeps
SkillID PeepID
------- ------
01 01
01 02
02 02
02 03
03 02
03 01
03 03
Now, I want to design a query that gives me the following:
PeepID PeepName Skills
------ -------- ------------
01 Joe 'SQL, IIS'
02 Jill 'SQL, ASP, IIS'
03 John 'ASP, IIS'
In short, I want concatenated contents from one table.
Is this possible in one, single neat query?
Thanks,
J
Report this post to a moderator
|
|
04-10-02 07:51 PM
|
|
AndyC
XML Spastic M

Registered: Dec 2000 Location: Country: England State: Certifications: See Signature Working on: CCIE Lab (Voice)
Total Posts: 577
|
|
How about creating a view for this?
__________________
Andy C
CCNP, CCVP, CCDA, CQS (WLANSE, WLANFE, Unity Support, IPT Design, IPT Express , IPCC Express, IP Communications Express, CallManager Express), JNCIS-M #1027, JNCIS-FWV #311, F5CSE, CCSE NG, MCSE NT4/2K, MCSE:Messaging, ITIL, A+
Report this post to a moderator
|
|
04-10-02 08:36 PM
|
|
J-Ho
Member
Registered: Mar 2002 Location: Country: Ireland State: Certifications: MCDBA, MCSD, MCSA, MCSE Working on: MCAD
Total Posts: 85
|
|
I'd prefer not using a view of possible. The main reason for which being that there are a lot of similar issues in the database (which means a _lot_ of views...).
Also, if it's possible to do this in one pretty query, I'd love to know how!
Maybe a function that creates a cursor to do the concatenation and return the concatenated string might do the trick? I have to try this out...
Report this post to a moderator
|
|
04-11-02 09:29 PM
|
|
emulti
Junior Member
Registered: Apr 2002 Location: Dorset, UK Country: United Kingdom State: Certifications: Working on: MCDBA/MCSD (passed 070-228)
Total Posts: 12
|
|
|
04-13-02 07:04 PM
|
|
J-Ho
Member
Registered: Mar 2002 Location: Country: Ireland State: Certifications: MCDBA, MCSD, MCSA, MCSE Working on: MCAD
Total Posts: 85
|
|
|
04-13-02 07:09 PM
|
|
amyland
Junior Member
Registered: Apr 2002 Location: Country: United States State: Certifications: MCP(70-316) Working on: MCAD(70-229, 70-320)
Total Posts: 22
|
|
I am just guessing
If there are really a lot of similar issues in your database (which means a _lot_ of views...) and you really want to do this in one pretty query, Can you denormalize your databse schema? In the price of data redundancy, you definitely only need one simply query to get what you want.

Report this post to a moderator
|
|
05-14-02 04:42 PM
|
|
amyland
Junior Member
Registered: Apr 2002 Location: Country: United States State: Certifications: MCP(70-316) Working on: MCAD(70-229, 70-320)
Total Posts: 22
|
|
In msdn, it stated:
denormalize only if you can demonstrate a performance gain (usually by not needing to perform multitable joins for each query) and only if losses in data integrity don't offset the performance gain. Data user convenience alone is never a justification for denormalizing. You can give users what they want by using virtual denormalization techniques, such as creating views and stored procedures.
Report this post to a moderator
|
|
05-14-02 04:48 PM
|
|
hard_coder
Senior Member
Registered: Apr 2001 Location: Dallas Country: USA State: Certifications: MCSD, MCDBA, MCSA, MCT Working on: Dot Nettin' it - MCAD, MCSD .Net
Total Posts: 188
|
|
Try this...
You don't need to denormalize. You just have to be willing to pound out some code like the big dogs (such as myself) do for a living.
Foreword: the tables I created were named tblSkills(SkillID INT, SkillName VARCHAR(10)); tblSkillPerson(SkillID INT, PersonID INT); tblPerson(PersonID INT, PersonName VARCHAR(10))
Create the tables listed above and load them with the data you provided from your earlier post.
Then run the script below in Query Analyzer.
I ran it against the tables and it gave the report in the format you are looking for.
Here's the code to do what you need:
----------------------------------------
----------------------------------------
SET NOCOUNT ON
CREATE TABLE #tempPersonReport(
PersonID int,
PersonName varchar(10),
Skills varchar(255)
)
insert into #tempPersonReport
select
PersonID, PersonName, ''
from
tblPerson P
DECLARE @intPersonID INT
DECLARE @strSkillName VARCHAR(20)
DECLARE curPerson CURSOR FOR
SELECT PERSONID FROM #tempPersonReport
OPEN curPerson
FETCH NEXT FROM curPerson INTO @intPersonID
WHILE @@FETCH_STATUS = 0
BEGIN
DECLARE curSkills CURSOR FOR
SELECT S.SKILLNAME FROM TBLSKILLPERSON SP
JOIN TBLSKILLS S ON SP.SKILLID = S.SKILLID
WHERE SP.PERSONID = @intPersonID
OPEN curSkills
FETCH NEXT FROM curSkills INTO @strSkillName
WHILE @@FETCH_STATUS = 0
BEGIN
UPDATE #tempPersonReport
SET SKILLS = (CASE SKILLS
WHEN '' THEN @strSkillName
ELSE SKILLS + ',' + @strSkillName
END)
WHERE PERSONID = @intPersonID
FETCH NEXT FROM curSkills INTO @strSkillName
END
CLOSE curSkills
DEALLOCATE curSkills
FETCH NEXT FROM curPerson INTO @intPersonID
END
CLOSE curPerson
DEALLOCATE curPerson
SELECT * FROM #tempPersonReport
DROP TABLE #tempPersonReport
----------------------------------------
----------------------------------------
In essence, it's a loop within a loop using 2 cursors. I know, I know, always do your best to stay away from cursors, but in this case it is a suffice solution.
Last edited by hard_coder on 05-14-02 at 07:21 PM
Report this post to a moderator
|
|
05-14-02 06:12 PM
|
|
J-Ho
Member
Registered: Mar 2002 Location: Country: Ireland State: Certifications: MCDBA, MCSD, MCSA, MCSE Working on: MCAD
Total Posts: 85
|
|
Temp table
Temp table, eh? Why didn't I think of that? I can see how the cursors might be considered undesirable, but then again - it's a whole lot better than all the extra network time involved in the client calling a stored procedure repeatedly to get the summary for each item...
Thanks a lot,
J
Report this post to a moderator
|
|
05-14-02 08:15 PM
|
|
hard_coder
Senior Member
Registered: Apr 2001 Location: Dallas Country: USA State: Certifications: MCSD, MCDBA, MCSA, MCT Working on: Dot Nettin' it - MCAD, MCSD .Net
Total Posts: 188
|
|
|
05-14-02 08:27 PM
|
|
|
MCSE exam notes
Forum Rules: Who Can Read The Forum? Any registered user or guest.
Who Can Post New Topics? Any registered user.
Who Can Post Replies? Any registered user.
Changes: Messages can be edited by their author.
Posts: HTML code is OFF. Smilies are ON. vB code is ON. [IMG] code is ON. |
|
ExamNotes forum archive
|