SQL MAX值为两个子查询

时间:2017-07-11 08:53:02

标签: sql sql-server

我有两个查询,我想获得其中两个的最大值。

myString.Contains(".all")

3 个答案:

答案 0 :(得分:2)

您甚至可以在一个查询中执行此操作:

SELECT
    CASE WHEN SUM(CASE WHEN HasHuman=0 THEN 1 ELSE 0 END) >
              SUM(CASE WHEN HasHuman=1 THEN 1 ELSE 0 END)
         THEN SUM(CASE WHEN HasHuman=0 THEN 1 ELSE 0 END)
         ELSE SUM(CASE WHEN HasHuman=1 THEN 1 ELSE 0 END) END
FROM [dbo].[Table]
WHERE ItemID IS NOT NULL    -- you were not counting NULLs

答案 1 :(得分:1)

SELECT MAX(RC) 
 FROM (SELECT COUNT(p.ItemID) AS RC FROM dbo.[Table]
       WHERE HasHuman=0
       UNION 
       SELECT COUNT(p.ItemID) AS RC FROM dbo.[Table]
       WHERE HasHuman=1
       ) A

答案 2 :(得分:1)

您可以在单个查询中计算两个结果,然后应用TOP:

select top 1 
   HasHuman,
   COUNT(p.[ItemID]) as cnt
from [dbo].[Table]
group by HasHuman 
order by cnt desc