Sql - 如何计算2个单独的子查询?

时间:2018-03-23 16:37:22

标签: sql sql-server subquery

我对SQL没有最好的理解,尽力学习。我已经构建了两个单独的查询,我希望从查询1中获取结果并将其除以查询2.

查询1(结果为114)

select count(*) as Numerator

from (

SELECT   *,  
           DATEDIFF(SECOND, xx, yy) AS SecondDiff, 
           DATEDIFF(DAY, xx, yy) AS DayDiff

FROM database1.dbo.tbl1

WHERE  month(datecompleted) = month(dateadd(month,-1,current_timestamp))
       and year(datecompleted) = year(dateadd(month,-1,current_timestamp))
       and DATEDIFF(DAY, xx,yy) <= 15

)t1

查询2(结果为131)

select count(*) as Denominator

from(

SELECT   *,  
           DATEDIFF(SECOND, xx, yy) AS SecondDiff, 
           DATEDIFF(DAY, xx, yy) AS DayDiff

FROM database1.dbo.tbl1

WHERE  month(datecompleted) = month(dateadd(month,-1,current_timestamp))
       and year(datecompleted) = year(dateadd(month,-1,current_timestamp))


)t2 

现在我如何从查询1中获取结果并将其除以查询2并将最终结果设为(列名:Total_X)?我曾尝试使用union,cross join,cross apply但我似乎无法获得有效的查询!

非常感谢每一条提示和建议

由于

4 个答案:

答案 0 :(得分:1)

我认为你正在寻找这个:

SELECT AVG(CASE WHEN DATEDIFF(DAY, xx,yy) <= 15 THEN 1.0 ELSE 0 END)
FROM database1.dbo.tbl1
WHERE month(datecompleted) = month(dateadd(month,-1,current_timestamp)) AND
      year(datecompleted) = year(dateadd(month,-1,current_timestamp));

您还可以将WHERE子句简化为:

SELECT AVG(CASE WHEN DATEDIFF(DAY, xx,yy) <= 15 THEN 1.0 ELSE 0 END)
FROM database1.dbo.tbl1
WHERE datecompleted < DATEADD(day, 1 - DAY(current_timestamp), CONVERT(DATE, datecompleted)) AND
      datecompleted >= DATEADD(month, 1, DATEADD(day, 1 - DAY(current_timestamp), CONVERT(DATE, datecompleted))) AND

此版本可以使用datecompleted上的索引。

答案 1 :(得分:1)

您可以在两个查询之间使用CROSS JOIN,例如:

  select TT1.Numerator/ TT2.Denominator
  from (
    select count(*) as Numerator

  from (

  SELECT   *,  
             DATEDIFF(SECOND, xx, yy) AS SecondDiff, 
             DATEDIFF(DAY, xx, yy) AS DayDiff

  FROM database1.dbo.tbl1

  WHERE  month(datecompleted) = month(dateadd(month,-1,current_timestamp))
         and year(datecompleted) = year(dateadd(month,-1,current_timestamp))
         and DATEDIFF(DAY, xx,yy) <= 15

  )t1) TT1 
  CROSS JOIN ( 

  select count(*) as Denominator

  from(

  SELECT   *,  
             DATEDIFF(SECOND, xx, yy) AS SecondDiff, 
             DATEDIFF(DAY, xx, yy) AS DayDiff

  FROM database1.dbo.tbl1

  WHERE  month(datecompleted) = month(dateadd(month,-1,current_timestamp))
         and year(datecompleted) = year(dateadd(month,-1,current_timestamp))


  )t2 ) TT2

可能与整数除法有关,尝试简单的转换

select cast(TT1.Numerator AS DECIMAL(9,2))/CAST( TT2.Denominator AS DECIMAL(9.2))
  from (
    select count(*) as Numerator

  from (

  SELECT   *,  
             DATEDIFF(SECOND, xx, yy) AS SecondDiff, 
             DATEDIFF(DAY, xx, yy) AS DayDiff

  FROM database1.dbo.tbl1

  WHERE  month(datecompleted) = month(dateadd(month,-1,current_timestamp))
         and year(datecompleted) = year(dateadd(month,-1,current_timestamp))
         and DATEDIFF(DAY, xx,yy) <= 15

  )t1) TT1 
  CROSS JOIN ( 

  select count(*) as Denominator

  from(

  SELECT   *,  
             DATEDIFF(SECOND, xx, yy) AS SecondDiff, 
             DATEDIFF(DAY, xx, yy) AS DayDiff

  FROM database1.dbo.tbl1

  WHERE  month(datecompleted) = month(dateadd(month,-1,current_timestamp))
         and year(datecompleted) = year(dateadd(month,-1,current_timestamp))


  )t2 ) TT2

答案 2 :(得分:0)

  1. 您可以将行转换为列并进行分割。 假设你的第一个查询是 从双重中选择1个值,'nm'类型; 第二个查询是 从双
  2. 中选择2值,'dm'类型

    将rwos转换为列并除以

      

    选择sum(decode(type,'nm',value))分子,
      sum(decode(type,'dm',value))分母,   总和(解码(类型, '纳米',值))/ SUM(解码(类型, 'DM',值))   divide_value from(选择1值,'nm'类型来自双联盟
      从1,2

    中选择2个值,'dm'类型来自双重)

    或直接

      select 1/(select 2 from dual) from dual;
    

答案 3 :(得分:0)

另一种方法是使用计算表表达式。

with numerator
as (
select count(*) n1
from someTable
) 
, denominator as
(select count(*) n2
from anotherTable)

select n1 / n2
from numerator, denominator
where n2 > 0

请注意

from numerator, denominator

实际上是一个交叉连接,正如@scaisEdge

所建议的那样

另请注意,您的实际查询看起来比必要的更复杂。