0

Sorry if this is a simple/silly question I am new to SQL so still learning and asking lots of questions about it.

In the code below I want to call ‘Location’ in my where statement but this obviously doesn’t work, I know I can copy down the case statement but is there not a way to recall ‘Location’? Thanks

    SELECT 
        case when [Case Type1] = 'Telephone Consultation' and ([Case Closed At]) is not null then 'Telephone Consultation & Base Visit'
              when [Case Closed At] is null then 'Telephone Consultation'

              when [Case Type1] = 'Home Visit' and [Case Closed At] not like '%Car%' then [Case Closed At] + ' Car'
              when [Case Type1] = 'Home Visit' and [Case Closed At] like '%1%' and left ([case closed at],4) = 'East' then 'East Oxford Car'
              when [Case Type1] = 'Home Visit' and [Case Closed At] like '%2%' and left ([case closed at],4) = 'East' then 'East Oxford Car'
              when [Case Type1] = 'Home Visit' and [Case Closed At] like '%1%' then left ([Case Closed At], charindex(' ',[Case Closed At])-1) + ' Car'
              when [Case Type1] = 'Home Visit' and [Case Closed At] like '%2%' then left ([Case Closed At], charindex(' ',[Case Closed At])-1) + ' Car'

              when [Case Type1] in ('Home Vis it','Base Visit') then [Case Closed At]
              else 'NOOOOooo'
               end as 'Location'

  FROM [db_Source_UrgentCare].[dbo].[tbl_Adastra_OOH_CaseExtract_alldata_to_31032016]

    where [Case Started] BETWEEN '2014-04-01 00:00:01.000' AND '31-March-2016 23:59:59.000'
    and [Location] = 'Abingdon'
HoneyBadger
  • 14,750
  • 3
  • 34
  • 48
g4cox
  • 1
  • 1
  • 2
    You can use a subquery or CTE. Column aliases are not recognized in the `WHERE` clause. – Gordon Linoff Jun 01 '16 at 11:33
  • @GordonLinoff This should really be an answer. Though I'm quite sure a question like this must have been asked here before, so it will most likely be closed as a duplicate soon :) – Luaan Jun 01 '16 at 11:39
  • @Luaan Many, many times actually. It took longer time deciding which dupe to use than to find any ;) – jpw Jun 01 '16 at 11:40
  • @jpw Yeah, I had the exact same problem... if we started closing all the old dupes, we might be here for a while... :D – Luaan Jun 01 '16 at 11:41
  • @g4cox You didn't specify what specific database system you are using - please do so in the future. This time it could be inferred from syntax (with a high likelihood, but not absolute certainty), but it's much better if you tag with the correct database. – jpw Jun 01 '16 at 11:43

0 Answers0