【问题标题】:Count number of row from subquerry which have data with groupby clause计算子查询中包含 group by 子句的数据的行数
【发布时间】:2022-11-02 18:23:40
【问题描述】:

下面是我的子查询:

Q1:

select count(a.ProcessDate),
        b.Market
from [dbo].[FileProcessLog] a
     LEFT JOIN [dbo].[FileMaster] b ON a.FileID = b.FileID
where Convert(date, a.ProcessDate) = Convert(date, getdate()-2)
GROUP BY b.Market

现在我想要结果表的行数,但是当我使用下面的查询时出现错误:

select count(*)
from (select count(a.ProcessDate),
             b.Market
      from [dbo].[FileProcessLog] a
      LEFT JOIN  [dbo].[FileMaster] b ON a.FileID = b.FileID
      where Convert(date, a.ProcessDate) = Convert(date, getdate()-2) 
      GROUP BY b.Market)

我也尝试过使用 alisa 名称,但它根本没有用。请帮助寻找解决方案。

【问题讨论】:

  • 下面的解决方案有效吗?
  • “我有错误”什么那是错误吗?
  • 另外,养成使用的习惯好的别名。 “a”不是“文件处理日志”,“B”不是“文件主控”;两者都不的对象甚至具有您在其名称中为它们起别名的字母。 FPLFM 会让远的更好的别名。 Bad Habits to Kick : Using table aliases like (a, b, c) or (t1, t2, t3)
  • @Larnu 错误是什么?

标签: sql sql-server subquery


【解决方案1】:
with main as (
select 
b.Market,
count(a.ProcessDate) as total
from [dbo].[FileProcessLog] a 
LEFT JOIN  [dbo].[FileMaster] b 
ON a.FileID = b.FileID 
where Convert(date, a.ProcessDate) = Convert(date, getdate()-2) 
GROUP BY b.Market 
)

select count(*) from main

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-05
    • 2015-10-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多