Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
liptonIcedTea
Starting Member
26 Posts |
Posted - 2008-02-27 : 01:34:23
|
| Excuse me if my terminlogy is inaccurate, i'm a .NETer that's new to SQL. I was wondering if it's possible to reference a column in the WHERE CLAUSE that has been customily defined in the SELECT statementfor exampleselect employeeID, case when JobCode = 'A' then 'Accountant' case when JobCode = 'C' then 'Consultant' case when JobCode = 'B' then 'Biller' End as JobTypefrom Employeewhere JobType is not nullThis does not work, and saids JobType is an invalid column name. Basically, what I want is to display the jobtype of the employee but i also want to only display the employees that are Accountants, consultants and billers.Is this possible and what would be the best way of doing this?I do not seem to be about to reference JobType in the WHERE statement. |
|
|
jackv
Master Smack Fu Yak Hacker
2179 Posts |
Posted - 2008-02-27 : 01:40:16
|
| Try this select employeeID, JobType = CASE when JobCode = 'A' then 'Accountant' when JobCode = 'C' then 'Consultant' when JobCode = 'B' then 'Biller'End from Employeewhere JobType is not null AND JobType IN ('A','C','B')Jack Vamvas--------------------Search IT jobs from multiple sources- http://www.ITjobfeed.com |
 |
