Facing issue by using some operation in MsSQL query












1















i am new to Mssql server i am trying to migrate Mysql queries to Mssql on the process facing issue with this query.



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.



When I am trying to execute to the query without aggregate function its working the combination of converT and sum is not working. Can anybody help me out this.



Thanks in advance










share|improve this question

























  • rule of thumb, whatever column not in aggregate need to be in the GROUP BY clause. Meaning you need to add this converT(datetime,SWITCHOFFSET(converT(datetimeoffset,created), 'America/New_York')) to the GROUP BY

    – Squirrel
    Nov 22 '18 at 6:46


















1















i am new to Mssql server i am trying to migrate Mysql queries to Mssql on the process facing issue with this query.



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.



When I am trying to execute to the query without aggregate function its working the combination of converT and sum is not working. Can anybody help me out this.



Thanks in advance










share|improve this question

























  • rule of thumb, whatever column not in aggregate need to be in the GROUP BY clause. Meaning you need to add this converT(datetime,SWITCHOFFSET(converT(datetimeoffset,created), 'America/New_York')) to the GROUP BY

    – Squirrel
    Nov 22 '18 at 6:46
















1












1








1








i am new to Mssql server i am trying to migrate Mysql queries to Mssql on the process facing issue with this query.



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.



When I am trying to execute to the query without aggregate function its working the combination of converT and sum is not working. Can anybody help me out this.



Thanks in advance










share|improve this question
















i am new to Mssql server i am trying to migrate Mysql queries to Mssql on the process facing issue with this query.



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.



When I am trying to execute to the query without aggregate function its working the combination of converT and sum is not working. Can anybody help me out this.



Thanks in advance







sql-server






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 22 '18 at 7:38









fewlinesofcode

2,0571818




2,0571818










asked Nov 22 '18 at 6:42









learnerlearner

103




103













  • rule of thumb, whatever column not in aggregate need to be in the GROUP BY clause. Meaning you need to add this converT(datetime,SWITCHOFFSET(converT(datetimeoffset,created), 'America/New_York')) to the GROUP BY

    – Squirrel
    Nov 22 '18 at 6:46





















  • rule of thumb, whatever column not in aggregate need to be in the GROUP BY clause. Meaning you need to add this converT(datetime,SWITCHOFFSET(converT(datetimeoffset,created), 'America/New_York')) to the GROUP BY

    – Squirrel
    Nov 22 '18 at 6:46



















rule of thumb, whatever column not in aggregate need to be in the GROUP BY clause. Meaning you need to add this converT(datetime,SWITCHOFFSET(converT(datetimeoffset,created), 'America/New_York')) to the GROUP BY

– Squirrel
Nov 22 '18 at 6:46







rule of thumb, whatever column not in aggregate need to be in the GROUP BY clause. Meaning you need to add this converT(datetime,SWITCHOFFSET(converT(datetimeoffset,created), 'America/New_York')) to the GROUP BY

– Squirrel
Nov 22 '18 at 6:46














1 Answer
1






active

oldest

votes


















0














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:



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.






share|improve this answer


























  • Thank you for the response it's working with exclude the order by

    – learner
    Nov 22 '18 at 7:14











  • 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), 'America/New_York')), year(created), month(created), DATEPART(week,created),day(created)

    – learner
    Nov 22 '18 at 7:16











  • @VrandaRaghuwanshi just add ORDER BY created to the end of your query. And it should work.

    – StepUp
    Nov 22 '18 at 7:18













Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53425214%2ffacing-issue-by-using-some-operation-in-mssql-query%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














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:



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.






share|improve this answer


























  • Thank you for the response it's working with exclude the order by

    – learner
    Nov 22 '18 at 7:14











  • 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), 'America/New_York')), year(created), month(created), DATEPART(week,created),day(created)

    – learner
    Nov 22 '18 at 7:16











  • @VrandaRaghuwanshi just add ORDER BY created to the end of your query. And it should work.

    – StepUp
    Nov 22 '18 at 7:18


















0














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:



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.






share|improve this answer


























  • Thank you for the response it's working with exclude the order by

    – learner
    Nov 22 '18 at 7:14











  • 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), 'America/New_York')), year(created), month(created), DATEPART(week,created),day(created)

    – learner
    Nov 22 '18 at 7:16











  • @VrandaRaghuwanshi just add ORDER BY created to the end of your query. And it should work.

    – StepUp
    Nov 22 '18 at 7:18
















0












0








0







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:



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.






share|improve this answer















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:



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.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 22 '18 at 7:05

























answered Nov 22 '18 at 6:56









StepUpStepUp

7,56474473




7,56474473













  • Thank you for the response it's working with exclude the order by

    – learner
    Nov 22 '18 at 7:14











  • 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), 'America/New_York')), year(created), month(created), DATEPART(week,created),day(created)

    – learner
    Nov 22 '18 at 7:16











  • @VrandaRaghuwanshi just add ORDER BY created to the end of your query. And it should work.

    – StepUp
    Nov 22 '18 at 7:18





















  • Thank you for the response it's working with exclude the order by

    – learner
    Nov 22 '18 at 7:14











  • 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), 'America/New_York')), year(created), month(created), DATEPART(week,created),day(created)

    – learner
    Nov 22 '18 at 7:16











  • @VrandaRaghuwanshi just add ORDER BY created to the end of your query. And it should work.

    – StepUp
    Nov 22 '18 at 7:18



















Thank you for the response it's working with exclude the order by

– learner
Nov 22 '18 at 7:14





Thank you for the response it's working with exclude the order by

– learner
Nov 22 '18 at 7:14













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), 'America/New_York')), year(created), month(created), DATEPART(week,created),day(created)

– learner
Nov 22 '18 at 7:16





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), 'America/New_York')), year(created), month(created), DATEPART(week,created),day(created)

– learner
Nov 22 '18 at 7:16













@VrandaRaghuwanshi just add ORDER BY created to the end of your query. And it should work.

– StepUp
Nov 22 '18 at 7:18







@VrandaRaghuwanshi just add ORDER BY created to the end of your query. And it should work.

– StepUp
Nov 22 '18 at 7:18




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53425214%2ffacing-issue-by-using-some-operation-in-mssql-query%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Create new schema in PostgreSQL using DBeaver

Deepest pit of an array with Javascript: test on Codility

Fotorealismo