显示标签为“brain”的博文。显示所有博文
显示标签为“brain”的博文。显示所有博文

2012年3月8日星期四

Any SELECT Statement Gurus Out There?

Hi all,

I'm having real trouble wrapping my newbie brain around this problem. Can
someone please tell me the most efficient (or any!) way to write a SELECT
statement to return a set of rows from 5 tables:

These tables are part of our medical database. For example, a person seeking
healthcare may want to know the names and contact information for all
doctors in Reno, NV, who do cardiology. Sounds simple, but it isn't.

A medical group can have many doctors.
A doctor may be a member of more than one medical group.
A doctor or group can have multiple specialties.
A group can have multiple facilities (physical locations).

So the tables look like this...

Table: Doctors
-----
DoctorName
DoctorID

Table: Groups
-----
GroupName
GroupID

Table: Docs2Groups (provides many-to-many relationship between Doctors and
Groups)
-------
DoctorID
GroupID

Table: Specialties (provides many-to-many relationshop between Doctors,
Groups, and Specialties)
------
SpecialtyID
SpecialtyDesc
DoctorID
GroupID

Facilities
---
FacilityName (provides one-to-many relationship between Groups and
Facilities)
FacilityID
GroupID
Address
City
State
Zip
Phone
E-mail

Any help would be GREATLY appreciated.

--Eric Robinsonthis table confuses me:

Table: Specialties (provides many-to-many relationshop between Doctors,
Groups, and Specialties)
------
SpecialtyID
SpecialtyDesc
DoctorID
GroupID

Are you saying that a doctor can only specialize in something within
the context of a certain group? In other words Dr.Smith is a pediatric
oncologist, but can only work in pediatrics for Group A and oncology
for group B? Does that happen?

It would be easier if a Group was a collection of doctors who all had
specialties.

SELECT d.DoctorName
FROM Doctors d JOIN Docs2Groups d2g on d.DoctorID = d2g.DoctorID
JOIN Specialties s ON d.DoctorID = s.DoctorID
JOIN Facilities f ON f.GroupID = d2g.GroupID
WHERE s.SpecialityDesc = 'cardiology'
AND f.City = 'Reno' AND f.State = 'NV'

But, if a Doctor can only specialize in something in the context of a
group, then you could do this:

SELECT d.DoctorName
FROM Doctors d JOIN Docs2Groups d2g on d.DoctorID = d2g.DoctorID
JOIN Specialties s ON d2g.DoctorID = s.DoctorID
AND d2g.GroupID = s.GroupID
JOIN Facilities f ON f.GroupID = d2g.GroupID
WHERE s.SpecialityDesc = 'cardiology'
AND f.City = 'Reno' AND f.State = 'NV'

Untested.

HTH,
Stu|||Stu,

Each doctor has his or her own primary specialty and 0 or more additional
specialties. These specialties are asociated with the doctor, not the group.
A group's specialty is a function of its participating doctors. If all the
docs for Group A are cardiologists, then Group A is considered a cardiology
group. If the docs are of different specialties, then the group is
considered "multispecialty." In that sense, a group is, as you say, a
collection of doctors.

HOWEVER, there are unusual cases where the group consists of docs with
different specialties, but it still wants to to be known (for the purposes
of our directory) as one certain kind of group. In these cases, the group
itself gets an "overriding" specialty associated with it to keep from being
listed as "multispecialty."

Therefore the Specialties table does double-duty, but there are only a few
records with GroupID <> 0.

Does that clear things up?

--Eric

