0

i have below sql

SELECT SUM(amount) from CustomerPayment where date like '%2013' GROUP BY TYPE

in Type column there are two choice one them is BANK and the other one is CASH

I want to sum these seperately. For ex: bank is totally 500 and cash is 400

However above sql does not sum the records. Instead, it returns first record of bank and cash records. Why?

2
  • 1
    Please paste your table data here May 1, 2013 at 9:28
  • Nothing wrong with your query except that like - use year(date) == 2013. It works fine for me: sqlfiddle.com/#!2/7c1c90/3
    – maroux
    May 1, 2013 at 9:39

4 Answers 4

0

I would say try and add type to the sql query. Then it has a visible representation of the type you wish to group by.

 SELECT SUM(amount), TYPE from CustomerPayment where date like '%2013' GROUP BY TYPE
3
  • gives this error at org.hibernate.hql.classic.PathExpressionParser.token(PathExpressionParser.java:1‌​30) at org.hibernate.hql.classic.ParserHelper.parse(ParserHelper.java:28) at org.hibernate.hql.classic.SelectParser.token(SelectParser.java:176) at org.hibernate.hql.classic.ClauseParser.token(ClauseParser.java:86) May 1, 2013 at 9:38
  • I can't find where the mistake might be in the sql statement. Might it be related with the data you try to enter? If this is the case, that might explain why with the current given info, we're not able to see the problem.
    – Skillcoil
    May 1, 2013 at 9:45
  • i use hibernate may be it is different in hibernate .s May 1, 2013 at 9:58
0

can you try using year(date)=2013, as data is not there so I am guessing if 'like' function is not letting group by to perform its functionality properly.

1
  • you should paste data first
    – Sunny
    May 1, 2013 at 10:08
0

If your date field has a datatype of Date or DateTime then the LIKE operator won't work.

For date comparisons use date functions.

SELECT SUM(amount)
FROM CustomerPayment
WHERE YEAR(date) = 2013
GROUP BY TYPE

https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_year

0
-1

include TYPE col also

SELECT TYPE, SUM(amount) from CustomerPayment where date like '%2013' GROUP BY TYPE
2
  • givez this error at org.hibernate.hql.classic.PathExpressionParser.token(PathExpressionParser.java:130) at org.hibernate.hql.classic.ParserHelper.parse(ParserHelper.java:28) at org.hibernate.hql.classic.SelectParser.token(SelectParser.java:176) at org.hibernate.hql.classic.ClauseParser.token(ClauseParser.java:86) May 1, 2013 at 9:37
  • Adding a field to the SELECT component won't make a difference when handling the GROUP BY or the WHERE conditions
    – Tomas
    May 1, 2013 at 9:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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