-1

I'm trying to filter, so the column salaryMonth only contains data which has 2020 inside, so 2019 is filtering out.

SELECT sum(km_amount) as total
     , user_id
     , salaryMonth 
  from kms
     , users 
 where users.id = kms.user_id 
 group 
    by salaryMonth
     , user_id 

enter image description here

Strawberry
  • 33,750
  • 13
  • 40
  • 57
Sajid Latif
  • 119
  • 2
  • 16

2 Answers2

0

Did you try something like this?

SELECT
    sum(km_amount) as total, 
    user_id, 
    salaryMonth 
FROM kms, users 
WHERE
    users.id=kms.user_id 
    AND salaryMonth LIKE '%2020%' 
GROUP BY 
    salaryMonth, user_id 
Moseleyi
  • 2,585
  • 1
  • 24
  • 46
DmitriyH
  • 126
  • 7
0

You could save yourself no end of misery be refactoring your table as:

total user_id salary_yearmonth
  625      64 2020-02-01 
  595      70 2020-02-01 
  600      74 2020-02-01
  632      75 2020-02-01
  471      77 2020-02-01
  788      29 2019-03-01
   35       4 2020-03-01
   22      39 2020-03-01
  373      47 2020-03-01
  196      53 2020-03-01
  140      74 2020-03-01
  228      75 2020-03-01
   49      29 2019-04-01 
   96      63 2019-05-01 
  406       4 2019-06-01 
  966       4 2019-07-01 
  514       1 2019-08-01 
  637       4 2019-08-01
  580      47 2019-08-01
   11       1 2019-09-01
Strawberry
  • 33,750
  • 13
  • 40
  • 57
  • Actually the salaryMonth column is a string but in the same table I have a date field for same purpose, so I could filter it from there as well. Your right :) – Sajid Latif Mar 06 '20 at 06:56