"Stu" <stuart.ainsworth@.gmail.com> wrote in message
news:1126550993.851782.190410@.g43g2000cwa.googlegr oups.com...
> this table confuses me:
> Table: Specialties (provides many-to-many relationshop between Doctors,
> Groups, and Specialties)
> ------
> SpecialtyID
> SpecialtyDesc
> DoctorID
> GroupID
> Are you saying that a doctor can only specialize in something within
> the context of a certain group? In other words Dr.Smith is a pediatric
> oncologist, but can only work in pediatrics for Group A and oncology
> for group B? Does that happen?
> It would be easier if a Group was a collection of doctors who all had
> specialties.
> SELECT d.DoctorName
> FROM Doctors d JOIN Docs2Groups d2g on d.DoctorID = d2g.DoctorID
> JOIN Specialties s ON d.DoctorID = s.DoctorID
> JOIN Facilities f ON f.GroupID = d2g.GroupID
> WHERE s.SpecialityDesc = 'cardiology'
> AND f.City = 'Reno' AND f.State = 'NV'
> But, if a Doctor can only specialize in something in the context of a
> group, then you could do this:
> SELECT d.DoctorName
> FROM Doctors d JOIN Docs2Groups d2g on d.DoctorID = d2g.DoctorID
> JOIN Specialties s ON d2g.DoctorID = s.DoctorID
> AND d2g.GroupID = s.GroupID
> JOIN Facilities f ON f.GroupID = d2g.GroupID
> WHERE s.SpecialityDesc = 'cardiology'
> AND f.City = 'Reno' AND f.State = 'NV'
> Untested.
> HTH,
> Stu|||Oops, I mistated the Specialties table. It is actually three tables:

Specialty
---
SpecialtyID
SpecialtyDesc

Doc2Specialty
------
SpecialtyID
DoctorID

Group2Specialty
------
SpecialtyID
GroupID

"Stu" <stuart.ainsworth@.gmail.com> wrote in message
news:1126550993.851782.190410@.g43g2000cwa.googlegr oups.com...
> this table confuses me:
> Table: Specialties (provides many-to-many relationshop between Doctors,
> Groups, and Specialties)
> ------
> SpecialtyID
> SpecialtyDesc
> DoctorID
> GroupID
> Are you saying that a doctor can only specialize in something within
> the context of a certain group? In other words Dr.Smith is a pediatric
> oncologist, but can only work in pediatrics for Group A and oncology
> for group B? Does that happen?
> It would be easier if a Group was a collection of doctors who all had
> specialties.
> SELECT d.DoctorName
> FROM Doctors d JOIN Docs2Groups d2g on d.DoctorID = d2g.DoctorID
> JOIN Specialties s ON d.DoctorID = s.DoctorID
> JOIN Facilities f ON f.GroupID = d2g.GroupID
> WHERE s.SpecialityDesc = 'cardiology'
> AND f.City = 'Reno' AND f.State = 'NV'
> But, if a Doctor can only specialize in something in the context of a
> group, then you could do this:
> SELECT d.DoctorName
> FROM Doctors d JOIN Docs2Groups d2g on d.DoctorID = d2g.DoctorID
> JOIN Specialties s ON d2g.DoctorID = s.DoctorID
> AND d2g.GroupID = s.GroupID
> JOIN Facilities f ON f.GroupID = d2g.GroupID
> WHERE s.SpecialityDesc = 'cardiology'
> AND f.City = 'Reno' AND f.State = 'NV'
> Untested.
> HTH,
> Stu|||Eric Robinson (eric @. pmcipa..{com}) writes:
> These tables are part of our medical database. For example, a person
> seeking healthcare may want to know the names and contact information
> for all doctors in Reno, NV, who do cardiology. Sounds simple, but it
> isn't.
> A medical group can have many doctors.
> A doctor may be a member of more than one medical group.
> A doctor or group can have multiple specialties.
> A group can have multiple facilities (physical locations).
> So the tables look like this...

It is always preferable to post CREATE TABLE scripts for the table.
That and test data in INSERT statemants, and the desired result of
the test data. That will give you a tested solution.

So this is an untested solution:

SELECT D.DoctorName
FROM Doctors D
WHERE EXISTS (SELECT *
FROM Doc2Groups DG
JOIN Facility F ON F.GroupID = DG.GroupID
WHERE DG.DoctorID = D.DoctorID
AND F.State = 'NV'
AND F.City = 'Reno')
AND EXISTS (SELECT *
FROM Doc2Specialiity DS
JOIN Specialtities S ON DS.SpecialityID = S.SpecialityID
WHERE DS.DoctorID = S.DoctorID
AND S.SpecialityDesc = 'Cardiology')
UNION
SELECT D.DoctorName
FROM Doctors D
WHERE EXISTS (SELECT *
FROM Doc2Groups DG
JOIN Facility F ON F.GroupID = DG.GroupID
JOIN Group2Specialiity GS ON GS.GroupID = DS.GroupID
JOIN Specialtities S ON GS.SpecialityID = S.SpecialityID
WHERE DG.DoctorID = D.DoctorID
AND F.State = 'NV'
AND F.City = 'Reno'
AND S.SpecialityDesc = 'Cardiology')

