简体   繁体   English

SQL查询中的多个嵌套SELECT

[英]Multiple nested SELECTs in SQL Query

I'm creating a query to return one number: YTD as followed: 我正在创建一个查询以返回一个数字: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 ;

This works perfect, but when I add another part to return another number YTD and thisMonth: 这很完美,但是当我添加另一部分以返回另一个数字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

Even if I simplify this to 即使我简化为

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

I'm getting a syntax error neer select COUNT(Items.ItemID) As thisMonth 我收到一个语法错误,需要选择COUNT(Items.ItemID)作为thisMonth

it seems that the syntax is wrong. 看来语法是错误的。 Any idea on how to do this? 关于如何执行此操作的任何想法?

Most probably, you are getting aggregate error. 最有可能的是,您遇到了汇总错误。 Add thisMonth in Group By Group By thisMonth中添加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 

I think you can simplify this using conditional aggregation: 我认为您可以使用条件聚合来简化此操作:

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

I don't see why a group by in the outer query is needed as the inner query will only return a single row anyway. 我不明白为什么需要在外部查询中使用group by ,因为内部查询无论如何只会返回一行。

Thank you all. 谢谢你们。 It was an issue with the parentheses. 这是括号的问题。 I was able to fix this. 我能够解决这个问题。

And yes, I don't really need the group by clause. 是的,我真的不需要group by子句。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM