【问题标题】:Need help with complex case statement SQL 2005需要有关复杂案例语句 SQL 2005 的帮助
【发布时间】:2011-01-05 21:55:27
【问题描述】:

我不确定我想要做什么的语法。我需要根据条件选择几个地址字段。

逻辑是可靠的,但似乎语法不正确。它在关键字“Case”、“Else”、“Else”附近出现“语法不正确”的错误。 思路是如果isprimary=1则将地址插入表中,如果没有primary则插入isactive=1的最新地址,如果没有active则插入最新地址。

请帮忙


-- 为记录 02 选择一个最佳地址

DECLARE @ygcaddress TABLE
(
[AccountID] varchar(10),
[Address1] varchar(25),
[City] varchar(22),
[State] varchar(3),
[Zip] varchar(9)
)
INSERT INTO @ygcaddress
CASE
WHEN Address.IsPrimary=1
THEN 
SELECT
LEFT(Address.AddressLine1,25),
Address.City,
[Lookup].LookupValue,
Address.Zip
FROM (((AccountPerson INNER JOIN Account ON AccountPerson.AccountID=Account.AccountID)
INNER JOIN Address ON AccountPerson.PersonID=Address.PersonID)
INNER JOIN @ygcaddress y ON Account.AccountID=y.AccountID)
INNER JOIN [Lookup] ON Address.StateID=[Lookup].LookupID
WHERE Address.IsPrimary=1
ELSE
CASE
WHEN Address.IsActive=1
THEN
SELECT TOP 1
LEFT(Address.AddressLine1,25),
Address.City,
[Lookup].LookupValue,
Address.Zip
FROM (((AccountPerson INNER JOIN Account ON AccountPerson.AccountID=Account.AccountID)
INNER JOIN Address ON AccountPerson.PersonID=Address.PersonID)
INNER JOIN @ygcaddress y ON Account.AccountID=y.AccountID)
INNER JOIN [Lookup] ON Address.StateID=[Lookup].LookupID
WHERE Address.IsActive=1
ELSE
SELECT TOP 1
LEFT(Address.AddressLine1,25),
Address.City,
[Lookup].LookupValue,
Address.Zip
FROM (((AccountPerson INNER JOIN Account ON AccountPerson.AccountID=Account.AccountID)
INNER JOIN Address ON AccountPerson.PersonID=Address.PersonID)
INNER JOIN @ygcaddress y ON Account.AccountID=y.AccountID)
INNER JOIN [Lookup] ON Address.StateID=[Lookup].LookupID
END
END

【问题讨论】:

  • 能否在不丢失问题本质的情况下简化查询?

标签: sql sql-server-2005 select case


【解决方案1】:

试试这个,从我可以看出它在你原来的问题中看起来像一个语法错误:

DECLARE @ygcaddress TABLE
(
    [AccountID] varchar(10),
    [Address1] varchar(25),
    [City] varchar(22),
    [State] varchar(3),
    [Zip] varchar(9)
)

INSERT INTO @ygcaddress
    CASE 

        WHEN Address.IsPrimary=1
        THEN 
        SELECT
        LEFT(Address.AddressLine1,25),
        Address.City,
        [Lookup].LookupValue,
        Address.Zip
        FROM (((AccountPerson INNER JOIN Account ON AccountPerson.AccountID=Account.AccountID)
            INNER JOIN Address ON AccountPerson.PersonID=Address.PersonID)
            INNER JOIN @ygcaddress y ON Account.AccountID=y.AccountID)
            INNER JOIN [Lookup] ON Address.StateID=[Lookup].LookupID
            WHERE Address.IsPrimary=1

        WHEN Address.IsActive=1
        THEN
        SELECT TOP 1
        LEFT(Address.AddressLine1,25),
        Address.City,
        [Lookup].LookupValue,
        Address.Zip
        FROM (((AccountPerson INNER JOIN Account ON AccountPerson.AccountID=Account.AccountID)
            INNER JOIN Address ON AccountPerson.PersonID=Address.PersonID)
            INNER JOIN @ygcaddress y ON Account.AccountID=y.AccountID)
            INNER JOIN [Lookup] ON Address.StateID=[Lookup].LookupID
            WHERE Address.IsActive=1

        ELSE
        SELECT TOP 1
        LEFT(Address.AddressLine1,25),
        Address.City,
        [Lookup].LookupValue,
        Address.Zip
        FROM (((AccountPerson INNER JOIN Account ON AccountPerson.AccountID=Account.AccountID)
            INNER JOIN Address ON AccountPerson.PersonID=Address.PersonID)
            INNER JOIN @ygcaddress y ON Account.AccountID=y.AccountID)
            INNER JOIN [Lookup] ON Address.StateID=[Lookup].LookupID
    END