--
Erland Sommarskog, SQL Server MVP, esquel@.sommarskog.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techin.../2000/books.asp|||On Mon, 12 Sep 2005 12:36:02 -0700, Eric Robinson wrote:

>Oops, I mistated the Specialties table. It is actually three tables:
(snip)

Hi Eric,

I'm glad you posted that, since I was just preparing to telll you how
you should change your design and all <grin>.

Anyway, since we now have doctors who do cardiology, groups that
specialize in cardiology and doctors that belong to groups that do
cardiology, it's not exactly clear what you want your query to return.
I'll assume you want to know what I would be interested in if my heart
starts acting funny while I'm in Reno - I'd like to know where to go
(and quick!).

The query below will return the name of the group and the details of the
location for each group with a location in Reno, NV that either has
cardiology as group specialization, or hosts at least one doctor who
specializes in cardiology.

SELECT g.GroupName, f.FacilityName, f.Address, f.Phone
FROM Groups AS g
INNER JOIN Facilities AS f
ON f.GroupID = g.GroupID
LEFT JOIN Group2Specialties AS g2s
ON g2s.GroupID = g.GroupID
CROSS JOIN (SELECT SpecialtyID
FROM Specialties
WHERE SpecialtyDesc = 'Cardiology') AS s(SpecialtyID)
WHERE f.City = 'Reno'
AND f.State = 'NV'
AND
( g2s.SpecialtyID = s.SpecialtyID
OR EXISTS
(SELECT *
FROM Docs2Groups AS d2g
INNER JOIN Doc2Specialties AS d2s
ON d2s.DoctorID = d2g.DoctorID
WHERE d2g.GroupID = g.GroupID
AND d2s.SpecialtyID = s.SpecialtyID))

(untested - see www.aspfaq.com/5006 if you prefer tested suggestions).

Best, Hugo
--

(Remove _NO_ and _SPAM_ to get my e-mail address)|||I was offline for a few hours and I see that I have some very throughtful
replies. I will take a closer look at them. In the meantime, based on Stu's
initial suggestion, I came up with the following, which SEEMS to work:

For simplicity's sake, I lied in my original message about the true names of
the tables, so the following query looks slightly different because it
contains real table names. (In the future I'll take Erland's suggestion and
post CREATE TABLE scripts instead.)

SELECT s.Specialty, p.LastName, p.FirstName, p.Degree, g.GroupName,
f.PhysAddr1, f.PhysCity, f.PhysAddr2, f.PhysState, f.Phone1
FROM tblProviders p JOIN tblBindProviderGroup p2g on p.ProviderID =
p2g.ProviderID
JOIN tblGroups g on g.GroupID=p2g.GroupID
JOIN tblFacilities f on f.GroupID=p2g.GroupID
JOIN tblBindProviderSpecialty p2s on p2s.ProviderID=p.ProviderID
JOIN tblSpecialties s on s.SpecialtyID=p2s.SpecialtyID
WHERE f.PhysCity='Reno'
ORDER BY p.LastName

I've run this against the actual data and it SEEMS to return correct
results.

I'm guessing that some of the other approaches people have posted are better
in ways I have not yet thought of.

--Eric

