简体   繁体   English

在 SQL Azure“导入数据”中对表进行分组时编写数学公式

[英]Writing a Mathematical Formula when grouping table in SQL Azure "Import data"

This query is working fine此查询工作正常

select OrderForSeries.memberid, 
       Orderforseries.seriesid  ,
       log(count(orderforcontent.id)) as rating,
       Series.Mainauthorid, 
       series.Likecount, 
       series.viewcount
 from series join  OrderForSeries on OrderForSeries.SeriesId=Series.id
 join orderforcontent on orderforcontent.OrderForSeriesid=OrderForSeries.id
 group by  OrderForSeries.memberid, orderforseries.seriesid  ,
      Series.Mainauthorid, series.Likecount, series.viewcount 
 order by OrderForSeries.seriesid

but when I change "series.viewcount" in third row to log(series.viewcount) I got error.但是当我将第三行中的“series.viewcount”更改为log(series.viewcount)时出现错误。 I consider that this because of GRUOP by query.我认为这是因为 GRUOP by query。 How can I use log function here?我如何在这里使用日志功能?

I am using Microsoft Azure 'import data' But When I use Visual studio it works fine我正在使用 Microsoft Azure“导入数据”但是当我使用 Visual Studio 时它工作正常

And this is error :这是错误

requestId = 96da06356e5d42b6bf5aede3170f718f errorComponent=Module.
taskStatusCode=400. 
{"Exception":
    { "ErrorId":"InvalidSQLScript",
      "ErrorCode":"0069",
      "ExceptionType":"ModuleException",
      "Message":"Error 0069: SQL query \" 
        select top 75000
          OrderForSeries.memberid, 
          Orderforseries.seriesid ,
          log(count(orderforcontent.id)) as rating,
          Series.Mainauthorid, 
          series.Likecount,
          log(series.viewcount)
        from series 
        join OrderForSeries on OrderForSeries.SeriesId=Series.id
        join orderforcontent on orderforcontent.OrderForSeriesid=OrderForSeries.id
        group by OrderForSeries.memberid, orderforseries.seriesid ,
          Series.Mainauthorid, series.Likecount, series.viewcount
        order by OrderForSeries.seriesid
    " is not correct:
    ??? ?? ??? ??? ??????.
    Please double check your query against your database."
    }
}
Error: Error 0069: 
  SQL query " .... " is not correct:
  ??? ?? ??? ??? ??????.
  Please double check your query against your database. 
Process exited with error code -2 

You should add log function behind GROUP BY .您应该在GROUP BY后面添加log功能。 Most of the database systems requires that the attributes or expressions outside the aggregate function needs to be behing GROUP BY and behind SELECT as well大多数数据库系统要求聚合函数之外的属性或表达式需要在GROUP BY之后,也需要在SELECT之后

select OrderForSeries.memberid, 
       Orderforseries.seriesid  ,
       log(count(orderforcontent.id)) as rating,
       Series.Mainauthorid, 
       series.Likecount, 
       log(series.viewcount)
 from series join  OrderForSeries on OrderForSeries.SeriesId=Series.id
 join orderforcontent on orderforcontent.OrderForSeriesid=OrderForSeries.id
 group by  OrderForSeries.memberid, 
       Orderforseries.seriesid  ,
       Series.Mainauthorid, 
       Series.Likecount, 
       log(Series.viewcount)
 order by OrderForSeries.seriesid

Not sure what DBMS you're using - MS SQL was able to handle this.不确定您使用的是什么 DBMS - MS SQL 能够处理这个问题。

In any case, you've got two options if your DB can't perform operations like log() on grouped-by fields.在任何情况下,如果您的数据库无法对分组字段执行 log() 之类的操作,您有两种选择。

Option A - Use a Subquery选项 A - 使用子查询

Nothing's stopping you from writing没有什么能阻止你写作

select fieldA, fieldB, log(fieldC)
from
(
    select fieldA, fieldB, fieldC from somewhere
) as subQ

... in other words, use a subquery to generate your raw data, and then use a higher-level query to perform the math you want to do on it. ...换句话说,使用子查询生成您的原始数据,然后使用更高级别的查询来执行您想要对其执行的数学运算。

Option B - change the Group-By to Log() as well.选项 B - 也将 Group-By 更改为 Log()。

If the engine doesn't allow you to perform functions on a grouped-by field, you can always add the function into the group by clause.如果引擎不允许您在分组依据字段上执行函数,您始终可以将该函数添加到分组依据子句中。

Select fieldA, log(fieldB)
from somewhere
group by fieldA, log(fieldB)

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

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