Where Function in sqlite - sql

Basically I have so far managed to produce this:
sqlite> SELECT city, COUNT(bname) AS sum FROM building GROUP BY city;
city sum
---------- ----------
Leeds 3
London 1
New York 2
Paris 1
However what I want to do is only print the Cities that have a sum > 1.
I have tried this:
sqlite> SELECT city, COUNT(bname) AS sum FROM building WHERE sum>1 GROUP BY city;
But I get the error:
Error: misuse of aggregate: COUNT()
Can someone explain why this isn't working, an what I shoud do instead?
Thanks

When you want to limit your resultset based on an aggregate function you use the HAVING clause instead of where:
SELECT city,
COUNT(bname) AS sum
FROM building
GROUP BY city
HAVING COUNT(bname) > 1;
There are good explanations on stackoverflow on why you can't use WHERE, but here's a short one:
Here's what happens when you do a SELECT:
FROM
WHERE
GROUP BY
HAVING
SELECT
ORDER BY
(This list can be bigger in some RDBMS, but this should give you an idea)
Because you are doing a GROUP BY to get your COUNT, you can't limit that on WHERE clause because it already happened.

You need to use having when filtering based on the result of an aggregate function
SELECT city, COUNT(bname) AS sum
FROM building
GROUP BY city
HAVING COUNT(bname) > 1;

Related

error when Im using group by in select query

I have this query:
select id, convert(nvarchar(10), pubdate, 102) as pubdate,
channel_title, title, description, link, vertinimas
from table1
where statusid > 0
and channel_title = 'channel1'
group by title
order by pubdate desc
to exclude duplicate entries in the field "title" i added group by title in the end, but an error occurs:
"is invalid in the select list because it is not contained in either an aggregate function or the GROUP BY clause."
GROUP BY clause can only be used with aggregate functions like count(), min(), max(), sum() etc. The select query can only select the columns which are part of GROUP BY clause or on which you are applying an aggregate function.
For example you have a STUDENT table like below:
ID
NAME
SUBJECT
MARKS
1
FOO
ENGLISH
80
2
FOO
MATH
70
3
BAR
ENGLISH
100
4
BAR
MATH
50
5
ZIL
ENGLISH
90
6
ZIL
MATH
75
you can write a query like:
SELECT NAME, SUM(MARKS) AS TOTAL FROM STUDENT GROUP BY NAME;
Hear in the above query NAME is part of your GROUP BY clause and we are applying sum() aggregate function on column on MARKS. This will give us a result like below:
NAME
MARKS
FOO
150
BAR
150
ZIL
165
In your query above in the post, only title is part of GROUP BY column. Rest all the column like id, pubdate, channel_title, title, description, link, vertinimas, they are neither part of GROUP BY clause nor passed as a parameter in any aggregate function.
If you want to find / exclude / delete duplicate rows, you can checkout this blog post. This guy has explained it pretty well. Here is the like to find and delete duplicate records!

Count with Group By always shows '1'

I'm trying to count the number of articles mentioning Donald Trump exist in a Google BigQuery table.
SELECT
sourcecommonname,
COUNT(DISTINCT sourcecommonname) counter
FROM
`israel_media`
WHERE
persons LIKE '%donald trump%'
GROUP BY
sourcecommonname
Results are always
sourcecommonname
counter
first_newspaper
1
second_newspaper
1
third_newspaper
1
forth_newspaper
1
What am I not seeing?
Of course. You are counting distinct values. But there is only one value per group. That is what the group by does.
Perhaps you just want count() without distinct:
SELECT sourcecommonname,
COUNT(*) as counter
FROM `israel_media`
WHERE persons LIKE '%donald trump%'
GROUP BY sourcecommonname

SQL how can i get sum,avg and count of my table?

I have a table with 3 columns: name,continent and population. I need to make a new table with the sum of countries, avg of population , and sum of population.
this is my code but i get an error
SELECT COUNT(name) AS number,
AVG(population) AS average,
SUM(population) AS total FROM coutries;
this is my error
ERROR 1146 (42S02) at line 99: Table 'ri_db.coutries' doesn't exist
Your table's name is countries and there is no column countries, so do this:
SELECT
COUNT(*) AS number,
AVG(population) AS average,
SUM(population) AS total
FROM countries;
"from coutries" should probably be "from countries".
And as mentioned in a comment use "count(*)".
"I have a table with 3 columns: name,continent and population."
Then if you want the total count of rows, you need COUNT(1) or COUNT(*). COUNT(countries) will not work: there is no column called 'countries'.

How can I query the percentage of a value to the total of all values?

In SQL Server, I have a table called credit_hours. The table has fields College and Sum_Credit_Hours and I want to calculate the percentage of each "Sum_Credit_Hours" value as it relates to the sum of all credit hours. How can I achieve this?
Sample Data:
College Credit_hours
------------ ------------
Liberal arts 2253.2
Social Work 442.2
Nursing 223.65
Nursing 2
Expected Result:
College Credit_hours Percentage
------------ ------------ ----------
Liberal Arts 2253.2 77
Social Work 442.2 16
Nursing 225.65 7
I currently have the following
SELECT College,
sum(credit_hours),
Percentage=( credit_hours / Sum(credit_hours)OVER() ) * 100
FROM Student_credit_hour_copy
However SQL server is stating that
Column 'Student_credit_hour_copy.COURSE_COLLEGE_NEW' is invalid in the
select list because it is not contained in either an aggregate
function or the GROUP BY clause.
Which is strange, because it uses two aggregate functions in the select.
Use Sum() Over() window function to find the sum of all credithours and divide the each credithours with the sum to find the percentage
SELECT college,
credit_hours,
Percentage=( credit_hours / Sum(Credit_hours)OVER() ) * 100
FROM (SELECT college,
Sum(credit_hours) credit_hours
FROM Yourtable
GROUP BY college) a
SQLFIDDLE DEMO
For the exact code, we'll need to see what your data structure looks like.
But if you have a query that gives you the sum, it'll be the same query with avg in it's place. You may have to round it though.
So, something like,
Select sum(column1) Total_Column1, avg(column1) Average_Column1
from table_name
Group by PK_Column

sql count or sum?

I'm a newbie programmer, I want to sum a value of employee's attendance record
Anyway, what should I choose? COUNT or SUM?
I tried to use COUNT functions like this...
SELECT COUNT(jlh_sakit) AS sakit FROM rekap_absen
It shows value changed to "1" for 1 Record only.
And I try to use SUM functions like this...
SELECT SUM(jlh_sakit) AS sakit FROM rekap_absen
It shows all values changed ALL value to "1"
I want to display only 1 person for each sum
(e.g : John (2 sick, 2 permissions, 1 alpha)
Can you help me please?
If you are using any aggregate function like min/max/sum/count you should use group by. Now your question says "what should I choose? COUNT or SUM?" Assuming you have person_name, jlh_sakit which means sick/permission/alpha in your case you could use
select person_name, count(jhl_sakit) as attribute
from rekap_absen
group by person
This will give you output like:
person_name attribute
John 2
King 5
In order to sum by specified column, use group by statement.
SELECT SUM(sick),SUM(alpha),SUM(permissions),person FROM rekap_absen group by person
It will group your sums according to person.
You may name your sums like:
SELECT SUM(sick) as sick,SUM(alpha) as alpha,SUM(permissions) as permissions,person FROM rekap_absen group by person
Assuming that you have table rekap_absen with columns: person,sick,alpha,permissions