views:

123

answers:

8

This is a simplified version of what I'm doing, but I can't get anything to work. The statement gives me an error without the comma after 'ERR'. I want the column to be 'Month' and I tohught this would work but I'm having a ton of trouble. Thanks for your help!

   select 
 a.POL_PRI_RSK_ST_CD, a.MASTER_COMPANY_NBR,

case

when a.char046 is NULL then 'ERR'

when a.char046 > '010' then '11+'

else a.char046 end as Policy_Years,

a.Last7Days, a.Last30Days, a.Last90Days

from reporting a inner join

Repository b 

on a.RECORD_ID = b.RECORD_ID

where a.POL_OGN_EFF_DT >= '2008-11-01'

group by

a.POL_PRI_RSK_ST_CD, a.MASTER_COMPANY_NBR, 

case

when a.char046 is NULL then 'ERR'

when a.char046 > '010' then '11+'

else a.char046 end as Policy_Years,

a.Last7Days, a.Last30Days, a.Last90Days
+1  A: 

Try it without commas... example to follow.

select  
   case 
      when a.month is NULL then 'ERR'
      when a.month > '011' then '12' 
      else a.month 
   end as Month, 
   a.Last7Days 
from ... 
Mayo
Without commas gives me this message immediately for every day, year, etc.Msg 4104, Level 16, State 1, Line 1The multi-part identifier "day" could not be bound.
Daniel
Commas are still required to separate the columns of the returned recordset. You just don't need them within the case statement.
Mayo
right, i meant without that one comma after 'ERR'. same error
Daniel
Ah, then that's another error. I would suggest googling the error - i.e. http://aartemiou.blogspot.com/2009/01/multi-part-identifier-could-not-be.html.
Mayo
The error you get suggests that the field "day" is not part of the table you're selecting from. Check the schema of the table you're selecting from.
Ender
If a.Month is integer, then cast it to string to match other output values, (or use Str() function) All cases within a Case must have the same type. This may be cause of the error
Charles Bretana
A: 
select 
     day, year,
case
   when a.month is NULL then 'ERR'
   when a.month > '011' then '12'
   else a.month end as Month,
 a.Last7Days
Keith Rousseau
still getting errors. see above.
Daniel
A: 

Hi, i think you are missing 'AS' after the end of the case Best Regards, Iordan

IordanTanev
As is optional, not required...
Charles Bretana
A: 

Fix the commas :

select 
     day, year,
     case
       when a.month is NULL then 'ERR'
       when a.month > '011' then '12'
       else a.month 
     end Month,
     a.Last7Days
from [table]
Ender
A: 

Put square brackets around Day and Year, like this:

select a.[Day], a.[Year], ...
RedFilter
it's just an example, day isn't in the actual statement
Daniel
Please post the actual statement.
RedFilter
A: 

Addressing your new error msg about 'multi-part identifier 'day',

Are day and year columns in the table ? What comes after the From in your query ? Are you joining multiple tables together in this? Please show the entire query?

ok, based on yr edited question, (You can't use an alias in a Group By) try this:

select a.POL_PRI_RSK_ST_CD, 
  a.MASTER_COMPANY_NBR,
  case when a.char046 is NULL then 'ERR'
       when a.char046 > '010' then '11+'
       else a.char046 end as Policy_Years,
  a.Last7Days, a.Last30Days, a.Last90Days
from reporting a 
   join Repository b 
      on a.RECORD_ID = b.RECORD_ID
where a.POL_OGN_EFF_DT >= '2008-11-01'
group by a.POL_PRI_RSK_ST_CD, 
   a.MASTER_COMPANY_NBR, 
   case when a.char046 is NULL then 'ERR'
        when a.char046 > '010' then '11+'
        else a.char046 end,
   a.Last7Days, a.Last30Days, a.Last90Days

but actually, you have no aggregate functions in there at all, just a group by on every expression in the select, so all you need is a Distinct key word , you don;t need the group by at all:

select Distinct a.POL_PRI_RSK_ST_CD, 
  a.MASTER_COMPANY_NBR,
  case when a.char046 is NULL then 'ERR'
       when a.char046 > '010' then '11+'
       else a.char046 end as Policy_Years,
  a.Last7Days, a.Last30Days, a.Last90Days
from reporting a 
   join Repository b 
      on a.RECORD_ID = b.RECORD_ID
where a.POL_OGN_EFF_DT >= '2008-11-01'
Charles Bretana
select a.POL_PRI_RSK_ST_CD as State, a.MASTER_COMPANY_NBR as Original_CO, case when a.char046 is NULL then 'ERR' when a.char046 > '010' then '11+' else a.char046 end as Policy_Years, a.Last7Days, a.Last30Days, a.Last90Daysfrom reporting a inner joinRepository b on a.RECORD_ID = b.RECORD_IDwhere a.POL_OGN_EFF_DT >= '2008-11-01'group by a.POL_PRI_RSK_ST_CD, a.MASTER_COMPANY_NBR, case when a.char046 is NULL then 'ERR' when a.char046 > '010' then '11+' else a.char046 end as Policy_Years, a.Last7Days, a.Last30Days, a.Last90Days
Daniel
I edited the question. Thanks
Daniel
A: 

You cannot use an AS alias in the GROUP BY. The expression should match the expression in your SELECT without the alias.

Cade Roux
+1  A: 

Note: This is the outcome of the debugging session in the question comments.

The error Incorrect syntax near the keyword 'as'. was caused by as Policy_Years in the GROUP BY clause. You are not allowed to use as within a GROUP BY clause.

Heinzi