"Erland Sommarskog" <esquel@.sommarskog.se> wrote in message
news:Xns96CFEF79E2BE1Yazorman@.127.0.0.1...
> Eric Robinson (eric @. pmcipa..{com}) writes:
>> These tables are part of our medical database. For example, a person
>> seeking healthcare may want to know the names and contact information
>> for all doctors in Reno, NV, who do cardiology. Sounds simple, but it
>> isn't.
>>
>> A medical group can have many doctors.
>> A doctor may be a member of more than one medical group.
>> A doctor or group can have multiple specialties.
>> A group can have multiple facilities (physical locations).
>>
>> So the tables look like this...
> It is always preferable to post CREATE TABLE scripts for the table.
> That and test data in INSERT statemants, and the desired result of
> the test data. That will give you a tested solution.
> So this is an untested solution:
> SELECT D.DoctorName
> FROM Doctors D
> WHERE EXISTS (SELECT *
> FROM Doc2Groups DG
> JOIN Facility F ON F.GroupID = DG.GroupID
> WHERE DG.DoctorID = D.DoctorID
> AND F.State = 'NV'
> AND F.City = 'Reno')
> AND EXISTS (SELECT *
> FROM Doc2Specialiity DS
> JOIN Specialtities S ON DS.SpecialityID = S.SpecialityID
> WHERE DS.DoctorID = S.DoctorID
> AND S.SpecialityDesc = 'Cardiology')
> UNION
> SELECT D.DoctorName
> FROM Doctors D
> WHERE EXISTS (SELECT *
> FROM Doc2Groups DG
> JOIN Facility F ON F.GroupID = DG.GroupID
> JOIN Group2Specialiity GS ON GS.GroupID = DS.GroupID
> JOIN Specialtities S ON GS.SpecialityID =
> S.SpecialityID
> WHERE DG.DoctorID = D.DoctorID
> AND F.State = 'NV'
> AND F.City = 'Reno'
> AND S.SpecialityDesc = 'Cardiology')
>
> --
> Erland Sommarskog, SQL Server MVP, esquel@.sommarskog.se
> Books Online for SQL Server SP3 at
> http://www.microsoft.com/sql/techin.../2000/books.asp

2012年2月11日星期六

Answer these brain-teasers

Thser brain teasers have been tried for years...

Brain-Teasers Part 1:=
================

1. Persistency of business data refers to which of the following?

a. Business data are kept in the computer main memory
b. Business data are saved on stable storage such as magnetic disks for the life of the business
c. Business data are saved on stable storage such as magnetic disks for as long as it is needed by the business
d. Once the business data has been saved, its values never change

2. DBMS recovery management deals with

a. restoring the database after a failure.
b. fixing a bad sector in the disk.
c. recovering passwords forgotten by users.
d. getting computers back online after a power outage.

3. Which of the following is neither a characteristic nor a feature of databases?

a. Many users can use the same database at the same time.
b. Relationships provide information connecting different entities.
c. Only one user at a time can access data from the database.
d. Access to data is facilitated by nonprocedural access.

4. Which one of the following is the least likely to be viewed as a transaction?

a. Retrieving an employee hiring date.
b. Transferring $1000 from a customer's savings account to the customer's checking account.
c. Reserving a seat on a train .
d. Reserving a hotel room.

5. A null value indicates:

a. A numeric value with value 0.
b. The absence of a value.
c. A very small value.
d. An erroneous value.

6. In general denormalization produces __________________.

a. more joins
b. a slower system response
c. less data redundancy
d. faster performance

7. All parts of a transaction must be successfully completed to prevent ____________.

a. slow transaction execution
b. data integrity problems
c. concurrency control
d. referential integrity problems

8. A _________________ is one in which all data integrity constraints are satisfied.

a. 2NF
b. completed database design
c. consistent retrieval
d. consistent database state

9. When a transaction is completed, the database reaches a consistent state, and that state cannot be lost, even in the event of the systems failure. That property is referred to as ________________.

a. Atomicity
b. Durability
c. Isolation
d. Serializability

10. Which of the following is important in multi-user and distributed databases to maintain database integrity, where several transactions are likely to be executed concurrently?

a. Structural independence
b. Distribution independence
c. Serializability
d. Isolation

11. In a _______________ client server system, the clients request may be handled by several different intermediate servers.

a. centalized
b. n-tier
c. 2-tier
d. 3-tier

12. A(n) ___________ client is one that conducts a minimum of processing on the client side.

a. loaded
b. push
c. thin
d. end-user

13. Which of the following statements is true when comparing XML to HTML.

a. HTML is more similar to SGML than XML
b. HTML is human readable and uses tags to mark up documents while XML is not.
c. HTML is to displaying content as to XML is describing content.
d. HTML can only display static content whereas XML can display dynamic content.

14. The scheduler ______________ the execution of database operations to ensure serializaility.

a. intermittently sequences
b. interleaves
c. randomly alternates
d. concurrently schedules

15. In this technique for dealing with deadlocks, the DBMS periodically tests the database for deadlocks and if a deadlock is found, the victim is aborted and the other transaction continues?

