简体   繁体   English

通过在MsSQL查询中使用某些操作来面对问题

[英]Facing issue by using some operation in MsSQL query

i am new to Mssql server i am trying to migrate Mysql queries to Mssql on the process facing issue with this query. 我是Mssql服务器的新手,我正在尝试将Mysql查询迁移到Mssql,因为该查询面临问题。

query is: 查询是:

SELECT  converT(datetime,SWITCHOFFSET(converT(datetimeoffset,created), 'America/New_York')),  
        SUM(case when channel='FACEBOOK' then 1 else 0 end) AS  Messenger,
        SUM(case when channel='EMAIL' then 1 else 0 end) AS  Email,  
        SUM(case when channel='ECHO' then 1 else 0 end) AS Echo  
FROM   Conversation  
WHERE tenant_id=2 AND createdBy_id in(3) 
AND   created >= '2018-11-01 06:00:01'  
GROUP BY year(created), month(created),  DATEPART(week,created),day(created) 
ORDER BY created ASC;

And the error which i am facing is: 我面临的错误是:

Column Conversation.created is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause. Conversation.created在选择列表中无效,因为它既不包含在聚合函数中,也不包含在GROUP BY子句中。

When I am trying to execute to the query without aggregate function its working the combination of converT and sum is not working. 当我尝试在没有聚合函数的情况下执行查询时, converTsum的组合不起作用。 Can anybody help me out this. 谁能帮我这个忙。

Thanks in advance 提前致谢

It is necessary to add this converT(datetime,SWITCHOFFSET(converT(datetimeoffset,created) in Group By as you already grouped other fields and you want to show this result in Group By : 有必要添加此converT(datetime,SWITCHOFFSET(converT(datetimeoffset,created)Group By ,你已经分组等领域,并要显示这样的结果在Group By

SELECT  
    converT(datetime,SWITCHOFFSET(converT(datetimeoffset,created), 'America/New_York')),  
    SUM(case when channel='FACEBOOK' then 1 else 0 end) AS  Messenger,
    SUM(case when channel='EMAIL' then 1 else 0 end) AS  Email,  
    SUM(case when channel='ECHO' then 1 else 0 end) AS Echo  
FROM   Conversation  
WHERE tenant_id=2 AND createdBy_id in(3) 
AND   created >= '2018-11-01 06:00:01'  
GROUP BY converT(datetime,SWITCHOFFSET(converT(datetimeoffset,created), year(created), 
    month(created),  DATEPART(week,created),day(created) 
ORDER BY created ASC;

This error means that you've got SUM of channel field - one row, but SQL Server has also many rows with created column and it does not know what row should be chosen. 该错误意味着您拥有channel字段的SUM -一行,但是SQL Server也有很多行具有created列,并且它不知道应该选择哪一行。

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

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