|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Wednesday, December 05, 2012 7:14 AM
Points: 9,
Visits: 27
|
|
I am trying to create a query to return certain information, but I am not sure how to get the query to return the information.
I have a table called Prvd which contains information similar to the sample below.
GrpID GrpName PrvID PrvName Spc 1234 Med Associates (blank) (blank) Mixed 1234 Med Associates 6875 Bill Ham Cardiology 1234 Med Associates 3274 Sarah Jones Cardiology 8975 Pulm Associates (blank) (blank) Mixed 5781 Babies Inc. (blank) (blank) Pediatrics 6321 ABC Therapy (blank) (blank) Mixed 6321 ABC Therapy 4925 Tate Silverman Physical Therapy 6321 ABC Therapy 3274 Sarah Jones Occupational Therapy
The column Spc contains either the Specialty of the Group (Grp) if the record has no Provider (Prv), or contains the Specialty of the Provider (Prv).
I am needing the query to do two things
1. Return the Groups (Grp) with a Specialty (Spc) equal to Mixed that have no Providers (Prv) And 2. Return the Groups (Grp) with a Specialty (Spc) equal to Mixed but where the Providers (Prv) only have one other type of Specialty (Spc).
Here is what the query would return based on the sample data above
GrpID GrpName Spc 1234 Med Associates Cardiology 8975 Pulm Associates Mixed
|
|
|
|
|
SSCrazy
      
Group: General Forum Members
Last Login: Yesterday @ 2:19 PM
Points: 2,037,
Visits: 3,761
|
|
Please post the TSQL that you've come up with thus far
______________________________________________________________________________ "Never argue with an idiot; They'll drag you down to their level and beat you with experience"
|
|
|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Wednesday, December 05, 2012 7:14 AM
Points: 9,
Visits: 27
|
|
| I do not have anything as I am even sure where to begin. I am a novice with SQL 2008 and this query is more complex.
|
|
|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Wednesday, December 05, 2012 7:14 AM
Points: 9,
Visits: 27
|
|
Can this even be accomplished?
It probably can not be accomplished, but figured it was worth asking.
|
|
|
|
|
SSCertifiable
       
Group: General Forum Members
Last Login: Today @ 4:36 AM
Points: 5,678,
Visits: 6,126
|
|
|
|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Wednesday, December 05, 2012 7:14 AM
Points: 9,
Visits: 27
|
|
| Thanks Craig! I will review the information.
|
|
|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Wednesday, December 05, 2012 7:14 AM
Points: 9,
Visits: 27
|
|
Ok I think I did this correct.
--===== If the test table already exists, drop it IF OBJECT_ID('TempDB..#mytable','U') IS NOT NULL DROP TABLE #mytable
--===== Create the test table with CREATE TABLE #mytable ( GrpID INT, GrpName TEXT, PrvID INT, PrvName TEXT, Spc TEXT, )
--===== Setup any special required conditions especially where dates are concerned
--===== All Inserts into the IDENTITY column SET IDENTITY_INSERT #mytable ON
--===== Insert the test data into the test table INSERT INTO #mytable (GrpID, GrpName, PrvID, PrvName, Spc) SELECT ‘1234’,‘Med Associates’,‘’,‘’,‘Mixed’ UNION ALL SELECT ‘1234’,‘Med Associates’,‘6875’,‘Bill Ham’,‘Cardiology’ UNION ALL SELECT ‘1234’,‘Med Associates’,‘3274’,‘Sarah Jones’,‘Cardiology’ UNION ALL SELECT ‘8975’,‘Pulm Associates’,‘’,‘’,‘Mixed’ UNION ALL SELECT ‘5781’,‘Babies Inc’,‘’,‘’,‘Pediatrics’ UNION ALL SELECT ‘6321’,‘ABC Therapy’,‘’,‘’,‘Mixed’ UNION ALL SELECT ‘6321’,‘ABC Therapy’,‘4925’,‘Tate Silverman’,‘Physical Therapy’ UNION ALL SELECT ‘6321’,‘ABC Therapy’,‘3274’,‘Sarah Jones’,‘Occupational Therapy’ UNION ALL
--===== Set the identity insert back to normal SET IDENTITY_INSERT #mytable OFF
|
|
|
|
|
Valued Member
      
Group: General Forum Members
Last Login: Today @ 7:57 AM
Points: 68,
Visits: 315
|
|
GrpID GrpName PrvID PrvName Spc 1234 Med Associates (blank) (blank) Mixed 1234 Med Associates 6875 Bill Ham Cardiology 1234 Med Associates 3274 Sarah Jones Cardiology 8975 Pulm Associates (blank) (blank) Mixed 5781 Babies Inc. (blank) (blank) Pediatrics 6321 ABC Therapy (blank) (blank) Mixed 6321 ABC Therapy 4925 Tate Silverman Physical Therapy 6321 ABC Therapy 3274 Sarah Jones Occupational Therapy The column Spc contains either the Specialty of the Group (Grp) if the record has no Provider (Prv), or contains the Specialty of the Provider (Prv). I am needing the query to do two things 1. Return the Groups (Grp) with a Specialty (Spc) equal to Mixed that have no Providers (Prv) And select GrpId from #mytable where spc like 'mixed' group by GrpId having max(PrvId) =0
2. Return the Groups (Grp) with a Specialty (Spc) equal to Mixed but where the Providers (Prv) only have one other type of Specialty (Spc). select GrpId from (select GrpId,varr=(case when PrvID IS null then 0 when PrvID = '' then 0 else 1 end) from #mytable where spc like 'mixed')a group by GrpId having sum(varr)=1
|
|
|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Wednesday, December 05, 2012 7:14 AM
Points: 9,
Visits: 27
|
|
Not sure if my last post was correct or not.
I was thinking maybe I could do some sort of Group by and then do a Count of Spc <= 2 to get the data somewhat close to what I need. Any thoughts?
|
|
|
|
|
Forum Newbie
      
Group: General Forum Members
Last Login: Wednesday, December 05, 2012 7:14 AM
Points: 9,
Visits: 27
|
|
Thank you for the reply.
Do I need to change/replace anything in the code you provided?
|
|
|
|