MS SQL Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsDatabasesMS SQL Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old November 15th, 2004, 09:10 AM
Tryst Tryst is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2003
Posts: 146 Tryst User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 12 h 26 m 49 sec
Reputation Power: 5
Complex Query problems

Hi all,

Yet again, I have a need to solve a complex query related problem.

I'll try and break this down as simple as possible...OK..

I have a table called contracts
-----------------------------
conid - description
-----------------------------
x01 - Area1
x02 - Area2
-----------------------------

and a hospital table, where 'X' amount of hospitals are tied to a given contract.
-----------------------------
hospid - conid
-----------------------------
hosp1 - x01
hosp2 - x01
hosp3 - x02
hosp4 - xo2

then I have patients table, which deals with the journeys patients have made under the hospitals. So I have a patients table like this...
--------------------------------------------
patid - fromhosp - tohosp - cancin - cancout
--------------------------------------------
01 - hosp1 - hosp3 - 2 - NULL
02 - hosp3 - NULL - NULL - NULL
03 - hosp2 - hosp4 - NULL - 3
04 - hosp4 - hosp1 - NULL - 1
-------------------------------------------

OK then, the patients table details a patients journey from a hospital (fromhosp) and to a hospital (tohosp). These both tie in with the Hospitals table (which I think is causing me the problem as there are two foriegn keys linking to the hospitals table). Also in this table we have a cancin and cancout columns. There are for if any cancellations are made (and act as akey to a cancellations table so we can get the cancellations types).

Ok, now for my query requirements . I want my query (if possible) to return return all hospitals grouped by their contract, and for there to be a column in the result set that counts the amount of journeys a hospital has made (transferring patients) were fromhosp and tohosp count as separate journeys and both columns can contain data in one row and represent two journeys (to and from). I also need a column that counts all the cancellations thats were undertaken per hospital (which are, as I previously mentioned, where grouped by there contracts). The NULLs in the cancin and cancout columns means that there were no cancallations, and the NULLs in the fromhosp or tohosp means that a patient was transferred to or from their home.

Hope I have made this clear, if not come back to me.

Thanks

Tryst
__________________
Tryst

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesMS SQL Development > Complex Query problems


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway