Syntax Error

  • Hi

    can someone please tell me what is wrong with this query

    select ee_occup_levels, gender, race

    from (select ee_occup_levels, gender, race

    from dtiheadcount inner join reportingstructure

    on dtiheadcount.costcenter = reportingstructure.costcenter

    where (period = 200909)

    and (dtiheadcount.headcount = 'headcount')

    and (dtiheadcount.hccat2 = 'permanent')

    and (dtiheadcount.staffno is not null)) AS x

    Pivot (Count(race) for gender in ('m', 'f')) as pvt

    when I run it, I get syntax error near 'm'

    Thanks

  • thatok (10/13/2009)


    Hi

    can someone please tell me what is wrong with this query

    select ee_occup_levels, gender, race

    from (select ee_occup_levels, gender, race

    from dtiheadcount inner join reportingstructure

    on dtiheadcount.costcenter = reportingstructure.costcenter

    where (period = 200909)

    and (dtiheadcount.headcount = 'headcount')

    and (dtiheadcount.hccat2 = 'permanent')

    and (dtiheadcount.staffno is not null)) AS x

    Pivot (Count(race) for gender in ('m', 'f')) as pvt

    when I run it, I get syntax error near 'm'

    Thanks

    As per BOL,

    FOR

    [<column that contains the values that will become column headers>]

    IN ( [first pivoted column] , [second pivoted column] ,

    ... [last pivoted column] )

    So, should not it be?

    Pivot (Count(race) for gender in ([m], [f])) as pvt

    and those two columns should be in select clause!

    hope that helps!

    ---------------------------------------------------------------------------------

  • Hi

    They are included in the select, but now I get an error that say invalid column names when I use brackets

  • How about this?

    select ee_occup_levels, [1] as M, [2] as F

    from (select ee_occup_levels, gender, race

    from dtiheadcount inner join reportingstructure

    on dtiheadcount.costcenter = reportingstructure.costcenter

    where (period = 200909)

    and (dtiheadcount.headcount = 'headcount')

    and (dtiheadcount.hccat2 = 'permanent')

    and (dtiheadcount.staffno is not null)) AS x

    Pivot (Count(race) for gender in ([1], [2])) as pvt

    ---------------------------------------------------------------------------------

  • It runs but does not count anything

  • Hi,

    What PP-564103 meant when he said "those two columns should be in select clause!" is that the NEW columns should be in the select like this:

    select ee_occup_levels, m, f

    from (select ee_occup_levels, gender, race

    from dtiheadcount inner join reportingstructure

    on dtiheadcount.costcenter = reportingstructure.costcenter

    where (period = 200909)

    and (dtiheadcount.headcount = 'headcount')

    and (dtiheadcount.hccat2 = 'permanent')

    and (dtiheadcount.staffno is not null)) AS x

    Pivot (Count(race) for gender in ([m], [f])) as pvt

    PP was quicker

  • Beautiful, Thanks a lot to both of you

  • PP was quicker

    Thanks:)

    thatok (10/13/2009)


    Beautiful, Thanks a lot to both of you

    you're welcome 🙂

    ---------------------------------------------------------------------------------

Viewing 8 posts - 1 through 7 (of 7 total)

You must be logged in to reply to this topic. Login to reply