Home > Software engineering >  Limit the list of objects sql
Limit the list of objects sql

Time:02-08

I am new to SQL (using SQLiteStudio) and I wrote such a query to calculate. List of municipalities where the number of sales increased by at least 20% between the first and second quarters of 2020. I'm trying to set a limit using HAVING but it doesn't work. Any help would really help. Thanks!

    SELECT commune, p.date_1,d.date_2,
    (((d.date_2)*100/(p.date_1)-100)) as "Sales"
    FROM pr_semestr p, dx_semestre d
    WHERE p.commune=d.commune
    HAVING ( (d.date_2)*100/(p.date_1)-100) > '20'

Data Sample:

commune date_1 date_2 Sales
ABBEVILLE 2 9 350
ACHERES 5 13 160
ACIGNE 11 8 -28
AGEN 1 1 0
AIGUES-VIVES 15 17 13

CodePudding user response:

HAVING clause is used to filter GROUPed data. Use a WHERE clause to filter selected row data.

CodePudding user response:

Yes, I changed using a WHERE clause, also added one parenthesis and deleted quotes. And also changed on JOIN ON. It worked!

    FROM pr_semestr p JOIN dx_semestre d ON p.commune=d.commune
    WHERE (((d.date_2)*100/(p.date_1)-100)) > 20
  •  Tags:  
  • Related