0

I am trying to write a query to pull group data by payment_method (one of columns in table) and by date. I can pull the query without grouping by date, but once I add the dates, I am unable to get the query to work. Does anyone have any idea how to fix my query?

Current query:

SELECT payment_method,SUM(net_amount)
DATEADD(MONTH, DATEDIFF(MONTH, 0, transaction_time),0) as OrderDateByMonth
FROM Database
WHERE [criteria]
GROUP BY payment_method
AND DATEADD(MONTH, DATEDIFF(MONTH, 0, transaction_time),0)
ORDER BY payment_method
AND OrderDateByMonth

closed as off-topic by Andriy M, Max Vernon, James Anderson, dezso, Michael Green Sep 16 '16 at 10:32

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Too localized - this could be because your code has a typo, basic error, or is not relevant to most of our audience. Consider revising your question so that it appeals to a broader audience. As it stands, the question is unlikely to help other users (regarding typo questions, see this meta question for background)." – Andriy M, Max Vernon, James Anderson, dezso, Michael Green
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 3
    Remove the AND from GROUP BY and ORDER BY. Use a comma to separate the fields. – tpet Sep 15 '16 at 18:39
6

You just need to use commas instead of AND:

SELECT payment_method, 
       SUM(net_amount),
       DATEADD(MONTH, DATEDIFF(MONTH, 0, transaction_time),0) AS OrderDateByMonth
FROM [Database]
WHERE [criteria]
GROUP BY payment_method,     -- <--- comma here
         DATEADD(MONTH, DATEDIFF(MONTH, 0, transaction_time),0)
ORDER BY payment_method,     -- <--- and here
         OrderDateByMonth ;

Not the answer you're looking for? Browse other questions tagged or ask your own question.