a. Deadlock avoidance
b. Deadlock detection
c. Two-phase deadlock protocol
d. Deadlock prevention

16. The most common aspects examined in DBMS ______________ are use of indexes, query-optimization algorithms, and management of storage resources.

a. System support
b. Backup and recovery
c. Performance tuning
d. Security monitoring

17. Authorization management defines access control procedures, which allow the DBA to ____________________.

a. Create audit trails to pinpoint violations.
b. Assign passwords to each user.
c. Assign access privileges to specific users to access specific databases.
d. Place limits on the use of DBMS query and reporting tools.

18. Why is the selection of proper field names very important?

a. Because standards exist and must be followed.
b. Because the data structure becomes self-documenting
c. Because record names depend on field names
d. Because it promotes inconsistencies between departments

19. Which of the following is not a statement type in SQL?

a. Database design
b. Database definition
c. Database manipulation
d. Database control

20. In the "three schema architecture", views belong to:

a. The internal level
b. The conceptual level
c. The external level
d. The client-server level

21. Which one of the following indicates poor data quality?

a. The business rule that an employee must belong to a department is enforced by the database
b. The same supplier is shown with two different addresses in two parts of the database
c. Two similar items have two different prices in the supermarket and in the database
d. New prices in a supermarket are updated in a timely fashion in the database

22. Which one of the following is true about the connection between database design and systems development?

a. Database design and systems development are broadly independent
b. Database design and systems development are loosely coupled
c. Database design and systems development are strongly coupled
d. Database design and systems development are totally independent
23. A goal of normalization is to:

a. minimize the number of relationships
b. minimize the number of entities
c. minimize the number of tables
d. minimize data redundancy

29. Durability of a transaction means the following:

a. All changes that took place during a transaction, including temporary changes, are permanently kept
b. Once a transaction is successfully completed, changes resulting from it are stored in the database on permanent storage
c. The results of a transaction are stored forever in the database
d. The effects of atomicity and consistency are durable
Answer: B Level: Medium Page: 420-421

30. Which of the following is an example of lost update?

a. The system assigns the last aisle seat on a flight to John whereas Peter receives a window seat, although both indicated preference for an aisle seat
b. John and Peter receive each a window seat as they both requested
c. John is initially assigned the last window seat, but this seat is later on assigned to Peter
d. None of the above

31. As the bank audit transaction reads Mary's savings balance, $100, Mary transfers $50 to her checking, making it $250, and the audit transaction completes with the combined value of $350 in both accounts. This is called:

a. An uncommitted dependency
b. An incorrect summary
c. A lost update
d. A data entry error

32. Julia has a lock on the savings account and Bill is trying to perform a conflicting action on the data:

a. Bill's transaction is granted permission to perform the desired action
b. Bill's transaction must wait until Julia's lock is released
c. Bill's transaction must wait until Julia has read the savings account value
d. None of the above

33. Julia has a shared lock on the savings account and Bill is trying to perform an action requesting a shared lock on the same data:

a. Bill's transaction is granted permission to perform the desired action
b. Bill's transaction must wait until Julia's lock is released
a. Bill's transaction must wait until Julia has read the savings account value
b. None of the above

34. Julia has an exclusive lock on the savings account and Bill is trying to perform an action requesting a shared lock on the same data:

a. Bill's transaction is granted permission to perform the desired action
b. Bill's transaction must wait until Julia's lock is released
c. Bill's transaction must wait until Julia has read the savings account value
d. None of the above

35. In the two-phase locking protocol:

a. A transaction only acquires a lock after the previously acquired lock has been released
b. A transaction acquires locks as needed. After it starts releasing locks, it does not acquire new locks
c. A transaction acquires all needed locks at once, then releases them after completion
d. The transaction locks the full database, then releases the locks


36. In the optimistic approach for concurrency control:

a. Only shared locks are used
b. Deadlocks are always detected before they occur
c. Only exclusive locks are used
d. Locks are not used

Thank you..

Indiawhat is this, some kind of homework assignment?

1. Persistency of business data refers to which of the following?
- computer salesmen who won't leave you alone

2. DBMS recovery management deals with
- when your database crashes, is your rsum up to date

3. Which of the following is neither a characteristic nor a feature of databases?
- strawberry marmelade

