[英]Trying to use UNION on multiple queries but won't work because of my subquery uses AVG
I'm trying to basically run 6 select queries(displayed two for the sake of readability but its basically that same pattern) and using union to create a single output.我正在尝试基本上运行 6 个 select 查询(为了便于阅读,显示了两个,但其模式基本相同)并使用联合创建单个 output。 However, when I run the query, I get 2 of the following errors.
但是,当我运行查询时,出现以下 2 个错误。
I understand and have looked at similar questions but nothing seems to work.我理解并看过类似的问题,但似乎没有任何效果。
I would like to have the output look like this我想让 output 看起来像这样
|Tabe|Rent|Thd|
---------------------------------
|table1 | 9999 | 8888 |
|table2 | 9999 | 8888 |
Any suggestions or direction would be greatly appreciated!任何建议或方向将不胜感激!
I don't see why you need variables at all here.我不明白为什么你在这里根本需要变量。 You need an
OVER
clause to calculate the average over all the rows:您需要一个
OVER
子句来计算所有行的平均值:
Select
'table1' as TableName,
count(*) as RecordCount,
0.75 * AVG(count(*)) over ()
from table1
where @something = specificDate
group by specificDate
union all
Select
'table2',
count(*) as RecordCount,
0.75 * AVG(count(*)) over ()
from table2
where @something = specificDate
group by specificDate;
I note that your query appears to be filtering on specificdate
.我注意到您的查询似乎正在过滤
specificdate
。 SO you can just group by the empty set: group by ()
所以你可以只按空集分组:
group by ()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.