存储过程逻辑错误

时间:2011-08-26 19:03:53

标签: sql tsql stored-procedures sql-server-2000

我正在尝试做类似的事情 How can I improve this SQL query?

然而,在那里有逻辑 基本上我有一个ID列表,可能有多个与之关联的值,值为Yes,No或其他字符串。 对于ID x,如果任何值为“是”,则x应为“是”,如果它们全部为“否”,则应为“否”,如果它们包含任何其他值但是“是”,则显示该值。我只希望每个ID返回1行,没有重复。

但是在我的逻辑中,如果所有值都是,则显示yes,如果所有值都为no则显示no,如果它是yes,no或其他任何显示混合的混合

DECLARE @tempTable table ( ID int, Val varchar(1) )

    INSERT INTO @tempTable ( ID, Val ) VALUES ( 10, 'Y')
    INSERT INTO @tempTable ( ID, Val ) VALUES ( 11, 'N')
    INSERT INTO @tempTable ( ID, Val ) VALUES ( 11, 'N')
    INSERT INTO @tempTable ( ID, Val ) VALUES ( 12, 'Y')
    INSERT INTO @tempTable ( ID, Val ) VALUES ( 12, 'Y')
    INSERT INTO @tempTable ( ID, Val ) VALUES ( 12, 'Y')
    INSERT INTO @tempTable ( ID, Val ) VALUES ( 13, 'N')
    INSERT INTO @tempTable ( ID, Val ) VALUES ( 14, 'Y')
    INSERT INTO @tempTable ( ID, Val ) VALUES ( 14, 'N')
    INSERT INTO @tempTable ( ID, Val ) VALUES ( 15, 'Y')
    INSERT INTO @tempTable ( ID, Val ) VALUES ( 16, 'Y')
    INSERT INTO @tempTable ( ID, Val ) VALUES ( 17, 'F')
    INSERT INTO @tempTable ( ID, Val ) VALUES ( 18, 'P')


    SELECT DISTINCT t.ID, COALESCE(t2.Val, t3.Val, t4.Val)
    FROM @tempTable t
    LEFT JOIN
    (
        SELECT ID, Val
        FROM @tempTable
        WHERE Val = 'Y'
    ) t2 ON t.ID = t2.ID
    LEFT JOIN
    (
        SELECT 
        ID, Val FROM @tempTable
        WHERE Val = 'N'
    ) t3 ON t.ID = t3.ID
    LEFT JOIN
    (
        SELECT ID, Val
        FROM @tempTable
        WHERE Val <> 'Y' AND Val <> 'N'
    ) t4 ON t.ID = t4.ID

Update dbo.households
SET dbo.households.code = #TempTable.code
FROM #TempTable
WHERE dbo.households.id = #TempTable.id 

3 个答案:

答案 0 :(得分:5)

SELECT  ID ,
        CASE WHEN MAX(Val) = MIN(Val)
                  AND MAX(Val) IN ( 'N', 'Y' ) THEN MAX(Val)
             ELSE 'M'
        END
FROM    @tempTable
GROUP BY ID

如果任何存在,则返回Y的版本,而不是 all <{1}}

Y

或最终版本,字面意思是“如果它们包含任何其他值,但是是和否,则显示该值。”言。

SELECT  ID ,
        ISNULL(MAX(CASE WHEN Val = 'Y' THEN 'Y' END), 
               CASE WHEN MAX(Val) = MIN(Val) THEN MAX(Val)
                              ELSE 'M'
                         END)
FROM    @tempTable
GROUP BY ID

答案 1 :(得分:2)

您可以使用max(),因为它是char值。给这个旋转:

注意:这里采用的逻辑是Non-Y / N&gt;任何Y>只有N

[编辑] 重构&amp;简化为:

select
 t.ID
,isnull(max(nyn.Val),max(yn.Val)) as Val
from @tempTable as t    
    left join @tempTable as nyn -- Non-Y/N value
    on nyn.ID = t.ID
    and nyn.Val not in ('Y','N')
    left join @tempTable as yn -- Y/N value
    on yn.ID = t.ID
    and yn.Val in ('Y','N')
group by t.ID

产生以下结果:

ID  Val
10  Y
11  N
12  Y
13  N
14  Y
15  Y
16  Y
17  F
18  P

答案 2 :(得分:0)

另一种方法。我使用MAX()以防止除Y和N之外的多个值返回

SELECT  
  ID,
  CASE  
    WHEN EXISTS(SELECT 1 FROM @tempTable tt WHERE t.ID = tt.ID AND tt.Val = 'Y')
             THEN 'Y'
    WHEN EXISTS(SELECT 1 FROM @tempTable tt WHERE t.ID = tt.ID AND tt.Val NOT IN ('Y','N')) 
             THEN (SELECT MAX(Val) FROM @tempTable  tt WHERE t.ID = tt.ID AND tt.Val NOT IN ('Y','N'))
    ELSE 'N'
  END           
FROM    @tempTable t
GROUP BY ID