SQL选择value ='X'或min值的位置

时间:2012-07-14 01:17:19

标签: sql sql-server-2008

我有一个庞大的客户数据库,客户表中包含人员及其与组织的关系。人们可能属于多个组织(这允许组织内的部门)。拥有多个组织的人员必须拥有默认组织,这通常由isDefault = T确定,但前端应用程序也可以通过选择isDefault = F来确定MIN(RowID)的默认应用程序。

因此,在下表中,我们知道PersonId 3的默认OrgID11(IsDefault = T) 但是,我需要找出查找PersonID 12默认值的查询。 即

Select orgId as default from myTable
where personID = 12
and isDefault = 'T'

如果返回0行,则执行如下查询:

Select orgId as default from myTable
where personID = 12
and 
RowId in (select Min(rowId) 
from myTable 
where PersonId = 12)


RowID | PersonID  | OrgId  |  isDefault
1     |    12     |   14   |     F
2     |    12     |   17   |     F
3     |    3      |   11   |     T
4     |    3      |   14   |     F

6 个答案:

答案 0 :(得分:2)

我认为您可以通过两次订购得到您想要的东西,首先是IsDefault是T还是F,然后是RowId,并取得最佳结果:

SELECT TOP 1 OrgId as [Default]
FROM MyTable
WHERE PersonId = 12
ORDER BY
    CASE WHEN IsDefault = 'T' THEN 0 ELSE 1 END,
    RowId

答案 1 :(得分:1)

在MySql服务器中,您可以使用限制1:

进行联合
SELECT OrgId AS `default`
FROM (
    SELECT OrgId FROM MyTable
    WHERE PersonID = 12
    and isDefault = 'T'

    UNION

    SELECT OrgId FROM MyTable
    WHERE PersonId = 12
    AND RowId in (
        SELECT MIN(RowId)
        FROM MyTable
        WHERE PersonId = 12
    )
) SomeName LIMIT 1;

对于SQL Server:

SELECT TOP 1 OrgId AS [default]
FROM (
    SELECT OrgId FROM MyTable
    WHERE PersonID = 12
    and isDefault = 'T'

    UNION

    SELECT OrgId FROM MyTable
    WHERE PersonId = 12
    AND RowId in (
        SELECT MIN(RowId)
        FROM MyTable
        WHERE PersonId = 12
    )
) SomeName;

答案 2 :(得分:1)

没有尝试执行此操作,但期望这应该有效。 IsDefault会使子查询短路。

  Select orgId as default from myTable o
   where personID = 12 
       and  
       ( IsDefault = 'T' OR
                   RowId = (select Min(rowId)  
                               from myTable i
                             where i.PersonId = o.PersonId
                           )  
        )

答案 3 :(得分:0)

将此逻辑放入一个查询中相当复杂。您必须将所有结果合并在一起,跟踪结果的来源。然后,您必须计算来自第一部分的行数,并且仅保留适当结果集中的行。

select orgid, mytable
from (select t.*, sum(which) over (partition by null) as whichcnt
      from ((Select 1 as which, orgId as default
             from myTable
             where personID = 12 and
                   isDefault = 'T'
            )
            union all
           (Select 0 as which, orgId as default
            from myTable
            where personID = 12 and 
                  RowId in (select Min(rowId) from myTable where PersonId = 12)
           )
          ) t
     ) t
where whichcnt > 0 and which = 1 or whichcnt = 0

如果您可以使用TSQL而不是单个查询,那么您可以运行第一个版本,检查结果中的行数,然后确定要采用的结果集。

答案 4 :(得分:0)

请试试这个

select PersonID ,OrgID from mytable where isDefault='T'
union all

select personID,min(OrgID) [default]
from myTable P
where  not exists 
(select * from mytable C where P.PersonID=C.PersonID and C.isDefault='T')
group by personID

答案 5 :(得分:0)

这取决于表的大小是什么索引。如果说rowid具有唯一的聚簇索引 and personid和isdefault有一个nc索引我将使用两个状态。

 declare @orgid int = null
 select @orgid =orgid from table where personid = 12 and isdefault = 'T'
 if (orgid  is null)
 begin
 select @orgid = orgid 
 from mytable
 where rowid = (
 select min(rowid)from table where personid = 12 and isdefault = 'F'
 )
 end