需要有关复杂案例陈述SQL 2005的帮助

时间:2011-01-05 21:55:27

标签: sql sql-server-2005 select case

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

逻辑是可靠的,但似乎语法不正确。在关键字'Case','Else','Else'附近的'语法错误'给我错误。 如果isprimary = 1,则想法是将地址插入表中,如果没有主要插入最新的isactive = 1地址,如果没有活动,则插入最新地址。

请帮助


- 为记录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

4 个答案:

答案 0 :(得分:0)

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

--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

答案 1 :(得分:0)

试试这个,从我看到的内容看起来像原始问题中的语法错误:

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 :(得分:0)

如果不进行太多挖掘,你似乎至少缺少“VALUES”关键字。 INSERT语句的框架语法是

INSERT INTO table_name (column1, column2, column3,...)
VALUES (value1, value2, value3,...)

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

答案 3 :(得分:0)

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

编辑删除我使用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