1

我正在使用 SQL Server 2008,并且有两个单独的 Count 查询。我想将这两个查询相除并乘以 100 得到一个百分比。

两个查询

SELECT COUNT(*) FROM
 (
     SELECT * FROM admin WHERE principal LIKE '%Mrs%'
     UNION
     SELECT * FROM admin where deputy_principal LIKE '%Mrs%'
) as Count1

SELECT COUNT(*) * 2 FROM admin as Count2

我尝试过这样的事情:

SELECT(
         SELECT COUNT(*) FROM
                  (
                       SELECT * FROM admin WHERE principal LIKE '%Mrs%'
                       UNION
                       SELECT * FROM admin where deputy_principal LIKE '%Mrs%'
                   )
             /

                  (
                       SELECT COUNT(*) * 2 FROM admin as Count2
                  )
       )

但是我得到了无法正确调试的语法错误。哪种方法是正确的?

它应该将 Count1 和 Count2 相除,然后乘以 100 并显示如下:

37%
4

7 回答 7

3
SELECT
    ((SELECT CAST(COUNT(*) AS DECIMAL(18,2)) FROM admin WHERE principal LIKE '%Mrs%' 
         OR deputy_principal LIKE '%Mrs%') / 
    (SELECT CAST(COUNT(*) * 2 AS DECIMAL(18,2)) FROM admin)) * 100

UDPATE:使用虚拟数据与原始方法进行比较......

DECLARE @admin TABLE(Id INTEGER IDENTITY(1,1), principal VARCHAR(50), 
    deputy_principal VARCHAR(50))
INSERT @admin (principal, deputy_principal)
VALUES ('Mr Person 1', 'Mr Person 2'),
        ('Mrs Person 3', 'Mr Person 4'),
        ('Mr Person 5', 'Mrs Person 6'),
        ('Mrs Person 7', 'Mrs Person 8')

-- Original attempted way (syntax corrected)        
SELECT ((
    SELECT CAST(COUNT(*) AS DECIMAL(18,2))
    FROM
    (
       SELECT * FROM @admin WHERE principal LIKE '%Mrs%'
       UNION
       SELECT * FROM @admin where deputy_principal LIKE '%Mrs%'
   ) x)
   /
   (SELECT CAST(COUNT(*) * 2 AS DECIMAL(18,2)) FROM @admin)) * 100

-- Shortened query      
SELECT
((SELECT CAST(COUNT(*) AS DECIMAL(18,2)) FROM @admin WHERE principal LIKE '%Mrs%' 
     OR deputy_principal LIKE '%Mrs%') / 
(SELECT CAST(COUNT(*) * 2 AS DECIMAL(18,2)) FROM @admin)) * 100

两者都输出相同的结果:在此示例中为 37.5

于 2013-11-12T10:26:08.540 回答
2

您可以尝试以下使用CASE Expressionand SUM() Function

SELECT  SUM(CASE WHEN principal LIKE '%Mrs%' OR 
                 deputy_principal LIKE '%Mrs%' THEN 100.0 END) / SUM(2) Pecentage
FROM admin

使用 @AdaTheDev 的数据进行小提琴演示

于 2013-11-12T11:12:59.227 回答
1

你可以像这样一次性完成所有事情,在这里摆弄

 SELECT
    CONVERT(
        VARCHAR(10),
        (
            CONVERT(Real, SUM(Mrs))
        /
            CONVERT(Real, COUNT(*))
        ) * 100
    ) + '%'
    [DoubledUpMrsPercentage]
FROM (
    SELECT
        CASE
            WHEN [principal] LIKE '%Mrs%' OR
                [deputy_principal] LIKE '%Mrs%' THEN 1
            ELSE 0
        END Mrs
    FROM
        [admin]
      ) Flags;
于 2013-11-12T10:57:38.770 回答
1
select 
cast(
  sum(case when principal LIKE '%Mrs%' then 50.0 else 0 end +
      case when deputy_principal LIKE '%Mrs%' then 50.0 else 0 end)
  / count(*)
 as decimal(5,2)) as percnt
from admin
于 2013-11-12T10:58:26.267 回答
1
Select Cast(a*100 as int)/b
from (
select count(*) a
from ERP_FMS_ChartOfAccount where CategoryID = 2)t1,
(
Select count(*) b
From ERP_FMS_ChartOfAccount where CategoryID = 1)t2
于 2013-11-12T11:02:22.790 回答
0
declare @firstquerycount as integer;
declare @secondquerycount as integer;

SELECT @firstquerycount = COUNT(*) FROM admin WHERE principal LIKE '%Mrs%';
SELECT @secondquerycount = COUNT(*) FROM admin WHERE deputy_principal LIKE '%Mrs%';

现在 firstquerycount 和 secondquerycount 保存相应查询的相应计数值。您可以根据您的要求使用它们,例如

select convert(varchar(10),(((@firstquerycount+@secondquerycount)/2)*100))+'%'

注意我没有尝试这个查询 - 请根据您的要求进行更改

于 2013-11-12T10:37:52.267 回答
0
SELECT princi, total, (princi*100)/CAST(total AS REAL)
FROM
(SELECT COUNT(CASE WHEN principal LIKE '%Mrs%' 
 OR deputy_principal LIKE '%Mrs%'
 THEN 1 END), COUNT(*)*2
FROM admin) AS X(princi,total)
于 2013-11-12T10:57:12.253 回答