4. Which one of the following is the least likely to be viewed as a transaction?
- the VP of Accounting doesn't like your expense report item for the hooker you hired on your last trip overseas

5. A null value indicates:
- there's nothing there -- not even a null value!

6. In general denormalization produces __________________.
- that bloated feeling; try Tums

7. All parts of a transaction must be successfully completed to prevent ____________.
- the VP of Accounting from coming to see you about your long term career prospects

8. A _________________ is one in which all data integrity constraints are satisfied.
- data integrity straightjacket

9. When a transaction is completed, the database reaches a consistent state, and that state cannot be lost, even in the event of the systems failure. That property is referred to as ________________.
- job security

10. Which of the following is important in multi-user and distributed databases to maintain database integrity, where several transactions are likely to be executed concurrently?
- an up-to-date patch from Microsoft

11. In a _______________ client server system, the clients request may be handled by several different intermediate servers.
- blue-sky in-your-dreams client-server system

12. A(n) ___________ client is one that conducts a minimum of processing on the client side.
- polite

13. Which of the following statements is true when comparing XML to HTML.
- well, they have ML in common, so i guess they are both markup languages, eh

14. The scheduler ______________ the execution of database operations to ensure serializaility.
- ties the noose for

15. In this technique for dealing with deadlocks, the DBMS periodically tests the database for deadlocks and if a deadlock is found, the victim is aborted and the other transaction continues?
- dance hall or reggae, you choose

oh, you said deadlock, not dreadlock -- my bad

16. The most common aspects examined in DBMS ______________ are use of indexes, query-optimization algorithms, and management of storage resources.
- senior year college courses, after crappy query design was taught in the first three years

17. Authorization management defines access control procedures, which allow the DBA to ____________________.
- take the occasional trip to Thailand

18. Why is the selection of proper field names very important?
- because with improper field names, you will run afoul of the censors

19. Which of the following is not a statement type in SQL?
- SELECT COUNT(*) FROM USERS WHERE CLUE IS NOT NULL
--> 0 rows returned

20. In the "three schema architecture", views belong to:
- anybody who has an opinion, especially about the third schema

21. Which one of the following indicates poor data quality?
- your paycheque is never the same from week to week and sometimes the deductions exceed the gross pay

22. Which one of the following is true about the connection between database design and systems development?
- unless the DBAs have full authority, the developers will always design a crappy database

23. A goal of normalization is to:
- produce stable and worthwhile members of society

HEY, WHERE DID QUESTIONS 24 THROUGH 28 GO???

29. Durability of a transaction means the following:
- you can just throw it in the washer and dryer on the "Normal" cycle

30. Which of the following is an example of lost update?
- The Raiders of the Next SuperBowl

31. As the bank audit transaction reads Mary's savings balance, $100, Mary transfers $50 to her checking, making it $250, and the audit transaction completes with the combined value of $350 in both accounts. This is called:
- Mary's been working the streets again

32. Julia has a lock on the savings account and Bill is trying to perform a conflicting action on the data:
- Julia should just punch him in the stomach

33. Julia has a shared lock on the savings account and Bill is trying to perform an action requesting a shared lock on the same data:
- okay, she should try hoofing him in the stones

34. Julia has an exclusive lock on the savings account and Bill is trying to perform an action requesting a shared lock on the same data:
- okay, now she's really mad, here she comes with the frying pan...

35. In the two-phase locking protocol:
- you have a dead-bolt AND a chain

36. In the optimistic approach for concurrency control:
- you are on the street looking for a DBA job where they don't ask you tough questions like these

rudy
http://r937.com/|||rudy, that is the funniest thing I've seen since watching The Daily Show with John Stewart last night.

:D

blindman|||LOL

*

Falling off bar stool...umm office chair...|||If Julia has a shared lock, can Bill attempt a pass-through query?

I think Julia ought to give Bill a break, or at least credit for being persistent. He's probably a very nice guy.

blindman|||Originally posted by blindman
If Julia has a shared lock, can Bill attempt a pass-through query?

I think Julia ought to give Bill a break, or at least credit for being persistent. He's probably a very nice guy.

blindman

Is that a shared lock on a chastity belt? Is that why Bill wants to attempt a pass thru?