Home All Groups Group Topic Archive Search About
Author
12 Aug 2005 5:28 PM
wnfisba
I am trying to use a derived Table and this will not work for me. Obviously,
I am NOT going to SELECT *, but for demonstration purposes, here it is.

SELECT    *
FROM    (SELECT    Filename,
        RACE    =
            CASE    WHEN    BRaceAA    =    'X'    THEN    '1,'    ELSE    ''    END    +
            CASE    WHEN    BRaceA    =    'X'    THEN    '2,'    ELSE    ''    END    +
            CASE    WHEN    BRaceB    =    'X'    THEN    '3,'    ELSE    ''    END    +
            CASE    WHEN    BRaceNH    =    'X'    THEN    '4,'    ELSE    ''     END    +
            CASE    WHEN    BRaceW    =    'X'    THEN    '5,'    ELSE    ''    END   
    FROM    HMDAFromPointIMP
    WHERE    BRaceAA    IS    NOT NULL
    OR    BRaceA    IS    NOT NULL
    OR    BRaceB    IS    NOT NULL
    OR    BRaceNH    IS    NOT NULL
    OR    BRaceW    IS    NOT NULL)

It is giving me Incorrect Syntax on the last ")"

Author
12 Aug 2005 5:33 PM
Aaron Bertrand [SQL Server MVP]
You need to give the derived table an alias, like I demonstrated in an
earlier thread about this same issue:

FROM
(
    SELECT /* blah blah */
) x
--^ alias

Please stop starting new threads!





Show quote
"wnfisba" <wnfi***@discussions.microsoft.com> wrote in message
news:34AAF6C5-E9FF-4D7C-A26C-09DDF7223265@microsoft.com...
>I am trying to use a derived Table and this will not work for me.
>Obviously,
> I am NOT going to SELECT *, but for demonstration purposes, here it is.
>
> SELECT *
> FROM (SELECT Filename,
> RACE =
> CASE WHEN BRaceAA = 'X' THEN '1,' ELSE '' END +
> CASE WHEN BRaceA = 'X' THEN '2,' ELSE '' END +
> CASE WHEN BRaceB = 'X' THEN '3,' ELSE '' END +
> CASE WHEN BRaceNH = 'X' THEN '4,' ELSE '' END +
> CASE WHEN BRaceW = 'X' THEN '5,' ELSE '' END
> FROM HMDAFromPointIMP
> WHERE BRaceAA IS NOT NULL
> OR BRaceA IS NOT NULL
> OR BRaceB IS NOT NULL
> OR BRaceNH IS NOT NULL
> OR BRaceW IS NOT NULL)
>
> It is giving me Incorrect Syntax on the last ")"
Author
12 Aug 2005 5:56 PM
Adam Machanic
"Aaron Bertrand [SQL Server MVP]" <ten.xoc@dnartreb.noraa> wrote in message
news:OVFSQP2nFHA.420@TK2MSFTNGP09.phx.gbl...
>
> Please stop starting new threads!

    As a member of the B,NH,W race, I take exception to your suggestion that
new threads not be created!  We are a proud race of thread-starters.


--
Adam Machanic
SQL Server MVP
http://www.datamanipulation.net
--
Author
12 Aug 2005 9:25 PM
ML
....and I thought Tiger Woods was supposed to be white, Asian and black. This
is the fourth socially disturbing thread today. Plus there's approximately 30
different races in the world.

This could make such a nice relational model...


ML
Author
12 Aug 2005 8:53 PM
Louis Davidson
In addition to the syntax error that my other fellow posters have mentioned,
you might want to consider (if you can) changing your table structures.
Having columns like this is messy (as your query demonstrates)

You might want to do something like:

HMDAFromPointIMP
===============
HMDAFromPointIMP_key --pk
<other columns>

HMDAFromPointIMPRace
===================
HMDAFromPointIMP_key --pk
Race_key                            --pk

Race
====
Race_key --pk
RaceCode --AA, B,NH, etc
Description --

Then adding a new race is easier, and building queries is also much easier.
Just an idea if you are building a new system :)


--
----------------------------------------------------------------------------
Louis Davidson - http://spaces.msn.com/members/drsql/
SQL Server MVP


Show quote
"wnfisba" <wnfi***@discussions.microsoft.com> wrote in message
news:34AAF6C5-E9FF-4D7C-A26C-09DDF7223265@microsoft.com...
>I am trying to use a derived Table and this will not work for me.
>Obviously,
> I am NOT going to SELECT *, but for demonstration purposes, here it is.
>
> SELECT *
> FROM (SELECT Filename,
> RACE =
> CASE WHEN BRaceAA = 'X' THEN '1,' ELSE '' END +
> CASE WHEN BRaceA = 'X' THEN '2,' ELSE '' END +
> CASE WHEN BRaceB = 'X' THEN '3,' ELSE '' END +
> CASE WHEN BRaceNH = 'X' THEN '4,' ELSE '' END +
> CASE WHEN BRaceW = 'X' THEN '5,' ELSE '' END
> FROM HMDAFromPointIMP
> WHERE BRaceAA IS NOT NULL
> OR BRaceA IS NOT NULL
> OR BRaceB IS NOT NULL
> OR BRaceNH IS NOT NULL
> OR BRaceW IS NOT NULL)
>
> It is giving me Incorrect Syntax on the last ")"

AddThis Social Bookmark Button