|
|
liptonIcedTea
Starting Member
26 Posts |
Posted - 2008-02-27 : 01:47:02
|
| This does not fix the error.It is still coming up with "Invalid column name 'Category'" |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2008-02-27 : 02:08:36
|
quote: Originally posted by liptonIcedTea This does not fix the error.It is still coming up with "Invalid column name 'Category'"
This error is not due to query you posted (there's no field called category in posted code). Please post full query if you want help to fix this. |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-02-27 : 02:11:43
|
Jackv, these two WHERE cases are mutual.You don't need IS NOT NULL when you also searches for NOT IN ('a', 'b', 'c')where JobType is not null AND JobType IN ('A','C','B') E 12°55'05.25"N 56°04'39.16" |
 |
|
|
liptonIcedTea
Starting Member
26 Posts |
Posted - 2008-02-27 : 17:08:47
|
| Hi guys, this is the full code, it's really long, but basically the bit i'm having trouble is the Category thing.Thanks[CODE]declare @CRN varchar(100)set @CRN = '420631391K'select tp.TrainingProviderId as TrainingProvider, tpn.Name as 'Provider Name', tps.name as 'Site Name', tps.TrainingProviderSiteIdentifier as SiteName, ji.CRN as CRN, ji.JobseekerIdentifier as JskNumber, tbh.TrainingBlockHistoryTypeCd, Category = case when tbh.TrainingBlockHistoryTypeCd ='Ast' then 'Actual Start Date' when tbh.TrainingBlockHistoryTypeCd='Sus' then 'Suspension' when tbh.TrainingBlockHistoryTypeCd='W' then 'Withdrawal' when tbh.TrainingBlockHistoryTypeCd='T' and not tbh.TrainingBlockHistoryTypeCd = 'Trd' then 'Termination' when tbh.TrainingBlockHistoryTypeCd='Trd' then 'Resumption' when tbh.TrainingBlockHistoryTypeCd ='Aed' then 'Actual End Date' End,ar.ReferringProviderReferredDt as ReferralDate,tbh.EventDt as EventDate, tbh.CreatedDt as DataEntryDate, datediff(day, tbh.EventDt, tbh.CreatedDt) as DaysOver,(ind.FamilyName + ',' + ind.FirstName + ' (' + ind.AccountLoginCode + ')') as DataEnteredByfrom tblTrainingBlockHistory tbh inner join tblTrainingBlock tb on tbh.TrainingBlockId = tb.TrainingBlockIdinner join tblActionedReferral ar on tb.ActionedReferralId = ar.ActionedReferralIdinner join tblJobseekerIdentifier ji on ar.JobseekerIdentifierId = ji.JobseekerIdentifierIdinner join tblTrainingProvider tp on ar.TrainingProviderId = tp.TrainingProviderIdinner join tblTrainingProviderSite tps on tps.TrainingProviderId = tp.TrainingProviderId inner join tblTrainingProviderName tpn on tpn.TrainingProviderId = tps.TrainingProviderIdinner join tblIndividual ind on tb.CreatedIndividualId = ind.IndividualIdwhere tpn.ProviderNameTypeCd = 'P'and ji.CRN = @CRNand datediff(day, tbh.EventDt, tbh.CreatedDt) > 7and tbh.IsActiveInd = 'Y'and ar.TrainingProviderSiteId = tps.TrainingProviderSiteIdand Category is not null[/CODE] |
 |
|
|
LoztInSpace
Aged Yak Warrior
940 Posts |
Posted - 2008-02-27 : 21:01:05
|
| You can't - you have to either repeat the expression or wrap it up.select * from(your select here without reference to Category)where Category is not null.It will save some typing.In this case though, I believe you can simply use tbh.TrainingBlockHistoryTypeCd is not null istead of Category on your last line.If there are more values than in your case statement then you will need tbh.TrainingBlockHistoryTypeCd in ('Sus', 'W','T').I am not sure what this means:tbh.TrainingBlockHistoryTypeCd='T' and not tbh.TrainingBlockHistoryTypeCd = 'Trd'If it's 'T' then it can't be 'Trd'. Or am I missing something? |
 |
|
|
liptonIcedTea
Starting Member
26 Posts |
Posted - 2008-02-28 : 00:06:47
|
| i can't just check TrainingBlockHistory is not null, becuase there will be situations where TrainingBlockHistory will equal something else other then the possibilities in the CASE statement.Basically, I've conceded that there is no way around this, you CANNOT reference a custom label from the WHERE clause. I just ended up repeating it the CASE statement in the WHERE clause.I guess i'm just used to .NET :p |
 |
|
|
SwePeso
Patron Saint of Lost Yaks
30421 Posts |
Posted - 2008-02-28 : 01:11:03
|
[code]declare @CRN varchar(100)set @CRN = '420631391K'select tp.TrainingProviderId as TrainingProvider, tpn.Name as [Provider Name], tps.name as [Site Name], tps.TrainingProviderSiteIdentifier as SiteName, ji.CRN as CRN, ji.JobseekerIdentifier as JskNumber, tbh.TrainingBlockHistoryTypeCd, case when tbh.TrainingBlockHistoryTypeCd = 'Ast' then 'Actual Start Date' when tbh.TrainingBlockHistoryTypeCd = 'Sus' then 'Suspension' when tbh.TrainingBlockHistoryTypeCd = 'W' then 'Withdrawal' when tbh.TrainingBlockHistoryTypeCd = 'T' then 'Termination' when tbh.TrainingBlockHistoryTypeCd = 'Trd' then 'Resumption' when tbh.TrainingBlockHistoryTypeCd = 'Aed' then 'Actual End Date' End as Category, ar.ReferringProviderReferredDt as ReferralDate, tbh.EventDt as EventDate, tbh.CreatedDt as DataEntryDate, datediff(day, tbh.EventDt, tbh.CreatedDt) as DaysOver, ind.FamilyName + ',' + ind.FirstName + ' (' + ind.AccountLoginCode + ')' as DataEnteredByfrom tblTrainingBlockHistory as tbh inner join tblTrainingBlock as tb on tb.TrainingBlockId = tbh.TrainingBlockIdinner join tblActionedReferral as ar on ar.ActionedReferralId = tb.ActionedReferralIdinner join tblJobseekerIdentifier as ji on ji.JobseekerIdentifierId = ar.JobseekerIdentifierIdinner join tblTrainingProvider as tp on tp.TrainingProviderId = ar.TrainingProviderIdinner join tblTrainingProviderSite as tps on tps.TrainingProviderId = tp.TrainingProviderIdinner join tblTrainingProviderName as tpn on tpn.TrainingProviderId = tps.TrainingProviderIdinner join tblIndividual as ind on ind.IndividualId = tb.CreatedIndividualIdwhere tpn.ProviderNameTypeCd = 'P' and ji.CRN = @CRN and datediff(day, tbh.EventDt, tbh.CreatedDt) > 7 and tbh.IsActiveInd = 'Y' and ar.TrainingProviderSiteId = tps.TrainingProviderSiteId and tbh.TrainingBlockHistoryTypeCd in ('Ast', 'Sus', 'W', 'T', 'Trd', 'Aed')[/code] E 12°55'05.25"N 56°04'39.16" |
 |
|
|
liptonIcedTea
Starting Member
26 Posts |
Posted - 2008-02-28 : 17:42:59
|
| peso... thanks. and yeah, that's basically what i did, but coming from a .NET background, i guess i'm used to having the mind set to not duplicate any of my code so that's why i was trying to find a way where i wouldn't have to re-specify all the acceptable history types in the Where statement. |
 |
|
|
LoztInSpace
Aged Yak Warrior
940 Posts |
Posted - 2008-02-28 : 20:25:11
|
Like I said, you can do it like this:select * from(your select here without reference to Category)where Category is not null. i.e. something like this:declare @CRN varchar(100)set @CRN = '420631391K'select * from(select tp.TrainingProviderId as TrainingProvider, tpn.Name as [Provider Name], tps.name as [Site Name], tps.TrainingProviderSiteIdentifier as SiteName, ji.CRN as CRN, ji.JobseekerIdentifier as JskNumber, tbh.TrainingBlockHistoryTypeCd, case when tbh.TrainingBlockHistoryTypeCd = 'Ast' then 'Actual Start Date' when tbh.TrainingBlockHistoryTypeCd = 'Sus' then 'Suspension' when tbh.TrainingBlockHistoryTypeCd = 'W' then 'Withdrawal' when tbh.TrainingBlockHistoryTypeCd = 'T' then 'Termination' when tbh.TrainingBlockHistoryTypeCd = 'Trd' then 'Resumption' when tbh.TrainingBlockHistoryTypeCd = 'Aed' then 'Actual End Date' End as Category, ar.ReferringProviderReferredDt as ReferralDate, tbh.EventDt as EventDate, tbh.CreatedDt as DataEntryDate, datediff(day, tbh.EventDt, tbh.CreatedDt) as DaysOver, ind.FamilyName + ',' + ind.FirstName + ' (' + ind.AccountLoginCode + ')' as DataEnteredByfrom tblTrainingBlockHistory as tbh inner join tblTrainingBlock as tb on tb.TrainingBlockId = tbh.TrainingBlockIdinner join tblActionedReferral as ar on ar.ActionedReferralId = tb.ActionedReferralIdinner join tblJobseekerIdentifier as ji on ji.JobseekerIdentifierId = ar.JobseekerIdentifierIdinner join tblTrainingProvider as tp on tp.TrainingProviderId = ar.TrainingProviderIdinner join tblTrainingProviderSite as tps on tps.TrainingProviderId = tp.TrainingProviderIdinner join tblTrainingProviderName as tpn on tpn.TrainingProviderId = tps.TrainingProviderIdinner join tblIndividual as ind on ind.IndividualId = tb.CreatedIndividualIdwhere tpn.ProviderNameTypeCd = 'P' and ji.CRN = @CRN and datediff(day, tbh.EventDt, tbh.CreatedDt) > 7 and tbh.IsActiveInd = 'Y' and ar.TrainingProviderSiteId = tps.TrainingProviderSiteId) xwhere category is not null;You don't have to reproduce it, you just have to bundle it up a bit. |
 |
|
|
|
|
|
|
|