| Author |
Topic |
|
meberg66219
Yak Posting Veteran
65 Posts |
Posted - 2012-06-21 : 16:13:24
|
| I have an account ID that has records that are linked to multiple categories in one group. I'm trying to find any account ID who is not a member of a certain group called SPGS-Masters Alumni. Any account that has a certain degree ID as well.SELECT TOP (100) PERCENT dbo.Account.AccountID, dbo.Account.FirstName, dbo.Account.LastName, dbo.AccountGroup.GroupCode, dbo.Group_.Group_, dbo.Degree.DegreeFROM dbo.Account INNER JOIN dbo.AccountGroup ON dbo.Account.AccountID = dbo.AccountGroup.AccountID INNER JOIN dbo.Education ON dbo.Account.AccountID = dbo.Education.AccountID INNER JOIN dbo.Degree ON dbo.Education.DegreeCode = dbo.Degree.DegreeCode LEFT OUTER JOIN dbo.Group_ ON dbo.AccountGroup.GroupCode = dbo.Group_.GroupCodeWHERE Degree.Degree LIKE 'm%' AND Group_ <> 'SPGS-Masters Alumni' and dbo.Degree.Degree in ('MAED', 'MBA', 'MASL')ORDER BY dbo.Account.AccountID |
|
|
Lamprey
Master Smack Fu Yak Hacker
4614 Posts |
Posted - 2012-06-21 : 16:52:59
|
Hard to say without knowing you schema or data. Can you just move the Group predicate out of the where clause and onto the join? SELECT TOP (100) PERCENT dbo.Account.AccountID, dbo.Account.FirstName, dbo.Account.LastName, dbo.AccountGroup.GroupCode, dbo.Group_.Group_, dbo.Degree.DegreeFROM dbo.Account INNER JOIN dbo.AccountGroup ON dbo.Account.AccountID = dbo.AccountGroup.AccountID INNER JOIN dbo.Education ON dbo.Account.AccountID = dbo.Education.AccountID INNER JOIN dbo.Degree ON dbo.Education.DegreeCode = dbo.Degree.DegreeCode LEFT OUTER JOIN dbo.Group_ ON dbo.AccountGroup.GroupCode = dbo.Group_.GroupCode AND Group_.Group_ <> 'SPGS-Masters Alumni' WHERE Degree.Degree LIKE 'm%' AND dbo.Degree.Degree in ('MAED', 'MBA', 'MASL')ORDER BY dbo.Account.AccountIDIf that is not what you want post some DDL, DML and expected output so we can help:http://weblogs.sqlteam.com/brettk/archive/2005/05/25/5276.aspxhttp://www.sqlservercentral.com/articles/Best+Practices/61537/ |
 |
|
|
meberg66219
Yak Posting Veteran
65 Posts |
Posted - 2012-06-21 : 17:22:34
|
| Account ID=3Last Name= WhitneyGroup_= Alumni, Identified and SPGS-Masters AlumniDegree=MAEDAccount ID=1LastName= SmithGroup_= Alumni and IdentifiedDegree=MBAI want to pull a list of names with criteria similar to account ID 1 I only want the people who are not in the Group_ SPGS-Masters Alumni. |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-06-21 : 17:44:09
|
| SELECT * FROM table WHERE Group_ NOT LIKE '% SPGS-Masters Alumni %'------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
meberg66219
Yak Posting Veteran
65 Posts |
Posted - 2012-06-22 : 09:49:16
|
| Both of these responses are returning results such as this:Account ID=3Group_ = AlumniDegree=MAEDSo then it appears that Account ID 3 is not in the Group_ SPGS_Masters Alumni when in fact it is. I need to identify anyone that is not in the group so that I can generate a list of folks who need to be in SPGS_Masters Alumni. |
 |
|
|
Lamprey
Master Smack Fu Yak Hacker
4614 Posts |
|
|
meberg66219
Yak Posting Veteran
65 Posts |
Posted - 2012-06-22 : 11:57:13
|
| I guess I'm not sure what additional information you need. The DDL is in the first post and I Thought I was pretty clear about my expected output. Account ID LastName Group_ Degree3 Whitney SPGS-Masters Alumni MAED3 Whitney Alumni MAED1 Smith Alumni MBA1 Snmith Identified MBASo, in this case I would Want a query that only returns the output from Account number 1 because he is not in the group SPGS-masters alumni. SELECT TOP (100) PERCENT dbo.Account.AccountID, dbo.Account.FirstName, dbo.Account.LastName, dbo.AccountGroup.GroupCode, dbo.Group_.Group_, dbo.Degree.DegreeFROM dbo.Account INNER JOIN dbo.AccountGroup ON dbo.Account.AccountID = dbo.AccountGroup.AccountID INNER JOIN dbo.Education ON dbo.Account.AccountID = dbo.Education.AccountID INNER JOIN dbo.Degree ON dbo.Education.DegreeCode = dbo.Degree.DegreeCode LEFT OUTER JOIN dbo.Group_ ON dbo.AccountGroup.GroupCode = dbo.Group_.GroupCodeWHERE (dbo.Degree.Degree LIKE 'm%')ORDER BY dbo.Account.AccountID |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2012-06-22 : 22:31:45
|
| [code]SELECT dbo.Account.AccountID, dbo.Account.FirstName, dbo.Account.LastName, dbo.AccountGroup.GroupCode, dbo.Group_.Group_, dbo.Degree.DegreeINTO #tempFROM dbo.Account INNER JOINdbo.AccountGroup ON dbo.Account.AccountID = dbo.AccountGroup.AccountID INNER JOINdbo.Education ON dbo.Account.AccountID = dbo.Education.AccountID INNER JOINdbo.Degree ON dbo.Education.DegreeCode = dbo.Degree.DegreeCode LEFT OUTER JOINdbo.Group_ ON dbo.AccountGroup.GroupCode = dbo.Group_.GroupCodeWHERE (dbo.Degree.Degree LIKE 'm%')SELECT t.*FROM #temp t WHERE NOT EXISTS (SELECT 1 FROM #temp WHERE [Account ID] = t.[Account ID] AND Group_ LIKE '% SPGS-Masters Alumni %')ORDER BY AccountIDDROP TABLE #Temp[/code]------------------------------------------------------------------------------------------------------SQL Server MVPhttp://visakhm.blogspot.com/ |
 |
|
|
|