【讨论】:

    【解决方案2】:

    为案例结果添加了括号,这应该有效(至少在语法上)。

    --Select One best address for Record 02 
    DECLARE @ygcaddress TABLE ( [AccountID] varchar(10), [Address1] varchar(25), [City] varchar(22), [State] varchar(3), [Zip] varchar(9) ) 
    INSERT INTO @ygcaddress 
    SELECT 
    CASE WHEN Address.IsPrimary=1 THEN 
        (
            SELECT LEFT(Address.AddressLine1,25), Address.City, [Lookup].LookupValue, Address.Zip 
            FROM (((AccountPerson INNER JOIN Account ON AccountPerson.AccountID=Account.AccountID) INNER JOIN Address ON AccountPerson.PersonID=Address.PersonID) INNER JOIN @ygcaddress y ON Account.AccountID=y.AccountID) INNER JOIN [Lookup] ON Address.StateID=[Lookup].LookupID WHERE Address.IsPrimary=1 
        )
    ELSE 
        CASE WHEN Address.IsActive=1 THEN 
            (SELECT TOP 1 LEFT(Address.AddressLine1,25), Address.City, [Lookup].LookupValue, Address.Zip FROM (((AccountPerson INNER JOIN Account ON AccountPerson.AccountID=Account.AccountID) INNER JOIN Address ON AccountPerson.PersonID=Address.PersonID) INNER JOIN @ygcaddress y ON Account.AccountID=y.AccountID) INNER JOIN [Lookup] ON Address.StateID=[Lookup].LookupID WHERE Address.IsActive=1 )
        ELSE 
            (SELECT TOP 1 LEFT(Address.AddressLine1,25), Address.City, [Lookup].LookupValue, Address.Zip FROM (((AccountPerson INNER JOIN Account ON AccountPerson.AccountID=Account.AccountID) INNER JOIN Address ON AccountPerson.PersonID=Address.PersonID) INNER JOIN @ygcaddress y ON Account.AccountID=y.AccountID) INNER JOIN [Lookup] ON Address.StateID=[Lookup].LookupID )
        END
    END
    

    【讨论】:

      【解决方案3】:

      如果不深入研究,您似乎至少缺少“VALUES”关键字。 INSERT 语句的框架语法是

      INSERT INTO table_name (column1, column2, column3,...) 值(值 1,值 2,值 3,...)

      我建议在尝试替换任何更复杂的内容之前,使用文字字符串(或其他任何内容)编写正确的 INSERT 语句。

      【讨论】:

        【解决方案4】:

        如果我正确理解了这个问题,我认为您根本不需要 CASE 语句:CASE 语句返回一个值表达式,而不是您尝试创建的元组。 (至少在我最熟悉的 SQL 数据库 SQL Server 中是这样)

        编辑删除我使用 UNION 的第一个建议解决方案,我意识到这是不正确的。

        再次编辑:

        试试这样的:

        INSERT INTO @ygcaddress 
        SELECT
             Account.AccountID
            ,LEFT(Address.AddressLine1,25)
            ,Address.City
            ,[Lookup].LookupValue
            ,Address.Zip 
        FROM AccountPerson 
        INNER JOIN Account ON AccountPerson.AccountID=Account.AccountID
        INNER JOIN 
        (
            SELECT TOP 1 
            A1.*
            CASE 
                WHEN A1.IsPrimary=1 THEN 10
                WHEN A1.IsPrimary=0 AND A1.IsActive=1 THEN 5
                ELSE 1
            END [Rank]
            FROM Address A1
            ORDER BY [Rank] DESC
        ) Address ON AccountPerson.PersonID=Address.PersonID 
        INNER JOIN [Lookup] ON Address.StateID=[Lookup].LookupID 
        

        【讨论】:

          猜你喜欢
          • 2010-09-27
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2010-12-10
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多