SQL Query中的多个嵌套SELECT

时间:2016-11-24 06:50:58

标签: sql select

我正在创建一个返回一个数字的查询:YTD如下:

SELECT  TableTemp.YTD       
FROM(   
    SELECT(( select COUNT(Items.ItemID) * 100 FROM Items WHERE Items.StatusID in (5,7,11) and year(Items.Close_Date) = 2016) / ( select COUNT(Items.ItemID)  FROM Items WHERE Items.StatusID in (5,7,11,6) and year(Items.Close_Date) = 2016)  )As YTD
) AS TableTemp                                                                                                           
GROUP BY TableTemp.YTD ;

这很完美,但当我添加另一部分以返回另一个数字YTD和thisMonth时:

SELECT  TableTemp.YTD  ,   TableTemp.thisMonth   
FROM(   

    (select(( select COUNT(Items.ItemID) * 100 FROM Items WHERE Items.StatusID in (5,7,11) and year(Items.Close_Date) = 2016) / ( select COUNT(Items.ItemID)  FROM Items WHERE Items.StatusID in (5,7,11,6) and year(Items.Close_Date) = 2016)) As YTD,
    (select ((select COUNT(Items.ItemID) * 100 FROM Items WHERE Items.StatusID in (5,7,11) and year(Items.Close_Date) = 2016 and month(Items.Close_Date) = 11) / ( select COUNT(Items.ItemID)  FROM Items WHERE Items.StatusID in (5,7,11,6) and year(Items.Close_Date) = 2016 and month(Items.Close_Date) = 11)) As thisMonth
) AS TableTemp                                                                                                           
GROUP BY TableTemp.YTD

即使我将其简化为

SELECT  TableTemp.YTD  ,   TableTemp.thisMonth   
FROM(   

     select COUNT(Items.ItemID) As YTD
  ,  select COUNT(Items.ItemID) As thisMonth
) AS TableTemp                                                                                                           
GROUP BY TableTemp.YTD, TableTemp.thisMonth

我收到语法错误neer select COUNT(Items.ItemID)as thisMonth

似乎语法错误。有关如何做到这一点的任何想法?

3 个答案:

答案 0 :(得分:0)

最有可能的是,您收到了汇总错误。在thisMonth

中添加Group By
SELECT  TableTemp.YTD, TableTemp.thisMonth   
FROM(   
    (select(( select COUNT(Items.ItemID) * 100 FROM Items WHERE Items.StatusID in (5,7,11) and year(Items.Close_Date) = 2016) / ( select COUNT(Items.ItemID)  FROM Items WHERE Items.StatusID in (5,7,11,6) and year(Items.Close_Date) = 2016)) As YTD,
    (select ((select COUNT(Items.ItemID) * 100 FROM Items WHERE Items.StatusID in (5,7,11) and year(Items.Close_Date) = 2016 and month(Items.Close_Date) = 11) / ( select COUNT(Items.ItemID)  FROM Items WHERE Items.StatusID in (5,7,11,6) and year(Items.Close_Date) = 2016 and month(Items.Close_Date) = 11)) As thisMonth
) AS TableTemp                                                                                                           
GROUP BY TableTemp.YTD, TableTemp.thisMonth 

答案 1 :(得分:0)

我认为你可以使用条件聚合来简化这个:

select selected_item_count * 100 / all_item_count, thismonth
FROM (
  SELECT count(case when Items.StatusID in (5,7,11) then 1 end) as selected_item_count, 
         count(case when Items.StatusID in (5,7,11,6) then 1 end) as all_item_count, 
         count(case when Items.StatusID in (5,7,11,6) and month(Items.Close_Date) = 11 then 1 end) As thisMonth
  FROM items
  WHERE Items.StatusID in (5,7,11,6) 
    and year(Items.Close_Date) = 2016
) t

我不明白为什么外部查询中需要group by,因为内部查询无论如何都只返回一行。

答案 2 :(得分:0)

谢谢大家。这是括号的问题。我能解决这个问题。

是的,我真的不需要group by子句。