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
  • 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 ;
| improve this answer | |

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